Clean up state

This commit is contained in:
Eric Bailey
2025-07-15 13:04:37 -05:00
parent 4709fa4355
commit 714d3eb5c4
5 changed files with 12 additions and 41 deletions
@@ -40,6 +40,7 @@ function Inner({style}: ViewStyleProp & {}) {
const copy = useAgeAssuranceCopy() const copy = useAgeAssuranceCopy()
const {assurance} = useAgeInfo() const {assurance} = useAgeInfo()
const isBlocked = assurance.status === 'blocked' const isBlocked = assurance.status === 'blocked'
const hasInitiated = !!assurance.lastInitiatedAt
const timeAgo = assurance.lastInitiatedAt const timeAgo = assurance.lastInitiatedAt
? getTimeAgo(assurance.lastInitiatedAt, new Date()) ? getTimeAgo(assurance.lastInitiatedAt, new Date())
: null : null
@@ -118,10 +119,10 @@ function Inner({style}: ViewStyleProp & {}) {
label={_(msg`Verify now`)} label={_(msg`Verify now`)}
size="small" size="small"
variant="solid" variant="solid"
color={assurance.hasInitiated ? 'secondary' : 'primary'} color={hasInitiated ? 'secondary' : 'primary'}
onPress={() => control.open()}> onPress={() => control.open()}>
<ButtonText> <ButtonText>
{assurance.hasInitiated ? ( {hasInitiated ? (
<Trans>Verify again</Trans> <Trans>Verify again</Trans>
) : ( ) : (
<Trans>Verify now</Trans> <Trans>Verify now</Trans>
@@ -26,7 +26,6 @@ export function AgeRestrictedScreen({
const copy = useAgeAssuranceCopy() const copy = useAgeAssuranceCopy()
const {isLoaded, assurance} = useAgeInfo() const {isLoaded, assurance} = useAgeInfo()
if (assurance.isExempt) return children
if (!isLoaded) { if (!isLoaded) {
return ( return (
<Layout.Screen> <Layout.Screen>
+2 -6
View File
@@ -27,9 +27,7 @@ const AgeAssuranceContext = createContext<AgeAssuranceContextType>({
status: 'unknown', status: 'unknown',
isLoaded: false, isLoaded: false,
isAgeRestricted: false, isAgeRestricted: false,
isExempt: false,
lastInitiatedAt: undefined, lastInitiatedAt: undefined,
hasInitiated: false,
}) })
const AgeAssuranceAPIContext = createContext<AgeAssuranceAPIContextType>({ const AgeAssuranceAPIContext = createContext<AgeAssuranceAPIContextType>({
// @ts-ignore // @ts-ignore
@@ -99,12 +97,10 @@ export function Provider({children}: {children: React.ReactNode}) {
const enabled = __DEV__ || gate('age_assurance') const enabled = __DEV__ || gate('age_assurance')
const {status, lastInitiatedAt} = data || DEFAULT_AGE_ASSURANCE_STATE const {status, lastInitiatedAt} = data || DEFAULT_AGE_ASSURANCE_STATE
const ctx: AgeAssuranceContextType = { const ctx: AgeAssuranceContextType = {
status,
isLoaded: isFetched, isLoaded: isFetched,
status,
lastInitiatedAt, lastInitiatedAt,
hasInitiated: !!lastInitiatedAt, isAgeRestricted: isAgeRestrictedGeo && status !== 'assured' && enabled,
isExempt: !isAgeRestrictedGeo || !enabled,
isAgeRestricted: Boolean(isAgeRestrictedGeo && status !== 'assured'),
} }
logger.debug(`context`, ctx) logger.debug(`context`, ctx)
+6 -12
View File
@@ -3,25 +3,19 @@ import {type QueryObserverBaseResult} from '@tanstack/react-query'
export type AgeAssuranceContextType = { export type AgeAssuranceContextType = {
isLoaded: boolean isLoaded: boolean
/**
* The server-reported status of the user's age verification process.
*/
status: AppBskyUnspeccedDefs.AgeAssuranceState['status'] status: AppBskyUnspeccedDefs.AgeAssuranceState['status']
/**
* Whether the current user is age-restricted based on their geolocation and
* age assurance state retrieved from the server.
*/
isAgeRestricted: boolean
/**
* Whether the user is exempt from age assurance checks, e.g., due to
* them not being in a region that requires age assurance.
*/
isExempt: boolean
/** /**
* The last time the age assurance state was attempted by the user. * The last time the age assurance state was attempted by the user.
*/ */
lastInitiatedAt: string | undefined lastInitiatedAt: string | undefined
/** /**
* Whether the user has initiated an age assurance check. * Whether the current user is age-restricted based on their geolocation and
* age assurance state retrieved from the server.
*/ */
hasInitiated: boolean isAgeRestricted: boolean
} }
export type AgeAssuranceAPIContextType = { export type AgeAssuranceAPIContextType = {
@@ -1,12 +1,9 @@
import {useCallback} from 'react' import {useCallback} from 'react'
import {type ModerationPrefs} from '@atproto/api' import {type ModerationPrefs} from '@atproto/api'
// import {Logger} from '#/logger'
import {useAgeAssuranceContext} from '#/state/age-assurance' import {useAgeAssuranceContext} from '#/state/age-assurance'
import {AGE_RESTRICTED_MODERATION_PREFS} from '#/state/age-assurance/const' import {AGE_RESTRICTED_MODERATION_PREFS} from '#/state/age-assurance/const'
// const logger = Logger.create(Logger.Context.AgeAssurance)
/** /**
* Hook to conditionally apply age-restricted moderation preferences, if * Hook to conditionally apply age-restricted moderation preferences, if
* needed. If not needed, the mod prefs passed to the callback will be used. * needed. If not needed, the mod prefs passed to the callback will be used.
@@ -15,26 +12,10 @@ export function useMaybeApplyAgeRestrictedModerationPrefs() {
const state = useAgeAssuranceContext() const state = useAgeAssuranceContext()
return useCallback( return useCallback(
(prev: ModerationPrefs) => { (prev: ModerationPrefs) => {
const isDefinitelyAgeRestricted = state.isLoaded && state.isAgeRestricted if (state.isAgeRestricted) {
const notSureYet = !state.isLoaded && state.isAgeRestricted
if (state.isExempt) {
// logger.debug('useMaybeApplyAgeRestrictedModerationPrefs: exempt', state)
return prev
}
if (notSureYet || isDefinitelyAgeRestricted) {
// logger.debug(
// 'useMaybeApplyAgeRestrictedModerationPrefs: overridden',
// state,
// )
return AGE_RESTRICTED_MODERATION_PREFS return AGE_RESTRICTED_MODERATION_PREFS
} }
// logger.debug(
// 'useMaybeApplyAgeRestrictedModerationPrefs: not overridden',
// state,
// )
return prev return prev
}, },
[state], [state],