tooot/src/components/Timelines/Timeline.tsx

239 lines
6.9 KiB
TypeScript
Raw Normal View History

2021-01-04 18:29:02 +01:00
import ComponentSeparator from '@components/Separator'
2020-12-13 14:04:25 +01:00
import TimelineConversation from '@components/Timelines/Timeline/Conversation'
2021-01-04 18:29:02 +01:00
import TimelineDefault from '@components/Timelines/Timeline/Default'
2020-12-13 14:04:25 +01:00
import TimelineEmpty from '@components/Timelines/Timeline/Empty'
2021-01-04 18:29:02 +01:00
import TimelineEnd from '@root/components/Timelines/Timeline/End'
2021-01-07 19:13:09 +01:00
import TimelineHeader from '@components/Timelines/Timeline/Header'
2021-01-04 18:29:02 +01:00
import TimelineNotifications from '@components/Timelines/Timeline/Notifications'
2020-12-13 23:02:54 +01:00
import { useScrollToTop } from '@react-navigation/native'
2021-01-07 19:13:09 +01:00
import { localUpdateNotification } from '@utils/slices/instancesSlice'
2021-01-04 18:29:02 +01:00
import { StyleConstants } from '@utils/styles/constants'
import React, { useCallback, useEffect, useMemo, useRef } from 'react'
2021-01-13 01:03:46 +01:00
import { RefreshControl, StyleSheet } from 'react-native'
2020-12-17 12:04:58 +01:00
import { FlatList } from 'react-native-gesture-handler'
2020-12-29 01:09:22 +01:00
import { useDispatch } from 'react-redux'
2021-01-11 21:36:57 +01:00
import { QueryKeyTimeline, useTimelineQuery } from '@utils/queryHooks/timeline'
import { findIndex } from 'lodash'
2021-01-12 00:12:44 +01:00
import CustomRefreshControl from '@components/CustomRefreshControl'
2021-01-13 01:03:46 +01:00
import { InfiniteData, useQueryClient } from 'react-query'
2020-12-19 18:21:37 +01:00
export interface Props {
page: App.Pages
hashtag?: Mastodon.Tag['name']
list?: Mastodon.List['id']
toot?: Mastodon.Status['id']
account?: Mastodon.Account['id']
2020-10-31 21:04:46 +01:00
disableRefresh?: boolean
2021-01-01 16:48:16 +01:00
disableInfinity?: boolean
}
2020-10-23 09:22:17 +02:00
const Timeline: React.FC<Props> = ({
page,
hashtag,
list,
toot,
account,
2021-01-01 16:48:16 +01:00
disableRefresh = false,
disableInfinity = false
}) => {
2021-01-07 19:13:09 +01:00
const queryKeyParams = {
page,
2021-01-07 19:13:09 +01:00
...(hashtag && { hashtag }),
...(list && { list }),
...(toot && { toot }),
...(account && { account })
}
const queryKey: QueryKeyTimeline = ['Timeline', queryKeyParams]
const {
2020-12-13 01:24:25 +01:00
status,
data,
2020-12-18 23:58:53 +01:00
refetch,
2020-12-21 22:08:29 +01:00
isSuccess,
2021-01-12 00:12:44 +01:00
isFetching,
2020-12-18 23:58:53 +01:00
hasPreviousPage,
fetchPreviousPage,
isFetchingPreviousPage,
hasNextPage,
2020-12-26 12:59:16 +01:00
fetchNextPage,
isFetchingNextPage
2021-01-11 21:36:57 +01:00
} = useTimelineQuery({
2021-01-07 19:13:09 +01:00
...queryKeyParams,
options: {
getPreviousPageParam: firstPage => {
2021-01-11 21:36:57 +01:00
return firstPage.length
2021-01-07 19:13:09 +01:00
? {
direction: 'prev',
2021-01-11 21:36:57 +01:00
id: firstPage[0].last_status
? firstPage[0].last_status.id
: firstPage[0].id
2021-01-07 19:13:09 +01:00
}
: undefined
},
getNextPageParam: lastPage => {
2021-01-11 21:36:57 +01:00
return lastPage.length
2021-01-07 19:13:09 +01:00
? {
direction: 'next',
2021-01-11 21:36:57 +01:00
id: lastPage[lastPage.length - 1].last_status
? lastPage[lastPage.length - 1].last_status.id
: lastPage[lastPage.length - 1].id
2021-01-07 19:13:09 +01:00
}
: undefined
}
2020-12-21 13:56:04 +01:00
}
})
2021-01-07 19:13:09 +01:00
2021-01-11 21:36:57 +01:00
const flattenData = data?.pages ? data.pages.flatMap(d => [...d]) : []
2020-12-12 12:49:29 +01:00
2020-12-29 01:09:22 +01:00
// Clear unread notification badge
const dispatch = useDispatch()
useEffect(() => {
if (page === 'Notifications' && flattenData.length) {
dispatch(
2021-01-07 19:13:09 +01:00
localUpdateNotification({
2020-12-29 01:09:22 +01:00
unread: false,
2021-01-07 19:13:09 +01:00
latestTime: (flattenData[0] as Mastodon.Notification).created_at
2020-12-29 01:09:22 +01:00
})
)
}
}, [flattenData])
2020-12-17 12:04:58 +01:00
const flRef = useRef<FlatList<any>>(null)
2020-12-12 12:49:29 +01:00
useEffect(() => {
2020-12-21 22:08:29 +01:00
if (toot && isSuccess) {
2021-01-11 21:36:57 +01:00
const pointer = findIndex(flattenData, ['id', toot])
2020-12-12 12:49:29 +01:00
setTimeout(() => {
flRef.current?.scrollToIndex({
2021-01-11 21:36:57 +01:00
index: pointer,
2020-12-12 12:49:29 +01:00
viewOffset: 100
})
}, 500)
}
2021-01-11 21:36:57 +01:00
}, [isSuccess, flattenData])
2020-10-23 09:22:17 +02:00
2020-12-21 22:58:53 +01:00
const keyExtractor = useCallback(({ id }) => id, [])
2021-01-11 21:36:57 +01:00
const renderItem = useCallback(({ item }) => {
switch (page) {
case 'Conversations':
return <TimelineConversation conversation={item} queryKey={queryKey} />
case 'Notifications':
return <TimelineNotifications notification={item} queryKey={queryKey} />
default:
return (
<TimelineDefault
item={item}
queryKey={queryKey}
{...(queryKey[1].page === 'RemotePublic' && {
disableDetails: true,
disableOnPress: true
})}
{...(toot === item.id && { highlighted: true })}
/>
)
}
}, [])
2020-12-21 22:58:53 +01:00
const ItemSeparatorComponent = useCallback(
2020-12-12 22:19:18 +01:00
({ leadingItem }) => (
2021-01-04 18:29:02 +01:00
<ComponentSeparator
{...(toot === leadingItem.id
? { extraMarginLeft: 0 }
: {
extraMarginLeft:
StyleConstants.Avatar.M + StyleConstants.Spacing.S
})}
2020-12-12 22:19:18 +01:00
/>
),
[]
)
const flItemEmptyComponent = useMemo(
2020-12-13 01:24:25 +01:00
() => <TimelineEmpty status={status} refetch={refetch} />,
2020-12-18 23:58:53 +01:00
[status]
2020-12-12 22:19:18 +01:00
)
2020-12-26 12:59:16 +01:00
const onEndReached = useCallback(
2021-01-01 16:48:16 +01:00
() => !disableInfinity && !isFetchingNextPage && fetchNextPage(),
2020-12-26 12:59:16 +01:00
[isFetchingNextPage]
)
2021-01-07 19:13:09 +01:00
const ListHeaderComponent = useCallback(() => <TimelineHeader />, [])
2020-12-21 22:58:53 +01:00
const ListFooterComponent = useCallback(
2021-01-01 16:48:16 +01:00
() => <TimelineEnd hasNextPage={!disableInfinity ? hasNextPage : false} />,
2020-12-19 01:57:57 +01:00
[hasNextPage]
)
2021-01-13 01:03:46 +01:00
const queryClient = useQueryClient()
const refreshCount = useRef(0)
2020-12-21 22:58:53 +01:00
const refreshControl = useMemo(
2020-12-20 23:49:35 +01:00
() => (
2021-01-13 01:03:46 +01:00
<RefreshControl
refreshing={
refreshCount.current < 2 ? isFetchingPreviousPage : isFetching
}
onRefresh={async () => {
if (refreshCount.current < 2) {
await fetchPreviousPage()
refreshCount.current++
} else {
queryClient.setQueryData<InfiniteData<any> | undefined>(
queryKey,
data => {
if (data) {
return {
pages: data.pages.slice(1),
pageParams: data.pageParams.slice(1)
}
}
}
)
await refetch()
refreshCount.current = 0
}
}}
2020-12-20 23:49:35 +01:00
/>
),
2021-01-12 00:12:44 +01:00
[isFetchingPreviousPage, isFetching]
2020-12-20 23:49:35 +01:00
)
2020-12-12 12:49:29 +01:00
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}
2020-12-20 23:49:35 +01:00
windowSize={11}
2020-12-12 12:49:29 +01:00
data={flattenData}
2020-12-20 23:49:35 +01:00
initialNumToRender={5}
maxToRenderPerBatch={5}
2020-12-12 12:49:29 +01:00
style={styles.flatList}
2020-12-21 22:58:53 +01:00
renderItem={renderItem}
onEndReached={onEndReached}
keyExtractor={keyExtractor}
onEndReachedThreshold={0.75}
ListFooterComponent={ListFooterComponent}
2020-12-12 22:19:18 +01:00
ListEmptyComponent={flItemEmptyComponent}
2020-12-21 22:58:53 +01:00
{...(!disableRefresh && { refreshControl })}
ItemSeparatorComponent={ItemSeparatorComponent}
2021-01-07 19:13:09 +01:00
{...(queryKey &&
queryKey[1].page === 'RemotePublic' && { ListHeaderComponent })}
2020-12-21 22:08:29 +01:00
{...(toot && isSuccess && { onScrollToIndexFailed })}
2021-01-13 01:03:46 +01:00
// maintainVisibleContentPosition={{
// minIndexForVisible: 0,
// autoscrollToTopThreshold: 2
// }}
2020-12-12 12:49:29 +01:00
/>
)
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