overleaf/services/web/frontend/js/features/ide-react/hooks/use-select-file-tree-entity.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

25 lines
717 B
TypeScript

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 useSelectFileTreeEntity() {
const { fileTreeData, selectedEntities } = useFileTreeData()
const selectEntity = useCallback(
id => {
if (
selectedEntities.length === 1 &&
selectedEntities[0].entity._id === id
) {
return
}
const entityToSelect = findInTree(fileTreeData, id)
if (entityToSelect) {
window.dispatchEvent(new CustomEvent('editor.openDoc', { detail: id }))
}
},
[fileTreeData, selectedEntities]
)
return { selectEntity }
}