Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798import {createContext, useContext, useMemo, useState} from 'react'
import * as Dialog from '#/components/Dialog'import {type Screen} from '#/components/dialogs/EmailDialog/types'import {type ReportSubject} from '#/components/moderation/ReportDialog'
type Control = Dialog.DialogControlProps
export type StatefulControl<T> = { control: Control open: (value: T) => void clear: () => void value: T | undefined}
type ControlsContext = { mutedWordsDialogControl: Control signinDialogControl: Control inAppBrowserConsentControl: StatefulControl<string> emailDialogControl: StatefulControl<Screen> linkWarningDialogControl: StatefulControl<{ href: string displayText: string share?: boolean }> reportDialogControl: StatefulControl<{subject: ReportSubject}>}
const ControlsContext = createContext<ControlsContext | null>(null)ControlsContext.displayName = 'GlobalDialogControlsContext'
export function useGlobalDialogsControlContext() { const ctx = useContext(ControlsContext) if (!ctx) { throw new Error( 'useGlobalDialogsControlContext must be used within a Provider', ) } return ctx}
export function Provider({children}: React.PropsWithChildren<{}>) { const mutedWordsDialogControl = Dialog.useDialogControl() const signinDialogControl = Dialog.useDialogControl() const inAppBrowserConsentControl = useStatefulDialogControl<string>() const emailDialogControl = useStatefulDialogControl<Screen>() const linkWarningDialogControl = useStatefulDialogControl<{ href: string displayText: string share?: boolean }>() const reportDialogControl = useStatefulDialogControl<{ subject: ReportSubject }>()
const ctx = useMemo<ControlsContext>( () => ({ mutedWordsDialogControl, signinDialogControl, inAppBrowserConsentControl, emailDialogControl, linkWarningDialogControl, reportDialogControl, }), [ mutedWordsDialogControl, signinDialogControl, inAppBrowserConsentControl, emailDialogControl, linkWarningDialogControl, reportDialogControl, ], )
return ( <ControlsContext.Provider value={ctx}>{children}</ControlsContext.Provider> )}
export function useStatefulDialogControl<T>( initialValue?: T,): StatefulControl<T> { const [value, setValue] = useState(initialValue) const control = Dialog.useDialogControl() return useMemo( () => ({ control, open: (v: T) => { setValue(v) control.open() }, clear: () => setValue(initialValue), value, }), [control, value, initialValue], )}