tooot/src/screens/Tabs/Shared/Relationships/List.tsx

85 lines
2.2 KiB
TypeScript
Raw Normal View History

2021-01-04 18:29:02 +01:00
import ComponentAccount from '@components/Account'
import ComponentSeparator from '@components/Separator'
2021-01-30 01:29:15 +01:00
import { useScrollToTop } from '@react-navigation/native'
2021-02-27 16:33:54 +01:00
import {
QueryKeyRelationships,
useRelationshipsQuery
} from '@utils/queryHooks/relationships'
2021-01-04 18:29:02 +01:00
import React, { useCallback, useMemo, useRef } from 'react'
import { RefreshControl, StyleSheet } from 'react-native'
import { FlatList } from 'react-native-gesture-handler'
export interface Props {
id: Mastodon.Account['id']
type: 'following' | 'followers'
}
const RelationshipsList: React.FC<Props> = ({ id, type }) => {
2021-02-27 16:33:54 +01:00
const queryKey: QueryKeyRelationships = ['Relationships', { type, id }]
2021-01-04 18:29:02 +01:00
const {
data,
isFetching,
refetch,
fetchNextPage,
isFetchingNextPage
2021-01-11 21:36:57 +01:00
} = useRelationshipsQuery({
2021-02-27 16:33:54 +01:00
...queryKey[1],
2021-01-07 19:13:09 +01:00
options: {
2021-02-11 01:33:31 +01:00
getPreviousPageParam: firstPage =>
firstPage.links?.prev && { since_id: firstPage.links.next },
getNextPageParam: lastPage =>
lastPage.links?.next && { max_id: lastPage.links.next }
2021-01-04 18:29:02 +01:00
}
})
2021-02-11 01:33:31 +01:00
const flattenData = data?.pages ? data.pages.flatMap(d => [...d.body]) : []
2021-01-04 18:29:02 +01:00
2021-01-30 01:29:15 +01:00
const flRef = useRef<FlatList<Mastodon.Account>>(null)
2021-01-04 18:29:02 +01:00
const keyExtractor = useCallback(({ id }) => id, [])
const renderItem = useCallback(
2021-01-24 02:25:43 +01:00
({ item }) => <ComponentAccount account={item} origin='relationship' />,
2021-01-04 18:29:02 +01:00
[]
)
const onEndReached = useCallback(
() => !isFetchingNextPage && fetchNextPage(),
[isFetchingNextPage]
)
const refreshControl = useMemo(
() => (
<RefreshControl refreshing={isFetching} onRefresh={() => refetch()} />
),
[isFetching]
)
useScrollToTop(flRef)
return (
<FlatList
ref={flRef}
windowSize={11}
data={flattenData}
initialNumToRender={5}
maxToRenderPerBatch={5}
style={styles.flatList}
renderItem={renderItem}
onEndReached={onEndReached}
keyExtractor={keyExtractor}
onEndReachedThreshold={0.75}
refreshControl={refreshControl}
ItemSeparatorComponent={ComponentSeparator}
maintainVisibleContentPosition={{
minIndexForVisible: 0,
autoscrollToTopThreshold: 2
}}
/>
)
}
const styles = StyleSheet.create({
flatList: {
minHeight: '100%'
}
})
export default RelationshipsList