file-tree-open-context.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 { openDocWithId, currentDocumentId, openInitialDoc } =
  41. useEditorManagerContext()
  42. const { selectEntity } = useSelectFileTreeEntity()
  43. const [, setOpenFile] = useScopeValue<BinaryFile | null>('openFile')
  44. const [openEntity, setOpenEntity] = useState<
  45. FileTreeDocumentFindResult | FileTreeFileRefFindResult | null
  46. >(null)
  47. const [selectedEntityCount, setSelectedEntityCount] = useState(0)
  48. const [fileTreeReady, setFileTreeReady] = useState(false)
  49. const handleFileTreeInit = useCallback(() => {
  50. setFileTreeReady(true)
  51. }, [])
  52. // Open a document in the editor when one is selected in the file tree
  53. const handleFileTreeSelect = useCallback(
  54. (selectedEntities: FileTreeFindResult[]) => {
  55. debugConsole.log('File tree selection changed', selectedEntities)
  56. setSelectedEntityCount(selectedEntities.length)
  57. if (selectedEntities.length !== 1) {
  58. setOpenEntity(null)
  59. return
  60. }
  61. const [selected] = selectedEntities
  62. if (selected.type === 'folder') {
  63. return
  64. }
  65. setOpenEntity(selected)
  66. if (selected.type === 'doc' && fileTreeReady) {
  67. openDocWithId(selected.entity._id, { keepCurrentView: true })
  68. if (selected.entity.name.endsWith('.bib')) {
  69. sendMB('open-bib-file', {
  70. projectOwner: owner._id,
  71. isSampleFile: selected.entity.name === 'sample.bib',
  72. linkedFileProvider: null,
  73. })
  74. }
  75. }
  76. // Keep openFile scope value in sync with the file tree
  77. const openFile =
  78. selected.type === 'fileRef'
  79. ? convertFileRefToBinaryFile(selected.entity)
  80. : null
  81. setOpenFile(openFile)
  82. if (openFile) {
  83. if (selected?.entity?.name?.endsWith('.bib')) {
  84. sendMB('open-bib-file', {
  85. projectOwner: owner._id,
  86. isSampleFile: false,
  87. linkedFileProvider: (selected.entity as FileRef).linkedFileData
  88. ?.provider,
  89. })
  90. }
  91. window.dispatchEvent(new CustomEvent('file-view:file-opened'))
  92. }
  93. },
  94. [fileTreeReady, setOpenFile, openDocWithId, owner]
  95. )
  96. const handleFileTreeDelete = useCallback(
  97. (entity: FileTreeFindResult) => {
  98. eventEmitter.emit('entity:deleted', entity)
  99. // Select the root document if the current document was deleted
  100. if (entity.entity._id === currentDocumentId) {
  101. openDocWithId(rootDocId!)
  102. }
  103. },
  104. [eventEmitter, currentDocumentId, openDocWithId, rootDocId]
  105. )
  106. // Synchronize the file tree when openDoc or openDocWithId is called on the editor
  107. // manager context from elsewhere. If the file tree does change, it will
  108. // trigger the onSelect handler in this component, which will update the local
  109. // state.
  110. useEventListener(
  111. 'doc:after-opened',
  112. useCallback(
  113. (event: CustomEvent<{ docId: string }>) => {
  114. selectEntity(event.detail.docId)
  115. },
  116. [selectEntity]
  117. )
  118. )
  119. // Open a document once the file tree and project are ready
  120. const initialOpenDoneRef = useRef(false)
  121. useEffect(() => {
  122. if (
  123. rootDocId &&
  124. fileTreeReady &&
  125. projectJoined &&
  126. !initialOpenDoneRef.current
  127. ) {
  128. initialOpenDoneRef.current = true
  129. openInitialDoc(rootDocId)
  130. }
  131. }, [fileTreeReady, openInitialDoc, projectJoined, rootDocId])
  132. const value = useMemo(() => {
  133. return {
  134. selectedEntityCount,
  135. openEntity,
  136. handleFileTreeInit,
  137. handleFileTreeSelect,
  138. handleFileTreeDelete,
  139. }
  140. }, [
  141. handleFileTreeDelete,
  142. handleFileTreeInit,
  143. handleFileTreeSelect,
  144. openEntity,
  145. selectedEntityCount,
  146. ])
  147. return (
  148. <FileTreeOpenContext.Provider value={value}>
  149. {children}
  150. </FileTreeOpenContext.Provider>
  151. )
  152. }
  153. export const useFileTreeOpenContext = () => {
  154. const context = useContext(FileTreeOpenContext)
  155. if (!context) {
  156. throw new Error(
  157. 'useFileTreeOpenContext is only available inside FileTreeOpenProvider'
  158. )
  159. }
  160. return context
  161. }