editor-and-sidebar.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import React, { useCallback, useEffect, useRef, useState } from 'react'
  2. import TwoColumnMainContent from '@/features/ide-react/components/layout/two-column-main-content'
  3. import EditorSidebar from '@/features/ide-react/components/editor-sidebar'
  4. import History from '@/features/ide-react/components/history'
  5. import { HistoryProvider } from '@/features/history/context/history-context'
  6. import { useProjectContext } from '@/shared/context/project-context'
  7. import { useLayoutContext } from '@/shared/context/layout-context'
  8. import {
  9. FileTreeDeleteHandler,
  10. FileTreeDocumentFindResult,
  11. FileTreeFileRefFindResult,
  12. FileTreeFindResult,
  13. } from '@/features/ide-react/types/file-tree'
  14. import FileView from '@/features/file-view/components/file-view'
  15. import { FileRef } from '../../../../../types/file-ref'
  16. import { EditorPane } from '@/features/ide-react/components/editor/editor-pane'
  17. import EditorAndPdf from '@/features/ide-react/components/editor-and-pdf'
  18. import MultipleSelectionPane from '@/features/ide-react/components/editor/multiple-selection-pane'
  19. import NoSelectionPane from '@/features/ide-react/components/editor/no-selection-pane'
  20. import { useIdeReactContext } from '@/features/ide-react/context/ide-react-context'
  21. import { useEditorManagerContext } from '@/features/ide-react/context/editor-manager-context'
  22. import NoOpenDocPane from '@/features/ide-react/components/editor/no-open-doc-pane'
  23. import { debugConsole } from '@/utils/debugging'
  24. import { HistorySidebar } from '@/features/ide-react/components/history-sidebar'
  25. import { BinaryFile } from '@/features/file-view/types/binary-file'
  26. import useScopeValue from '@/shared/hooks/use-scope-value'
  27. import { useSelectFileTreeEntity } from '@/features/ide-react/hooks/use-select-file-tree-entity'
  28. type EditorAndSidebarProps = {
  29. shouldPersistLayout: boolean
  30. leftColumnDefaultSize: number
  31. setLeftColumnDefaultSize: React.Dispatch<React.SetStateAction<number>>
  32. }
  33. function convertFileRefToBinaryFile(fileRef: FileRef): BinaryFile {
  34. return {
  35. _id: fileRef._id,
  36. name: fileRef.name,
  37. id: fileRef._id,
  38. type: 'file',
  39. selected: true,
  40. linkedFileData: fileRef.linkedFileData,
  41. created: fileRef.created ? new Date(fileRef.created) : new Date(),
  42. }
  43. }
  44. // `FileViewHeader`, which is TypeScript, expects a BinaryFile, which has a
  45. // `created` property of type `Date`, while `TPRFileViewInfo`, written in JS,
  46. // into which `FileViewHeader` passes its BinaryFile, expects a file object with
  47. // `created` property of type `string`, which is a mismatch. `TPRFileViewInfo`
  48. // is the only one making runtime complaints and it seems that other uses of
  49. // `FileViewHeader` pass in a string for `created`, so that's what this function
  50. // does too.
  51. function fileViewFile(fileRef: FileRef) {
  52. return {
  53. ...convertFileRefToBinaryFile(fileRef),
  54. created: fileRef.created,
  55. }
  56. }
  57. export function EditorAndSidebar({
  58. shouldPersistLayout,
  59. leftColumnDefaultSize,
  60. setLeftColumnDefaultSize,
  61. }: EditorAndSidebarProps) {
  62. const [leftColumnIsOpen, setLeftColumnIsOpen] = useState(true)
  63. const { rootDocId } = useProjectContext()
  64. const { eventEmitter } = useIdeReactContext()
  65. const {
  66. openDocId: openDocWithId,
  67. currentDocumentId: openDocId,
  68. openInitialDoc,
  69. } = useEditorManagerContext()
  70. const { view } = useLayoutContext()
  71. const { projectJoined } = useIdeReactContext()
  72. const { selectEntity } = useSelectFileTreeEntity()
  73. const [, setOpenFile] = useScopeValue<BinaryFile | null>('openFile')
  74. const historyIsOpen = view === 'history'
  75. const [openEntity, setOpenEntity] = useState<
  76. FileTreeDocumentFindResult | FileTreeFileRefFindResult | null
  77. >(null)
  78. const [selectedEntityCount, setSelectedEntityCount] = useState(0)
  79. const [fileTreeReady, setFileTreeReady] = useState(false)
  80. const handleFileTreeInit = useCallback(() => {
  81. setFileTreeReady(true)
  82. }, [])
  83. // Open a document in the editor when one is selected in the file tree
  84. const handleFileTreeSelect = useCallback(
  85. (selectedEntities: FileTreeFindResult[]) => {
  86. debugConsole.log('File tree selection changed', selectedEntities)
  87. setSelectedEntityCount(selectedEntities.length)
  88. if (selectedEntities.length !== 1) {
  89. setOpenEntity(null)
  90. return
  91. }
  92. const [selected] = selectedEntities
  93. if (selected.type === 'folder') {
  94. return
  95. }
  96. setOpenEntity(selected)
  97. if (selected.type === 'doc' && fileTreeReady) {
  98. openDocWithId(selected.entity._id)
  99. }
  100. // Keep openFile scope value in sync with the file tree
  101. const openFile =
  102. selected.type === 'fileRef'
  103. ? convertFileRefToBinaryFile(selected.entity)
  104. : null
  105. setOpenFile(openFile)
  106. if (openFile) {
  107. window.dispatchEvent(new CustomEvent('file-view:file-opened'))
  108. }
  109. },
  110. [fileTreeReady, setOpenFile, openDocWithId]
  111. )
  112. const handleFileTreeDelete: FileTreeDeleteHandler = useCallback(
  113. entity => {
  114. eventEmitter.emit('entity:deleted', entity)
  115. // Select the root document if the current document was deleted
  116. if (entity.entity._id === openDocId) {
  117. openDocWithId(rootDocId)
  118. }
  119. },
  120. [eventEmitter, openDocId, openDocWithId, rootDocId]
  121. )
  122. const openDocIdRef = useRef<typeof openDocId | null>(null)
  123. // Synchronize the file tree when openDoc or openDocId is called on the editor
  124. // manager context from elsewhere. If the file tree does change, it will
  125. // trigger the onSelect handler in this component, which will update the local
  126. // state.
  127. useEffect(() => {
  128. if (openDocId !== openDocIdRef.current) {
  129. debugConsole.log(`openDocId changed to ${openDocId}`)
  130. openDocIdRef.current = openDocId
  131. if (openDocId !== null) {
  132. selectEntity(openDocId)
  133. }
  134. }
  135. }, [openDocId, selectEntity])
  136. // Open a document once the file tree and project are ready
  137. const initialOpenDoneRef = useRef(false)
  138. useEffect(() => {
  139. if (fileTreeReady && projectJoined && !initialOpenDoneRef.current) {
  140. initialOpenDoneRef.current = true
  141. openInitialDoc(rootDocId)
  142. }
  143. }, [fileTreeReady, openInitialDoc, projectJoined, rootDocId])
  144. // Keep the editor file tree around so that it is available and up to date
  145. // when restoring a file
  146. const leftColumnContent = (
  147. <>
  148. <EditorSidebar
  149. shouldShow={!historyIsOpen}
  150. shouldPersistLayout={shouldPersistLayout}
  151. onFileTreeInit={handleFileTreeInit}
  152. onFileTreeSelect={handleFileTreeSelect}
  153. onFileTreeDelete={handleFileTreeDelete}
  154. />
  155. {historyIsOpen ? <HistorySidebar /> : null}
  156. </>
  157. )
  158. let rightColumnContent
  159. if (historyIsOpen) {
  160. rightColumnContent = (
  161. <HistoryProvider>
  162. <History />
  163. </HistoryProvider>
  164. )
  165. } else {
  166. let editorContent = null
  167. // Always have the editor mounted when not in history view, and hide and
  168. // show it as necessary
  169. const editorPane = (
  170. <EditorPane
  171. shouldPersistLayout={shouldPersistLayout}
  172. show={openEntity?.type === 'doc' && selectedEntityCount === 1}
  173. />
  174. )
  175. if (openDocId === undefined) {
  176. rightColumnContent = <NoOpenDocPane />
  177. } else if (selectedEntityCount === 0) {
  178. rightColumnContent = (
  179. <>
  180. {editorPane}
  181. <NoSelectionPane />
  182. </>
  183. )
  184. } else if (selectedEntityCount > 1) {
  185. editorContent = (
  186. <>
  187. {editorPane}
  188. <MultipleSelectionPane selectedEntityCount={selectedEntityCount} />
  189. </>
  190. )
  191. } else if (openEntity) {
  192. editorContent =
  193. openEntity.type === 'doc' ? (
  194. editorPane
  195. ) : (
  196. <>
  197. {editorPane}
  198. <FileView file={fileViewFile(openEntity.entity)} />
  199. </>
  200. )
  201. }
  202. if (editorContent) {
  203. rightColumnContent = (
  204. <EditorAndPdf
  205. editorContent={editorContent}
  206. shouldPersistLayout={shouldPersistLayout}
  207. />
  208. )
  209. }
  210. }
  211. return (
  212. <TwoColumnMainContent
  213. leftColumnId="editor-left-column"
  214. leftColumnContent={leftColumnContent}
  215. leftColumnDefaultSize={leftColumnDefaultSize}
  216. setLeftColumnDefaultSize={setLeftColumnDefaultSize}
  217. rightColumnContent={rightColumnContent}
  218. leftColumnIsOpen={leftColumnIsOpen}
  219. setLeftColumnIsOpen={setLeftColumnIsOpen}
  220. shouldPersistLayout={shouldPersistLayout}
  221. />
  222. )
  223. }