Skip to content

Commit 5fc9869

Browse files
committed
Handle adblocked
1 parent ec94991 commit 5fc9869

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/components/common/AdFeed.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
import React, { useEffect } from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import styled from 'styled-components';
33
import { mediaQuery } from '../../lib/styles/media';
44

55
function AdFeed() {
6+
const [adBlocked, setAdBlocked] = useState(false);
67
useEffect(() => {
7-
(window as any).adsbygoogle = ((window as any).adsbygoogle || []).push({});
8+
if ((window as any)?.adsbygoogle?.push) {
9+
(window as any).adsbygoogle = ((window as any).adsbygoogle || []).push(
10+
{},
11+
);
12+
} else {
13+
setAdBlocked(true);
14+
}
815
}, []);
16+
17+
if (adBlocked) return null;
18+
919
return (
1020
<Block>
1121
<ins

src/components/common/PostCardGrid.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect, useMemo } from 'react';
1+
import React, { useMemo } from 'react';
22
import styled from 'styled-components';
33
import PostCard, { PostCardSkeleton } from './PostCard';
44
import { PartialPost } from '../../lib/graphql/post';
@@ -21,20 +21,30 @@ function PostCardGrid({ posts, loading, forHome }: PostCardGridProps) {
2121

2222
const postsWithAds = useMemo(() => {
2323
if (!forHome) return posts;
24+
if (posts.length === 0) return posts;
2425
const cloned: (PartialPost | undefined)[] = [...posts];
2526
cloned.splice(4, 0, undefined);
26-
if (cloned.length > 30) {
27+
if (cloned.length > 21) {
2728
cloned.splice(20, 0, undefined);
2829
}
30+
if (cloned.length > 33) {
31+
cloned.splice(32, 0, undefined);
32+
}
33+
if (cloned.length > 49) {
34+
cloned.splice(48, 0, undefined);
35+
}
36+
if (cloned.length > 63) {
37+
cloned.splice(62, 0, undefined);
38+
}
2939
return cloned;
3040
}, [posts, forHome]);
3141

3242
return (
3343
<Block>
34-
{postsWithAds.map((post) => {
44+
{postsWithAds.map((post, i) => {
3545
if (post)
3646
return <PostCard post={post} key={post.id} forHome={forHome} />;
37-
return <AdFeed />;
47+
return <AdFeed key={i} />;
3848
})}
3949
{loading &&
4050
Array.from({ length: 8 }).map((_, i) => (

0 commit comments

Comments
 (0)