overleaf/services/web/frontend/js/features/ide-react/hooks/use-open-file.ts
Tim Down 9e72d55ea6 Merge pull request #15792 from overleaf/td-ide-page-select-in-file-tree
Make selecting a file update the file tree reliably in React IDE page

GitOrigin-RevId: a59ccc53371ee22d577b0de70abad4ffbe7f5398
2023-11-20 09:03:43 +00:00

26 lines
847 B
TypeScript

import { useIdeContext } from '@/shared/context/ide-context'
import { useFileTreeData } from '@/shared/context/file-tree-data-context'
import { useCallback } from 'react'
import { findInTree } from '@/features/file-tree/util/find-in-tree'
export function useOpenFile() {
const ide = useIdeContext()
const { fileTreeData } = useFileTreeData()
const openFileWithId = useCallback(
(id: string) => {
const result = findInTree(fileTreeData, id)
if (result?.type === 'fileRef') {
window.dispatchEvent(new CustomEvent('editor.openDoc', { detail: id }))
}
},
[fileTreeData]
)
// Expose BinaryFilesManager via ide object solely for the benefit of the file
// restore feature in history. This can be removed once Angular is gone.
ide.binaryFilesManager = { openFileWithId }
return openFileWithId
}