tooot/src/stacks/common/Timeline.jsx

66 lines
1.8 KiB
React
Raw Normal View History

2020-10-26 00:27:53 +01:00
import React, { useEffect, useState } from 'react'
2020-10-23 09:22:17 +02:00
import PropTypes from 'prop-types'
import { ActivityIndicator, FlatList, 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
import TootTimeline from 'src/components/TootTimeline'
2020-10-26 00:27:53 +01:00
import { fetch, getState } 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-26 00:27:53 +01:00
export default function Timeline ({ page, hashtag, list }) {
2020-10-23 09:22:17 +02:00
const dispatch = useDispatch()
2020-10-26 00:27:53 +01:00
const state = useSelector(state => getState(state)(page))
const [timelineReady, setTimelineReady] = useState(false)
2020-10-23 09:22:17 +02:00
useEffect(() => {
2020-10-26 00:27:53 +01:00
if (state.status === 'idle') {
dispatch(fetch({ page, hashtag, list }))
setTimelineReady(true)
2020-10-23 09:22:17 +02:00
}
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-26 00:27:53 +01:00
if (state.status === 'error') {
2020-10-23 09:22:17 +02:00
content = <Text>Error message</Text>
} else {
2020-10-26 00:27:53 +01:00
content = (
<>
<FlatList
data={state.toots}
keyExtractor={({ id }) => id}
renderItem={TootTimeline}
onRefresh={() =>
dispatch(fetch({ page, query: { since_id: state.toots[0].id } }))
}
refreshing={state.status === 'loading'}
onEndReached={() => {
if (!timelineReady) {
dispatch(
fetch({
page,
query: {
max_id: state.toots[state.toots.length - 1].id
}
})
)
setTimelineReady(true)
}
}}
onEndReachedThreshold={0.5}
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>
}
Timeline.propTypes = {
2020-10-26 00:27:53 +01:00
page: PropTypes.string.isRequired,
hashtag: PropTypes.string,
list: PropTypes.string
2020-10-23 09:22:17 +02:00
}