modals-context.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 GenericConfirmModal, {
  16. GenericConfirmModalOwnProps,
  17. } from '../components/modals/generic-confirm-modal'
  18. type ModalsContextValue = {
  19. genericModalVisible: boolean
  20. showGenericConfirmModal: (data: GenericConfirmModalOwnProps) => void
  21. showGenericMessageModal: (
  22. title: GenericMessageModalOwnProps['title'],
  23. message: GenericMessageModalOwnProps['message']
  24. ) => void
  25. showOutOfSyncModal: (
  26. editorContent: OutOfSyncModalProps['editorContent']
  27. ) => void
  28. }
  29. const ModalsContext = createContext<ModalsContextValue | undefined>(undefined)
  30. export const ModalsContextProvider: FC<React.PropsWithChildren> = ({
  31. children,
  32. }) => {
  33. const [showGenericModal, setShowGenericModal] = useState(false)
  34. const [showConfirmModal, setShowConfirmModal] = useState(false)
  35. const [genericMessageModalData, setGenericMessageModalData] =
  36. useState<GenericMessageModalOwnProps>({ title: '', message: '' })
  37. const [genericConfirmModalData, setGenericConfirmModalData] =
  38. useState<GenericConfirmModalOwnProps>({
  39. title: '',
  40. message: '',
  41. onConfirm: () => {},
  42. })
  43. const [shouldShowOutOfSyncModal, setShouldShowOutOfSyncModal] =
  44. useState(false)
  45. const [outOfSyncModalData, setOutOfSyncModalData] = useState({
  46. editorContent: '',
  47. })
  48. const handleHideGenericModal = useCallback(() => {
  49. setShowGenericModal(false)
  50. }, [])
  51. const handleHideGenericConfirmModal = useCallback(() => {
  52. setShowConfirmModal(false)
  53. }, [])
  54. const handleConfirmGenericConfirmModal = useCallback(() => {
  55. genericConfirmModalData.onConfirm()
  56. setShowConfirmModal(false)
  57. }, [genericConfirmModalData])
  58. const showGenericMessageModal = useCallback(
  59. (
  60. title: GenericMessageModalOwnProps['title'],
  61. message: GenericMessageModalOwnProps['message']
  62. ) => {
  63. setGenericMessageModalData({ title, message })
  64. setShowGenericModal(true)
  65. },
  66. []
  67. )
  68. const showGenericConfirmModal = useCallback(
  69. (data: GenericConfirmModalOwnProps) => {
  70. setGenericConfirmModalData(data)
  71. setShowConfirmModal(true)
  72. },
  73. []
  74. )
  75. const handleHideOutOfSyncModal = useCallback(() => {
  76. setShouldShowOutOfSyncModal(false)
  77. }, [])
  78. const showOutOfSyncModal = useCallback((editorContent: string) => {
  79. setOutOfSyncModalData({ editorContent })
  80. setShouldShowOutOfSyncModal(true)
  81. }, [])
  82. const value = useMemo<ModalsContextValue>(
  83. () => ({
  84. showGenericMessageModal,
  85. showGenericConfirmModal,
  86. genericModalVisible: showGenericModal,
  87. showOutOfSyncModal,
  88. }),
  89. [
  90. showGenericMessageModal,
  91. showGenericConfirmModal,
  92. showGenericModal,
  93. showOutOfSyncModal,
  94. ]
  95. )
  96. return (
  97. <ModalsContext.Provider value={value}>
  98. {children}
  99. <GenericMessageModal
  100. show={showGenericModal}
  101. onHide={handleHideGenericModal}
  102. {...genericMessageModalData}
  103. />
  104. <GenericConfirmModal
  105. show={showConfirmModal}
  106. onHide={handleHideGenericConfirmModal}
  107. {...genericConfirmModalData}
  108. onConfirm={handleConfirmGenericConfirmModal}
  109. />
  110. <OutOfSyncModal
  111. {...outOfSyncModalData}
  112. show={shouldShowOutOfSyncModal}
  113. onHide={handleHideOutOfSyncModal}
  114. />
  115. </ModalsContext.Provider>
  116. )
  117. }
  118. export function useModalsContext(): ModalsContextValue {
  119. const context = useContext(ModalsContext)
  120. if (!context) {
  121. throw new Error(
  122. 'useModalsContext is only available inside ModalsContextProvider'
  123. )
  124. }
  125. return context
  126. }