tooot/src/components/Timelines/Timeline.tsx

120 lines
3.4 KiB
TypeScript
Raw Normal View History

import React from 'react'
import { ActivityIndicator, AppState, FlatList, Text, View } from 'react-native'
import { setFocusHandler, useInfiniteQuery } from 'react-query'
2020-10-23 09:22:17 +02:00
2020-11-23 00:07:32 +01:00
import TimelineNotifications from 'src/components/Timelines/Timeline/Notifications'
import TimelineDefault from 'src/components/Timelines/Timeline/Default'
import TimelineConversation from 'src/components/Timelines/Timeline/Conversation'
2020-11-21 13:19:05 +01:00
import { timelineFetch } from 'src/utils/fetches/timelineFetch'
2020-11-23 00:07:32 +01:00
import TimelineSeparator from './Timeline/Separator'
2020-10-23 09:22:17 +02:00
2020-10-26 00:27:53 +01:00
// Opening nesting hashtag pages
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
toot?: string
account?: string
disableRefresh?: boolean
2020-11-05 00:47:31 +01:00
scrollEnabled?: boolean
}
2020-10-23 09:22:17 +02:00
const Timeline: React.FC<Props> = ({
page,
hashtag,
list,
toot,
account,
2020-11-05 00:47:31 +01:00
disableRefresh = false,
scrollEnabled = true
}) => {
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)
})
2020-11-22 00:46:23 +01:00
const queryKey: App.QueryKey = [page, { page, hashtag, list, toot, account }]
const {
isLoading,
isFetchingMore,
isError,
isSuccess,
data,
fetchMore
2020-11-05 00:47:31 +01:00
} = useInfiniteQuery(queryKey, timelineFetch)
const flattenData = data ? data.flatMap(d => [...d?.toots]) : []
2020-11-22 00:46:23 +01:00
// const flattenPointer = data ? data.flatMap(d => [d?.pointer]) : []
2020-10-23 09:22:17 +02:00
2020-10-24 18:07:09 +02:00
let content
if (!isSuccess) {
content = <ActivityIndicator />
} else if (isError) {
2020-10-23 09:22:17 +02:00
content = <Text>Error message</Text>
} else {
2020-10-26 00:27:53 +01:00
content = (
<>
<FlatList
2020-10-29 14:52:28 +01:00
style={{ minHeight: '100%' }}
2020-11-05 00:47:31 +01:00
scrollEnabled={scrollEnabled} // For timeline in Account view
data={flattenData}
2020-10-26 00:27:53 +01:00
keyExtractor={({ id }) => id}
2020-11-22 00:46:23 +01:00
renderItem={({ item, index, separators }) => {
switch (page) {
case 'Conversations':
return <TimelineConversation key={index} item={item} />
case 'Notifications':
return (
2020-11-23 00:07:32 +01:00
<TimelineNotifications
2020-11-22 00:46:23 +01:00
key={index}
notification={item}
queryKey={queryKey}
/>
)
default:
return (
<TimelineDefault
key={index}
item={item}
queryKey={queryKey}
/>
)
}
}}
2020-11-23 00:07:32 +01:00
ItemSeparatorComponent={() => <TimelineSeparator />}
2020-11-22 00:46:23 +01:00
// require getItemLayout
// {...(flattenPointer[0] && { initialScrollIndex: flattenPointer[0] })}
2020-10-29 14:52:28 +01:00
{...(!disableRefresh && {
onRefresh: () =>
fetchMore(
{
direction: 'prev',
id: flattenData[0].id
},
{ previous: true }
2020-10-31 02:22:08 +01:00
),
refreshing: isLoading,
2020-10-29 14:52:28 +01:00
onEndReached: () => {
fetchMore({
direction: 'next',
id: flattenData[flattenData.length - 1].id
})
2020-10-29 14:52:28 +01:00
},
onEndReachedThreshold: 0.5
})}
2020-10-24 19:15:05 +02:00
/>
{isFetchingMore && <ActivityIndicator />}
2020-10-26 00:27:53 +01:00
</>
)
2020-10-23 09:22:17 +02:00
}
return <View>{content}</View>
}
2020-10-31 21:04:46 +01:00
export default Timeline