Skip to content

Commit 342fdbf

Browse files
committed
fix: handle mobile
1 parent bda1b7f commit 342fdbf

File tree

2 files changed

+112
-3
lines changed

2 files changed

+112
-3
lines changed

src/components/common/NarrowAd.tsx

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import React, { useEffect, useState } from 'react';
2+
import styled from 'styled-components';
3+
import media from '../../lib/styles/media';
4+
5+
const Wrapper = styled.div`
6+
display: none;
7+
${media.medium} {
8+
display: flex;
9+
justify-content: center;
10+
align-items: center;
11+
margin: 16px 0;
12+
}
13+
`;
14+
15+
const AdBlock = styled.div<{ width: number; height: number }>`
16+
width: ${(props) => props.width}px;
17+
height: ${(props) => props.height}px;
18+
display: flex;
19+
justify-content: center;
20+
align-items: center;
21+
`;
22+
23+
function AdInsComponent({
24+
width,
25+
height,
26+
slot,
27+
}: {
28+
width: number;
29+
height: number;
30+
slot: string;
31+
}) {
32+
useEffect(() => {
33+
(window.adsbygoogle = window.adsbygoogle || []).push({});
34+
}, []);
35+
36+
return (
37+
<ins
38+
className="adsbygoogle"
39+
style={{
40+
display: 'inline-block',
41+
width: `${width}px`,
42+
height: `${height}px`,
43+
}}
44+
data-ad-client="ca-pub-5574866530496701"
45+
data-ad-slot={slot}
46+
></ins>
47+
);
48+
}
49+
50+
export default function NarrowAd() {
51+
const [isMobile, setIsMobile] = useState(false);
52+
const [mode, setMode] = useState<'wide' | 'narrow'>('wide');
53+
54+
useEffect(() => {
55+
const userAgent = navigator.userAgent.toLowerCase();
56+
const checkIsMobile =
57+
/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(
58+
userAgent,
59+
);
60+
61+
setIsMobile(checkIsMobile);
62+
63+
// Set mode based on window width
64+
const windowWidth = window.innerWidth;
65+
setMode(windowWidth < 768 ? 'narrow' : 'wide');
66+
}, []);
67+
68+
useEffect(() => {
69+
const handleResize = () => {
70+
const windowWidth = window.innerWidth;
71+
72+
// Update mode based on width
73+
setMode(windowWidth < 768 ? 'narrow' : 'wide');
74+
};
75+
76+
window.addEventListener('resize', handleResize);
77+
return () => {
78+
window.removeEventListener('resize', handleResize);
79+
};
80+
}, []);
81+
82+
const size =
83+
mode === 'narrow'
84+
? { width: 300, height: 50 }
85+
: { width: 728, height: 90 };
86+
const adSlot = mode === 'narrow' ? '1778476944' : '5606751754';
87+
88+
return (
89+
<Wrapper>
90+
{isMobile && mode === 'narrow' && (
91+
<AdBlock width={size.width} height={size.height}>
92+
<AdInsComponent
93+
width={size.width}
94+
height={size.height}
95+
slot={adSlot}
96+
/>
97+
</AdBlock>
98+
)}
99+
{isMobile && mode === 'wide' && (
100+
<AdBlock width={size.width} height={size.height}>
101+
<AdInsComponent
102+
width={size.width}
103+
height={size.height}
104+
slot={adSlot}
105+
/>
106+
</AdBlock>
107+
)}
108+
</Wrapper>
109+
);
110+
}

src/containers/post/PostViewer.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import { BANNER_ADS } from '../../lib/graphql/ad';
4848
import PostBanner from '../../components/post/PostBanner';
4949
import JobPositions from '../../components/post/JobPositions';
5050
import SideAd from '../../components/post/SideAd';
51+
import NarrowAd from '../../components/common/NarrowAd';
5152

5253
const UserProfileWrapper = styled(VelogResponsive)`
5354
margin-top: 16rem;
@@ -570,9 +571,7 @@ const PostViewer: React.FC<PostViewerProps> = ({
570571
/>
571572
</UserProfileWrapper>
572573
<LinkedPostList linkedPosts={post.linked_posts} />
573-
{/* {shouldShowBanner && isContentLongEnough ? (
574-
<PostBanner customAd={customAd} isDisplayAd={true} />
575-
) : null} */}
574+
{shouldShowAds ? <NarrowAd /> : null}
576575
{/* {shouldShowFooterBanner ? (
577576
<PostBanner isDisplayAd={true} customAd={customAd} />
578577
) : null} */}

0 commit comments

Comments
 (0)