layout-context.tsx 7.5 KB

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