|
1
|
"use client" |
|
2
|
|
|
3
|
import React, { useContext, useState } from "react" |
|
4
|
import { createContext } from "react" |
|
5
|
import { Modal, type ModalProps } from "@/components/Modal" |
|
6
|
|
|
7
|
export type ModalContextType = { |
|
8
|
modalProps: ModalProps | null |
|
9
|
setModalProps: (value: ModalProps | null) => void |
|
10
|
closeModal: () => void |
|
11
|
} |
|
12
|
|
|
13
|
const ModalContext = createContext<ModalContextType | null>(null) |
|
14
|
|
|
15
|
export type ModalProviderProps = { |
|
16
|
children?: React.ReactNode |
|
17
|
} |
|
18
|
|
|
19
|
export const ModalProvider = ({ children }: ModalProviderProps) => { |
|
20
|
const [modalProps, setModalProps] = useState<ModalProps | null>(null) |
|
21
|
|
|
22
|
const closeModal = () => { |
|
23
|
setModalProps(null) |
|
24
|
} |
|
25
|
|
|
26
|
return ( |
|
27
|
<ModalContext.Provider |
|
28
|
value={{ |
|
29
|
modalProps, |
|
30
|
setModalProps, |
|
31
|
closeModal, |
|
32
|
}} |
|
33
|
> |
|
34
|
{children} |
|
35
|
{modalProps && <Modal {...modalProps} onClose={closeModal} />} |
|
36
|
</ModalContext.Provider> |
|
37
|
) |
|
38
|
} |
|
39
|
|
|
40
|
export const useModal = () => { |
|
41
|
const context = useContext(ModalContext) |
|
42
|
|
|
43
|
if (!context) { |
|
44
|
throw new Error("useModal must be used within a ModalProvider") |
|
45
|
} |
|
46
|
|
|
47
|
return context |
|
48
|
} |