2023-11-03 08:15:36 -04:00
|
|
|
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()
|
2023-11-15 06:35:18 -05:00
|
|
|
|
2023-11-17 07:30:11 -05:00
|
|
|
const { fileTreeData } = useFileTreeData()
|
2023-11-03 08:15:36 -04:00
|
|
|
|
|
|
|
const openFileWithId = useCallback(
|
|
|
|
(id: string) => {
|
|
|
|
const result = findInTree(fileTreeData, id)
|
|
|
|
if (result?.type === 'fileRef') {
|
2023-11-17 07:30:11 -05:00
|
|
|
window.dispatchEvent(new CustomEvent('editor.openDoc', { detail: id }))
|
2023-11-03 08:15:36 -04:00
|
|
|
}
|
|
|
|
},
|
2023-11-17 07:30:11 -05:00
|
|
|
[fileTreeData]
|
2023-11-03 08:15:36 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|