Checkpoint: almost there
This commit is contained in:
@@ -13,7 +13,6 @@ import {
|
||||
type AgeAssuranceContextType,
|
||||
} from '#/state/ageAssurance/types'
|
||||
import {useGeolocation} from '#/state/geolocation'
|
||||
import {STALE} from '#/state/queries'
|
||||
import {useAgent} from '#/state/session'
|
||||
|
||||
const logger = Logger.create(Logger.Context.AgeAssurance)
|
||||
@@ -26,7 +25,6 @@ const DEFAULT_AGE_ASSURANCE_STATE: AppBskyUnspeccedDefs.AgeAssuranceState = {
|
||||
const AgeAssuranceContext = createContext<AgeAssuranceContextType>({
|
||||
status: 'unknown',
|
||||
isLoaded: false,
|
||||
isAgeRestricted: false,
|
||||
lastInitiatedAt: undefined,
|
||||
})
|
||||
const AgeAssuranceAPIContext = createContext<AgeAssuranceAPIContextType>({
|
||||
@@ -54,8 +52,6 @@ export function Provider({children}: {children: React.ReactNode}) {
|
||||
*/
|
||||
enabled: true,
|
||||
queryKey: createAgeAssuranceQueryKey(agent.session?.did ?? 'never'),
|
||||
staleTime: STALE.MINUTES.ONE,
|
||||
refetchOnWindowFocus: geolocation?.isAgeRestrictedGeo === true,
|
||||
async queryFn() {
|
||||
if (!agent.session) return null
|
||||
|
||||
@@ -78,11 +74,13 @@ export function Provider({children}: {children: React.ReactNode}) {
|
||||
account: agent.session?.did,
|
||||
})
|
||||
|
||||
await getAndRegisterPushToken({
|
||||
isAgeRestricted: Boolean(
|
||||
isAgeRestrictedGeo && data.status !== 'assured',
|
||||
),
|
||||
})
|
||||
if (gate('age_assurance')) {
|
||||
await getAndRegisterPushToken({
|
||||
isAgeRestricted: Boolean(
|
||||
isAgeRestrictedGeo && data.status !== 'assured',
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
return data
|
||||
} catch (e) {
|
||||
@@ -99,19 +97,15 @@ export function Provider({children}: {children: React.ReactNode}) {
|
||||
* Derive state, or fall back to defaults
|
||||
*/
|
||||
const ageAssuranceContext = useMemo<AgeAssuranceContextType>(() => {
|
||||
const enabled = __DEV__ || gate('age_assurance')
|
||||
const {status, lastInitiatedAt} = data || DEFAULT_AGE_ASSURANCE_STATE
|
||||
const ctx: AgeAssuranceContextType = {
|
||||
isLoaded: isFetched,
|
||||
status,
|
||||
lastInitiatedAt,
|
||||
isAgeRestricted: isAgeRestrictedGeo && status !== 'assured' && enabled,
|
||||
}
|
||||
|
||||
logger.debug(`context`, ctx)
|
||||
|
||||
return ctx
|
||||
}, [gate, isAgeRestrictedGeo, isFetched, data])
|
||||
}, [isFetched, data])
|
||||
|
||||
const ageAssuranceAPIContext = useMemo<AgeAssuranceAPIContextType>(
|
||||
() => ({
|
||||
|
||||
@@ -10,12 +10,7 @@ export type AgeAssuranceContextType = {
|
||||
/**
|
||||
* The last time the age assurance state was attempted by the user.
|
||||
*/
|
||||
lastInitiatedAt: string | undefined
|
||||
/**
|
||||
* Whether the current user is age-restricted based on their geolocation and
|
||||
* age assurance state retrieved from the server.
|
||||
*/
|
||||
isAgeRestricted: boolean
|
||||
lastInitiatedAt: AppBskyUnspeccedDefs.AgeAssuranceState['lastInitiatedAt']
|
||||
}
|
||||
|
||||
export type AgeAssuranceAPIContextType = {
|
||||
|
||||
@@ -2,7 +2,7 @@ import {useMemo} from 'react'
|
||||
|
||||
import {Logger} from '#/logger'
|
||||
import {useAgeAssuranceContext} from '#/state/ageAssurance'
|
||||
import {useMustCompleteAgeAssurance} from '#/state/ageAssurance/useMustCompleteAgeAssurance'
|
||||
import {useIsAgeRestricted} from '#/state/ageAssurance/useIsAgeRestricted'
|
||||
import {usePreferencesQuery} from '#/state/queries/preferences'
|
||||
|
||||
const logger = Logger.create(Logger.Context.AgeAssurance)
|
||||
@@ -11,7 +11,7 @@ type AgeInfo = {
|
||||
isLoaded: boolean
|
||||
declaredAge: number | undefined
|
||||
isUnderage: boolean
|
||||
mustCompleteAgeAssurance: boolean
|
||||
isAgeRestricted: boolean
|
||||
assurance: ReturnType<typeof useAgeAssuranceContext>
|
||||
}
|
||||
|
||||
@@ -21,8 +21,8 @@ type AgeInfo = {
|
||||
* more user-friendly interface.
|
||||
*/
|
||||
export function useAgeInfo(): AgeInfo {
|
||||
const mustCompleteAgeAssurance = useMustCompleteAgeAssurance()
|
||||
const ctx = useAgeAssuranceContext()
|
||||
const {isAgeRestricted} = useIsAgeRestricted()
|
||||
const {isFetched: preferencesLoaded, data: preferences} =
|
||||
usePreferencesQuery()
|
||||
const declaredAge = preferences?.userAge
|
||||
@@ -34,11 +34,11 @@ export function useAgeInfo(): AgeInfo {
|
||||
isLoaded,
|
||||
declaredAge,
|
||||
isUnderage,
|
||||
mustCompleteAgeAssurance,
|
||||
isAgeRestricted,
|
||||
|
||||
assurance: ctx,
|
||||
}
|
||||
logger.debug(`useAgeInfo`, info)
|
||||
return info
|
||||
}, [ctx, preferencesLoaded, declaredAge, mustCompleteAgeAssurance])
|
||||
}, [ctx, preferencesLoaded, declaredAge, isAgeRestricted])
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import {useMemo} from 'react'
|
||||
|
||||
import {useGate} from '#/lib/statsig/statsig'
|
||||
import {useAgeAssuranceContext} from '#/state/ageAssurance'
|
||||
import {useGeolocation} from '#/state/geolocation'
|
||||
|
||||
export function useIsAgeRestricted() {
|
||||
const {isLoaded, status} = useAgeAssuranceContext()
|
||||
const {geolocation} = useGeolocation()
|
||||
const gate = useGate()
|
||||
|
||||
return useMemo(() => {
|
||||
if (!gate('age_assurance') || !geolocation?.isAgeRestrictedGeo) {
|
||||
return {
|
||||
isReady: true,
|
||||
isAgeRestricted: false,
|
||||
}
|
||||
}
|
||||
return {
|
||||
isReady: isLoaded,
|
||||
isAgeRestricted: status !== 'assured',
|
||||
}
|
||||
}, [isLoaded, status, geolocation, gate])
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
import {useMemo} from 'react'
|
||||
|
||||
import {useGate} from '#/lib/statsig/statsig'
|
||||
import {useAgeAssuranceContext} from '#/state/ageAssurance'
|
||||
import {useGeolocation} from '#/state/geolocation'
|
||||
|
||||
export function useMustCompleteAgeAssurance() {
|
||||
const gate = useGate()
|
||||
const {geolocation} = useGeolocation()
|
||||
const {isLoaded, status: ageAssuranceStatus} = useAgeAssuranceContext()
|
||||
return useMemo(() => {
|
||||
if (!gate('age_assurance')) return false
|
||||
if (!geolocation?.isAgeRestrictedGeo) return false
|
||||
if (!isLoaded) return false
|
||||
return ageAssuranceStatus !== 'assured'
|
||||
}, [isLoaded, gate, geolocation, ageAssuranceStatus])
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import {replaceEqualDeep} from '#/lib/functions'
|
||||
import {getAge} from '#/lib/strings/time'
|
||||
import {logger} from '#/logger'
|
||||
import {AGE_RESTRICTED_MODERATION_PREFS} from '#/state/ageAssurance/const'
|
||||
import {useMustCompleteAgeAssurance} from '#/state/ageAssurance/useMustCompleteAgeAssurance'
|
||||
import {useIsAgeRestricted} from '#/state/ageAssurance/useIsAgeRestricted'
|
||||
import {STALE} from '#/state/queries'
|
||||
import {
|
||||
DEFAULT_HOME_FEED_PREFS,
|
||||
@@ -34,7 +34,7 @@ export const preferencesQueryKey = [preferencesQueryKeyRoot]
|
||||
|
||||
export function usePreferencesQuery() {
|
||||
const agent = useAgent()
|
||||
const mustCompleteAgeAssurance = useMustCompleteAgeAssurance()
|
||||
const {isReady: isAgeRestrictionReady, isAgeRestricted} = useIsAgeRestricted()
|
||||
|
||||
return useQuery({
|
||||
staleTime: STALE.SECONDS.FIFTEEN,
|
||||
@@ -76,12 +76,12 @@ export function usePreferencesQuery() {
|
||||
select: useCallback(
|
||||
(data: UsePreferencesQueryResponse) => {
|
||||
const isUnderage = (data.userAge || 0) < 18
|
||||
if (isUnderage || mustCompleteAgeAssurance) {
|
||||
if (isUnderage || (isAgeRestrictionReady && isAgeRestricted)) {
|
||||
data.moderationPrefs = AGE_RESTRICTED_MODERATION_PREFS
|
||||
}
|
||||
return data
|
||||
},
|
||||
[mustCompleteAgeAssurance],
|
||||
[isAgeRestrictionReady, isAgeRestricted],
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user