2023-01-10 23:13:37 +00:00
|
|
|
import { createContext, useCallback, useContext, useMemo, useState } from 'react'
|
2023-07-24 18:35:05 +00:00
|
|
|
import Modal from 'react-bootstrap/Modal'
|
2023-01-10 23:13:37 +00:00
|
|
|
|
|
|
|
export const ShowModalContext = createContext(() => null)
|
|
|
|
|
|
|
|
export function ShowModalProvider ({ children }) {
|
|
|
|
const [modal, showModal] = useModal()
|
|
|
|
const contextValue = showModal
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ShowModalContext.Provider value={contextValue}>
|
|
|
|
{children}
|
|
|
|
{modal}
|
|
|
|
</ShowModalContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useShowModal () {
|
|
|
|
return useContext(ShowModalContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function useModal () {
|
|
|
|
const [modalContent, setModalContent] = useState(null)
|
2023-07-22 10:56:30 +00:00
|
|
|
const [modalOptions, setModalOptions] = useState(null)
|
2023-01-10 23:13:37 +00:00
|
|
|
|
|
|
|
const onClose = useCallback(() => {
|
|
|
|
setModalContent(null)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const modal = useMemo(() => {
|
|
|
|
if (modalContent === null) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return (
|
2023-07-22 10:56:30 +00:00
|
|
|
<Modal onHide={modalOptions?.keepOpen ? null : onClose} show={!!modalContent}>
|
2023-01-10 23:13:37 +00:00
|
|
|
<div className='modal-close' onClick={onClose}>X</div>
|
|
|
|
<Modal.Body>
|
|
|
|
{modalContent}
|
|
|
|
</Modal.Body>
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}, [modalContent, onClose])
|
|
|
|
|
|
|
|
const showModal = useCallback(
|
2023-07-22 10:56:30 +00:00
|
|
|
(getContent, options) => {
|
|
|
|
setModalOptions(options)
|
2023-01-10 23:13:37 +00:00
|
|
|
setModalContent(getContent(onClose))
|
|
|
|
},
|
|
|
|
[onClose]
|
|
|
|
)
|
|
|
|
|
|
|
|
return [modal, showModal]
|
|
|
|
}
|