mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React from 'react'
2
3import {useDialogStateContext} from '#/state/dialogs'
4import {
5 DialogContextProps,
6 DialogControlRefProps,
7 DialogOuterProps,
8} from '#/components/Dialog/types'
9import {BottomSheetSnapPoint} from '../../../modules/bottom-sheet/src/BottomSheet.types'
10
11export const Context = React.createContext<DialogContextProps>({
12 close: () => {},
13 isNativeDialog: false,
14 nativeSnapPoint: BottomSheetSnapPoint.Hidden,
15 disableDrag: false,
16 setDisableDrag: () => {},
17 isWithinDialog: false,
18})
19
20export function useDialogContext() {
21 return React.useContext(Context)
22}
23
24export function useDialogControl(): DialogOuterProps['control'] {
25 const id = React.useId()
26 const control = React.useRef<DialogControlRefProps>({
27 open: () => {},
28 close: () => {},
29 })
30 const {activeDialogs} = useDialogStateContext()
31
32 React.useEffect(() => {
33 activeDialogs.current.set(id, control)
34 return () => {
35 // eslint-disable-next-line react-hooks/exhaustive-deps
36 activeDialogs.current.delete(id)
37 }
38 }, [id, activeDialogs])
39
40 return React.useMemo<DialogOuterProps['control']>(
41 () => ({
42 id,
43 ref: control,
44 open: () => {
45 control.current.open()
46 },
47 close: cb => {
48 control.current.close(cb)
49 },
50 }),
51 [id, control],
52 )
53}