add focus refresh + polling (#3846)

zio/stable
Samuel Newman 2024-05-03 17:57:20 +01:00 committed by GitHub
parent 4a2d4253e5
commit ce02a41163
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 2 deletions

View File

@ -0,0 +1,17 @@
import {useCallback, useRef} from 'react'
import {useFocusEffect} from '@react-navigation/native'
export function useRefreshOnFocus<T>(refetch: () => Promise<T>) {
const firstTimeRef = useRef(true)
useFocusEffect(
useCallback(() => {
if (firstTimeRef.current) {
firstTimeRef.current = false
return
}
refetch()
}, [refetch]),
)
}

View File

@ -27,6 +27,7 @@ import {DialogControlProps, useDialogControl} from '#/components/Dialog'
import {ConvoMenu} from '#/components/dms/ConvoMenu' import {ConvoMenu} from '#/components/dms/ConvoMenu'
import {NewChat} from '#/components/dms/NewChat' import {NewChat} from '#/components/dms/NewChat'
import * as TextField from '#/components/forms/TextField' import * as TextField from '#/components/forms/TextField'
import {useRefreshOnFocus} from '#/components/hooks/useRefreshOnFocus'
import {PlusLarge_Stroke2_Corner0_Rounded as Plus} from '#/components/icons/Plus' import {PlusLarge_Stroke2_Corner0_Rounded as Plus} from '#/components/icons/Plus'
import {SettingsSliderVertical_Stroke2_Corner0_Rounded as SettingsSlider} from '#/components/icons/SettingsSlider' import {SettingsSliderVertical_Stroke2_Corner0_Rounded as SettingsSlider} from '#/components/icons/SettingsSlider'
import {Link} from '#/components/Link' import {Link} from '#/components/Link'
@ -75,7 +76,9 @@ export function MessagesScreen({navigation}: Props) {
fetchNextPage, fetchNextPage,
error, error,
refetch, refetch,
} = useListConvos() } = useListConvos({refetchInterval: 15_000})
useRefreshOnFocus(refetch)
const isError = !!error const isError = !!error

View File

@ -7,7 +7,7 @@ import {useHeaders} from './temp-headers'
export const RQKEY = ['convo-list'] export const RQKEY = ['convo-list']
type RQPageParam = string | undefined type RQPageParam = string | undefined
export function useListConvos() { export function useListConvos({refetchInterval}: {refetchInterval: number}) {
const headers = useHeaders() const headers = useHeaders()
const {serviceUrl} = useDmServiceUrlStorage() const {serviceUrl} = useDmServiceUrlStorage()
@ -24,5 +24,6 @@ export function useListConvos() {
}, },
initialPageParam: undefined as RQPageParam, initialPageParam: undefined as RQPageParam,
getNextPageParam: lastPage => lastPage.cursor, getNextPageParam: lastPage => lastPage.cursor,
refetchInterval,
}) })
} }