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 {assurance} = useAgeInfo()
const isBlocked = assurance.status === 'blocked'
const hasInitiated = !!assurance.lastInitiatedAt
const timeAgo = assurance.lastInitiatedAt
? getTimeAgo(assurance.lastInitiatedAt, new Date())
: null
@@ -118,10 +119,10 @@ function Inner({style}: ViewStyleProp & {}) {
label={_(msg`Verify now`)}
size="small"
variant="solid"
color={assurance.hasInitiated ? 'secondary' : 'primary'}
color={hasInitiated ? 'secondary' : 'primary'}
onPress={() => control.open()}>
<ButtonText>
{assurance.hasInitiated ? (
{hasInitiated ? (
<Trans>Verify again</Trans>
) : (
<Trans>Verify now</Trans>
@@ -26,7 +26,6 @@ export function AgeRestrictedScreen({
const copy = useAgeAssuranceCopy()
const {isLoaded, assurance} = useAgeInfo()
if (assurance.isExempt) return children
if (!isLoaded) {
return (
<Layout.Screen>
+2 -6
View File
@@ -27,9 +27,7 @@ const AgeAssuranceContext = createContext<AgeAssuranceContextType>({
status: 'unknown',
isLoaded: false,
isAgeRestricted: false,
isExempt: false,
lastInitiatedAt: undefined,
hasInitiated: false,
})
const AgeAssuranceAPIContext = createContext<AgeAssuranceAPIContextType>({
// @ts-ignore
@@ -99,12 +97,10 @@ export function Provider({children}: {children: React.ReactNode}) {
const enabled = __DEV__ || gate('age_assurance')
const {status, lastInitiatedAt} = data || DEFAULT_AGE_ASSURANCE_STATE
const ctx: AgeAssuranceContextType = {
status,
isLoaded: isFetched,
status,
lastInitiatedAt,
hasInitiated: !!lastInitiatedAt,
isExempt: !isAgeRestrictedGeo || !enabled,
isAgeRestricted: Boolean(isAgeRestrictedGeo && status !== 'assured'),
isAgeRestricted: isAgeRestrictedGeo && status !== 'assured' && enabled,
}
logger.debug(`context`, ctx)
+6 -12
View File
@@ -3,25 +3,19 @@ import {type QueryObserverBaseResult} from '@tanstack/react-query'
export type AgeAssuranceContextType = {
isLoaded: boolean
/**
* The server-reported status of the user's age verification process.
*/
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.
*/
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 = {
@@ -1,12 +1,9 @@
import {useCallback} from 'react'
import {type ModerationPrefs} from '@atproto/api'
// import {Logger} from '#/logger'
import {useAgeAssuranceContext} from '#/state/age-assurance'
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
* needed. If not needed, the mod prefs passed to the callback will be used.
@@ -15,26 +12,10 @@ export function useMaybeApplyAgeRestrictedModerationPrefs() {
const state = useAgeAssuranceContext()
return useCallback(
(prev: ModerationPrefs) => {
const isDefinitelyAgeRestricted = state.isLoaded && 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,
// )
if (state.isAgeRestricted) {
return AGE_RESTRICTED_MODERATION_PREFS
}
// logger.debug(
// 'useMaybeApplyAgeRestrictedModerationPrefs: not overridden',
// state,
// )
return prev
},
[state],