Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@radix-ui/react-tabs": "^1.1.4",
"@radix-ui/react-tooltip": "^1.2.0",
"@rc-component/mentions": "^1.2.0",
"@react-pdf/renderer": "^4.3.0",
"@t3-oss/env-nextjs": "^0.11.0",
"@tailwindcss/typography": "^0.5.16",
"@tiptap/extension-document": "^2.12.0",
Expand All @@ -51,10 +52,14 @@
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"cmdk": "^1.1.1",
"docx": "^9.5.0",
"file-saver": "^2.0.5",
"framer-motion": "^12.6.5",
"hast": "^1.0.0",
"highlight.js": "^11.11.1",
"html2canvas": "^1.4.1",
"immer": "^10.1.1",
"jspdf": "^3.0.1",
"katex": "^0.16.21",
"lowlight": "^3.3.0",
"lru-cache": "^11.1.0",
Expand Down Expand Up @@ -86,6 +91,7 @@
"devDependencies": {
"@eslint/eslintrc": "^3.3.1",
"@tailwindcss/postcss": "^4.0.15",
"@types/file-saver": "^2.0.7",
"@types/hast": "^3.0.4",
"@types/node": "^20.14.10",
"@types/react": "^19.0.0",
Expand Down
48 changes: 12 additions & 36 deletions web/src/app/chat/components/research-block.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
// SPDX-License-Identifier: MIT

import { Check, Copy, Headphones, Pencil, Undo2, X, Download } from "lucide-react";
import { Check, Copy, Headphones, Pencil, Undo2, X } from "lucide-react";
import { useCallback, useEffect, useState } from "react";

import { ScrollContainer } from "~/components/deer-flow/scroll-container";
import { Tooltip } from "~/components/deer-flow/tooltip";
import { Button } from "~/components/ui/button";
import { Card } from "~/components/ui/card";
import { DownloadDropdown } from "~/components/ui/download-dropdown";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "~/components/ui/tabs";
import { useReplay } from "~/core/replay";
import { closeResearch, listenToPodcast, useStore } from "~/core/store";
Expand Down Expand Up @@ -49,6 +50,11 @@ export function ResearchBlock({

const [editing, setEditing] = useState(false);
const [copied, setCopied] = useState(false);

// 获取报告内容用于下载
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is recommended to use English in codes.

const reportContent = useStore((state) =>
reportId ? (state.messages.get(reportId)?.content ?? "") : ""
);
const handleCopy = useCallback(() => {
if (!reportId) {
return;
Expand All @@ -64,31 +70,7 @@ export function ResearchBlock({
}, 1000);
}, [reportId]);

// Download report as markdown
const handleDownload = useCallback(() => {
if (!reportId) {
return;
}
const report = useStore.getState().messages.get(reportId);
if (!report) {
return;
}
const now = new Date();
const pad = (n: number) => n.toString().padStart(2, '0');
const timestamp = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}_${pad(now.getHours())}-${pad(now.getMinutes())}-${pad(now.getSeconds())}`;
const filename = `research-report-${timestamp}.md`;
const blob = new Blob([report.content], { type: 'text/markdown' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(() => {
document.body.removeChild(a);
URL.revokeObjectURL(url);
}, 0);
}, [reportId]);



const handleEdit = useCallback(() => {
Expand Down Expand Up @@ -140,16 +122,10 @@ export function ResearchBlock({
{copied ? <Check /> : <Copy />}
</Button>
</Tooltip>
<Tooltip title="Download report as markdown">
<Button
className="text-gray-400"
size="icon"
variant="ghost"
onClick={handleDownload}
>
<Download />
</Button>
</Tooltip>
<DownloadDropdown
content={reportContent}
className="text-gray-400"
/>
</>
)}
<Tooltip title="Close">
Expand Down
134 changes: 134 additions & 0 deletions web/src/components/ui/download-dropdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
// SPDX-License-Identifier: MIT

"use client";

import { Download, FileText, FileImage, FileDown } from "lucide-react";
import { useCallback, useState } from "react";

import { Button } from "~/components/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "~/components/ui/dropdown-menu";
import { Tooltip } from "~/components/deer-flow/tooltip";
import {
downloadAsMarkdown,
downloadAsPDF,
downloadAsImage,
downloadAsWord,
createTempElementForScreenshot,
cleanupTempElement,
} from "~/lib/download-utils";

export interface DownloadDropdownProps {
content: string;
className?: string;
disabled?: boolean;
}

export function DownloadDropdown({
content,
className,
disabled = false
}: DownloadDropdownProps) {
const [isDownloading, setIsDownloading] = useState(false);

const handleDownloadMarkdown = useCallback(() => {
downloadAsMarkdown(content);
}, [content]);

const handleDownloadPDF = useCallback(async () => {
if (isDownloading) return;
setIsDownloading(true);
try {
await downloadAsPDF(content);
} catch (error) {
console.error('PDF download failed:', error);
} finally {
setIsDownloading(false);
}
}, [content, isDownloading]);

const handleDownloadImage = useCallback(async () => {
if (isDownloading) return;
setIsDownloading(true);
try {
// 创建临时元素用于截图
Copy link

@hobbytp hobbytp Jun 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above. It is generic comment for all codes in your commit.

const tempId = createTempElementForScreenshot(content);
// 等待DOM更新
await new Promise(resolve => setTimeout(resolve, 100));
await downloadAsImage(tempId);
cleanupTempElement(tempId);
} catch (error) {
console.error('Image download failed:', error);
} finally {
setIsDownloading(false);
}
}, [content, isDownloading]);

const handleDownloadWord = useCallback(async () => {
if (isDownloading) return;
setIsDownloading(true);
try {
await downloadAsWord(content);
} catch (error) {
console.error('Word download failed:', error);
} finally {
setIsDownloading(false);
}
}, [content, isDownloading]);

return (
<DropdownMenu>
<Tooltip title="Download Report">
<DropdownMenuTrigger asChild>
<Button
className={className}
size="icon"
variant="ghost"
disabled={disabled || isDownloading}
>
<Download className={isDownloading ? "animate-bounce" : ""} />
</Button>
</DropdownMenuTrigger>
</Tooltip>
<DropdownMenuContent align="end" className="w-56">
<DropdownMenuItem
onClick={handleDownloadMarkdown}
disabled={isDownloading}
className="whitespace-nowrap"
>
<FileDown className="mr-2 h-4 w-4" />
<span>Download as Markdown</span>
</DropdownMenuItem>
<DropdownMenuItem
onClick={handleDownloadPDF}
disabled={isDownloading}
className="whitespace-nowrap"
>
<FileText className="mr-2 h-4 w-4" />
<span>Download as PDF</span>
</DropdownMenuItem>
<DropdownMenuItem
onClick={handleDownloadImage}
disabled={isDownloading}
className="whitespace-nowrap"
>
<FileImage className="mr-2 h-4 w-4" />
<span>Download as Image</span>
</DropdownMenuItem>
<DropdownMenuItem
onClick={handleDownloadWord}
disabled={isDownloading}
className="whitespace-nowrap"
>
<FileText className="mr-2 h-4 w-4" />
<span>Download as Word</span>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);
}
Loading