Skip to content

Commit 8f4a030

Browse files
committed
replace: provider for post card
1 parent 5fa39c4 commit 8f4a030

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

src/components/common/PostCardGrid.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { mediaQuery } from '../../lib/styles/media';
66
import AdFeed from './AdFeed';
77
import { detectAnyAdblocker } from 'just-detect-adblock';
88
import useUser from '../../lib/hooks/useUser';
9+
import SetupAdFeed from './SetupAdFeed';
910

1011
export type PostCardGridProps = {
1112
posts: (PartialPost | undefined)[];
@@ -16,8 +17,15 @@ export type PostCardGridProps = {
1617

1718
function PostCardGrid({ posts, loading, forHome, forPost }: PostCardGridProps) {
1819
const [adBlocked, setAdBlocked] = useState(false);
20+
const [mode, setMode] = useState<'google' | 'setupad'>('setupad');
1921
const user = useUser();
2022

23+
useEffect(() => {
24+
if (window.innerWidth < 768) {
25+
setMode('google');
26+
}
27+
}, []);
28+
2129
useEffect(() => {
2230
detectAnyAdblocker().then((detected: boolean) => {
2331
if (detected) {
@@ -60,7 +68,12 @@ function PostCardGrid({ posts, loading, forHome, forPost }: PostCardGridProps) {
6068
forPost={forPost}
6169
/>
6270
);
63-
return <AdFeed key={i} index={i} forPost={forPost} />;
71+
72+
return mode === 'google' ? (
73+
<AdFeed key={i} index={i} forPost={forPost} />
74+
) : (
75+
<SetupAdFeed key={i} />
76+
);
6477
})}
6578
{loading &&
6679
Array.from({ length: 8 }).map((_, i) => (

src/components/common/SetupAdFeed.tsx

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import React, { useEffect, useRef } from 'react';
2+
import styled from 'styled-components';
3+
import { mediaQuery } from '../../lib/styles/media';
4+
import gtag from '../../lib/gtag';
5+
import { themedPalette } from '../../lib/styles/themes';
6+
import { useTheme } from '../../lib/hooks/useTheme';
7+
8+
function init() {
9+
(function () {
10+
var size = '320x378',
11+
adunit = 'velog.io_320x378_native_DFP',
12+
childNetworkId = '22738196472',
13+
xmlhttp = new XMLHttpRequest();
14+
xmlhttp.onreadystatechange = function () {
15+
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
16+
var es = document.querySelectorAll("[data-id='" + adunit + "']");
17+
var e = Array.from(es).filter(function (e) {
18+
return !e.hasAttribute('data-rendered');
19+
});
20+
if (e.length > 0) {
21+
e.forEach(function (el: any) {
22+
var iframe = el.contentWindow.document;
23+
iframe.open();
24+
iframe.write(xmlhttp.responseText);
25+
iframe.close();
26+
el.setAttribute('data-rendered', true);
27+
});
28+
}
29+
}
30+
};
31+
var child = childNetworkId.trim() ? ',' + childNetworkId.trim() : '';
32+
xmlhttp.open(
33+
'GET',
34+
'https://pubads.g.doubleclick.net/gampad/adx?iu=/147246189' +
35+
child +
36+
'/' +
37+
adunit +
38+
'&sz=' +
39+
encodeURI(size) +
40+
'&t=Placement_type%3Dserving&' +
41+
Date.now(),
42+
true,
43+
);
44+
xmlhttp.send();
45+
})();
46+
}
47+
48+
function SetupAdFeed() {
49+
useEffect(() => {
50+
init();
51+
}, []);
52+
53+
return (
54+
<Block>
55+
<iframe
56+
title="nativead"
57+
data-id="velog.io_320x378_native_DFP"
58+
frameBorder="0"
59+
scrolling="no"
60+
marginHeight={0}
61+
marginWidth={0}
62+
width="1"
63+
height="1"
64+
></iframe>
65+
</Block>
66+
);
67+
}
68+
69+
const Block = styled.div`
70+
width: 20rem;
71+
margin: 1rem;
72+
min-height: 23.5625rem;
73+
height: auto;
74+
border-radius: 4px;
75+
box-shadow: 0 4px 16px 0 rgba(0, 0, 0, 0.04);
76+
background: ${themedPalette.bg_element1};
77+
78+
${mediaQuery(1056)} {
79+
width: calc(50% - 2rem);
80+
}
81+
${mediaQuery(767)} {
82+
margin: 0;
83+
width: 100%;
84+
margin-top: 1rem;
85+
margin-bottom: 1rem;
86+
min-height: 5rem;
87+
}
88+
`;
89+
90+
export default SetupAdFeed;

0 commit comments

Comments
 (0)