layout-context.tsx 7.3 KB

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