modals-context.tsx 3.8 KB

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