-
Notifications
You must be signed in to change notification settings - Fork 22
/
FeedPostArticleMarkdownView.tsx
399 lines (373 loc) · 13 KB
/
FeedPostArticleMarkdownView.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import React, { FC, useEffect, useMemo, useRef, useState } from "react";
import { ScrollView, useWindowDimensions, View, ViewStyle } from "react-native";
import Animated, {
useAnimatedRef,
useAnimatedScrollHandler,
useSharedValue,
} from "react-native-reanimated";
import RenderHtml from "react-native-render-html";
import { Post } from "@/api/feed/v1/feed";
import { BrandText } from "@/components/BrandText";
import { ScreenContainer } from "@/components/ScreenContainer";
import { MobileTitle } from "@/components/ScreenContainer/ScreenContainerMobile";
import { ScreenTitle } from "@/components/ScreenContainer/ScreenTitle";
import { CommentsContainer } from "@/components/cards/CommentsContainer";
import { CreateShortPostButton } from "@/components/socialFeed/NewsFeed/CreateShortPost/CreateShortPostButton";
import { CreateShortPostModal } from "@/components/socialFeed/NewsFeed/CreateShortPost/CreateShortPostModal";
import {
NewsFeedInput,
NewsFeedInputHandle,
} from "@/components/socialFeed/NewsFeed/NewsFeedInput";
import { RefreshButton } from "@/components/socialFeed/NewsFeed/RefreshButton/RefreshButton";
import { RefreshButtonRound } from "@/components/socialFeed/NewsFeed/RefreshButton/RefreshButtonRound";
import { SocialCardFooter } from "@/components/socialFeed/SocialCard/SocialCardFooter";
import { SocialCardHeader } from "@/components/socialFeed/SocialCard/SocialCardHeader";
import { SocialCardWrapper } from "@/components/socialFeed/SocialCard/SocialCardWrapper";
import { SpacerColumn, SpacerRow } from "@/components/spacer";
import { useFetchComments } from "@/hooks/feed/useFetchComments";
import { useAppNavigation } from "@/hooks/navigation/useAppNavigation";
import { useIsMobile } from "@/hooks/useIsMobile";
import { useMaxResolution } from "@/hooks/useMaxResolution";
import { useNSUserInfo } from "@/hooks/useNSUserInfo";
import { parseUserId } from "@/networks";
import {
renderHtmlTagStyles,
articleMd as md,
renderHtmlDomVisitors,
} from "@/utils/feed/markdown";
import { zodTryParseJSON } from "@/utils/sanitize";
import {
ARTICLE_MAX_WIDTH,
DEFAULT_USERNAME,
LINES_HORIZONTAL_SPACE,
SOCIAl_CARD_BORDER_RADIUS,
} from "@/utils/social-feed";
import { neutral33 } from "@/utils/style/colors";
import {
layout,
RESPONSIVE_BREAKPOINT_S,
screenContentMaxWidth,
} from "@/utils/style/layout";
import { tinyAddress } from "@/utils/text";
import {
OnPressReplyType,
PostCategory,
ReplyToType,
ZodSocialFeedArticleMarkdownMetadata,
} from "@/utils/types/feed";
// TODO: It's a copy of FeedPostArticleView.tsx, just made waiting for a posts UI (and data) refacto
const contentPaddingHorizontal = layout.spacing_x2;
export const FeedPostArticleMarkdownView: FC<{
post: Post;
refetchPost: () => Promise<any>;
isLoadingPost?: boolean;
}> = ({ post, refetchPost, isLoadingPost }) => {
const navigation = useAppNavigation();
const { width: windowWidth } = useWindowDimensions();
const { width } = useMaxResolution();
const isMobile = useIsMobile();
const [parentOffsetValue, setParentOffsetValue] = useState(0);
const authorId = post?.authorId;
const authorNSInfo = useNSUserInfo(authorId);
const [, authorAddress] = parseUserId(post?.authorId);
const username = authorNSInfo?.metadata?.tokenId || authorAddress;
const [localPost, setLocalPost] = useState(post);
const feedInputRef = useRef<NewsFeedInputHandle>(null);
const [replyTo, setReplyTo] = useState<ReplyToType>();
const aref = useAnimatedRef<Animated.ScrollView>();
const [flatListContentOffsetY, setFlatListContentOffsetY] = useState(0);
const [articleOffsetY, setArticleOffsetY] = useState(0);
const [articleWidth, setArticleWidth] = useState(0);
const [renderHtmlWidth, setRenderHtmlWidth] = useState(0);
const isGoingUp = useSharedValue(false);
const isLoadingSharedValue = useSharedValue(true);
const [isCreateModalVisible, setCreateModalVisible] = useState(false);
const {
data: comments,
refetch: refetchComments,
hasNextPage,
fetchNextPage,
isLoading: isLoadingComments,
} = useFetchComments({
parentId: post.id,
totalCount: post.subPostLength,
enabled: true,
});
const isNextPageAvailable = useSharedValue(hasNextPage);
const articleMetadata = zodTryParseJSON(
ZodSocialFeedArticleMarkdownMetadata,
post.metadata,
);
const message = articleMetadata?.message;
const html = message ? md.render(message) : null;
const title = articleMetadata?.title;
const location = articleMetadata?.location;
const headerLabel = useMemo(() => {
const authorDisplayName =
authorNSInfo?.metadata?.tokenId ||
tinyAddress(authorAddress) ||
DEFAULT_USERNAME;
return `Article by ${authorDisplayName}`;
}, [authorNSInfo?.metadata?.tokenId, authorAddress]);
const onPressReply: OnPressReplyType = (data) => {
feedInputRef.current?.resetForm();
setReplyTo(data);
feedInputRef.current?.setValue(`@${username} `);
feedInputRef.current?.focusInput();
};
const handleSubmitInProgress = () => {
if (replyTo?.parentId && replyTo.yOffsetValue)
aref.current?.scrollTo(replyTo.yOffsetValue);
else aref.current?.scrollTo(0);
};
const scrollHandler = useAnimatedScrollHandler(
{
onScroll: (event) => {
let offsetPadding = 40;
offsetPadding += event.layoutMeasurement.height;
if (
event.contentOffset.y >= event.contentSize.height - offsetPadding &&
isNextPageAvailable.value
) {
fetchNextPage();
}
if (flatListContentOffsetY > event.contentOffset.y) {
isGoingUp.value = true;
} else if (flatListContentOffsetY < event.contentOffset.y) {
isGoingUp.value = false;
}
setFlatListContentOffsetY(event.contentOffset.y);
},
},
[post.id],
);
useEffect(() => {
isLoadingSharedValue.value = isLoadingPost || isLoadingComments;
}, [isLoadingPost, isLoadingComments, isLoadingSharedValue]);
useEffect(() => {
if (post.category === PostCategory.Video)
navigation.replace("FeedPostView", {
id: post.id,
});
}, [post.category, post.id, navigation]);
useEffect(() => {
// HECK: updated state was not showing up in scrollhander
isNextPageAvailable.value = hasNextPage;
}, [hasNextPage, isNextPageAvailable]);
if (!articleMetadata || !html) return null;
return (
<ScreenContainer
forceNetworkId={post.networkId}
fullWidth
responsive
noMargin
headerChildren={<ScreenTitle>{headerLabel}</ScreenTitle>}
onBackPress={() =>
post?.parentPostIdentifier
? navigation.navigate("FeedPostView", {
id: post.id,
})
: navigation.canGoBack()
? navigation.goBack()
: navigation.navigate("Feed")
}
footerChildren
noScroll
>
<Animated.ScrollView
ref={aref}
contentContainerStyle={contentContainerCStyle}
onScroll={scrollHandler}
scrollEventThrottle={1}
>
{/* ScreenContainer has noScroll, so we need to add MobileTitle here */}
{isMobile && <MobileTitle title={headerLabel.toUpperCase()} />}
<View
style={{
width: windowWidth < RESPONSIVE_BREAKPOINT_S ? windowWidth : width,
maxWidth: screenContentMaxWidth,
alignItems: "center",
paddingVertical: layout.spacing_x2,
}}
>
<View
onLayout={({
nativeEvent: {
layout: { height, width },
},
}) => {
setArticleOffsetY(height);
setArticleWidth(width);
}}
style={{
width: "100%",
maxWidth: ARTICLE_MAX_WIDTH + contentPaddingHorizontal * 2,
borderBottomWidth: 1,
borderBottomColor: neutral33,
borderRadius:
windowWidth < RESPONSIVE_BREAKPOINT_S
? 0
: SOCIAl_CARD_BORDER_RADIUS,
paddingHorizontal: contentPaddingHorizontal,
paddingBottom: layout.spacing_x2,
}}
>
<SocialCardWrapper post={localPost} refetchFeed={refetchPost}>
{/*========== Article title, author info */}
{!!title && <BrandText>{title}</BrandText>}
<SpacerColumn size={1.5} />
<SocialCardHeader
authorId={localPost.authorId}
createdAt={post.createdAt}
postWithLocationId={location && localPost.id}
/>
<SpacerColumn size={1.5} />
{/*========== Article content */}
<View
style={{
minHeight: "auto",
position: "relative",
zIndex: 99999,
}}
>
<ScrollView
onLayout={(e) =>
setRenderHtmlWidth(e.nativeEvent.layout.width)
}
>
<RenderHtml
source={{ html }}
tagsStyles={renderHtmlTagStyles}
domVisitors={renderHtmlDomVisitors}
contentWidth={renderHtmlWidth}
/>
</ScrollView>
</View>
<SpacerColumn size={1.5} />
{/*========== Actions */}
<SocialCardFooter
cardWidth={articleWidth}
isPostConsultation
post={localPost}
handleReply={() => onPressReply({ username })}
refetchFeed={refetchPost}
setPost={setLocalPost}
/>
</SocialCardWrapper>
</View>
{/*========== Refresh button no mobile */}
{!isMobile && (
<Animated.View
style={[
{
position: "absolute",
top: articleOffsetY + 6,
flexDirection: "row",
width: "100%",
alignItems: "center",
justifyContent: "center",
zIndex: 10000,
},
]}
>
<RefreshButton
isRefreshing={isLoadingSharedValue}
onPress={() => {
refetchComments();
}}
/>
</Animated.View>
)}
<View
onLayout={(e) => setParentOffsetValue(e.nativeEvent.layout.y)}
style={{ width: "100%" }}
>
<CommentsContainer
cardWidth={
isMobile ? articleWidth : articleWidth - LINES_HORIZONTAL_SPACE
}
comments={comments}
onPressReply={onPressReply}
parentOffsetValue={parentOffsetValue}
/>
</View>
</View>
{/*========== Comment input */}
{!isMobile && (
<>
<SpacerColumn size={2.5} />
<NewsFeedInput
style={{ alignSelf: "center" }}
ref={feedInputRef}
type="comment"
replyTo={replyTo}
parentId={post.localIdentifier}
onSubmitInProgress={handleSubmitInProgress}
onSubmitSuccess={() => {
setReplyTo(undefined);
refetchComments();
}}
/>
</>
)}
</Animated.ScrollView>
{/*========== Refresh button mobile */}
{flatListContentOffsetY >= articleOffsetY + 66 && !isMobile && (
<View style={floatingActionsCStyle}>
<RefreshButtonRound
isRefreshing={isLoadingSharedValue}
onPress={refetchComments}
/>
</View>
)}
{/*========== Refresh button and Comment button mobile */}
{isMobile && (
<>
<SpacerColumn size={2} />
<View
style={{
flexDirection: "row",
alignItems: "center",
justifyContent: "center",
}}
>
<CreateShortPostButton
label="Create Comment"
onPress={() => setCreateModalVisible(true)}
/>
<SpacerRow size={1.5} />
<RefreshButton
isRefreshing={isLoadingSharedValue}
onPress={() => {
refetchComments();
}}
/>
</View>
<SpacerColumn size={2} />
</>
)}
<CreateShortPostModal
label="Create a Comment"
isVisible={isCreateModalVisible}
onClose={() => setCreateModalVisible(false)}
onSubmitSuccess={() => {
setReplyTo(undefined);
refetchComments();
}}
replyTo={replyTo}
parentId={post.localIdentifier}
/>
</ScreenContainer>
);
};
const contentContainerCStyle: ViewStyle = {
alignItems: "center",
alignSelf: "center",
};
const floatingActionsCStyle: ViewStyle = {
position: "absolute",
justifyContent: "center",
alignItems: "center",
right: 68,
bottom: 230,
};