mirror of
https://github.com/langgenius/dify.git
synced 2026-03-03 06:46:19 +08:00
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.
38 lines
1.1 KiB
TypeScript
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)
|
|
},
|
|
})
|
|
}
|