Files
dify/web/app/components/workflow/skill/hooks/use-skill-file-data.ts
yyh dc213ca76c refactor(skill)!: add file node view-state flow and mode-based file data hook
- introduce resolving/ready/missing node view-state to avoid unsupported flicker

- switch useSkillFileData to explicit mode: none/content/download

- add hook tests for view-state transitions and mode query gating

BREAKING CHANGE: useSkillFileData signature changed from (appId, nodeId, isEditable) to (appId, nodeId, mode).
2026-02-06 15:39:00 +08:00

58 lines
1.5 KiB
TypeScript

import { useGetAppAssetFileContent, useGetAppAssetFileDownloadUrl } from '@/service/use-app-asset'
export type SkillFileDataMode = 'none' | 'content' | 'download'
export type SkillFileDataResult = {
fileContent: ReturnType<typeof useGetAppAssetFileContent>['data']
downloadUrlData: ReturnType<typeof useGetAppAssetFileDownloadUrl>['data']
isLoading: boolean
error: Error | null
}
/**
* Hook to fetch file data for skill documents.
* Uses explicit mode to control data fetching:
* - 'content': fetch editable file content
* - 'download': fetch non-editable file download URL
* - 'none': skip file-related requests while node metadata is unresolved
*/
export function useSkillFileData(
appId: string,
nodeId: string | null | undefined,
mode: SkillFileDataMode,
): SkillFileDataResult {
const {
data: fileContent,
isLoading: isContentLoading,
error: contentError,
} = useGetAppAssetFileContent(appId, nodeId || '', {
enabled: mode === 'content',
})
const {
data: downloadUrlData,
isLoading: isDownloadUrlLoading,
error: downloadUrlError,
} = useGetAppAssetFileDownloadUrl(appId, nodeId || '', {
enabled: mode === 'download' && !!nodeId,
})
const isLoading = mode === 'content'
? isContentLoading
: mode === 'download'
? isDownloadUrlLoading
: false
const error = mode === 'content'
? contentError
: mode === 'download'
? downloadUrlError
: null
return {
fileContent,
downloadUrlData,
isLoading,
error,
}
}