tooot/src/stacks/common/Timeline.tsx

90 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-10-26 00:27:53 +01:00
import React, { useEffect, useState } from 'react'
2020-10-29 14:52:28 +01:00
import { ActivityIndicator, FlatList, Text, View } from 'react-native'
2020-10-24 18:07:09 +02:00
import { useSelector, useDispatch } from 'react-redux'
2020-10-23 09:22:17 +02:00
2020-10-30 00:10:25 +01:00
import TootNotification from 'src/components/TootNotification'
import TootTimeline from 'src/components/TootTimeline'
2020-10-31 21:04:46 +01:00
import { RootState } from 'src/stacks/common/store'
2020-10-29 14:52:28 +01:00
import { fetch } from './timelineSlice'
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-10-31 21:04:46 +01:00
const Timeline: React.FC<{
page: store.TimelinePage
hashtag?: string
list?: string
toot?: string
account?: string
disableRefresh?: boolean
}> = ({ page, hashtag, list, toot, account, disableRefresh = false }) => {
2020-10-23 09:22:17 +02:00
const dispatch = useDispatch()
2020-10-31 21:04:46 +01:00
const state = useSelector((state: RootState) => state.timelines[page])
2020-10-26 00:27:53 +01:00
const [timelineReady, setTimelineReady] = useState(false)
2020-10-23 09:22:17 +02:00
useEffect(() => {
2020-10-30 20:03:44 +01:00
let mounted = true
if (state.status === 'idle' && mounted) {
2020-10-29 14:52:28 +01:00
dispatch(fetch({ page, hashtag, list, toot, account }))
2020-10-26 00:27:53 +01:00
setTimelineReady(true)
2020-10-23 09:22:17 +02:00
}
2020-10-31 21:04:46 +01:00
return () => {
mounted = false
}
2020-10-26 00:27:53 +01:00
}, [state, dispatch])
2020-10-23 09:22:17 +02:00
2020-10-24 18:07:09 +02:00
let content
2020-10-29 14:52:28 +01:00
if (state.status === 'failed') {
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-10-26 00:27:53 +01:00
data={state.toots}
keyExtractor={({ id }) => id}
2020-10-30 00:10:25 +01:00
renderItem={({ item, index, separators }) =>
page === 'Notifications' ? (
2020-10-31 21:04:46 +01:00
<TootNotification key={index} toot={item} />
2020-10-30 00:10:25 +01:00
) : (
2020-10-31 21:04:46 +01:00
<TootTimeline key={index} toot={item} />
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-10-31 02:22:08 +01:00
dispatch(
fetch({
page,
hashtag,
list,
paginationDirection: 'prev'
})
),
2020-10-29 14:52:28 +01:00
refreshing: state.status === 'loading',
onEndReached: () => {
if (!timelineReady) {
2020-10-31 02:22:08 +01:00
dispatch(
fetch({
page,
hashtag,
list,
paginationDirection: 'next'
})
)
2020-10-29 14:52:28 +01:00
setTimelineReady(true)
}
},
onEndReachedThreshold: 0.5
})}
2020-10-26 00:27:53 +01:00
onMomentumScrollBegin={() => setTimelineReady(false)}
2020-10-24 19:15:05 +02:00
/>
2020-10-26 00:27:53 +01:00
{state.status === 'loading' && <ActivityIndicator />}
</>
)
2020-10-23 09:22:17 +02:00
}
return <View>{content}</View>
}
2020-10-31 21:04:46 +01:00
export default Timeline