file-tree-open-context.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import {
  2. createContext,
  3. FC,
  4. useCallback,
  5. useContext,
  6. useEffect,
  7. useMemo,
  8. useRef,
  9. useState,
  10. } from 'react'
  11. import { useProjectContext } from '@/shared/context/project-context'
  12. import { useIdeReactContext } from '@/features/ide-react/context/ide-react-context'
  13. import { useEditorManagerContext } from '@/features/ide-react/context/editor-manager-context'
  14. import { useSelectFileTreeEntity } from '@/features/ide-react/hooks/use-select-file-tree-entity'
  15. import useScopeValue from '@/shared/hooks/use-scope-value'
  16. import { BinaryFile } from '@/features/file-view/types/binary-file'
  17. import {
  18. FileTreeDocumentFindResult,
  19. FileTreeFileRefFindResult,
  20. FileTreeFindResult,
  21. } from '@/features/ide-react/types/file-tree'
  22. import { debugConsole } from '@/utils/debugging'
  23. import { convertFileRefToBinaryFile } from '@/features/ide-react/util/file-view'
  24. import { sendMB } from '@/infrastructure/event-tracking'
  25. import { FileRef } from '../../../../../types/file-ref'
  26. import useEventListener from '@/shared/hooks/use-event-listener'
  27. const FileTreeOpenContext = createContext<
  28. | {
  29. selectedEntityCount: number
  30. openEntity: FileTreeDocumentFindResult | FileTreeFileRefFindResult | null
  31. handleFileTreeInit: () => void
  32. handleFileTreeSelect: (selectedEntities: FileTreeFindResult[]) => void
  33. handleFileTreeDelete: (entity: FileTreeFindResult) => void
  34. }
  35. | undefined
  36. >(undefined)
  37. export const FileTreeOpenProvider: FC = ({ children }) => {
  38. const { rootDocId, owner } = useProjectContext()
  39. const { eventEmitter, projectJoined } = useIdeReactContext()
  40. const {
  41. openDocId: openDocWithId,
  42. currentDocumentId: openDocId,
  43. openInitialDoc,
  44. } = useEditorManagerContext()
  45. const { selectEntity } = useSelectFileTreeEntity()
  46. const [, setOpenFile] = useScopeValue<BinaryFile | null>('openFile')
  47. const [openEntity, setOpenEntity] = useState<
  48. FileTreeDocumentFindResult | FileTreeFileRefFindResult | null
  49. >(null)
  50. const [selectedEntityCount, setSelectedEntityCount] = useState(0)
  51. const [fileTreeReady, setFileTreeReady] = useState(false)
  52. const handleFileTreeInit = useCallback(() => {
  53. setFileTreeReady(true)
  54. }, [])
  55. // Open a document in the editor when one is selected in the file tree
  56. const handleFileTreeSelect = useCallback(
  57. (selectedEntities: FileTreeFindResult[]) => {
  58. debugConsole.log('File tree selection changed', selectedEntities)
  59. setSelectedEntityCount(selectedEntities.length)
  60. if (selectedEntities.length !== 1) {
  61. setOpenEntity(null)
  62. return
  63. }
  64. const [selected] = selectedEntities
  65. if (selected.type === 'folder') {
  66. return
  67. }
  68. setOpenEntity(selected)
  69. if (selected.type === 'doc' && fileTreeReady) {
  70. openDocWithId(selected.entity._id, { keepCurrentView: true })
  71. if (selected.entity.name.endsWith('.bib')) {
  72. sendMB('open-bib-file', {
  73. projectOwner: owner._id,
  74. isSampleFile: selected.entity.name === 'sample.bib',
  75. linkedFileProvider: null,
  76. })
  77. }
  78. }
  79. // Keep openFile scope value in sync with the file tree
  80. const openFile =
  81. selected.type === 'fileRef'
  82. ? convertFileRefToBinaryFile(selected.entity)
  83. : null
  84. setOpenFile(openFile)
  85. if (openFile) {
  86. if (selected?.entity?.name?.endsWith('.bib')) {
  87. sendMB('open-bib-file', {
  88. projectOwner: owner._id,
  89. isSampleFile: false,
  90. linkedFileProvider: (selected.entity as FileRef).linkedFileData
  91. ?.provider,
  92. })
  93. }
  94. window.dispatchEvent(new CustomEvent('file-view:file-opened'))
  95. }
  96. },
  97. [fileTreeReady, setOpenFile, openDocWithId, owner]
  98. )
  99. const handleFileTreeDelete = useCallback(
  100. (entity: FileTreeFindResult) => {
  101. eventEmitter.emit('entity:deleted', entity)
  102. // Select the root document if the current document was deleted
  103. if (entity.entity._id === openDocId) {
  104. openDocWithId(rootDocId!)
  105. }
  106. },
  107. [eventEmitter, openDocId, openDocWithId, rootDocId]
  108. )
  109. // Synchronize the file tree when openDoc or openDocId is called on the editor
  110. // manager context from elsewhere. If the file tree does change, it will
  111. // trigger the onSelect handler in this component, which will update the local
  112. // state.
  113. useEventListener(
  114. 'doc:after-opened',
  115. useCallback(
  116. (event: CustomEvent<{ docId: string }>) => {
  117. selectEntity(event.detail.docId)
  118. },
  119. [selectEntity]
  120. )
  121. )
  122. // Open a document once the file tree and project are ready
  123. const initialOpenDoneRef = useRef(false)
  124. useEffect(() => {
  125. if (
  126. rootDocId &&
  127. fileTreeReady &&
  128. projectJoined &&
  129. !initialOpenDoneRef.current
  130. ) {
  131. initialOpenDoneRef.current = true
  132. openInitialDoc(rootDocId)
  133. }
  134. }, [fileTreeReady, openInitialDoc, projectJoined, rootDocId])
  135. const value = useMemo(() => {
  136. return {
  137. selectedEntityCount,
  138. openEntity,
  139. handleFileTreeInit,
  140. handleFileTreeSelect,
  141. handleFileTreeDelete,
  142. }
  143. }, [
  144. handleFileTreeDelete,
  145. handleFileTreeInit,
  146. handleFileTreeSelect,
  147. openEntity,
  148. selectedEntityCount,
  149. ])
  150. return (
  151. <FileTreeOpenContext.Provider value={value}>
  152. {children}
  153. </FileTreeOpenContext.Provider>
  154. )
  155. }
  156. export const useFileTreeOpenContext = () => {
  157. const context = useContext(FileTreeOpenContext)
  158. if (!context) {
  159. throw new Error(
  160. 'useFileTreeOpenContext is only available inside FileTreeOpenProvider'
  161. )
  162. }
  163. return context
  164. }