tooot/src/screens/Tabs/Shared/Toot.tsx

205 lines
7.0 KiB
TypeScript
Raw Normal View History

2022-12-03 16:50:54 +01:00
import { HeaderLeft } from '@components/Header'
2021-02-08 23:47:20 +01:00
import Timeline from '@components/Timeline'
2021-02-27 16:33:54 +01:00
import TimelineDefault from '@components/Timeline/Default'
2021-08-29 15:25:38 +02:00
import { TabSharedStackScreenProps } from '@utils/navigation/navigators'
2021-02-27 16:33:54 +01:00
import { QueryKeyTimeline } from '@utils/queryHooks/timeline'
2022-08-07 01:18:10 +02:00
import React, { useEffect, useRef, useState } from 'react'
2022-12-03 16:50:54 +01:00
import { useTranslation } from 'react-i18next'
2022-12-16 00:21:53 +01:00
import { FlatList, View } from 'react-native'
import { InfiniteQueryObserver, useQueryClient } from '@tanstack/react-query'
2022-12-16 00:21:53 +01:00
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2020-10-29 14:52:28 +01:00
2021-08-29 15:25:38 +02:00
const TabSharedToot: React.FC<TabSharedStackScreenProps<'Tab-Shared-Toot'>> = ({
2022-12-03 16:50:54 +01:00
navigation,
2020-10-29 14:52:28 +01:00
route: {
2021-02-13 01:26:02 +01:00
params: { toot, rootQueryKey }
2020-10-29 14:52:28 +01:00
}
2020-10-31 21:04:46 +01:00
}) => {
2022-12-16 00:21:53 +01:00
const { colors } = useTheme()
2022-12-03 16:50:54 +01:00
const { t } = useTranslation('screenTabs')
useEffect(() => {
navigation.setOptions({
title: t('shared.toot.name'),
headerLeft: () => <HeaderLeft onPress={() => navigation.goBack()} />
})
}, [])
2022-10-27 22:51:02 +02:00
const queryKey: QueryKeyTimeline = ['Timeline', { page: 'Toot', toot: toot.id }]
2021-02-27 16:33:54 +01:00
const flRef = useRef<FlatList>(null)
2022-01-03 21:25:53 +01:00
const [itemsLength, setItemsLength] = useState(0)
2021-02-27 16:33:54 +01:00
const scrolled = useRef(false)
const queryClient = useQueryClient()
const observer = new InfiniteQueryObserver(queryClient, {
queryKey,
enabled: false
})
2022-12-16 00:21:53 +01:00
const replyLevels = useRef<{ id: string; level: number }[]>([])
const data = useRef<Mastodon.Status[]>()
const highlightIndex = useRef<number>(0)
2021-02-27 16:33:54 +01:00
useEffect(() => {
return observer.subscribe(result => {
2021-02-27 16:33:54 +01:00
if (result.isSuccess) {
const flattenData = result.data?.pages
? // @ts-ignore
result.data.pages.flatMap(d => [...d.body])
: []
// Auto go back when toot page is empty
2022-05-18 00:11:31 +02:00
if (flattenData.length < 1) {
2021-02-27 16:33:54 +01:00
navigation.goBack()
return
2021-02-27 16:33:54 +01:00
}
2022-12-16 00:21:53 +01:00
data.current = flattenData
highlightIndex.current = flattenData.findIndex(({ id }) => id === toot.id)
for (const [index, status] of flattenData.entries()) {
if (status.id === toot.id) continue
if (status.in_reply_to_id === toot.id) continue
if (!replyLevels.current.find(reply => reply.id === status.in_reply_to_id)) {
const prevLevel =
replyLevels.current.find(reply => reply.id === flattenData[index - 1].in_reply_to_id)
?.level || 0
replyLevels.current.push({
id: status.in_reply_to_id,
level: prevLevel + 1
})
}
}
2022-02-11 21:30:45 +01:00
setItemsLength(flattenData.length)
2021-02-27 16:33:54 +01:00
if (!scrolled.current) {
scrolled.current = true
2022-01-30 22:51:03 +01:00
const pointer = flattenData.findIndex(({ id }) => id === toot.id)
2022-02-17 23:10:32 +01:00
if (pointer < 1) return
2022-12-04 17:56:47 +01:00
const length = flRef.current?.props.data?.length
if (!length) return
2022-01-08 11:19:24 +01:00
try {
2022-02-11 21:30:45 +01:00
setTimeout(() => {
2022-10-27 22:51:02 +02:00
try {
flRef.current?.scrollToIndex({
index: pointer,
viewOffset: 100
})
} catch {}
2022-02-11 21:30:45 +01:00
}, 500)
2022-05-18 00:11:31 +02:00
} catch (error) {
return
}
2021-02-27 16:33:54 +01:00
}
}
})
2022-12-16 00:21:53 +01:00
}, [scrolled.current, replyLevels.current])
2021-02-27 16:33:54 +01:00
2021-02-13 01:26:02 +01:00
return (
<Timeline
2021-02-27 16:33:54 +01:00
flRef={flRef}
queryKey={queryKey}
2022-12-11 12:12:46 +01:00
queryOptions={{ staleTime: 0, refetchOnMount: true }}
2022-08-07 01:18:10 +02:00
customProps={{
2022-12-16 00:21:53 +01:00
renderItem: ({ item, index }) => {
const levels = {
previous:
replyLevels.current.find(
reply => reply.id === data.current?.[index - 1]?.in_reply_to_id
)?.level || 0,
current:
replyLevels.current.find(reply => reply.id === item.in_reply_to_id)?.level || 0,
next:
replyLevels.current.find(
reply => reply.id === data.current?.[index + 1]?.in_reply_to_id
)?.level || 0
}
return (
<>
<TimelineDefault
item={{ ...item, content: `${item.content}${JSON.stringify(levels)}` }}
queryKey={queryKey}
rootQueryKey={rootQueryKey}
highlighted={toot.id === item.id}
isConversation={toot.id !== item.id}
/>
{Array.from(Array(levels.current)).map((_, i) => {
if (index < highlightIndex.current) return null
if (
levels.previous + 1 === levels.current ||
(levels.previous && levels.current && levels.previous === levels.current)
) {
return (
<View
key={i}
style={{
position: 'absolute',
top: 0,
left: StyleConstants.Spacing.Global.PagePadding / 2 + 8 * i,
height:
levels.current === levels.next
? StyleConstants.Spacing.Global.PagePadding
: StyleConstants.Spacing.Global.PagePadding + StyleConstants.Avatar.XS,
borderLeftColor: colors.border,
borderLeftWidth: 1
}}
/>
)
} else {
return null
}
})}
{Array.from(Array(levels.next)).map((_, i) => {
if (index < highlightIndex.current) return null
if (
levels.current + 1 === levels.next ||
(levels.current && levels.next && levels.current === levels.next)
) {
return (
<View
key={i}
style={{
position: 'absolute',
top:
levels.current + 1 === levels.next && levels.next > i + 1
? StyleConstants.Spacing.Global.PagePadding + StyleConstants.Avatar.XS
: StyleConstants.Spacing.Global.PagePadding,
left: StyleConstants.Spacing.Global.PagePadding / 2 + 8 * i,
height: 200,
borderLeftColor: colors.border,
borderLeftWidth: 1
}}
/>
)
} else {
return null
}
})}
</>
)
},
2022-08-07 01:18:10 +02:00
onScrollToIndexFailed: error => {
const offset = error.averageItemLength * error.index
flRef.current?.scrollToOffset({ offset })
try {
error.index < itemsLength &&
setTimeout(
() =>
flRef.current?.scrollToIndex({
index: error.index,
viewOffset: 100
}),
500
)
} catch {}
}
}}
2021-02-13 01:26:02 +01:00
disableRefresh
disableInfinity
/>
)
2020-10-29 14:52:28 +01:00
}
2020-10-31 21:04:46 +01:00
2021-01-30 01:29:15 +01:00
export default TabSharedToot