2020-11-04 22:26:38 +01:00
|
|
|
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-04 22:26:38 +01:00
|
|
|
import StatusInNotifications from 'src/components/StatusInNotifications'
|
|
|
|
import StatusInTimeline from 'src/components/StatusInTimeline'
|
2020-11-05 00:47:31 +01:00
|
|
|
import store from './store'
|
2020-11-04 22:26:38 +01:00
|
|
|
import { timelineFetch } from './timelineFetch'
|
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
|
|
|
|
2020-11-04 22:26:38 +01:00
|
|
|
export interface Props {
|
2020-11-05 00:47:31 +01:00
|
|
|
page: store.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-11-04 22:26:38 +01:00
|
|
|
}
|
2020-10-23 09:22:17 +02:00
|
|
|
|
2020-11-04 22:26:38 +01:00
|
|
|
const Timeline: React.FC<Props> = ({
|
|
|
|
page,
|
|
|
|
hashtag,
|
|
|
|
list,
|
|
|
|
toot,
|
|
|
|
account,
|
2020-11-05 00:47:31 +01:00
|
|
|
disableRefresh = false,
|
|
|
|
scrollEnabled = true
|
2020-11-04 22:26:38 +01:00
|
|
|
}) => {
|
|
|
|
setFocusHandler(handleFocus => {
|
|
|
|
const handleAppStateChange = (appState: string) => {
|
|
|
|
if (appState === 'active') {
|
|
|
|
handleFocus()
|
|
|
|
}
|
2020-10-31 21:04:46 +01:00
|
|
|
}
|
2020-11-04 22:26:38 +01:00
|
|
|
AppState.addEventListener('change', handleAppStateChange)
|
|
|
|
return () => AppState.removeEventListener('change', handleAppStateChange)
|
|
|
|
})
|
|
|
|
|
2020-11-05 00:47:31 +01:00
|
|
|
const queryKey: store.QueryKey = [
|
|
|
|
page,
|
|
|
|
{ page, hashtag, list, toot, account }
|
|
|
|
]
|
2020-11-04 22:26:38 +01:00
|
|
|
const {
|
|
|
|
isLoading,
|
|
|
|
isFetchingMore,
|
|
|
|
isError,
|
|
|
|
isSuccess,
|
|
|
|
data,
|
|
|
|
fetchMore
|
2020-11-05 00:47:31 +01:00
|
|
|
} = useInfiniteQuery(queryKey, timelineFetch)
|
2020-11-04 22:26:38 +01:00
|
|
|
const flattenData = data ? data.flatMap(d => [...d?.toots]) : []
|
2020-11-05 21:47:50 +01:00
|
|
|
// if (page==='Toot'){
|
|
|
|
// console.log(data)
|
|
|
|
// }
|
2020-10-23 09:22:17 +02:00
|
|
|
|
2020-10-24 18:07:09 +02:00
|
|
|
let content
|
2020-11-04 22:26:38 +01:00
|
|
|
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
|
2020-11-04 22:26:38 +01:00
|
|
|
data={flattenData}
|
2020-10-26 00:27:53 +01:00
|
|
|
keyExtractor={({ id }) => id}
|
2020-10-30 00:10:25 +01:00
|
|
|
renderItem={({ item, index, separators }) =>
|
|
|
|
page === 'Notifications' ? (
|
2020-11-06 00:59:33 +01:00
|
|
|
<StatusInNotifications
|
|
|
|
key={index}
|
|
|
|
notification={item}
|
|
|
|
queryKey={queryKey}
|
|
|
|
/>
|
2020-10-30 00:10:25 +01:00
|
|
|
) : (
|
2020-11-05 00:47:31 +01:00
|
|
|
<StatusInTimeline key={index} status={item} queryKey={queryKey} />
|
2020-10-30 00:10:25 +01:00
|
|
|
)
|
|
|
|
}
|
2020-10-31 21:04:46 +01:00
|
|
|
// {...(state.pointer && { initialScrollIndex: state.pointer })}
|
2020-10-29 14:52:28 +01:00
|
|
|
{...(!disableRefresh && {
|
|
|
|
onRefresh: () =>
|
2020-11-04 22:26:38 +01:00
|
|
|
fetchMore(
|
|
|
|
{
|
|
|
|
direction: 'prev',
|
|
|
|
id: flattenData[0].id
|
|
|
|
},
|
|
|
|
{ previous: true }
|
2020-10-31 02:22:08 +01:00
|
|
|
),
|
2020-11-04 22:26:38 +01:00
|
|
|
refreshing: isLoading,
|
2020-10-29 14:52:28 +01:00
|
|
|
onEndReached: () => {
|
2020-11-04 22:26:38 +01:00
|
|
|
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
|
|
|
/>
|
2020-11-04 22:26:38 +01: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
|