File tree Expand file tree Collapse file tree 2 files changed +59
-1
lines changed Expand file tree Collapse file tree 2 files changed +59
-1
lines changed Original file line number Diff line number Diff line change
1
+ import React , { useEffect , useRef } from 'react' ;
2
+ import styled from 'styled-components' ;
3
+ interface Props { }
4
+
5
+ function HorizontalAd ( { } : Props ) {
6
+ const ref = useRef < HTMLDivElement > ( null ) ;
7
+ const initializedRef = useRef ( false ) ;
8
+
9
+ useEffect ( ( ) => {
10
+ const observer = new IntersectionObserver (
11
+ ( entries ) => {
12
+ entries . forEach ( ( entry ) => {
13
+ if ( entry . isIntersecting && ! initializedRef . current ) {
14
+ initializedRef . current = true ;
15
+ ( window . adsbygoogle = window . adsbygoogle || [ ] ) . push ( { } ) ;
16
+ console . log ( 'initialized!' ) ;
17
+ }
18
+ } ) ;
19
+ } ,
20
+ {
21
+ rootMargin : '90px' ,
22
+ threshold : 0 ,
23
+ } ,
24
+ ) ;
25
+ observer . observe ( ref . current ! ) ;
26
+ return ( ) => {
27
+ observer . disconnect ( ) ;
28
+ } ;
29
+ } , [ ] ) ;
30
+
31
+ return (
32
+ < Wrapper >
33
+ < ins
34
+ className = "adsbygoogle"
35
+ style = { { display : 'block' } }
36
+ data-ad-client = "ca-pub-5574866530496701"
37
+ data-ad-slot = "8809887603"
38
+ data-ad-format = "auto"
39
+ data-full-width-responsive = "true"
40
+ />
41
+ </ Wrapper >
42
+ ) ;
43
+ }
44
+
45
+ const Wrapper = styled . div `
46
+ margin-top: 1.5rem;
47
+ margin-bottom: 1.5rem;
48
+ ` ;
49
+
50
+ export default HorizontalAd ;
Original file line number Diff line number Diff line change @@ -36,6 +36,7 @@ import MobileLikeButton from '../../components/post/MobileLikeButton';
36
36
import RelatedPost from './RelatedPost' ;
37
37
import optimizeImage from '../../lib/optimizeImage' ;
38
38
import RelatedPostsForGuest from './RelatedPostsForGuest' ;
39
+ import HorizontalAd from './HorizontalAd' ;
39
40
40
41
const UserProfileWrapper = styled ( VelogResponsive ) `
41
42
margin-top: 16rem;
@@ -308,6 +309,10 @@ const PostViewer: React.FC<PostViewerProps> = ({
308
309
309
310
const { post } = data ;
310
311
312
+ const isVeryOld =
313
+ Date . now ( ) - new Date ( post . released_at ) . getTime ( ) >
314
+ 1000 * 60 * 60 * 24 * 365 ;
315
+
311
316
const url = `https://velog.io/@${ username } /${ post . url_slug } ` ;
312
317
313
318
return (
@@ -387,11 +392,13 @@ const PostViewer: React.FC<PostViewerProps> = ({
387
392
< RelatedPostsForGuest
388
393
postId = { post . id }
389
394
showAds = {
395
+ ! isVeryOld &&
390
396
Date . now ( ) - new Date ( post . released_at ) . getTime ( ) >
391
- 1000 * 60 * 60 * 24 * 21
397
+ 1000 * 60 * 60 * 24 * 21
392
398
}
393
399
/>
394
400
) }
401
+ { isVeryOld && < HorizontalAd /> }
395
402
< PostComments
396
403
count = { post . comments_count }
397
404
comments = { post . comments }
@@ -401,6 +408,7 @@ const PostViewer: React.FC<PostViewerProps> = ({
401
408
< RelatedPost
402
409
postId = { post . id }
403
410
showAds = {
411
+ ! isVeryOld &&
404
412
post . user . id !== userId &&
405
413
Date . now ( ) - new Date ( post . released_at ) . getTime ( ) >
406
414
1000 * 60 * 60 * 24 * 30
You can’t perform that action at this time.
0 commit comments