modals-context.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import {
  2. createContext,
  3. useContext,
  4. FC,
  5. useCallback,
  6. useMemo,
  7. useState,
  8. } from 'react'
  9. import GenericMessageModal, {
  10. GenericMessageModalOwnProps,
  11. } from '@/features/ide-react/components/modals/generic-message-modal'
  12. import OutOfSyncModal, {
  13. OutOfSyncModalProps,
  14. } from '@/features/ide-react/components/modals/out-of-sync-modal'
  15. import LockEditorMessageModal from '@/features/ide-react/components/modals/lock-editor-message-modal'
  16. type ModalsContextValue = {
  17. genericModalVisible: boolean
  18. showGenericMessageModal: (
  19. title: GenericMessageModalOwnProps['title'],
  20. message: GenericMessageModalOwnProps['message']
  21. ) => void
  22. showOutOfSyncModal: (
  23. editorContent: OutOfSyncModalProps['editorContent']
  24. ) => void
  25. showLockEditorMessageModal: (delay: number) => void
  26. }
  27. const ModalsContext = createContext<ModalsContextValue | undefined>(undefined)
  28. export const ModalsContextProvider: FC = ({ children }) => {
  29. const [showGenericModal, setShowGenericModal] = useState(false)
  30. const [genericMessageModalData, setGenericMessageModalData] =
  31. useState<GenericMessageModalOwnProps>({ title: '', message: '' })
  32. const [shouldShowOutOfSyncModal, setShouldShowOutOfSyncModal] =
  33. useState(false)
  34. const [outOfSyncModalData, setOutOfSyncModalData] = useState({
  35. editorContent: '',
  36. })
  37. const [shouldShowLockEditorModal, setShouldShowLockEditorModal] =
  38. useState(false)
  39. const [lockEditorModalData, setLockEditorModalData] = useState({
  40. delay: 0,
  41. })
  42. const handleHideGenericModal = useCallback(() => {
  43. setShowGenericModal(false)
  44. }, [])
  45. const showGenericMessageModal = useCallback(
  46. (
  47. title: GenericMessageModalOwnProps['title'],
  48. message: GenericMessageModalOwnProps['message']
  49. ) => {
  50. setGenericMessageModalData({ title, message })
  51. setShowGenericModal(true)
  52. },
  53. []
  54. )
  55. const handleHideOutOfSyncModal = useCallback(() => {
  56. setShouldShowOutOfSyncModal(false)
  57. }, [])
  58. const showOutOfSyncModal = useCallback((editorContent: string) => {
  59. setOutOfSyncModalData({ editorContent })
  60. setShouldShowOutOfSyncModal(true)
  61. }, [])
  62. const showLockEditorMessageModal = useCallback((delay: number) => {
  63. setLockEditorModalData({ delay })
  64. setShouldShowLockEditorModal(true)
  65. }, [])
  66. const value = useMemo<ModalsContextValue>(
  67. () => ({
  68. showGenericMessageModal,
  69. genericModalVisible: showGenericModal,
  70. showOutOfSyncModal,
  71. showLockEditorMessageModal,
  72. }),
  73. [
  74. showGenericMessageModal,
  75. showGenericModal,
  76. showOutOfSyncModal,
  77. showLockEditorMessageModal,
  78. ]
  79. )
  80. return (
  81. <ModalsContext.Provider value={value}>
  82. {children}
  83. <GenericMessageModal
  84. show={showGenericModal}
  85. onHide={handleHideGenericModal}
  86. {...genericMessageModalData}
  87. />
  88. <OutOfSyncModal
  89. {...outOfSyncModalData}
  90. show={shouldShowOutOfSyncModal}
  91. onHide={handleHideOutOfSyncModal}
  92. />
  93. <LockEditorMessageModal
  94. {...lockEditorModalData}
  95. show={shouldShowLockEditorModal}
  96. />
  97. </ModalsContext.Provider>
  98. )
  99. }
  100. export function useModalsContext(): ModalsContextValue {
  101. const context = useContext(ModalsContext)
  102. if (!context) {
  103. throw new Error(
  104. 'useModalsContext is only available inside ModalsContextProvider'
  105. )
  106. }
  107. return context
  108. }