"use client"; import { useMemo, useState } from "react"; import { ArrowDownUp, Copy, Download } from "lucide-react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { Textarea } from "@/components/ui/textarea"; import { useI18n } from "@/i18n/i18n-context"; import type { Messages } from "@/i18n/dictionaries"; import { exportChatSessions, importChatSessions, type ChatSession, } from "@/hooks/use-chat-history"; export function ChatHistoryTransfer({ sessions, activeInstance, disabled, }: { sessions: ChatSession[]; activeInstance: string | null; disabled: boolean; }) { const { t } = useI18n(); const [open, setOpen] = useState(false); const [selected, setSelected] = useState([]); const [input, setInput] = useState(""); const [error, setError] = useState(""); const backup = useMemo(() => { try { return { content: exportChatSessions( sessions.filter((session) => selected.includes(session.id)), ), error: false, }; } catch { return { content: "", error: true }; } }, [sessions, selected]); const label = (key: keyof Messages["chat"]["history"]) => t(`chat.history.${key}`); async function copy() { try { await navigator.clipboard.writeText(backup.content); setError(""); toast.success(label("backupCopied")); } catch { setError(label("backupCopyFailed")); } } function download() { const url = URL.createObjectURL( new Blob([backup.content], { type: "application/json" }), ); const link = document.createElement("a"); link.href = url; link.download = `agent-storage-chats-${new Date().toISOString().slice(0, 10)}.json`; link.click(); setTimeout(() => URL.revokeObjectURL(url), 1000); } function restore() { try { const result = importChatSessions(input, activeInstance); toast.success( t("chat.history.backupImported", { count: result.imported, skipped: result.skipped, }), ); setInput(""); setError(""); setOpen(false); } catch (cause) { const key = cause instanceof Error ? cause.message : "invalidBackup"; setError( label( key === "backupLimit" || key === "backupProject" || key === "backupStorage" ? key : "invalidBackup", ), ); } } return ( <> {label("backup")} {label("backupDescription")} setError("")} > {label("backupExport")} {label("backupImport")}
{!sessions.length && (

{label("empty")}

)} {sessions.map((session) => ( ))}