file-tree-open-context.tsx 5.3 KB

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