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})
18
19export function useDialogContext() {
20 return React.useContext(Context)
21}
22
23export function useDialogControl(): DialogOuterProps['control'] {
24 const id = React.useId()
25 const control = React.useRef<DialogControlRefProps>({
26 open: () => {},
27 close: () => {},
28 })
29 const {activeDialogs} = useDialogStateContext()
30
31 React.useEffect(() => {
32 activeDialogs.current.set(id, control)
33 return () => {
34 // eslint-disable-next-line react-hooks/exhaustive-deps
35 activeDialogs.current.delete(id)
36 }
37 }, [id, activeDialogs])
38
39 return React.useMemo<DialogOuterProps['control']>(
40 () => ({
41 id,
42 ref: control,
43 open: () => {
44 control.current.open()
45 },
46 close: cb => {
47 control.current.close(cb)
48 },
49 }),
50 [id, control],
51 )
52}