Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667import {useMemo} from 'react'import {useMutation, useQueryClient} from '@tanstack/react-query'
import {preferencesQueryKey} from '#/state/queries/preferences'import {useAgent, useSession} from '#/state/session'import {IS_DEV} from '#/env'import {account} from '#/storage'
// 6s in dev, 48h in prodconst BIRTHDATE_DELAY_HOURS = IS_DEV ? 0.001 : 48
/** * Stores the timestamp of the birthday update locally. This is used to * debounce birthday updates globally. * * Use {@link useIsBirthdateUpdateAllowed} to check if an update is allowed. */export function snoozeBirthdateUpdateAllowedForDid(did: string) { account.set([did, 'birthdateLastUpdatedAt'], new Date().toISOString())}
/** * Checks if we've already snoozed bday updates. */export function hasSnoozedBirthdateUpdateForDid(did: string) { return !!account.get([did, 'birthdateLastUpdatedAt'])}
/** * Returns whether a birthdate update is currently allowed, based on the last * update timestamp stored locally. Rate-limiting discourages toggling the * declared age back and forth to bypass the adult-content age gate. */export function useIsBirthdateUpdateAllowed() { const {currentAccount} = useSession() return useMemo(() => { if (!currentAccount) return false const lastUpdated = account.get([ currentAccount.did, 'birthdateLastUpdatedAt', ]) if (!lastUpdated) return true const lastUpdatedDate = new Date(lastUpdated) // eslint-disable-next-line react-hooks/purity const diffMs = Date.now() - lastUpdatedDate.getTime() const diffHours = diffMs / (1000 * 60 * 60) return diffHours >= BIRTHDATE_DELAY_HOURS }, [currentAccount])}
export function useBirthdateMutation() { const queryClient = useQueryClient() const agent = useAgent()
return useMutation<void, unknown, {birthDate: Date}>({ mutationFn: async ({birthDate}: {birthDate: Date}) => { const bday = birthDate.toISOString() await agent.setPersonalDetails({birthDate: bday}) // triggers a refetch, which re-derives the age gate in moderation-opts await queryClient.invalidateQueries({ queryKey: preferencesQueryKey, }) snoozeBirthdateUpdateAllowedForDid(agent.sessionManager.did!) }, })}