Skip to content

Commit e4e484d

Browse files
committed
Prepare mobile view for rleated posts
1 parent 581911c commit e4e484d

File tree

2 files changed

+126
-8
lines changed

2 files changed

+126
-8
lines changed

src/containers/post/RelatedPostAd.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
import React, { useEffect } from 'react';
2-
interface Props {}
2+
interface Props {
3+
isMobile?: boolean;
4+
}
35

4-
function RelatedPostAd(props: Props) {
6+
function RelatedPostAd({ isMobile }: Props) {
57
useEffect(() => {
6-
(window.adsbygoogle = window.adsbygoogle || []).push({});
7-
}, []);
8+
try {
9+
(window.adsbygoogle = window.adsbygoogle || []).push({});
10+
} catch (e) {}
11+
}, [isMobile]);
812

13+
if (isMobile) {
14+
return (
15+
<ins
16+
className="adsbygoogle"
17+
style={{ display: 'block' }}
18+
data-ad-format="fluid"
19+
data-ad-layout-key="-g0+60+3y-ct+85"
20+
data-ad-client="ca-pub-5574866530496701"
21+
data-ad-slot="8237449336"
22+
></ins>
23+
);
24+
}
925
return (
1026
<ins
1127
className="adsbygoogle"

src/containers/post/RelatedPostsForGuest.tsx

Lines changed: 106 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useMemo } from 'react';
1+
import React, { useMemo, useState, useEffect } from 'react';
22
import styled from 'styled-components';
33
import media from '../../lib/styles/media';
44
import { useQuery } from '@apollo/react-hooks';
@@ -14,6 +14,7 @@ import { Link } from 'react-router-dom';
1414
import gtag from '../../lib/gtag';
1515
import optimizeImage from '../../lib/optimizeImage';
1616
import RelatedPostAd from './RelatedPostAd';
17+
import { ellipsis } from '../../lib/styles/utils';
1718
// import { detectAnyAdblocker } from 'just-detect-adblock';
1819

1920
function RelatedPostsForGuest({
@@ -29,6 +30,19 @@ function RelatedPostsForGuest({
2930
},
3031
});
3132

33+
const [isMobile, setIsMobile] = useState(false);
34+
35+
useEffect(() => {
36+
setIsMobile(window.innerWidth < 519);
37+
const handler = () => {
38+
setIsMobile(window.innerWidth < 519);
39+
};
40+
window.addEventListener('resize', handler);
41+
return () => {
42+
window.removeEventListener('resize', handler);
43+
};
44+
}, []);
45+
3246
const postsWithAds = useMemo(() => {
3347
if (!data?.post) return null;
3448
let sliced: (PartialPost | undefined)[] =
@@ -60,13 +74,52 @@ function RelatedPostsForGuest({
6074

6175
if (!postsWithAds) return null;
6276

77+
if (isMobile) {
78+
return (
79+
<Wrapper>
80+
<h4>관심 있을 만한 포스트</h4>
81+
{postsWithAds.map((post, index) => {
82+
return post ? (
83+
<Row key={post.id ?? index}>
84+
<div className="fourty">
85+
<div className="thumbnail-wrapper">
86+
<img
87+
src={optimizeImage(
88+
post.thumbnail ??
89+
post.user.profile.thumbnail ??
90+
userThumbnail,
91+
640,
92+
)}
93+
alt={post.title}
94+
/>
95+
</div>
96+
</div>
97+
<div className="content">
98+
<h5>{post.title}</h5>
99+
<p>
100+
{post.short_description.replace(/&#x3A;/g, ':')}
101+
{post.short_description.length === 150 && '...'}
102+
</p>
103+
</div>
104+
</Row>
105+
) : (
106+
<RelatedPostAd isMobile />
107+
);
108+
})}
109+
</Wrapper>
110+
);
111+
}
112+
63113
return (
64114
<Wrapper>
65115
<h4>관심 있을 만한 포스트</h4>
66116
<Grid>
67-
{postsWithAds.map((post) =>
117+
{postsWithAds.map((post, index) =>
68118
post ? (
69-
<Item onClick={() => gtag('event', 'recommend_guest_click')}>
119+
<Item
120+
onClick={() => gtag('event', 'recommend_guest_click')}
121+
key={post.id ?? index}
122+
>
70123
<StyledLink to={`/@${post.user.username}/${post.url_slug}`}>
71124
<div className="thumbnail-wrapper">
72125
<img
@@ -83,7 +136,7 @@ function RelatedPostsForGuest({
83136
</StyledLink>
84137
</Item>
85138
) : (
86-
<Item>
139+
<Item style={{ margin: '-3.375rem' }}>
87140
<RelatedPostAd />
88141
</Item>
89142
),
@@ -115,6 +168,55 @@ const Grid = styled.div`
115168
margin-top: 1.5rem;
116169
`;
117170

171+
const Row = styled.div`
172+
display: flex;
173+
.fourty {
174+
width: 40%;
175+
margin-right: 0.75rem;
176+
}
177+
.thumbnail-wrapper {
178+
width: 100%;
179+
padding-top: 52.35%;
180+
position: relative;
181+
182+
img {
183+
position: absolute;
184+
left: 0;
185+
top: 0;
186+
width: 100%;
187+
height: 100%;
188+
object-fit: cover;
189+
display: block;
190+
}
191+
}
192+
& + & {
193+
margin-top: 0.5rem;
194+
}
195+
.content {
196+
width: 60%;
197+
}
198+
h5 {
199+
margin-top: 0;
200+
margin-bottom: 0.625rem;
201+
font-size: 0.875rem;
202+
color: ${palette.gray8};
203+
${ellipsis}
204+
}
205+
206+
p {
207+
margin: 0;
208+
font-size: 0.75rem;
209+
display: -webkit-box;
210+
-webkit-line-clamp: 3;
211+
line-height: 1.5;
212+
height: 3.375rem;
213+
overflow: hidden;
214+
word-break: keep-all;
215+
color: ${palette.gray6};
216+
-webkit-box-orient: vertical;
217+
}
218+
`;
219+
118220
const Item = styled.div`
119221
position: relative;
120222
width: 33.333%;

0 commit comments

Comments
 (0)