file-tree-open-context.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. }
  33. | undefined
  34. >(undefined)
  35. export const FileTreeOpenProvider: FC = ({ children }) => {
  36. const { rootDocId, owner } = useProjectContext()
  37. const { eventEmitter, projectJoined } = useIdeReactContext()
  38. const { openDocWithId, currentDocumentId, openInitialDoc } =
  39. useEditorManagerContext()
  40. const [, setOpenFile] = useScopeValueSetterOnly<BinaryFile | null>('openFile')
  41. const [openEntity, setOpenEntity] = useState<
  42. FileTreeDocumentFindResult | FileTreeFileRefFindResult | null
  43. >(null)
  44. const [selectedEntityCount, setSelectedEntityCount] = useState(0)
  45. const [fileTreeReady, setFileTreeReady] = useState(false)
  46. const handleFileTreeInit = useCallback(() => {
  47. setFileTreeReady(true)
  48. }, [])
  49. // Open a document in the editor when one is selected in the file tree
  50. const handleFileTreeSelect = useCallback(
  51. (selectedEntities: FileTreeFindResult[]) => {
  52. debugConsole.log('File tree selection changed', selectedEntities)
  53. setSelectedEntityCount(selectedEntities.length)
  54. if (selectedEntities.length !== 1) {
  55. setOpenEntity(null)
  56. return
  57. }
  58. const [selected] = selectedEntities
  59. if (selected.type === 'folder') {
  60. return
  61. }
  62. setOpenEntity(selected)
  63. if (selected.type === 'doc' && fileTreeReady) {
  64. openDocWithId(selected.entity._id, { keepCurrentView: true })
  65. if (selected.entity.name.endsWith('.bib')) {
  66. sendMB('open-bib-file', {
  67. projectOwner: owner._id,
  68. isSampleFile: selected.entity.name === 'sample.bib',
  69. linkedFileProvider: null,
  70. })
  71. }
  72. }
  73. // Keep openFile scope value in sync with the file tree
  74. const openFile =
  75. selected.type === 'fileRef'
  76. ? convertFileRefToBinaryFile(selected.entity)
  77. : null
  78. setOpenFile(openFile)
  79. if (openFile) {
  80. if (selected?.entity?.name?.endsWith('.bib')) {
  81. sendMB('open-bib-file', {
  82. projectOwner: owner._id,
  83. isSampleFile: false,
  84. linkedFileProvider: (selected.entity as FileRef).linkedFileData
  85. ?.provider,
  86. })
  87. }
  88. window.dispatchEvent(new CustomEvent('file-view:file-opened'))
  89. }
  90. },
  91. [fileTreeReady, setOpenFile, openDocWithId, owner]
  92. )
  93. const handleFileTreeDelete = useCallback(
  94. (entity: FileTreeFindResult) => {
  95. eventEmitter.emit('entity:deleted', entity)
  96. // Select the root document if the current document was deleted
  97. if (entity.entity._id === currentDocumentId) {
  98. openDocWithId(rootDocId!)
  99. }
  100. },
  101. [eventEmitter, currentDocumentId, openDocWithId, rootDocId]
  102. )
  103. // Open a document once the file tree and project are ready
  104. const initialOpenDoneRef = useRef(false)
  105. useEffect(() => {
  106. if (
  107. rootDocId &&
  108. fileTreeReady &&
  109. projectJoined &&
  110. !initialOpenDoneRef.current
  111. ) {
  112. initialOpenDoneRef.current = true
  113. openInitialDoc(rootDocId)
  114. }
  115. }, [fileTreeReady, openInitialDoc, projectJoined, rootDocId])
  116. const value = useMemo(() => {
  117. return {
  118. selectedEntityCount,
  119. openEntity,
  120. handleFileTreeInit,
  121. handleFileTreeSelect,
  122. handleFileTreeDelete,
  123. }
  124. }, [
  125. handleFileTreeDelete,
  126. handleFileTreeInit,
  127. handleFileTreeSelect,
  128. openEntity,
  129. selectedEntityCount,
  130. ])
  131. return (
  132. <FileTreeOpenContext.Provider value={value}>
  133. {children}
  134. </FileTreeOpenContext.Provider>
  135. )
  136. }
  137. export const useFileTreeOpenContext = () => {
  138. const context = useContext(FileTreeOpenContext)
  139. if (!context) {
  140. throw new Error(
  141. 'useFileTreeOpenContext is only available inside FileTreeOpenProvider'
  142. )
  143. }
  144. return context
  145. }