about summary refs log tree commit diff
path: root/src/state/ageAssurance/index.tsx
diff options
context:
space:
mode:
authorEric Bailey <git@esb.lol>2025-07-16 13:58:07 -0500
committerGitHub <noreply@github.com>2025-07-16 13:58:07 -0500
commit1dbc331314278cb7a42ded9b190dac7038ad9878 (patch)
treeb5d44e1ea75ea9d5343eec90425c8c7ac74df39f /src/state/ageAssurance/index.tsx
parent712c3ad4211e2e68d0cdbcc480967c63aeaa6c0e (diff)
downloadvoidsky-1dbc331314278cb7a42ded9b190dac7038ad9878.tar.zst
UI for age assurance compliance (#8652)
* Add geo prop

* Add prelim fetch

* Add geo debug

* Pass in assurance state to notifications registration

* Comments

* Bump git index

* Add some component utils, no design, gate chat

* Disable mod prefs buttons, does not yet edit mod prefs

* Add initial prompt component

* Refine logic for showing prompt

* Add send email dialog

* Hook up dialog to fake mutation

* Fix geo debug bug

* Move provider inside query provider

* Slightly better screen gater

* Ok decent fallback with isExempt

* Reorg

* Wrap prompt in new logic

* Override mod prefs

* Use real endpoints, optimistic state

* Add persistent card, add time-ago, warning to dialog

* Add comment

* No undefined query values

* Fix case in import

* Wait for AA to load before registering push

* Override prefs in all locations

* Small refactor of notifications registration

* Register push after aa state

* Add retries

* Update blocked screens UI

* Strengthen email validation

* Add intent dialog

* Do service auth for init

* Rug refreshJwt

* Update copy

* Some mobile styles, add dev mode option

* Fix links on native

* Clean up intent dialog on native

* Don't mutate existing session, only copy

* Handle email validation error from server

* Clarity is better

* Moar clear

* Fixes

* Tweaks

* Add country code

* Gate it

* Refresh state after redirect

* Re-check on window focus

* Remove todo

* Enable in dev

* Check for did match on redirect

* Add blocked state

* Add appeal dialog

* Copy tweaks

* Inset in blue well

* Nux the prompt

* Copy updates

* Refetch just in case

* Uppercase country code

* Align copy, add notice to chat screens

* Tweak copy

* Add test code

* Add debug code

* Refactor AccountCard

* Big refactor

* Delay post-feed queries instead

* Debug code

* Clean up state

* Reorg

* Clean up copy

* Comments

* Reorg

* UPdate URL

* Cleanup

* Remove todo

* Update debug code

* revert unneeded changes

* UPdate nux name

* Revert unneeded change

* Updaet storage schema

* Checkpoint: cleanup

* Checkpoint: almost there

* isLoaded -> isReady

* Rename useAgeAssurance

* isUnderage -> isDeclaredUnderage

* Decompose, add docblocks

* Refactor

* UPdate debug

* Apply suggestion from @surfdude29

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

* Apply suggestion from @surfdude29

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

* Apply suggestion from @surfdude29

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

* Apply suggestion from @surfdude29

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

* Apply suggestion from @surfdude29

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

* Apply suggestion from @surfdude29

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

* Drop including Bluesky

* Apply suggestion from @surfdude29

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

* Apply suggestion from @surfdude29

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

* Remove todo

* Gate debug

* Revert unneeded change

* Fail closed

* Comments

* Comment

* Comment

* fix prettier

* rm viewheader

* bump sdk

* prevent overlap in admonition

* add age assurance intent route

* Just meow

Co-authored-by: Samuel Newman <mozzius@protonmail.com>

* Nix callback

* Fix spelling of dismissible lol

* Don't compare translated string

* Better KWS link labels

* Hide DMs send options in menu

* Add button

* Fix order

* Use only supported languages

* Rm button

* best-effort language mapping

* improve typing

---------

Co-authored-by: surfdude29 <149612116+surfdude29@users.noreply.github.com>
Co-authored-by: Samuel Newman <mozzius@protonmail.com>
Diffstat (limited to 'src/state/ageAssurance/index.tsx')
-rw-r--r--src/state/ageAssurance/index.tsx140
1 files changed, 140 insertions, 0 deletions
diff --git a/src/state/ageAssurance/index.tsx b/src/state/ageAssurance/index.tsx
new file mode 100644
index 000000000..aab954e6c
--- /dev/null
+++ b/src/state/ageAssurance/index.tsx
@@ -0,0 +1,140 @@
+import {createContext, useContext, useMemo} from 'react'
+import {type AppBskyUnspeccedDefs} from '@atproto/api'
+import {useQuery} from '@tanstack/react-query'
+
+import {networkRetry} from '#/lib/async/retry'
+import {useGetAndRegisterPushToken} from '#/lib/notifications/notifications'
+import {useGate} from '#/lib/statsig/statsig'
+import {isNetworkError} from '#/lib/strings/errors'
+import {Logger} from '#/logger'
+import {
+  type AgeAssuranceAPIContextType,
+  type AgeAssuranceContextType,
+} from '#/state/ageAssurance/types'
+import {useIsAgeAssuranceEnabled} from '#/state/ageAssurance/useIsAgeAssuranceEnabled'
+import {useGeolocation} from '#/state/geolocation'
+import {useAgent} from '#/state/session'
+
+const logger = Logger.create(Logger.Context.AgeAssurance)
+
+export const createAgeAssuranceQueryKey = (did: string) =>
+  ['ageAssurance', did] as const
+
+const DEFAULT_AGE_ASSURANCE_STATE: AppBskyUnspeccedDefs.AgeAssuranceState = {
+  lastInitiatedAt: undefined,
+  status: 'unknown',
+}
+
+const AgeAssuranceContext = createContext<AgeAssuranceContextType>({
+  status: 'unknown',
+  isReady: false,
+  lastInitiatedAt: undefined,
+  isAgeRestricted: false,
+})
+
+const AgeAssuranceAPIContext = createContext<AgeAssuranceAPIContextType>({
+  // @ts-ignore can't be bothered to type this
+  refetch: () => Promise.resolve(),
+})
+
+/**
+ * Low-level provider for fetching age assurance state on app load. Do not add
+ * any other data fetching in here to avoid complications and reduced
+ * performance.
+ */
+export function Provider({children}: {children: React.ReactNode}) {
+  const gate = useGate()
+  const agent = useAgent()
+  const {geolocation} = useGeolocation()
+  const isAgeAssuranceEnabled = useIsAgeAssuranceEnabled()
+  const getAndRegisterPushToken = useGetAndRegisterPushToken()
+
+  const {data, isFetched, refetch} = useQuery({
+    /**
+     * This is load bearing. We always want this query to run and end in a
+     * "fetched" state, even if we fall back to defaults. This lets the rest of
+     * the app know that we've at least attempted to load the AA state.
+     *
+     * However, it only needs to run if AA is enabled.
+     */
+    enabled: isAgeAssuranceEnabled,
+    queryKey: createAgeAssuranceQueryKey(agent.session?.did ?? 'never'),
+    async queryFn() {
+      if (!agent.session) return null
+
+      try {
+        const {data} = await networkRetry(3, () =>
+          agent.app.bsky.unspecced.getAgeAssuranceState(),
+        )
+        // const {data} = {
+        //   data: {
+        //     lastInitiatedAt: new Date().toISOString(),
+        //     status: 'pending',
+        //   } as AppBskyUnspeccedDefs.AgeAssuranceState,
+        // }
+
+        logger.debug(`fetch`, {
+          data,
+          account: agent.session?.did,
+        })
+
+        if (gate('age_assurance')) {
+          await getAndRegisterPushToken({
+            isAgeRestricted:
+              !!geolocation?.isAgeRestrictedGeo && data.status !== 'assured',
+          })
+        }
+
+        return data
+      } catch (e) {
+        if (!isNetworkError(e)) {
+          logger.error(`ageAssurance: failed to fetch`, {safeMessage: e})
+        }
+        // don't re-throw error, we'll just fall back to defaults
+        return null
+      }
+    },
+  })
+
+  /**
+   * Derive state, or fall back to defaults
+   */
+  const ageAssuranceContext = useMemo<AgeAssuranceContextType>(() => {
+    const {status, lastInitiatedAt} = data || DEFAULT_AGE_ASSURANCE_STATE
+    const ctx: AgeAssuranceContextType = {
+      isReady: isFetched || !isAgeAssuranceEnabled,
+      status,
+      lastInitiatedAt,
+      isAgeRestricted: isAgeAssuranceEnabled ? status !== 'assured' : false,
+    }
+    logger.debug(`context`, ctx)
+    return ctx
+  }, [isFetched, data, isAgeAssuranceEnabled])
+
+  const ageAssuranceAPIContext = useMemo<AgeAssuranceAPIContextType>(
+    () => ({
+      refetch,
+    }),
+    [refetch],
+  )
+
+  return (
+    <AgeAssuranceAPIContext.Provider value={ageAssuranceAPIContext}>
+      <AgeAssuranceContext.Provider value={ageAssuranceContext}>
+        {children}
+      </AgeAssuranceContext.Provider>
+    </AgeAssuranceAPIContext.Provider>
+  )
+}
+
+/**
+ * Access to low-level AA state. Prefer using {@link useAgeInfo} for a
+ * more user-friendly interface.
+ */
+export function useAgeAssuranceContext() {
+  return useContext(AgeAssuranceContext)
+}
+
+export function useAgeAssuranceAPIContext() {
+  return useContext(AgeAssuranceAPIContext)
+}