Refetch state while in pending status

This commit is contained in:
Eric Bailey
2025-07-18 11:13:19 -05:00
parent 6a15679fb1
commit 186ddc8385
+15 -1
View File
@@ -1,4 +1,4 @@
import {createContext, useContext, useMemo} from 'react' import {createContext, useContext, useMemo, useState} from 'react'
import {type AppBskyUnspeccedDefs} from '@atproto/api' import {type AppBskyUnspeccedDefs} from '@atproto/api'
import {useQuery} from '@tanstack/react-query' import {useQuery} from '@tanstack/react-query'
@@ -46,6 +46,7 @@ export function Provider({children}: {children: React.ReactNode}) {
const {geolocation} = useGeolocation() const {geolocation} = useGeolocation()
const isAgeAssuranceEnabled = useIsAgeAssuranceEnabled() const isAgeAssuranceEnabled = useIsAgeAssuranceEnabled()
const getAndRegisterPushToken = useGetAndRegisterPushToken() const getAndRegisterPushToken = useGetAndRegisterPushToken()
const [refetchWhilePending, setRefetchWhilePending] = useState(false)
const {data, isFetched, refetch} = useQuery({ const {data, isFetched, refetch} = useQuery({
/** /**
@@ -56,6 +57,7 @@ export function Provider({children}: {children: React.ReactNode}) {
* However, it only needs to run if AA is enabled. * However, it only needs to run if AA is enabled.
*/ */
enabled: isAgeAssuranceEnabled, enabled: isAgeAssuranceEnabled,
refetchOnWindowFocus: refetchWhilePending,
queryKey: createAgeAssuranceQueryKey(agent.session?.did ?? 'never'), queryKey: createAgeAssuranceQueryKey(agent.session?.did ?? 'never'),
async queryFn() { async queryFn() {
if (!agent.session) return null if (!agent.session) return null
@@ -109,6 +111,18 @@ export function Provider({children}: {children: React.ReactNode}) {
return ctx return ctx
}, [isFetched, data, isAgeAssuranceEnabled]) }, [isFetched, data, isAgeAssuranceEnabled])
if (
!!ageAssuranceContext.lastInitiatedAt &&
ageAssuranceContext.status === 'pending' &&
!refetchWhilePending
) {
/*
* If we have a pending state, we want to refetch on window focus to ensure
* that we get the latest state when the user returns to the app.
*/
setRefetchWhilePending(true)
}
const ageAssuranceAPIContext = useMemo<AgeAssuranceAPIContextType>( const ageAssuranceAPIContext = useMemo<AgeAssuranceAPIContextType>(
() => ({ () => ({
refetch, refetch,