layout-context.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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] = useScopeValue<boolean>(
  142. 'ui.loadingStyleSheet'
  143. )
  144. const changeLayout = useCallback(
  145. (newLayout: IdeLayout, newView: IdeView = 'editor') => {
  146. setPdfLayout(newLayout)
  147. setView(newLayout === 'sideBySide' ? 'editor' : newView)
  148. setLayoutInLocalStorage(newLayout)
  149. },
  150. [setPdfLayout, setView]
  151. )
  152. const {
  153. reattach,
  154. detach,
  155. isLinking: detachIsLinking,
  156. isLinked: detachIsLinked,
  157. role: detachRole,
  158. isRedundant: detachIsRedundant,
  159. } = useDetachLayout()
  160. const pdfPreviewOpen =
  161. pdfLayout === 'sideBySide' || view === 'pdf' || detachRole === 'detacher'
  162. useEffect(() => {
  163. if (debugPdfDetach) {
  164. debugConsole.warn('Layout Effect', {
  165. detachIsRedundant,
  166. detachRole,
  167. detachIsLinking,
  168. detachIsLinked,
  169. })
  170. }
  171. if (detachRole !== 'detacher') return // not in a PDF detacher layout
  172. if (detachIsRedundant) {
  173. changeLayout('sideBySide')
  174. return
  175. }
  176. if (detachIsLinking || detachIsLinked) {
  177. // the tab is linked to a detached tab (or about to be linked); show
  178. // editor only
  179. changeLayout('flat', 'editor')
  180. }
  181. }, [
  182. detachIsRedundant,
  183. detachRole,
  184. detachIsLinking,
  185. detachIsLinked,
  186. changeLayout,
  187. ])
  188. const value = useMemo<LayoutContextValue>(
  189. () => ({
  190. reattach,
  191. detach,
  192. detachIsLinked,
  193. detachRole,
  194. changeLayout,
  195. chatIsOpen,
  196. leftMenuShown,
  197. pdfLayout,
  198. pdfPreviewOpen,
  199. projectSearchIsOpen,
  200. setProjectSearchIsOpen,
  201. reviewPanelOpen,
  202. miniReviewPanelVisible,
  203. loadingStyleSheet,
  204. setChatIsOpen,
  205. setLeftMenuShown,
  206. setPdfLayout,
  207. setReviewPanelOpen,
  208. setMiniReviewPanelVisible,
  209. setLoadingStyleSheet,
  210. setView,
  211. view,
  212. }),
  213. [
  214. reattach,
  215. detach,
  216. detachIsLinked,
  217. detachRole,
  218. changeLayout,
  219. chatIsOpen,
  220. leftMenuShown,
  221. pdfLayout,
  222. pdfPreviewOpen,
  223. projectSearchIsOpen,
  224. setProjectSearchIsOpen,
  225. reviewPanelOpen,
  226. miniReviewPanelVisible,
  227. loadingStyleSheet,
  228. setChatIsOpen,
  229. setLeftMenuShown,
  230. setPdfLayout,
  231. setReviewPanelOpen,
  232. setMiniReviewPanelVisible,
  233. setLoadingStyleSheet,
  234. setView,
  235. view,
  236. ]
  237. )
  238. return (
  239. <LayoutContext.Provider value={value}>{children}</LayoutContext.Provider>
  240. )
  241. }
  242. export function useLayoutContext() {
  243. const context = useContext(LayoutContext)
  244. if (!context) {
  245. throw new Error('useLayoutContext is only available inside LayoutProvider')
  246. }
  247. return context
  248. }