Age assurance tweaks (#8671)
* Refetch state while in pending status * Add success state to redirect, just have user close, nicer experience * Stop refetching after pending
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user