tooot/src/components/Timelines/Timeline.tsx

178 lines
4.9 KiB
TypeScript
Raw Normal View History

2020-12-12 22:19:18 +01:00
import React, { useCallback, useEffect, useMemo, useRef } from 'react'
2020-12-17 12:04:58 +01:00
import { AppState, StyleSheet } from 'react-native'
import { setFocusHandler, useInfiniteQuery } from 'react-query'
2020-10-23 09:22:17 +02:00
2020-12-13 14:04:25 +01:00
import TimelineNotifications from '@components/Timelines/Timeline/Notifications'
import TimelineDefault from '@components/Timelines/Timeline/Default'
import TimelineConversation from '@components/Timelines/Timeline/Conversation'
import { timelineFetch } from '@utils/fetches/timelineFetch'
import TimelineSeparator from '@components/Timelines/Timeline/Separator'
import TimelineEmpty from '@components/Timelines/Timeline/Empty'
import TimelineEnd from '@components/Timelines/Timeline/Shared/End'
2020-12-13 23:02:54 +01:00
import { useScrollToTop } from '@react-navigation/native'
2020-12-17 12:04:58 +01:00
import { FlatList } from 'react-native-gesture-handler'
2020-10-24 18:07:09 +02:00
export interface Props {
page: App.Pages
2020-10-31 21:04:46 +01:00
hashtag?: string
list?: string
2020-12-12 12:49:29 +01:00
toot?: Mastodon.Status
2020-10-31 21:04:46 +01:00
account?: string
disableRefresh?: boolean
}
2020-10-23 09:22:17 +02:00
const Timeline: React.FC<Props> = ({
page,
hashtag,
list,
toot,
account,
2020-12-18 00:00:45 +01:00
disableRefresh = false
}) => {
setFocusHandler(handleFocus => {
const handleAppStateChange = (appState: string) => {
if (appState === 'active') {
handleFocus()
}
2020-10-31 21:04:46 +01:00
}
AppState.addEventListener('change', handleAppStateChange)
return () => AppState.removeEventListener('change', handleAppStateChange)
})
const queryKey: App.QueryKey = [
page,
{
page,
...(hashtag && { hashtag }),
...(list && { list }),
...(toot && { toot }),
...(account && { account })
}
]
const {
2020-12-13 01:24:25 +01:00
status,
2020-12-12 12:49:29 +01:00
isSuccess,
isLoading,
isError,
2020-12-12 12:49:29 +01:00
isFetchingMore,
canFetchMore,
data,
2020-12-12 12:49:29 +01:00
fetchMore,
refetch
} = useInfiniteQuery(queryKey, timelineFetch, {
getFetchMore: last => last?.toots.length > 0
})
const flattenData = data ? data.flatMap(d => [...d?.toots]) : []
2020-12-12 12:49:29 +01:00
const flattenPointer = data ? data.flatMap(d => [d?.pointer]) : []
2020-12-14 23:44:57 +01:00
const flattenPinnedLength = data ? data.flatMap(d => [d?.pinnedLength]) : []
2020-12-12 12:49:29 +01:00
2020-12-17 12:04:58 +01:00
const flRef = useRef<FlatList<any>>(null)
2020-12-12 12:49:29 +01:00
useEffect(() => {
if (toot && isSuccess) {
setTimeout(() => {
flRef.current?.scrollToIndex({
2020-12-14 23:44:57 +01:00
index: flattenPointer[0]!,
2020-12-12 12:49:29 +01:00
viewOffset: 100
})
}, 500)
}
}, [isSuccess])
2020-10-23 09:22:17 +02:00
2020-11-28 17:07:30 +01:00
const flKeyExtrator = useCallback(({ id }) => id, [])
2020-12-14 23:44:57 +01:00
const flRenderItem = useCallback(({ item, index }) => {
2020-11-28 17:07:30 +01:00
switch (page) {
case 'Conversations':
return <TimelineConversation item={item} queryKey={queryKey} />
2020-11-28 17:07:30 +01:00
case 'Notifications':
return <TimelineNotifications notification={item} queryKey={queryKey} />
default:
2020-12-12 22:19:18 +01:00
return (
<TimelineDefault
item={item}
queryKey={queryKey}
2020-12-14 23:44:57 +01:00
index={index}
{...(flattenPinnedLength &&
flattenPinnedLength[0] && {
pinnedLength: flattenPinnedLength[0]
})}
2020-12-12 22:19:18 +01:00
{...(toot && toot.id === item.id && { highlighted: true })}
/>
)
2020-11-28 17:07:30 +01:00
}
}, [])
2020-12-12 22:19:18 +01:00
const flItemSeparatorComponent = useCallback(
({ leadingItem }) => (
<TimelineSeparator
{...(toot && toot.id === leadingItem.id && { highlighted: true })}
/>
),
[]
)
const flItemEmptyComponent = useMemo(
2020-12-13 01:24:25 +01:00
() => <TimelineEmpty status={status} refetch={refetch} />,
[isLoading, isError, isSuccess]
2020-12-12 22:19:18 +01:00
)
2020-11-28 17:07:30 +01:00
const flOnRefresh = useCallback(
() =>
!disableRefresh &&
fetchMore(
{
direction: 'prev',
2020-12-13 01:24:25 +01:00
id: flattenData.length ? flattenData[0].id : null
2020-11-28 17:07:30 +01:00
},
{ previous: true }
),
2020-12-12 12:49:29 +01:00
[flattenData]
2020-11-28 17:07:30 +01:00
)
const flOnEndReach = useCallback(
() =>
!disableRefresh &&
canFetchMore &&
2020-11-28 17:07:30 +01:00
fetchMore({
direction: 'next',
id: flattenData[flattenData.length - 1].id
}),
2020-12-12 12:49:29 +01:00
[flattenData]
2020-11-28 17:07:30 +01:00
)
2020-12-12 12:49:29 +01:00
const flFooter = useCallback(() => {
2020-12-12 22:19:18 +01:00
return <TimelineEnd isFetchingMore={isFetchingMore} />
2020-12-12 12:49:29 +01:00
}, [isFetchingMore])
const onScrollToIndexFailed = useCallback(error => {
const offset = error.averageItemLength * error.index
flRef.current?.scrollToOffset({ offset })
setTimeout(
() =>
flRef.current?.scrollToIndex({ index: error.index, viewOffset: 100 }),
350
2020-10-26 00:27:53 +01:00
)
2020-12-12 12:49:29 +01:00
}, [])
2020-10-23 09:22:17 +02:00
2020-12-13 23:02:54 +01:00
useScrollToTop(flRef)
2020-12-12 12:49:29 +01:00
return (
<FlatList
ref={flRef}
data={flattenData}
style={styles.flatList}
onRefresh={flOnRefresh}
renderItem={flRenderItem}
onEndReached={flOnEndReach}
keyExtractor={flKeyExtrator}
2020-12-12 22:19:18 +01:00
ListFooterComponent={flFooter}
ListEmptyComponent={flItemEmptyComponent}
2020-12-12 12:49:29 +01:00
ItemSeparatorComponent={flItemSeparatorComponent}
onEndReachedThreshold={!disableRefresh ? 0.75 : null}
refreshing={!disableRefresh && isLoading && flattenData.length > 0}
{...(toot && isSuccess && { onScrollToIndexFailed })}
/>
)
2020-10-23 09:22:17 +02:00
}
2020-11-28 17:07:30 +01:00
const styles = StyleSheet.create({
flatList: {
minHeight: '100%'
}
})
2020-10-31 21:04:46 +01:00
export default Timeline