From 186ddc8385a36b473f1e8c4c4ca073b6b5d3cf54 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Fri, 18 Jul 2025 11:13:19 -0500 Subject: [PATCH] Refetch state while in pending status --- src/state/ageAssurance/index.tsx | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/state/ageAssurance/index.tsx b/src/state/ageAssurance/index.tsx index eded747732..40111fea09 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,18 @@ 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) + } + const ageAssuranceAPIContext = useMemo( () => ({ refetch,