/
FlatPostCard.tsx
318 lines (304 loc) · 7.44 KB
/
FlatPostCard.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import React, { useRef } from 'react';
import { Link } from 'react-router-dom';
import styled from 'styled-components';
import { themedPalette } from '../../lib/styles/themes';
import { userThumbnail } from '../../static/images';
import Tag from './TagItem';
import { PartialPost } from '../../lib/graphql/post';
import { formatDate } from '../../lib/utils';
import usePrefetchPost from '../../lib/hooks/usePrefetchPost';
import Skeleton from './Skeleton';
import SkeletonTexts from './SkeletonTexts';
import RatioImage from './RatioImage';
import media from '../../lib/styles/media';
import PrivatePostLabel from './PrivatePostLabel';
import optimizeImage from '../../lib/optimizeImage';
import { LikeIcon } from '../../static/svg';
import VLink from './VLink';
const PostCardBlock = styled.div`
padding-top: 4rem;
padding-bottom: 4rem;
${media.small} {
padding-top: 2rem;
padding-bottom: 2rem;
}
& > a {
color: inherit;
text-decoration: none;
}
&:first-child {
padding-top: 0;
}
.user-info {
display: flex;
align-items: center;
img {
width: 3rem;
height: 3rem;
display: block;
margin-right: 1rem;
background: ${themedPalette.bg_element2};
object-fit: cover;
border-radius: 1.5rem;
box-shadow: 0px 0 8px rgba(0, 0, 0, 0.1);
${media.small} {
width: 2rem;
height: 2rem;
border-radius: 1rem;
}
}
.username {
font-size: 0.875rem;
color: ${themedPalette.text1};
font-weight: bold;
a {
color: inherit;
text-decoration: none;
&:hover {
color: ${themedPalette.text1};
}
}
}
margin-bottom: 1.5rem;
${media.small} {
margin-bottom: 0.75rem;
}
}
.post-thumbnail {
margin-bottom: 1rem;
${media.small} {
}
}
line-height: 1.5;
h2 {
font-size: 1.5rem;
margin: 0;
color: ${themedPalette.text1};
word-break: keep-all;
${media.small} {
font-size: 1rem;
}
}
p {
margin-bottom: 2rem;
margin-top: 0.5rem;
font-size: 1rem;
color: ${themedPalette.text2};
word-break: keep-all;
overflow-wrap: break-word;
${media.small} {
font-size: 0.875rem;
margin-bottom: 1.5rem;
}
}
.subinfo {
display: flex;
align-items: center;
margin-top: 1rem;
color: ${themedPalette.text3};
font-size: 0.875rem;
${media.small} {
font-size: 0.75rem;
}
span {
}
.separator {
margin-left: 0.5rem;
margin-right: 0.5rem;
}
.likes {
display: flex;
align-items: center;
svg {
width: 0.875rem;
height: 0.875rem;
margin-right: 0.25rem;
}
}
}
.tags-wrapper {
margin-bottom: -0.875rem;
${media.small} {
margin-bottom: -0.5rem;
}
}
& + & {
border-top: 1px solid ${themedPalette.border4};
}
`;
interface PostCardProps {
post: PartialPost;
hideUser?: boolean;
}
const FlatPostCard = ({ post, hideUser }: PostCardProps) => {
const prefetch = usePrefetchPost(post.user.username, post.url_slug);
const prefetchTimeoutId = useRef<ReturnType<typeof setTimeout> | null>(null);
const onMouseEnter = () => {
prefetchTimeoutId.current = setTimeout(prefetch, 2000);
};
const onMouseLeave = () => {
if (prefetchTimeoutId.current) {
clearTimeout(prefetchTimeoutId.current);
}
};
const url = `/@${post.user.username}/${post.url_slug}`;
const velogUrl = `/@${post.user.username}/posts`;
if (!post.user.profile) {
console.log(post);
}
return (
<PostCardBlock onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave}>
{!hideUser && (
<div className="user-info">
<VLink to={velogUrl}>
<img
src={optimizeImage(
post.user.profile?.thumbnail || userThumbnail,
120,
)}
alt="thumbnail"
/>
</VLink>
<div className="username">
<VLink to={`${velogUrl}`}>
{post.user?.profile?.display_name ?? post.user.username}
</VLink>
</div>
</div>
)}
{post.thumbnail && (
<Link to={url}>
<RatioImage
src={optimizeImage(post.thumbnail, 768)}
alt="post-thumbnail"
widthRatio={1.91}
heightRatio={1}
className="post-thumbnail"
/>
</Link>
)}
<Link to={url}>
<h2>{post.title}</h2>
</Link>
<p>{post.short_description}</p>
<div className="tags-wrapper">
{post.tags.map((tag) => (
<Tag key={tag} name={tag} link />
))}
</div>
<div className="subinfo">
<span>{formatDate(post.released_at)}</span>
<div className="separator">·</div>
<span>{post.comments_count}개의 댓글</span>
<div className="separator">·</div>
<span className="likes">
<LikeIcon />
{post.likes}
</span>
{post.is_private && (
<>
<div className="separator">·</div>
<span>
<PrivatePostLabel />
</span>
</>
)}
</div>
</PostCardBlock>
);
};
export type PostCardSkeletonProps = {
hideUser?: boolean;
};
export function PostCardSkeleton({ hideUser }: PostCardSkeletonProps) {
return (
<SkeletonBlock>
{!hideUser && (
<div className="user-info">
<Skeleton
className="user-thumbnail-skeleton"
circle
marginRight="1rem"
/>
<div className="username">
<Skeleton width="5rem" />
</div>
</div>
)}
<div className="post-thumbnail">
<div className="thumbnail-skeleton-wrapper">
<Skeleton className="skeleton" />
</div>
</div>
<h2>
<SkeletonTexts wordLengths={[4, 3, 2, 5, 3, 6]} useFlex />
</h2>
<div className="short-description">
<div className="line">
<SkeletonTexts wordLengths={[2, 4, 3, 6, 2, 7]} useFlex />
</div>
<div className="line">
<SkeletonTexts wordLengths={[3, 2, 3, 4, 7, 3]} useFlex />
</div>
<div className="line">
<SkeletonTexts wordLengths={[4, 3, 3]} />
</div>
</div>
<div className="tags-skeleton">
<Skeleton width="6rem" marginRight="0.875rem" />
<Skeleton width="4rem" marginRight="0.875rem" />
<Skeleton width="5rem" noSpacing />
</div>
<div className="subinfo">
<Skeleton width="3em" marginRight="1rem" />
<Skeleton width="6em" noSpacing />
</div>
</SkeletonBlock>
);
}
const SkeletonBlock = styled(PostCardBlock)`
h2 {
display: flex;
margin-top: 1.375rem;
margin-bottom: 0.375rem;
}
.user-thumbnail-skeleton {
width: 3rem;
height: 3rem;
${media.small} {
width: 2rem;
height: 2rem;
}
}
.thumbnail-skeleton-wrapper {
width: 100%;
padding-top: 52.35%;
position: relative;
.skeleton {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
}
.short-description {
margin-bottom: 2rem;
margin-top: 1rem;
font-size: 1rem;
.line {
display: flex;
}
.line + .line {
margin-top: 0.5rem;
}
}
.tags-skeleton {
line-height: 1;
font-size: 2rem;
${media.small} {
font-size: 1.25rem;
}
}
`;
export default React.memo(FlatPostCard);