figure-modal-context.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { FC, createContext, useContext, useReducer } from 'react'
  2. import { PastedImageData } from '../../extensions/figure-modal'
  3. /* eslint-disable no-unused-vars */
  4. export enum FigureModalSource {
  5. NONE,
  6. FILE_UPLOAD,
  7. FILE_TREE,
  8. FROM_URL,
  9. OTHER_PROJECT,
  10. EDIT_FIGURE,
  11. }
  12. /* eslint-enable no-unused-vars */
  13. type FigureModalState = {
  14. source: FigureModalSource
  15. helpShown: boolean
  16. sourcePickerShown: boolean
  17. getPath?: () => Promise<string>
  18. width: number | undefined
  19. includeCaption: boolean
  20. includeLabel: boolean
  21. error?: string
  22. pastedImageData?: PastedImageData
  23. }
  24. type FigureModalStateUpdate = Partial<FigureModalState>
  25. const FigureModalContext = createContext<
  26. | (FigureModalState & {
  27. dispatch: (update: FigureModalStateUpdate) => void
  28. })
  29. | undefined
  30. >(undefined)
  31. export const useFigureModalContext = () => {
  32. const context = useContext(FigureModalContext)
  33. if (!context) {
  34. throw new Error(
  35. 'useFigureModalContext is only available inside FigureModalProvider'
  36. )
  37. }
  38. return context
  39. }
  40. const reducer = (prev: FigureModalState, action: Partial<FigureModalState>) => {
  41. if ('source' in action && prev.source === FigureModalSource.NONE) {
  42. // Reset when showing modal
  43. return {
  44. ...prev,
  45. width: 0.5,
  46. includeLabel: true,
  47. includeCaption: true,
  48. helpShown: false,
  49. sourcePickerShown: false,
  50. getPath: undefined,
  51. error: undefined,
  52. pastedImageData: undefined,
  53. ...action,
  54. }
  55. }
  56. return { ...prev, ...action }
  57. }
  58. type FigureModalExistingFigureState = {
  59. name: string | undefined
  60. hasComplexGraphicsArgument?: boolean
  61. }
  62. type FigureModalExistingFigureStateUpdate =
  63. Partial<FigureModalExistingFigureState>
  64. const FigureModalExistingFigureContext = createContext<
  65. | (FigureModalExistingFigureState & {
  66. dispatch: (update: FigureModalExistingFigureStateUpdate) => void
  67. })
  68. | undefined
  69. >(undefined)
  70. export const FigureModalProvider: FC = ({ children }) => {
  71. const [state, dispatch] = useReducer(reducer, {
  72. source: FigureModalSource.NONE,
  73. helpShown: false,
  74. sourcePickerShown: false,
  75. getPath: undefined,
  76. includeLabel: true,
  77. includeCaption: true,
  78. width: 0.5,
  79. })
  80. const [existingFigureState, dispatchFigureState] = useReducer(
  81. (
  82. prev: FigureModalExistingFigureState,
  83. action: FigureModalExistingFigureStateUpdate
  84. ) => ({ ...prev, ...action }),
  85. {
  86. name: undefined,
  87. }
  88. )
  89. return (
  90. <FigureModalContext.Provider value={{ ...state, dispatch }}>
  91. <FigureModalExistingFigureContext.Provider
  92. value={{ ...existingFigureState, dispatch: dispatchFigureState }}
  93. >
  94. {children}
  95. </FigureModalExistingFigureContext.Provider>
  96. </FigureModalContext.Provider>
  97. )
  98. }
  99. export const useFigureModalExistingFigureContext = () => {
  100. const context = useContext(FigureModalExistingFigureContext)
  101. if (!context) {
  102. throw new Error(
  103. 'useFigureModalExistingFigureContext is only available inside FigureModalProvider'
  104. )
  105. }
  106. return context
  107. }