mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
c3afce73c1
React IDE page: add out-of-sync modal and project deleted modal GitOrigin-RevId: 6081a40c3009e8ebc5b3f9752c4059af3d52966d
123 lines
3.3 KiB
TypeScript
123 lines
3.3 KiB
TypeScript
import {
|
|
createContext,
|
|
useContext,
|
|
FC,
|
|
useCallback,
|
|
useMemo,
|
|
useState,
|
|
} from 'react'
|
|
import GenericMessageModal, {
|
|
GenericMessageModalOwnProps,
|
|
} from '@/features/ide-react/components/modals/generic-message-modal'
|
|
import OutOfSyncModal, {
|
|
OutOfSyncModalProps,
|
|
} from '@/features/ide-react/components/modals/out-of-sync-modal'
|
|
import LockEditorMessageModal from '@/features/ide-react/components/modals/lock-editor-message-modal'
|
|
|
|
type ModalsContextValue = {
|
|
genericModalVisible: boolean
|
|
showGenericMessageModal: (
|
|
title: GenericMessageModalOwnProps['title'],
|
|
message: GenericMessageModalOwnProps['message']
|
|
) => void
|
|
showOutOfSyncModal: (
|
|
editorContent: OutOfSyncModalProps['editorContent']
|
|
) => void
|
|
showLockEditorMessageModal: (delay: number) => void
|
|
}
|
|
|
|
const ModalsContext = createContext<ModalsContextValue | undefined>(undefined)
|
|
|
|
export const ModalsContextProvider: FC = ({ children }) => {
|
|
const [showGenericModal, setShowGenericModal] = useState(false)
|
|
const [genericMessageModalData, setGenericMessageModalData] =
|
|
useState<GenericMessageModalOwnProps>({ title: '', message: '' })
|
|
|
|
const [shouldShowOutOfSyncModal, setShouldShowOutOfSyncModal] =
|
|
useState(false)
|
|
const [outOfSyncModalData, setOutOfSyncModalData] = useState({
|
|
editorContent: '',
|
|
})
|
|
|
|
const [shouldShowLockEditorModal, setShouldShowLockEditorModal] =
|
|
useState(false)
|
|
const [lockEditorModalData, setLockEditorModalData] = useState({
|
|
delay: 0,
|
|
})
|
|
|
|
const handleHideGenericModal = useCallback(() => {
|
|
setShowGenericModal(false)
|
|
}, [])
|
|
|
|
const showGenericMessageModal = useCallback(
|
|
(
|
|
title: GenericMessageModalOwnProps['title'],
|
|
message: GenericMessageModalOwnProps['message']
|
|
) => {
|
|
setGenericMessageModalData({ title, message })
|
|
setShowGenericModal(true)
|
|
},
|
|
[]
|
|
)
|
|
|
|
const handleHideOutOfSyncModal = useCallback(() => {
|
|
setShouldShowOutOfSyncModal(false)
|
|
}, [])
|
|
|
|
const showOutOfSyncModal = useCallback((editorContent: string) => {
|
|
setOutOfSyncModalData({ editorContent })
|
|
setShouldShowOutOfSyncModal(true)
|
|
}, [])
|
|
|
|
const showLockEditorMessageModal = useCallback((delay: number) => {
|
|
setLockEditorModalData({ delay })
|
|
setShouldShowLockEditorModal(true)
|
|
}, [])
|
|
|
|
const value = useMemo<ModalsContextValue>(
|
|
() => ({
|
|
showGenericMessageModal,
|
|
genericModalVisible: showGenericModal,
|
|
showOutOfSyncModal,
|
|
showLockEditorMessageModal,
|
|
}),
|
|
[
|
|
showGenericMessageModal,
|
|
showGenericModal,
|
|
showOutOfSyncModal,
|
|
showLockEditorMessageModal,
|
|
]
|
|
)
|
|
|
|
return (
|
|
<ModalsContext.Provider value={value}>
|
|
{children}
|
|
<GenericMessageModal
|
|
show={showGenericModal}
|
|
onHide={handleHideGenericModal}
|
|
{...genericMessageModalData}
|
|
/>
|
|
<OutOfSyncModal
|
|
{...outOfSyncModalData}
|
|
show={shouldShowOutOfSyncModal}
|
|
onHide={handleHideOutOfSyncModal}
|
|
/>
|
|
<LockEditorMessageModal
|
|
{...lockEditorModalData}
|
|
show={shouldShowLockEditorModal}
|
|
/>
|
|
</ModalsContext.Provider>
|
|
)
|
|
}
|
|
|
|
export function useModalsContext(): ModalsContextValue {
|
|
const context = useContext(ModalsContext)
|
|
|
|
if (!context) {
|
|
throw new Error(
|
|
'useModalsContext is only available inside ModalsContextProvider'
|
|
)
|
|
}
|
|
|
|
return context
|
|
}
|