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'
|
2020-10-24 02:47:57 +02:00
|
|
|
import { fetch, getToots, getStatus } from './timelineSlice'
|
2020-10-23 09:22:17 +02:00
|
|
|
|
2020-10-24 18:07:09 +02:00
|
|
|
const Default = ({ toots, status, remote, endpoint, local }) => {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<FlatList
|
|
|
|
data={toots}
|
|
|
|
keyExtractor={({ id }) => id}
|
|
|
|
renderItem={TootTimeline}
|
|
|
|
onRefresh={() =>
|
|
|
|
dispatch(
|
|
|
|
fetch({ remote, endpoint, local, id: toots[0].id, newer: true })
|
|
|
|
)
|
|
|
|
}
|
|
|
|
refreshing={status === 'loading'}
|
|
|
|
onEndReached={() =>
|
|
|
|
dispatch(
|
|
|
|
fetch({ remote, endpoint, local, id: toots[toots.length - 1].id })
|
|
|
|
)
|
|
|
|
}
|
|
|
|
onEndReachedThreshold={0.5}
|
|
|
|
style={{ height: '100%', width: '100%' }}
|
|
|
|
/>
|
|
|
|
{status === 'loading' && <ActivityIndicator />}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const Notifications = ({ toots, status, endpoint }) => {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<FlatList
|
|
|
|
data={toots}
|
|
|
|
keyExtractor={({ status: { id } }) => id}
|
|
|
|
renderItem={({ item }) => (
|
|
|
|
<TootTimeline item={item} notification={true} />
|
|
|
|
)}
|
|
|
|
// onRefresh={() =>
|
|
|
|
// dispatch(fetch({ endpoint, id: toots[0].status.id, newer: true }))
|
|
|
|
// }
|
|
|
|
// refreshing={status === 'loading'}
|
|
|
|
// onEndReached={() =>
|
|
|
|
// dispatch(fetch({ endpoint, id: toots[toots.length - 1].status.id }))
|
|
|
|
// }
|
|
|
|
onEndReachedThreshold={0.5}
|
|
|
|
style={{ height: '100%', width: '100%' }}
|
|
|
|
/>
|
|
|
|
{status === 'loading' && <ActivityIndicator />}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-10-24 02:47:57 +02:00
|
|
|
export default function Timeline ({ remote, endpoint, local }) {
|
2020-10-23 09:22:17 +02:00
|
|
|
const dispatch = useDispatch()
|
2020-10-24 02:47:57 +02:00
|
|
|
const toots = useSelector(state =>
|
|
|
|
getToots(state)({ remote, endpoint, local })
|
|
|
|
)
|
|
|
|
const status = useSelector(state =>
|
|
|
|
getStatus(state)({ remote, endpoint, local })
|
|
|
|
)
|
2020-10-23 09:22:17 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (status === 'idle') {
|
2020-10-24 02:47:57 +02:00
|
|
|
dispatch(fetch({ remote, endpoint, local }))
|
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 18:07:09 +02:00
|
|
|
if (endpoint === 'notifications') {
|
|
|
|
content = (
|
|
|
|
<Notifications toots={toots} status={status} endpoint={endpoint} />
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
content = (
|
|
|
|
<Default
|
|
|
|
toots={toots}
|
|
|
|
status={status}
|
|
|
|
remote={remote}
|
|
|
|
endpoint={endpoint}
|
|
|
|
local={local}
|
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 02:47:57 +02:00
|
|
|
remote: PropTypes.bool,
|
2020-10-24 16:44:32 +02:00
|
|
|
endpoint: PropTypes.string,
|
2020-10-24 02:47:57 +02:00
|
|
|
local: PropTypes.bool
|
2020-10-23 09:22:17 +02:00
|
|
|
}
|