modals-context.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. type ModalsContextValue = {
  16. genericModalVisible: boolean
  17. showGenericMessageModal: (
  18. title: GenericMessageModalOwnProps['title'],
  19. message: GenericMessageModalOwnProps['message']
  20. ) => void
  21. showOutOfSyncModal: (
  22. editorContent: OutOfSyncModalProps['editorContent']
  23. ) => void
  24. }
  25. const ModalsContext = createContext<ModalsContextValue | undefined>(undefined)
  26. export const ModalsContextProvider: FC = ({ children }) => {
  27. const [showGenericModal, setShowGenericModal] = useState(false)
  28. const [genericMessageModalData, setGenericMessageModalData] =
  29. useState<GenericMessageModalOwnProps>({ title: '', message: '' })
  30. const [shouldShowOutOfSyncModal, setShouldShowOutOfSyncModal] =
  31. useState(false)
  32. const [outOfSyncModalData, setOutOfSyncModalData] = useState({
  33. editorContent: '',
  34. })
  35. const handleHideGenericModal = useCallback(() => {
  36. setShowGenericModal(false)
  37. }, [])
  38. const showGenericMessageModal = useCallback(
  39. (
  40. title: GenericMessageModalOwnProps['title'],
  41. message: GenericMessageModalOwnProps['message']
  42. ) => {
  43. setGenericMessageModalData({ title, message })
  44. setShowGenericModal(true)
  45. },
  46. []
  47. )
  48. const handleHideOutOfSyncModal = useCallback(() => {
  49. setShouldShowOutOfSyncModal(false)
  50. }, [])
  51. const showOutOfSyncModal = useCallback((editorContent: string) => {
  52. setOutOfSyncModalData({ editorContent })
  53. setShouldShowOutOfSyncModal(true)
  54. }, [])
  55. const value = useMemo<ModalsContextValue>(
  56. () => ({
  57. showGenericMessageModal,
  58. genericModalVisible: showGenericModal,
  59. showOutOfSyncModal,
  60. }),
  61. [showGenericMessageModal, showGenericModal, showOutOfSyncModal]
  62. )
  63. return (
  64. <ModalsContext.Provider value={value}>
  65. {children}
  66. <GenericMessageModal
  67. show={showGenericModal}
  68. onHide={handleHideGenericModal}
  69. {...genericMessageModalData}
  70. />
  71. <OutOfSyncModal
  72. {...outOfSyncModalData}
  73. show={shouldShowOutOfSyncModal}
  74. onHide={handleHideOutOfSyncModal}
  75. />
  76. </ModalsContext.Provider>
  77. )
  78. }
  79. export function useModalsContext(): ModalsContextValue {
  80. const context = useContext(ModalsContext)
  81. if (!context) {
  82. throw new Error(
  83. 'useModalsContext is only available inside ModalsContextProvider'
  84. )
  85. }
  86. return context
  87. }