Files
dify/web/app/components/workflow/skill/hooks/use-skill-asset-tree.ts
yyh 3b6946d3da refactor(skill): centralize asset tree data fetching with custom hooks
Extract repeated appId retrieval and tree data fetching patterns into
dedicated hooks (useSkillAssetTreeData, useSkillAssetNodeMap) to reduce
code duplication across 6 components and leverage TanStack Query's
select option for efficient nodeMap computation.
2026-01-15 19:45:33 +08:00

38 lines
1.1 KiB
TypeScript

import type { AppAssetTreeResponse, AppAssetTreeView } from '@/types/app-asset'
import { useStore as useAppStore } from '@/app/components/app/store'
import { useGetAppAssetTree } from '@/service/use-app-asset'
import { buildNodeMap } from '../utils/tree-utils'
/**
* Get the current app ID from the app store.
* Used internally by skill asset tree hooks.
*/
function useSkillAppId(): string {
const appDetail = useAppStore(s => s.appDetail)
return appDetail?.id || ''
}
/**
* Hook to get the asset tree data for the current skill app.
* Returns the raw tree data along with loading and error states.
*/
export function useSkillAssetTreeData() {
const appId = useSkillAppId()
return useGetAppAssetTree(appId)
}
/**
* Hook to get the node map (id -> node) for the current skill app.
* Uses TanStack Query's select option to compute and cache the map.
*/
export function useSkillAssetNodeMap() {
const appId = useSkillAppId()
return useGetAppAssetTree(appId, {
select: (data: AppAssetTreeResponse): Map<string, AppAssetTreeView> => {
if (!data?.children)
return new Map()
return buildNodeMap(data.children)
},
})
}