layout-context.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import {
  2. createContext,
  3. useContext,
  4. useCallback,
  5. useMemo,
  6. useEffect,
  7. Dispatch,
  8. SetStateAction,
  9. FC,
  10. } from 'react'
  11. import useScopeValue from '../hooks/use-scope-value'
  12. import useDetachLayout from '../hooks/use-detach-layout'
  13. import localStorage from '../../infrastructure/local-storage'
  14. import getMeta from '../../utils/meta'
  15. import { DetachRole } from './detach-context'
  16. import { debugConsole } from '@/utils/debugging'
  17. import { BinaryFile } from '@/features/file-view/types/binary-file'
  18. import useScopeEventEmitter from '@/shared/hooks/use-scope-event-emitter'
  19. import useEventListener from '@/shared/hooks/use-event-listener'
  20. export type IdeLayout = 'sideBySide' | 'flat'
  21. export type IdeView = 'editor' | 'file' | 'pdf' | 'history'
  22. type LayoutContextValue = {
  23. reattach: () => void
  24. detach: () => void
  25. detachIsLinked: boolean
  26. detachRole: DetachRole
  27. changeLayout: (newLayout: IdeLayout, newView?: IdeView) => void
  28. view: IdeView | null
  29. setView: (view: IdeView | null) => void
  30. chatIsOpen: boolean
  31. setChatIsOpen: Dispatch<SetStateAction<LayoutContextValue['chatIsOpen']>>
  32. reviewPanelOpen: boolean
  33. setReviewPanelOpen: Dispatch<
  34. SetStateAction<LayoutContextValue['reviewPanelOpen']>
  35. >
  36. miniReviewPanelVisible: boolean
  37. setMiniReviewPanelVisible: Dispatch<
  38. SetStateAction<LayoutContextValue['miniReviewPanelVisible']>
  39. >
  40. leftMenuShown: boolean
  41. setLeftMenuShown: Dispatch<
  42. SetStateAction<LayoutContextValue['leftMenuShown']>
  43. >
  44. loadingStyleSheet: boolean
  45. setLoadingStyleSheet: Dispatch<
  46. SetStateAction<LayoutContextValue['loadingStyleSheet']>
  47. >
  48. pdfLayout: IdeLayout
  49. pdfPreviewOpen: boolean
  50. }
  51. const debugPdfDetach = getMeta('ol-debugPdfDetach')
  52. export const LayoutContext = createContext<LayoutContextValue | undefined>(
  53. undefined
  54. )
  55. function setLayoutInLocalStorage(pdfLayout: IdeLayout) {
  56. localStorage.setItem(
  57. 'pdf.layout',
  58. pdfLayout === 'sideBySide' ? 'split' : 'flat'
  59. )
  60. }
  61. export const LayoutProvider: FC = ({ children }) => {
  62. // what to show in the "flat" view (editor or pdf)
  63. const [view, _setView] = useScopeValue<IdeView | null>('ui.view')
  64. const [openFile] = useScopeValue<BinaryFile | null>('openFile')
  65. const historyToggleEmitter = useScopeEventEmitter('history:toggle', true)
  66. const setView = useCallback(
  67. (value: IdeView | null) => {
  68. _setView(oldValue => {
  69. // ensure that the "history:toggle" event is broadcast when switching in or out of history view
  70. if (value === 'history' || oldValue === 'history') {
  71. historyToggleEmitter()
  72. }
  73. if (value === 'editor' && openFile) {
  74. // if a file is currently opened, ensure the view is 'file' instead of
  75. // 'editor' when the 'editor' view is requested. This is to ensure
  76. // that the entity selected in the file tree is the one visible and
  77. // that docs don't take precedence over files.
  78. return 'file'
  79. }
  80. return value
  81. })
  82. },
  83. [_setView, openFile, historyToggleEmitter]
  84. )
  85. // whether the chat pane is open
  86. const [chatIsOpen, setChatIsOpen] = useScopeValue<boolean>('ui.chatOpen')
  87. // whether the review pane is open
  88. const [reviewPanelOpen, setReviewPanelOpen] =
  89. useScopeValue('ui.reviewPanelOpen')
  90. // whether the review pane is collapsed
  91. const [miniReviewPanelVisible, setMiniReviewPanelVisible] =
  92. useScopeValue<boolean>('ui.miniReviewPanelVisible')
  93. // whether the menu pane is open
  94. const [leftMenuShown, setLeftMenuShown] =
  95. useScopeValue<boolean>('ui.leftMenuShown')
  96. useEventListener(
  97. 'ui.toggle-left-menu',
  98. useCallback(
  99. event => {
  100. setLeftMenuShown((event as CustomEvent<boolean>).detail)
  101. },
  102. [setLeftMenuShown]
  103. )
  104. )
  105. // whether to display the editor and preview side-by-side or full-width ("flat")
  106. const [pdfLayout, setPdfLayout] = useScopeValue<IdeLayout>('ui.pdfLayout')
  107. // whether stylesheet on theme is loading
  108. const [loadingStyleSheet, setLoadingStyleSheet] = useScopeValue<boolean>(
  109. 'ui.loadingStyleSheet'
  110. )
  111. const changeLayout = useCallback(
  112. (newLayout: IdeLayout, newView: IdeView = 'editor') => {
  113. setPdfLayout(newLayout)
  114. setView(newLayout === 'sideBySide' ? 'editor' : newView)
  115. setLayoutInLocalStorage(newLayout)
  116. },
  117. [setPdfLayout, setView]
  118. )
  119. const {
  120. reattach,
  121. detach,
  122. isLinking: detachIsLinking,
  123. isLinked: detachIsLinked,
  124. role: detachRole,
  125. isRedundant: detachIsRedundant,
  126. } = useDetachLayout()
  127. const pdfPreviewOpen =
  128. pdfLayout === 'sideBySide' || view === 'pdf' || detachRole === 'detacher'
  129. useEffect(() => {
  130. if (debugPdfDetach) {
  131. debugConsole.warn('Layout Effect', {
  132. detachIsRedundant,
  133. detachRole,
  134. detachIsLinking,
  135. detachIsLinked,
  136. })
  137. }
  138. if (detachRole !== 'detacher') return // not in a PDF detacher layout
  139. if (detachIsRedundant) {
  140. changeLayout('sideBySide')
  141. return
  142. }
  143. if (detachIsLinking || detachIsLinked) {
  144. // the tab is linked to a detached tab (or about to be linked); show
  145. // editor only
  146. changeLayout('flat', 'editor')
  147. }
  148. }, [
  149. detachIsRedundant,
  150. detachRole,
  151. detachIsLinking,
  152. detachIsLinked,
  153. changeLayout,
  154. ])
  155. const value = useMemo<LayoutContextValue>(
  156. () => ({
  157. reattach,
  158. detach,
  159. detachIsLinked,
  160. detachRole,
  161. changeLayout,
  162. chatIsOpen,
  163. leftMenuShown,
  164. pdfLayout,
  165. pdfPreviewOpen,
  166. reviewPanelOpen,
  167. miniReviewPanelVisible,
  168. loadingStyleSheet,
  169. setChatIsOpen,
  170. setLeftMenuShown,
  171. setPdfLayout,
  172. setReviewPanelOpen,
  173. setMiniReviewPanelVisible,
  174. setLoadingStyleSheet,
  175. setView,
  176. view,
  177. }),
  178. [
  179. reattach,
  180. detach,
  181. detachIsLinked,
  182. detachRole,
  183. changeLayout,
  184. chatIsOpen,
  185. leftMenuShown,
  186. pdfLayout,
  187. pdfPreviewOpen,
  188. reviewPanelOpen,
  189. miniReviewPanelVisible,
  190. loadingStyleSheet,
  191. setChatIsOpen,
  192. setLeftMenuShown,
  193. setPdfLayout,
  194. setReviewPanelOpen,
  195. setMiniReviewPanelVisible,
  196. setLoadingStyleSheet,
  197. setView,
  198. view,
  199. ]
  200. )
  201. return (
  202. <LayoutContext.Provider value={value}>{children}</LayoutContext.Provider>
  203. )
  204. }
  205. export function useLayoutContext() {
  206. const context = useContext(LayoutContext)
  207. if (!context) {
  208. throw new Error('useLayoutContext is only available inside LayoutProvider')
  209. }
  210. return context
  211. }