Files
bsky-social-app/src/state/ageAssurance/useAgeAssurance.ts
T
Eric Bailey 964eed54ea Age assurance fast-follows (#8656)
* Add feed banner

* Comment

* Update nux name

* Handle did error

* Hide mod settings if underage or age restricted

* Add metrics

* Remove DEV override

* Copy suggestion

* Small copy edits

* useState

* Fix bug

* Update src/components/ageAssurance/useAgeAssuranceCopy.ts

Co-authored-by: surfdude29 <149612116+surfdude29@users.noreply.github.com>

* Get rid of debug button

---------

Co-authored-by: surfdude29 <149612116+surfdude29@users.noreply.github.com>
2025-07-17 14:32:58 -05:00

44 lines
1.3 KiB
TypeScript

import {useMemo} from 'react'
import {useAgeAssuranceContext} from '#/state/ageAssurance'
import {logger} from '#/state/ageAssurance/util'
import {usePreferencesQuery} from '#/state/queries/preferences'
type AgeAssurance = ReturnType<typeof useAgeAssuranceContext> & {
/**
* The age the user has declared in their preferences, if any.
*/
declaredAge: number | undefined
/**
* Indicates whether the user has declared an age under 18.
*/
isDeclaredUnderage: boolean
}
/**
* Computed age information based on age assurance status and the user's
* declared age. Use this instead of {@link useAgeAssuranceContext} to get a
* more user-friendly interface.
*/
export function useAgeAssurance(): AgeAssurance {
const aa = useAgeAssuranceContext()
const {isFetched: preferencesLoaded, data: preferences} =
usePreferencesQuery()
const declaredAge = preferences?.userAge
return useMemo(() => {
const isReady = aa.isReady && preferencesLoaded
const isDeclaredUnderage = (declaredAge || 0) < 18
const state: AgeAssurance = {
isReady,
status: aa.status,
lastInitiatedAt: aa.lastInitiatedAt,
isAgeRestricted: aa.isAgeRestricted,
declaredAge,
isDeclaredUnderage,
}
logger.debug(`state`, state)
return state
}, [aa, preferencesLoaded, declaredAge])
}