2023-10-27 04:43:50 -04:00
|
|
|
import {
|
|
|
|
createContext,
|
|
|
|
useContext,
|
|
|
|
FC,
|
|
|
|
useCallback,
|
|
|
|
useMemo,
|
|
|
|
useState,
|
|
|
|
} from 'react'
|
|
|
|
import GenericMessageModal, {
|
|
|
|
GenericMessageModalOwnProps,
|
|
|
|
} from '@/features/ide-react/components/modals/generic-message-modal'
|
2023-11-06 08:11:06 -05:00
|
|
|
import OutOfSyncModal, {
|
|
|
|
OutOfSyncModalProps,
|
|
|
|
} from '@/features/ide-react/components/modals/out-of-sync-modal'
|
2023-10-27 04:43:50 -04:00
|
|
|
|
|
|
|
type ModalsContextValue = {
|
2023-11-02 07:36:04 -04:00
|
|
|
genericModalVisible: boolean
|
2023-10-27 04:43:50 -04:00
|
|
|
showGenericMessageModal: (
|
|
|
|
title: GenericMessageModalOwnProps['title'],
|
|
|
|
message: GenericMessageModalOwnProps['message']
|
|
|
|
) => void
|
2023-11-06 08:11:06 -05:00
|
|
|
showOutOfSyncModal: (
|
|
|
|
editorContent: OutOfSyncModalProps['editorContent']
|
|
|
|
) => void
|
2023-10-27 04:43:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const ModalsContext = createContext<ModalsContextValue | undefined>(undefined)
|
|
|
|
|
|
|
|
export const ModalsContextProvider: FC = ({ children }) => {
|
|
|
|
const [showGenericModal, setShowGenericModal] = useState(false)
|
|
|
|
const [genericMessageModalData, setGenericMessageModalData] =
|
|
|
|
useState<GenericMessageModalOwnProps>({ title: '', message: '' })
|
|
|
|
|
2023-11-06 08:11:06 -05:00
|
|
|
const [shouldShowOutOfSyncModal, setShouldShowOutOfSyncModal] =
|
|
|
|
useState(false)
|
|
|
|
const [outOfSyncModalData, setOutOfSyncModalData] = useState({
|
|
|
|
editorContent: '',
|
|
|
|
})
|
|
|
|
|
2023-10-27 04:43:50 -04:00
|
|
|
const handleHideGenericModal = useCallback(() => {
|
|
|
|
setShowGenericModal(false)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const showGenericMessageModal = useCallback(
|
|
|
|
(
|
|
|
|
title: GenericMessageModalOwnProps['title'],
|
|
|
|
message: GenericMessageModalOwnProps['message']
|
|
|
|
) => {
|
|
|
|
setGenericMessageModalData({ title, message })
|
|
|
|
setShowGenericModal(true)
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
|
2023-11-06 08:11:06 -05:00
|
|
|
const handleHideOutOfSyncModal = useCallback(() => {
|
|
|
|
setShouldShowOutOfSyncModal(false)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const showOutOfSyncModal = useCallback((editorContent: string) => {
|
|
|
|
setOutOfSyncModalData({ editorContent })
|
|
|
|
setShouldShowOutOfSyncModal(true)
|
|
|
|
}, [])
|
|
|
|
|
2023-10-27 04:43:50 -04:00
|
|
|
const value = useMemo<ModalsContextValue>(
|
|
|
|
() => ({
|
|
|
|
showGenericMessageModal,
|
2023-11-02 07:36:04 -04:00
|
|
|
genericModalVisible: showGenericModal,
|
2023-11-06 08:11:06 -05:00
|
|
|
showOutOfSyncModal,
|
2023-10-27 04:43:50 -04:00
|
|
|
}),
|
2023-12-14 06:06:36 -05:00
|
|
|
[showGenericMessageModal, showGenericModal, showOutOfSyncModal]
|
2023-10-27 04:43:50 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ModalsContext.Provider value={value}>
|
|
|
|
{children}
|
|
|
|
<GenericMessageModal
|
|
|
|
show={showGenericModal}
|
|
|
|
onHide={handleHideGenericModal}
|
|
|
|
{...genericMessageModalData}
|
|
|
|
/>
|
2023-11-06 08:11:06 -05:00
|
|
|
<OutOfSyncModal
|
|
|
|
{...outOfSyncModalData}
|
|
|
|
show={shouldShowOutOfSyncModal}
|
|
|
|
onHide={handleHideOutOfSyncModal}
|
|
|
|
/>
|
2023-10-27 04:43:50 -04:00
|
|
|
</ModalsContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useModalsContext(): ModalsContextValue {
|
|
|
|
const context = useContext(ModalsContext)
|
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
throw new Error(
|
|
|
|
'useModalsContext is only available inside ModalsContextProvider'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return context
|
|
|
|
}
|