2021-01-04 18:29:02 +01:00
|
|
|
import ComponentSeparator from '@components/Separator'
|
2021-02-27 16:33:54 +01:00
|
|
|
import { useScrollToTop } from '@react-navigation/native'
|
2023-01-06 01:41:46 +01:00
|
|
|
import { FlashList, FlashListProps } from '@shopify/flash-list'
|
2022-12-11 12:12:46 +01:00
|
|
|
import { UseInfiniteQueryOptions } from '@tanstack/react-query'
|
2022-06-01 23:13:43 +02:00
|
|
|
import { QueryKeyTimeline, useTimelineQuery } from '@utils/queryHooks/timeline'
|
2022-12-31 00:07:28 +01:00
|
|
|
import { flattenPages } from '@utils/queryHooks/utils'
|
2022-12-28 23:41:36 +01:00
|
|
|
import { useGlobalStorageListener } from '@utils/storage/actions'
|
2021-01-04 18:29:02 +01:00
|
|
|
import { StyleConstants } from '@utils/styles/constants'
|
2021-02-13 13:08:34 +01:00
|
|
|
import { useTheme } from '@utils/styles/ThemeManager'
|
2022-12-29 00:36:35 +01:00
|
|
|
import React, { RefObject, useRef } from 'react'
|
2023-01-06 01:41:46 +01:00
|
|
|
import { Platform, RefreshControl } from 'react-native'
|
2022-12-11 01:08:38 +01:00
|
|
|
import Animated, { useAnimatedScrollHandler, useSharedValue } from 'react-native-reanimated'
|
2022-12-28 23:41:36 +01:00
|
|
|
import TimelineEmpty from './Empty'
|
|
|
|
import TimelineFooter from './Footer'
|
|
|
|
import TimelineRefresh, { SEPARATION_Y_1, SEPARATION_Y_2 } from './Refresh'
|
2022-06-01 23:13:43 +02:00
|
|
|
|
2023-01-06 01:41:46 +01:00
|
|
|
const AnimatedFlatList = Animated.createAnimatedComponent(FlashList)
|
2020-12-19 18:21:37 +01:00
|
|
|
|
2020-11-04 22:26:38 +01:00
|
|
|
export interface Props {
|
2023-01-06 01:41:46 +01:00
|
|
|
flRef?: RefObject<FlashList<any>>
|
2021-02-27 16:33:54 +01:00
|
|
|
queryKey: QueryKeyTimeline
|
2022-12-11 12:12:46 +01:00
|
|
|
queryOptions?: Omit<
|
|
|
|
UseInfiniteQueryOptions<any>,
|
|
|
|
'notifyOnChangeProps' | 'getNextPageParam' | 'getPreviousPageParam' | 'select' | 'onSuccess'
|
|
|
|
>
|
2020-10-31 21:04:46 +01:00
|
|
|
disableRefresh?: boolean
|
2021-01-01 16:48:16 +01:00
|
|
|
disableInfinity?: boolean
|
2023-01-06 01:41:46 +01:00
|
|
|
customProps: Partial<FlashListProps<any>> & Pick<FlashListProps<any>, 'renderItem'>
|
2020-11-04 22:26:38 +01:00
|
|
|
}
|
2020-10-23 09:22:17 +02:00
|
|
|
|
2020-11-04 22:26:38 +01:00
|
|
|
const Timeline: React.FC<Props> = ({
|
2021-02-27 16:33:54 +01:00
|
|
|
flRef: customFLRef,
|
|
|
|
queryKey,
|
2022-12-11 12:12:46 +01:00
|
|
|
queryOptions,
|
2021-01-01 16:48:16 +01:00
|
|
|
disableRefresh = false,
|
2021-01-15 01:15:27 +01:00
|
|
|
disableInfinity = false,
|
|
|
|
customProps
|
2020-11-04 22:26:38 +01:00
|
|
|
}) => {
|
2022-02-12 14:51:01 +01:00
|
|
|
const { colors } = useTheme()
|
2021-02-13 13:08:34 +01:00
|
|
|
|
2022-12-11 01:08:38 +01:00
|
|
|
const { data, refetch, isFetching, isLoading, fetchNextPage, isFetchingNextPage } =
|
|
|
|
useTimelineQuery({
|
|
|
|
...queryKey[1],
|
|
|
|
options: {
|
2022-12-11 12:12:46 +01:00
|
|
|
...queryOptions,
|
2022-12-11 01:08:38 +01:00
|
|
|
notifyOnChangeProps: Platform.select({
|
|
|
|
ios: ['dataUpdatedAt', 'isFetching'],
|
|
|
|
android: ['dataUpdatedAt', 'isFetching', 'isLoading']
|
|
|
|
}),
|
|
|
|
getNextPageParam: lastPage =>
|
|
|
|
lastPage?.links?.next && {
|
|
|
|
...(lastPage.links.next.isOffset
|
|
|
|
? { offset: lastPage.links.next.id }
|
|
|
|
: { max_id: lastPage.links.next.id })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-01-07 19:13:09 +01:00
|
|
|
|
2023-01-06 01:41:46 +01:00
|
|
|
const flRef = useRef<FlashList<any>>(null)
|
2020-12-13 23:02:54 +01:00
|
|
|
|
2022-06-01 23:13:43 +02:00
|
|
|
const scrollY = useSharedValue(0)
|
|
|
|
const fetchingType = useSharedValue<0 | 1 | 2>(0)
|
|
|
|
|
|
|
|
const onScroll = useAnimatedScrollHandler(
|
|
|
|
{
|
|
|
|
onScroll: ({ contentOffset: { y } }) => {
|
|
|
|
scrollY.value = y
|
|
|
|
},
|
|
|
|
onEndDrag: ({ contentOffset: { y } }) => {
|
|
|
|
if (!disableRefresh && !isFetching) {
|
|
|
|
if (y <= SEPARATION_Y_2) {
|
|
|
|
fetchingType.value = 2
|
|
|
|
} else if (y <= SEPARATION_Y_1) {
|
|
|
|
fetchingType.value = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[isFetching]
|
|
|
|
)
|
|
|
|
|
2021-02-27 16:33:54 +01:00
|
|
|
const androidRefreshControl = Platform.select({
|
|
|
|
android: {
|
|
|
|
refreshControl: (
|
|
|
|
<RefreshControl
|
|
|
|
enabled
|
2022-02-12 14:51:01 +01:00
|
|
|
colors={[colors.primaryDefault]}
|
|
|
|
progressBackgroundColor={colors.backgroundDefault}
|
2021-02-27 16:33:54 +01:00
|
|
|
refreshing={isFetching || isLoading}
|
|
|
|
onRefresh={() => refetch()}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
2021-02-13 13:08:34 +01:00
|
|
|
|
2023-01-06 01:41:46 +01:00
|
|
|
// @ts-ignore
|
2021-02-27 16:33:54 +01:00
|
|
|
useScrollToTop(flRef)
|
2022-12-28 23:41:36 +01:00
|
|
|
useGlobalStorageListener('account.active', () =>
|
|
|
|
flRef.current?.scrollToOffset({ offset: 0, animated: false })
|
|
|
|
)
|
2022-01-30 22:51:03 +01:00
|
|
|
|
2020-12-12 12:49:29 +01:00
|
|
|
return (
|
2022-06-01 23:13:43 +02:00
|
|
|
<>
|
|
|
|
<TimelineRefresh
|
|
|
|
flRef={flRef}
|
|
|
|
queryKey={queryKey}
|
|
|
|
scrollY={scrollY}
|
|
|
|
fetchingType={fetchingType}
|
|
|
|
disableRefresh={disableRefresh}
|
|
|
|
/>
|
|
|
|
<AnimatedFlatList
|
|
|
|
ref={customFLRef || flRef}
|
2023-01-06 01:41:46 +01:00
|
|
|
estimatedItemSize={200}
|
2022-06-01 23:13:43 +02:00
|
|
|
scrollEventThrottle={16}
|
|
|
|
onScroll={onScroll}
|
2022-12-31 00:07:28 +01:00
|
|
|
data={flattenPages(data)}
|
2022-12-29 00:36:35 +01:00
|
|
|
onEndReached={() => !disableInfinity && !isFetchingNextPage && fetchNextPage()}
|
2022-06-01 23:13:43 +02:00
|
|
|
onEndReachedThreshold={0.75}
|
|
|
|
ListFooterComponent={
|
2022-12-11 01:08:38 +01:00
|
|
|
<TimelineFooter queryKey={queryKey} disableInfinity={disableInfinity} />
|
2021-02-27 16:33:54 +01:00
|
|
|
}
|
2022-06-01 23:13:43 +02:00
|
|
|
ListEmptyComponent={<TimelineEmpty queryKey={queryKey} />}
|
2022-08-07 01:18:10 +02:00
|
|
|
ItemSeparatorComponent={({ leadingItem }) =>
|
|
|
|
queryKey[1].page === 'Toot' && queryKey[1].toot === leadingItem.id ? (
|
|
|
|
<ComponentSeparator extraMarginLeft={0} />
|
|
|
|
) : (
|
|
|
|
<ComponentSeparator
|
2022-12-11 01:08:38 +01:00
|
|
|
extraMarginLeft={StyleConstants.Avatar.M + StyleConstants.Spacing.S}
|
2022-08-07 01:18:10 +02:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
2022-06-07 23:01:59 +02:00
|
|
|
maintainVisibleContentPosition={
|
|
|
|
isFetching
|
|
|
|
? {
|
|
|
|
minIndexForVisible: 0
|
|
|
|
}
|
|
|
|
: undefined
|
|
|
|
}
|
2022-06-01 23:13:43 +02:00
|
|
|
{...androidRefreshControl}
|
|
|
|
{...customProps}
|
|
|
|
/>
|
|
|
|
</>
|
2020-12-12 12:49:29 +01:00
|
|
|
)
|
2020-10-23 09:22:17 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 21:04:46 +01:00
|
|
|
export default Timeline
|