mirror of
https://github.com/langgenius/dify.git
synced 2026-02-25 04:06:30 +08:00
55 lines
1.3 KiB
TypeScript
55 lines
1.3 KiB
TypeScript
'use client'
|
|
|
|
import type { TreeApi } from 'react-arborist'
|
|
import type { TreeNodeData } from '../type'
|
|
import { useEffect } from 'react'
|
|
import { useWorkflowStore } from '@/app/components/workflow/store'
|
|
|
|
type UseSyncTreeWithActiveTabOptions = {
|
|
treeRef: React.RefObject<TreeApi<TreeNodeData> | null>
|
|
activeTabId: string | null
|
|
}
|
|
|
|
/**
|
|
* Hook that synchronizes the file tree with the active tab.
|
|
* Expands ancestor folders and scrolls to the active node.
|
|
*
|
|
* Uses node.parent chain for efficient ancestor traversal instead of
|
|
* re-traversing the tree data structure.
|
|
*/
|
|
export function useSyncTreeWithActiveTab({
|
|
treeRef,
|
|
activeTabId,
|
|
}: UseSyncTreeWithActiveTabOptions): void {
|
|
const storeApi = useWorkflowStore()
|
|
|
|
useEffect(() => {
|
|
if (!activeTabId)
|
|
return
|
|
|
|
const tree = treeRef.current
|
|
if (!tree)
|
|
return
|
|
|
|
requestAnimationFrame(() => {
|
|
const node = tree.get(activeTabId)
|
|
if (!node)
|
|
return
|
|
|
|
const ancestors: string[] = []
|
|
let current = node.parent
|
|
while (current && !current.isRoot) {
|
|
ancestors.push(current.id)
|
|
current = current.parent
|
|
}
|
|
|
|
if (ancestors.length > 0)
|
|
storeApi.getState().revealFile(ancestors)
|
|
|
|
tree.openParents(node)
|
|
tree.select(activeTabId)
|
|
tree.scrollTo(activeTabId)
|
|
})
|
|
}, [activeTabId, treeRef, storeApi])
|
|
}
|