2023-11-15 06:35:18 -05:00
|
|
|
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() {
|
2023-11-17 07:30:11 -05:00
|
|
|
const { fileTreeData, selectedEntities } = useFileTreeData()
|
2023-11-15 06:35:18 -05:00
|
|
|
|
|
|
|
const selectEntity = useCallback(
|
|
|
|
id => {
|
|
|
|
if (
|
|
|
|
selectedEntities.length === 1 &&
|
|
|
|
selectedEntities[0].entity._id === id
|
|
|
|
) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const entityToSelect = findInTree(fileTreeData, id)
|
|
|
|
if (entityToSelect) {
|
2023-11-17 07:30:11 -05:00
|
|
|
window.dispatchEvent(new CustomEvent('editor.openDoc', { detail: id }))
|
2023-11-15 06:35:18 -05:00
|
|
|
}
|
|
|
|
},
|
2023-11-17 07:30:11 -05:00
|
|
|
[fileTreeData, selectedEntities]
|
2023-11-15 06:35:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
return { selectEntity }
|
|
|
|
}
|