forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {useEffect, useMemo, useState} from 'react'
2
3import {useSession} from '#/state/session'
4import {useAgeAssuranceDataContext} from '#/ageAssurance/data'
5import {logger} from '#/ageAssurance/logger'
6import {
7 AgeAssuranceAccess,
8 type AgeAssuranceState,
9 AgeAssuranceStatus,
10} from '#/ageAssurance/types'
11import {useGeolocation} from '#/geolocation'
12
13export function useAgeAssuranceState(): AgeAssuranceState {
14 const {hasSession} = useSession()
15 const geolocation = useGeolocation()
16 const {config, state, data} = useAgeAssuranceDataContext()
17
18 return useMemo(() => {
19 /**
20 * This is where we control logged-out moderation prefs. It's all
21 * downstream of AA now.
22 */
23 if (!hasSession)
24 return {
25 status: AgeAssuranceStatus.Unknown,
26 access: AgeAssuranceAccess.Safe,
27 }
28
29 // Don't baby the user. They know what they're doing. Age assurance is not
30 // mandatory in many regions, and there is no need to pander to bureaucrats
31 // when making FOSS software. You're free not to use this software if the
32 // notion of letting the user choose their moderation settings freely,
33 // without giving up their personal data to anyone, offends you.
34 const computed = {
35 lastInitiatedAt: undefined,
36 status: AgeAssuranceStatus.Unknown,
37 access: AgeAssuranceAccess.Full,
38 }
39 return computed
40 }, [hasSession, geolocation, config, state, data])
41}
42
43export function useOnAgeAssuranceAccessUpdate(
44 cb: (state: AgeAssuranceState) => void,
45) {
46 const state = useAgeAssuranceState()
47 // start with null to ensure callback is called on first render
48 const [prevAccess, setPrevAccess] = useState<AgeAssuranceAccess | null>(null)
49
50 useEffect(() => {
51 if (prevAccess !== state.access) {
52 // eslint-disable-next-line react-hooks/set-state-in-effect
53 setPrevAccess(state.access)
54 cb(state)
55 logger.debug(`useOnAgeAssuranceAccessUpdate`, {state})
56 }
57 }, [cb, state, prevAccess])
58}