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