review-panel-view-context.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import {
  2. createContext,
  3. Dispatch,
  4. FC,
  5. SetStateAction,
  6. useContext,
  7. useMemo,
  8. useState,
  9. } from 'react'
  10. export type View = 'cur_file' | 'overview'
  11. export const ReviewPanelViewContext = createContext<View>('cur_file')
  12. type ViewActions = {
  13. setView: Dispatch<SetStateAction<View>>
  14. }
  15. const ReviewPanelViewActionsContext = createContext<ViewActions | undefined>(
  16. undefined
  17. )
  18. export const ReviewPanelViewProvider: FC<React.PropsWithChildren> = ({
  19. children,
  20. }) => {
  21. const [view, setView] = useState<View>('cur_file')
  22. const actions = useMemo(
  23. () => ({
  24. setView,
  25. }),
  26. [setView]
  27. )
  28. return (
  29. <ReviewPanelViewActionsContext.Provider value={actions}>
  30. <ReviewPanelViewContext.Provider value={view}>
  31. {children}
  32. </ReviewPanelViewContext.Provider>
  33. </ReviewPanelViewActionsContext.Provider>
  34. )
  35. }
  36. export const useReviewPanelViewContext = () => {
  37. return useContext(ReviewPanelViewContext)
  38. }
  39. export const useReviewPanelViewActionsContext = () => {
  40. const context = useContext(ReviewPanelViewActionsContext)
  41. if (!context) {
  42. throw new Error(
  43. 'useViewActionsContext is only available inside ViewProvider'
  44. )
  45. }
  46. return context
  47. }