Files
dify/web/app/components/workflow/skill/hooks/use-download-operation.ts
yyh efb3657cfe fix(skill): use downloadUrl utility instead of window.open for file downloads
Replaces window.open with the downloadUrl helper from utils/download.ts
to trigger proper browser download behavior via <a download> instead of
opening a new tab that may display garbled content.
2026-01-29 12:49:15 +08:00

55 lines
1.2 KiB
TypeScript

'use client'
import { useCallback, useState } from 'react'
import { useTranslation } from 'react-i18next'
import Toast from '@/app/components/base/toast'
import { consoleClient } from '@/service/client'
import { downloadUrl } from '@/utils/download'
type UseDownloadOperationOptions = {
appId: string
nodeId: string
fileName?: string
onClose: () => void
}
export function useDownloadOperation({
appId,
nodeId,
fileName,
onClose,
}: UseDownloadOperationOptions) {
const { t } = useTranslation('workflow')
const [isDownloading, setIsDownloading] = useState(false)
const handleDownload = useCallback(async () => {
if (!nodeId || !appId)
return
onClose()
setIsDownloading(true)
try {
const { download_url } = await consoleClient.appAsset.getFileDownloadUrl({
params: { appId, nodeId },
})
downloadUrl({ url: download_url, fileName })
}
catch {
Toast.notify({
type: 'error',
message: t('skillSidebar.menu.downloadError'),
})
}
finally {
setIsDownloading(false)
}
}, [appId, nodeId, fileName, onClose, t])
return {
handleDownload,
isDownloading,
}
}