tooot/src/stacks/common/Timeline.jsx

99 lines
2.5 KiB
React
Raw Normal View History

2020-10-23 09:22:17 +02:00
import PropTypes from 'prop-types'
import React, { useEffect } from 'react'
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'
import { fetch, getToots, getStatus } from './timelineSlice'
2020-10-23 09:22:17 +02:00
2020-10-24 19:15:05 +02:00
const Default = ({ dispatch, toots, status, timeline }) => {
2020-10-24 18:07:09 +02:00
return (
<>
<FlatList
data={toots}
keyExtractor={({ id }) => id}
renderItem={TootTimeline}
onRefresh={() =>
2020-10-24 19:15:05 +02:00
dispatch(fetch({ ...timeline, id: toots[0].id, newer: true }))
2020-10-24 18:07:09 +02:00
}
refreshing={status === 'loading'}
onEndReached={() =>
2020-10-24 19:15:05 +02:00
dispatch(fetch({ ...timeline, id: toots[toots.length - 1].id }))
2020-10-24 18:07:09 +02:00
}
onEndReachedThreshold={0.5}
/>
{status === 'loading' && <ActivityIndicator />}
</>
)
}
2020-10-24 19:15:05 +02:00
const Notifications = ({ dispatch, toots, status, timeline }) => {
2020-10-24 18:07:09 +02:00
return (
<>
<FlatList
data={toots}
keyExtractor={({ status: { id } }) => id}
renderItem={({ item }) => (
<TootTimeline item={item} notification={true} />
)}
2020-10-24 19:15:05 +02:00
onRefresh={() =>
dispatch(fetch({ ...timeline, id: toots[0].id, newer: true }))
}
refreshing={status === 'loading'}
onEndReached={() =>
2020-10-25 01:35:41 +02:00
dispatch(fetch({ ...timeline, id: toots[toots.length - 1].id }))
2020-10-24 19:15:05 +02:00
}
2020-10-24 18:07:09 +02:00
onEndReachedThreshold={0.5}
/>
{status === 'loading' && <ActivityIndicator />}
</>
)
}
2020-10-24 19:15:05 +02:00
export default function Timeline ({ timeline }) {
2020-10-23 09:22:17 +02:00
const dispatch = useDispatch()
2020-10-24 19:15:05 +02:00
const toots = useSelector(state => getToots(state)(timeline))
const status = useSelector(state => getStatus(state)(timeline))
2020-10-23 09:22:17 +02:00
useEffect(() => {
if (status === 'idle') {
2020-10-24 19:15:05 +02:00
dispatch(fetch(timeline))
2020-10-23 09:22:17 +02:00
}
}, [status, dispatch])
2020-10-24 18:07:09 +02:00
let content
2020-10-23 09:22:17 +02:00
if (status === 'error') {
content = <Text>Error message</Text>
} else {
2020-10-24 19:15:05 +02:00
if (timeline.endpoint === 'notifications') {
2020-10-24 18:07:09 +02:00
content = (
2020-10-24 19:15:05 +02:00
<Notifications
dispatch={dispatch}
toots={toots}
status={status}
timeline={timeline}
/>
2020-10-24 18:07:09 +02:00
)
} else {
content = (
<Default
2020-10-24 19:15:05 +02:00
dispatch={dispatch}
2020-10-24 18:07:09 +02:00
toots={toots}
status={status}
2020-10-24 19:15:05 +02:00
timeline={timeline}
2020-10-23 09:22:17 +02:00
/>
2020-10-24 18:07:09 +02:00
)
}
2020-10-23 09:22:17 +02:00
}
return <View>{content}</View>
}
Timeline.propTypes = {
2020-10-24 19:15:05 +02:00
timeline: PropTypes.exact({
remote: PropTypes.bool,
endpoint: PropTypes.string,
local: PropTypes.bool
}).isRequired
2020-10-23 09:22:17 +02:00
}