dify/web/app/components/workflow/skill/hooks/use-reveal-active-tab.ts
yyh 76da178cc1
refactor(skill): extract tree node handlers into reusable hooks
Extract complex event handling and side effects from file tree components
into dedicated hooks for better separation of concerns and reusability.
2026-01-16 14:15:21 +08:00

48 lines
1.3 KiB
TypeScript

'use client'
import type { TreeApi } from 'react-arborist'
import type { TreeNodeData } from '../type'
import type { AppAssetTreeView } from '@/types/app-asset'
import { useEffect } from 'react'
import { useWorkflowStore } from '@/app/components/workflow/store'
import { getAncestorIds } from '../utils/tree-utils'
type UseRevealActiveTabOptions = {
treeRef: React.RefObject<TreeApi<TreeNodeData> | null>
activeTabId: string | null
treeChildren: AppAssetTreeView[] | undefined
}
/**
* Hook that handles revealing the active tab in the file tree.
* Expands ancestor folders and scrolls to the active node.
*/
export function useRevealActiveTab({
treeRef,
activeTabId,
treeChildren,
}: UseRevealActiveTabOptions): void {
const storeApi = useWorkflowStore()
useEffect(() => {
if (!activeTabId || !treeChildren)
return
const tree = treeRef.current
if (!tree)
return
const ancestors = getAncestorIds(activeTabId, treeChildren)
if (ancestors.length > 0)
storeApi.getState().revealFile(ancestors)
requestAnimationFrame(() => {
const node = tree.get(activeTabId)
if (node) {
tree.openParents(node)
tree.scrollTo(activeTabId)
}
})
}, [activeTabId, treeChildren, storeApi, treeRef])
}