diff options
Diffstat (limited to 'src/state/ageAssurance/index.tsx')
-rw-r--r-- | src/state/ageAssurance/index.tsx | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/src/state/ageAssurance/index.tsx b/src/state/ageAssurance/index.tsx index eded74773..3451b1139 100644 --- a/src/state/ageAssurance/index.tsx +++ b/src/state/ageAssurance/index.tsx @@ -1,4 +1,4 @@ -import {createContext, useContext, useMemo} from 'react' +import {createContext, useContext, useMemo, useState} from 'react' import {type AppBskyUnspeccedDefs} from '@atproto/api' import {useQuery} from '@tanstack/react-query' @@ -46,6 +46,7 @@ export function Provider({children}: {children: React.ReactNode}) { const {geolocation} = useGeolocation() const isAgeAssuranceEnabled = useIsAgeAssuranceEnabled() const getAndRegisterPushToken = useGetAndRegisterPushToken() + const [refetchWhilePending, setRefetchWhilePending] = useState(false) 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. */ enabled: isAgeAssuranceEnabled, + refetchOnWindowFocus: refetchWhilePending, queryKey: createAgeAssuranceQueryKey(agent.session?.did ?? 'never'), async queryFn() { if (!agent.session) return null @@ -109,6 +111,24 @@ export function Provider({children}: {children: React.ReactNode}) { return ctx }, [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) + } else if ( + !!ageAssuranceContext.lastInitiatedAt && + ageAssuranceContext.status !== 'pending' && + refetchWhilePending + ) { + setRefetchWhilePending(false) + } + const ageAssuranceAPIContext = useMemo<AgeAssuranceAPIContextType>( () => ({ refetch, |