Files
GovAI/apps/web/src/hooks/use-file-export.ts
T
2026-06-15 23:48:37 +08:00

32 lines
839 B
TypeScript

"use client";
import { useCallback } from "react";
import { toast } from "sonner";
/**
* 触发浏览器文件下载。
* chatbot-ui / agent-ui / doc-writer-ui / analysis-ui 的导出功能共用。
*/
export function useFileExport() {
const download = useCallback(
(content: string, filename: string, successMsg = "已导出") => {
const blob = new Blob([content], { type: "application/octet-stream" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
a.style.display = "none";
document.body.appendChild(a);
a.click();
setTimeout(() => {
document.body.removeChild(a);
URL.revokeObjectURL(url);
}, 30000);
toast.success(successMsg);
},
[],
);
return { download };
}