Skip to content

Commit

Permalink
add focus refresh + polling (#3846)
Browse files Browse the repository at this point in the history
  • Loading branch information
mozzius authored May 3, 2024
1 parent 4a2d425 commit ce02a41
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/components/hooks/useRefreshOnFocus.ts
Original file line number Diff line number Diff line change
@@ -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]),
)
}
5 changes: 4 additions & 1 deletion src/screens/Messages/List/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {DialogControlProps, useDialogControl} from '#/components/Dialog'
import {ConvoMenu} from '#/components/dms/ConvoMenu'
import {NewChat} from '#/components/dms/NewChat'
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 {SettingsSliderVertical_Stroke2_Corner0_Rounded as SettingsSlider} from '#/components/icons/SettingsSlider'
import {Link} from '#/components/Link'
Expand Down Expand Up @@ -75,7 +76,9 @@ export function MessagesScreen({navigation}: Props) {
fetchNextPage,
error,
refetch,
} = useListConvos()
} = useListConvos({refetchInterval: 15_000})

useRefreshOnFocus(refetch)

const isError = !!error

Expand Down
3 changes: 2 additions & 1 deletion src/state/queries/messages/list-converations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {useHeaders} from './temp-headers'
export const RQKEY = ['convo-list']
type RQPageParam = string | undefined

export function useListConvos() {
export function useListConvos({refetchInterval}: {refetchInterval: number}) {
const headers = useHeaders()
const {serviceUrl} = useDmServiceUrlStorage()

Expand All @@ -24,5 +24,6 @@ export function useListConvos() {
},
initialPageParam: undefined as RQPageParam,
getNextPageParam: lastPage => lastPage.cursor,
refetchInterval,
})
}

0 comments on commit ce02a41

Please sign in to comment.