mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React, {useEffect, useRef} from 'react'
2import {StyleSheet, useWindowDimensions, View} from 'react-native'
3import {runOnJS} from 'react-native-reanimated'
4import {AppBskyFeedDefs} from '@atproto/api'
5import {msg, Trans} from '@lingui/macro'
6import {useLingui} from '@lingui/react'
7
8import {moderatePost_wrapped as moderatePost} from '#/lib/moderatePost_wrapped'
9import {ScrollProvider} from '#/lib/ScrollContext'
10import {isAndroid, isNative, isWeb} from '#/platform/detection'
11import {useModerationOpts} from '#/state/preferences/moderation-opts'
12import {
13 sortThread,
14 ThreadBlocked,
15 ThreadNode,
16 ThreadNotFound,
17 ThreadPost,
18 usePostThreadQuery,
19} from '#/state/queries/post-thread'
20import {usePreferencesQuery} from '#/state/queries/preferences'
21import {useSession} from '#/state/session'
22import {useInitialNumToRender} from 'lib/hooks/useInitialNumToRender'
23import {usePalette} from 'lib/hooks/usePalette'
24import {useSetTitle} from 'lib/hooks/useSetTitle'
25import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries'
26import {sanitizeDisplayName} from 'lib/strings/display-names'
27import {cleanError} from 'lib/strings/errors'
28import {ListFooter, ListMaybePlaceholder} from '#/components/Lists'
29import {ComposePrompt} from '../composer/Prompt'
30import {List, ListMethods} from '../util/List'
31import {Text} from '../util/text/Text'
32import {ViewHeader} from '../util/ViewHeader'
33import {PostThreadItem} from './PostThreadItem'
34
35// FlatList maintainVisibleContentPosition breaks if too many items
36// are prepended. This seems to be an optimal number based on *shrug*.
37const PARENTS_CHUNK_SIZE = 15
38
39const MAINTAIN_VISIBLE_CONTENT_POSITION = {
40 // We don't insert any elements before the root row while loading.
41 // So the row we want to use as the scroll anchor is the first row.
42 minIndexForVisible: 0,
43}
44
45const TOP_COMPONENT = {_reactKey: '__top_component__'}
46const REPLY_PROMPT = {_reactKey: '__reply__'}
47const LOAD_MORE = {_reactKey: '__load_more__'}
48
49type YieldedItem = ThreadPost | ThreadBlocked | ThreadNotFound
50type RowItem =
51 | YieldedItem
52 // TODO: TS doesn't actually enforce it's one of these, it only enforces matching shape.
53 | typeof TOP_COMPONENT
54 | typeof REPLY_PROMPT
55 | typeof LOAD_MORE
56
57type ThreadSkeletonParts = {
58 parents: YieldedItem[]
59 highlightedPost: ThreadNode
60 replies: YieldedItem[]
61}
62
63const keyExtractor = (item: RowItem) => {
64 return item._reactKey
65}
66
67export function PostThread({
68 uri,
69 onCanReply,
70 onPressReply,
71}: {
72 uri: string | undefined
73 onCanReply: (canReply: boolean) => void
74 onPressReply: () => unknown
75}) {
76 const {hasSession} = useSession()
77 const {_} = useLingui()
78 const pal = usePalette('default')
79 const {isMobile, isTabletOrMobile} = useWebMediaQueries()
80 const initialNumToRender = useInitialNumToRender()
81 const {height: windowHeight} = useWindowDimensions()
82
83 const {data: preferences} = usePreferencesQuery()
84 const {
85 isFetching,
86 isError: isThreadError,
87 error: threadError,
88 refetch,
89 data: thread,
90 } = usePostThreadQuery(uri)
91
92 const treeView = React.useMemo(
93 () =>
94 !!preferences?.threadViewPrefs?.lab_treeViewEnabled &&
95 hasBranchingReplies(thread),
96 [preferences?.threadViewPrefs, thread],
97 )
98 const rootPost = thread?.type === 'post' ? thread.post : undefined
99 const rootPostRecord = thread?.type === 'post' ? thread.record : undefined
100
101 const moderationOpts = useModerationOpts()
102 const isNoPwi = React.useMemo(() => {
103 const mod =
104 rootPost && moderationOpts
105 ? moderatePost(rootPost, moderationOpts)
106 : undefined
107 return !!mod
108 ?.ui('contentList')
109 .blurs.find(
110 cause =>
111 cause.type === 'label' &&
112 cause.labelDef.identifier === '!no-unauthenticated',
113 )
114 }, [rootPost, moderationOpts])
115
116 // Values used for proper rendering of parents
117 const ref = useRef<ListMethods>(null)
118 const highlightedPostRef = useRef<View | null>(null)
119 const [maxParents, setMaxParents] = React.useState(
120 isWeb ? Infinity : PARENTS_CHUNK_SIZE,
121 )
122 const [maxReplies, setMaxReplies] = React.useState(50)
123
124 useSetTitle(
125 rootPost && !isNoPwi
126 ? `${sanitizeDisplayName(
127 rootPost.author.displayName || `@${rootPost.author.handle}`,
128 )}: "${rootPostRecord!.text}"`
129 : '',
130 )
131
132 // On native, this is going to start out `true`. We'll toggle it to `false` after the initial render if flushed.
133 // This ensures that the first render contains no parents--even if they are already available in the cache.
134 // We need to delay showing them so that we can use maintainVisibleContentPosition to keep the main post on screen.
135 // On the web this is not necessary because we can synchronously adjust the scroll in onContentSizeChange instead.
136 const [deferParents, setDeferParents] = React.useState(isNative)
137
138 const skeleton = React.useMemo(() => {
139 const threadViewPrefs = preferences?.threadViewPrefs
140 if (!threadViewPrefs || !thread) return null
141
142 return createThreadSkeleton(
143 sortThread(thread, threadViewPrefs),
144 hasSession,
145 treeView,
146 )
147 }, [thread, preferences?.threadViewPrefs, hasSession, treeView])
148
149 const error = React.useMemo(() => {
150 if (AppBskyFeedDefs.isNotFoundPost(thread)) {
151 return {
152 title: _(msg`Post not found`),
153 message: _(msg`The post may have been deleted.`),
154 }
155 } else if (skeleton?.highlightedPost.type === 'blocked') {
156 return {
157 title: _(msg`Post hidden`),
158 message: _(
159 msg`You have blocked the author or you have been blocked by the author.`,
160 ),
161 }
162 } else if (threadError?.message.startsWith('Post not found')) {
163 return {
164 title: _(msg`Post not found`),
165 message: _(msg`The post may have been deleted.`),
166 }
167 } else if (isThreadError) {
168 return {
169 message: threadError ? cleanError(threadError) : undefined,
170 }
171 }
172
173 return null
174 }, [thread, skeleton?.highlightedPost, isThreadError, _, threadError])
175
176 useEffect(() => {
177 if (error) {
178 onCanReply(false)
179 } else if (rootPost) {
180 onCanReply(!rootPost.viewer?.replyDisabled)
181 }
182 }, [rootPost, onCanReply, error])
183
184 // construct content
185 const posts = React.useMemo(() => {
186 if (!skeleton) return []
187
188 const {parents, highlightedPost, replies} = skeleton
189 let arr: RowItem[] = []
190 if (highlightedPost.type === 'post') {
191 const isRoot =
192 !highlightedPost.parent && !highlightedPost.ctx.isParentLoading
193 if (isRoot) {
194 // No parents to load.
195 arr.push(TOP_COMPONENT)
196 } else {
197 if (highlightedPost.ctx.isParentLoading || deferParents) {
198 // We're loading parents of the highlighted post.
199 // In this case, we don't render anything above the post.
200 // If you add something here, you'll need to update both
201 // maintainVisibleContentPosition and onContentSizeChange
202 // to "hold onto" the correct row instead of the first one.
203 } else {
204 // Everything is loaded
205 let startIndex = Math.max(0, parents.length - maxParents)
206 if (startIndex === 0) {
207 arr.push(TOP_COMPONENT)
208 } else {
209 // When progressively revealing parents, rendering a placeholder
210 // here will cause scrolling jumps. Don't add it unless you test it.
211 // QT'ing this thread is a great way to test all the scrolling hacks:
212 // https://bsky.app/profile/www.mozzius.dev/post/3kjqhblh6qk2o
213 }
214 for (let i = startIndex; i < parents.length; i++) {
215 arr.push(parents[i])
216 }
217 }
218 }
219 arr.push(highlightedPost)
220 if (!highlightedPost.post.viewer?.replyDisabled) {
221 arr.push(REPLY_PROMPT)
222 }
223 for (let i = 0; i < replies.length; i++) {
224 arr.push(replies[i])
225 if (i === maxReplies) {
226 break
227 }
228 }
229 }
230 return arr
231 }, [skeleton, deferParents, maxParents, maxReplies])
232
233 // This is only used on the web to keep the post in view when its parents load.
234 // On native, we rely on `maintainVisibleContentPosition` instead.
235 const didAdjustScrollWeb = useRef<boolean>(false)
236 const onContentSizeChangeWeb = React.useCallback(() => {
237 // only run once
238 if (didAdjustScrollWeb.current) {
239 return
240 }
241 // wait for loading to finish
242 if (thread?.type === 'post' && !!thread.parent) {
243 function onMeasure(pageY: number) {
244 ref.current?.scrollToOffset({
245 animated: false,
246 offset: pageY,
247 })
248 }
249 // Measure synchronously to avoid a layout jump.
250 const domNode = highlightedPostRef.current
251 if (domNode) {
252 const pageY = (domNode as any as Element).getBoundingClientRect().top
253 onMeasure(pageY)
254 }
255 didAdjustScrollWeb.current = true
256 }
257 }, [thread])
258
259 // On native, we reveal parents in chunks. Although they're all already
260 // loaded and FlatList already has its own virtualization, unfortunately FlatList
261 // has a bug that causes the content to jump around if too many items are getting
262 // prepended at once. It also jumps around if items get prepended during scroll.
263 // To work around this, we prepend rows after scroll bumps against the top and rests.
264 const needsBumpMaxParents = React.useRef(false)
265 const onStartReached = React.useCallback(() => {
266 if (skeleton?.parents && maxParents < skeleton.parents.length) {
267 needsBumpMaxParents.current = true
268 }
269 }, [maxParents, skeleton?.parents])
270 const bumpMaxParentsIfNeeded = React.useCallback(() => {
271 if (!isNative) {
272 return
273 }
274 if (needsBumpMaxParents.current) {
275 needsBumpMaxParents.current = false
276 setMaxParents(n => n + PARENTS_CHUNK_SIZE)
277 }
278 }, [])
279 const onScrollToTop = bumpMaxParentsIfNeeded
280 const onMomentumEnd = React.useCallback(() => {
281 'worklet'
282 runOnJS(bumpMaxParentsIfNeeded)()
283 }, [bumpMaxParentsIfNeeded])
284
285 const onEndReached = React.useCallback(() => {
286 if (isFetching || posts.length < maxReplies) return
287 setMaxReplies(prev => prev + 50)
288 }, [isFetching, maxReplies, posts.length])
289
290 const renderItem = React.useCallback(
291 ({item, index}: {item: RowItem; index: number}) => {
292 if (item === TOP_COMPONENT) {
293 return isTabletOrMobile ? (
294 <ViewHeader
295 title={_(msg({message: `Post`, context: 'description'}))}
296 />
297 ) : null
298 } else if (item === REPLY_PROMPT && hasSession) {
299 return (
300 <View>
301 {!isMobile && <ComposePrompt onPressCompose={onPressReply} />}
302 </View>
303 )
304 } else if (isThreadNotFound(item)) {
305 return (
306 <View style={[pal.border, pal.viewLight, styles.itemContainer]}>
307 <Text type="lg-bold" style={pal.textLight}>
308 <Trans>Deleted post.</Trans>
309 </Text>
310 </View>
311 )
312 } else if (isThreadBlocked(item)) {
313 return (
314 <View style={[pal.border, pal.viewLight, styles.itemContainer]}>
315 <Text type="lg-bold" style={pal.textLight}>
316 <Trans>Blocked post.</Trans>
317 </Text>
318 </View>
319 )
320 } else if (isThreadPost(item)) {
321 const prev = isThreadPost(posts[index - 1])
322 ? (posts[index - 1] as ThreadPost)
323 : undefined
324 const next = isThreadPost(posts[index - 1])
325 ? (posts[index - 1] as ThreadPost)
326 : undefined
327 const hasUnrevealedParents =
328 index === 0 &&
329 skeleton?.parents &&
330 maxParents < skeleton.parents.length
331 return (
332 <View
333 ref={item.ctx.isHighlightedPost ? highlightedPostRef : undefined}
334 onLayout={deferParents ? () => setDeferParents(false) : undefined}>
335 <PostThreadItem
336 post={item.post}
337 record={item.record}
338 treeView={treeView}
339 depth={item.ctx.depth}
340 prevPost={prev}
341 nextPost={next}
342 isHighlightedPost={item.ctx.isHighlightedPost}
343 hasMore={item.ctx.hasMore}
344 showChildReplyLine={item.ctx.showChildReplyLine}
345 showParentReplyLine={item.ctx.showParentReplyLine}
346 hasPrecedingItem={
347 !!prev?.ctx.showChildReplyLine || !!hasUnrevealedParents
348 }
349 onPostReply={refetch}
350 />
351 </View>
352 )
353 }
354 return null
355 },
356 [
357 hasSession,
358 isTabletOrMobile,
359 _,
360 isMobile,
361 onPressReply,
362 pal.border,
363 pal.viewLight,
364 pal.textLight,
365 posts,
366 skeleton?.parents,
367 maxParents,
368 deferParents,
369 treeView,
370 refetch,
371 ],
372 )
373
374 if (!thread || !preferences || error) {
375 return (
376 <ListMaybePlaceholder
377 isLoading={!error}
378 isError={Boolean(error)}
379 noEmpty
380 onRetry={refetch}
381 errorTitle={error?.title}
382 errorMessage={error?.message}
383 />
384 )
385 }
386
387 return (
388 <ScrollProvider onMomentumEnd={onMomentumEnd}>
389 <List
390 ref={ref}
391 data={posts}
392 renderItem={renderItem}
393 keyExtractor={keyExtractor}
394 onContentSizeChange={isNative ? undefined : onContentSizeChangeWeb}
395 onStartReached={onStartReached}
396 onEndReached={onEndReached}
397 onEndReachedThreshold={2}
398 onScrollToTop={onScrollToTop}
399 maintainVisibleContentPosition={
400 isNative ? MAINTAIN_VISIBLE_CONTENT_POSITION : undefined
401 }
402 // @ts-ignore our .web version only -prf
403 desktopFixedHeight
404 removeClippedSubviews={isAndroid ? false : undefined}
405 ListFooterComponent={
406 <ListFooter
407 // Using `isFetching` over `isFetchingNextPage` is done on purpose here so we get the loader on
408 // initial render
409 isFetchingNextPage={isFetching}
410 error={cleanError(threadError)}
411 onRetry={refetch}
412 // 300 is based on the minimum height of a post. This is enough extra height for the `maintainVisPos` to
413 // work without causing weird jumps on web or glitches on native
414 height={windowHeight - 200}
415 />
416 }
417 initialNumToRender={initialNumToRender}
418 windowSize={11}
419 />
420 </ScrollProvider>
421 )
422}
423
424function isThreadPost(v: unknown): v is ThreadPost {
425 return !!v && typeof v === 'object' && 'type' in v && v.type === 'post'
426}
427
428function isThreadNotFound(v: unknown): v is ThreadNotFound {
429 return !!v && typeof v === 'object' && 'type' in v && v.type === 'not-found'
430}
431
432function isThreadBlocked(v: unknown): v is ThreadBlocked {
433 return !!v && typeof v === 'object' && 'type' in v && v.type === 'blocked'
434}
435
436function createThreadSkeleton(
437 node: ThreadNode,
438 hasSession: boolean,
439 treeView: boolean,
440): ThreadSkeletonParts | null {
441 if (!node) return null
442
443 return {
444 parents: Array.from(flattenThreadParents(node, hasSession)),
445 highlightedPost: node,
446 replies: Array.from(flattenThreadReplies(node, hasSession, treeView)),
447 }
448}
449
450function* flattenThreadParents(
451 node: ThreadNode,
452 hasSession: boolean,
453): Generator<YieldedItem, void> {
454 if (node.type === 'post') {
455 if (node.parent) {
456 yield* flattenThreadParents(node.parent, hasSession)
457 }
458 if (!node.ctx.isHighlightedPost) {
459 yield node
460 }
461 } else if (node.type === 'not-found') {
462 yield node
463 } else if (node.type === 'blocked') {
464 yield node
465 }
466}
467
468function* flattenThreadReplies(
469 node: ThreadNode,
470 hasSession: boolean,
471 treeView: boolean,
472): Generator<YieldedItem, void> {
473 if (node.type === 'post') {
474 if (!hasSession && hasPwiOptOut(node)) {
475 return
476 }
477 if (!node.ctx.isHighlightedPost) {
478 yield node
479 }
480 if (node.replies?.length) {
481 for (const reply of node.replies) {
482 yield* flattenThreadReplies(reply, hasSession, treeView)
483 if (!treeView && !node.ctx.isHighlightedPost) {
484 break
485 }
486 }
487 }
488 } else if (node.type === 'not-found') {
489 yield node
490 } else if (node.type === 'blocked') {
491 yield node
492 }
493}
494
495function hasPwiOptOut(node: ThreadPost) {
496 return !!node.post.author.labels?.find(l => l.val === '!no-unauthenticated')
497}
498
499function hasBranchingReplies(node?: ThreadNode) {
500 if (!node) {
501 return false
502 }
503 if (node.type !== 'post') {
504 return false
505 }
506 if (!node.replies) {
507 return false
508 }
509 if (node.replies.length === 1) {
510 return hasBranchingReplies(node.replies[0])
511 }
512 return true
513}
514
515const styles = StyleSheet.create({
516 itemContainer: {
517 borderTopWidth: 1,
518 paddingHorizontal: 18,
519 paddingVertical: 18,
520 },
521})