|
1 |
| -import React from 'react'; |
| 1 | +import React, { useState, useEffect, useMemo } from 'react'; |
2 | 2 | import styled from 'styled-components';
|
3 | 3 | import media from '../../lib/styles/media';
|
4 | 4 | import { useQuery } from '@apollo/react-hooks';
|
5 | 5 | import {
|
6 | 6 | GetRecommendedPostResponse,
|
7 | 7 | GET_RECOMMENDED_POST,
|
| 8 | + PartialPost, |
8 | 9 | } from '../../lib/graphql/post';
|
9 | 10 | import PostCardGrid from '../../components/common/PostCardGrid';
|
10 | 11 | import palette from '../../lib/styles/palette';
|
11 |
| -// import { detectAnyAdblocker } from 'just-detect-adblock'; |
| 12 | +import { detectAnyAdblocker } from 'just-detect-adblock'; |
12 | 13 |
|
13 |
| -function RelatedPost({ postId }: { postId: string }) { |
14 |
| - // const [adBlocked, setAdBlocked] = useState(false); |
15 |
| - // useEffect(() => { |
16 |
| - // detectAnyAdblocker().then((detected: boolean) => { |
17 |
| - // if (detected) { |
18 |
| - // setAdBlocked(true); |
19 |
| - // } |
20 |
| - // }); |
21 |
| - // }, []); |
| 14 | +function RelatedPost({ |
| 15 | + postId, |
| 16 | + showAds, |
| 17 | +}: { |
| 18 | + postId: string; |
| 19 | + showAds: boolean; |
| 20 | +}) { |
| 21 | + const [adBlocked, setAdBlocked] = useState(false); |
| 22 | + useEffect(() => { |
| 23 | + detectAnyAdblocker().then((detected: boolean) => { |
| 24 | + if (detected) { |
| 25 | + setAdBlocked(true); |
| 26 | + } |
| 27 | + }); |
| 28 | + }, []); |
22 | 29 |
|
23 | 30 | const { data } = useQuery<GetRecommendedPostResponse>(GET_RECOMMENDED_POST, {
|
24 | 31 | variables: {
|
25 | 32 | id: postId,
|
26 | 33 | },
|
27 | 34 | });
|
28 | 35 |
|
29 |
| - // const postWithAds = useMemo(() => { |
30 |
| - // if (!data?.post) return null; |
31 |
| - // if (!showAds || adBlocked) return data.post.recommended_posts; |
32 |
| - // const cloned: (PartialPost | undefined)[] = [ |
33 |
| - // ...data.post.recommended_posts, |
34 |
| - // ]; |
35 |
| - // // get random number between 0 and length of array |
36 |
| - // const randomIndex = () => Math.floor(Math.random() * 8); |
37 |
| - // const firstAdIndex = randomIndex(); |
38 |
| - // const secondAdIndex = (() => { |
39 |
| - // let index = randomIndex(); |
40 |
| - // while (index === firstAdIndex) { |
41 |
| - // index = randomIndex(); |
42 |
| - // } |
43 |
| - // return index; |
44 |
| - // })(); |
| 36 | + const postWithAds = useMemo(() => { |
| 37 | + if (!data?.post) return null; |
| 38 | + if (!showAds || adBlocked) return data.post.recommended_posts; |
| 39 | + const cloned: (PartialPost | undefined)[] = [ |
| 40 | + ...data.post.recommended_posts, |
| 41 | + ]; |
| 42 | + // get random number between 0 and length of array |
| 43 | + const randomIndex = () => Math.floor(Math.random() * 8); |
| 44 | + const firstAdIndex = randomIndex(); |
| 45 | + const secondAdIndex = (() => { |
| 46 | + let index = randomIndex(); |
| 47 | + while (index === firstAdIndex) { |
| 48 | + index = randomIndex(); |
| 49 | + } |
| 50 | + return index; |
| 51 | + })(); |
45 | 52 |
|
46 |
| - // cloned[firstAdIndex] = undefined; |
47 |
| - // cloned[secondAdIndex] = undefined; |
48 |
| - // return cloned; |
49 |
| - // }, [data, showAds, adBlocked]); |
| 53 | + cloned[firstAdIndex] = undefined; |
| 54 | + cloned[secondAdIndex] = undefined; |
| 55 | + return cloned; |
| 56 | + }, [data, showAds, adBlocked]); |
50 | 57 |
|
51 |
| - if (!data?.post.recommended_posts) return null; |
| 58 | + if (!postWithAds) return null; |
52 | 59 |
|
53 | 60 | return (
|
54 | 61 | <>
|
55 | 62 | <Background>
|
56 | 63 | <Title>관심 있을 만한 포스트</Title>
|
57 | 64 | <Wrapper>
|
58 |
| - <PostCardGrid posts={data?.post.recommended_posts} forPost /> |
| 65 | + <PostCardGrid posts={postWithAds} forPost /> |
59 | 66 | </Wrapper>
|
60 | 67 | </Background>
|
61 | 68 | <PullUp />
|
|
0 commit comments