"use client" import { useEffect, useState } from "react" import { CopyIcon, Loader2Icon, SparklesIcon } from "lucide-react" import { toast } from "sonner" import { Button } from "@/components/ui/button" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog" import { useI18n } from "@/i18n/provider" import { summarizeAgentConversation } from "@/lib/api/agent" export type ConversationSummaryDialogProps = { open: boolean conversationId: number | null onOpenChange: (open: boolean) => void } export function ConversationSummaryDialog({ open, conversationId, onOpenChange, }: ConversationSummaryDialogProps) { return ( {open ? ( ) : null} ) } type ConversationSummaryDialogBodyProps = { conversationId: number | null onOpenChange: (open: boolean) => void } function ConversationSummaryDialogBody({ conversationId, onOpenChange, }: ConversationSummaryDialogBodyProps) { const t = useI18n() const [loading, setLoading] = useState(false) const [summary, setSummary] = useState("") const [copying, setCopying] = useState(false) useEffect(() => { if (!conversationId) { setSummary("") setLoading(false) return } let active = true setLoading(true) summarizeAgentConversation(conversationId) .then((res) => { if (!active) return setSummary(res || "") }) .catch((error) => { if (!active) return toast.error(error instanceof Error ? error.message : t("aiCopilot.errorGenerating")) }) .finally(() => { if (!active) return setLoading(false) }) return () => { active = false } }, [conversationId, t]) async function handleCopy() { if (!summary || copying) { return } setCopying(true) try { await navigator.clipboard.writeText(summary) toast.success(t("aiCopilot.copied")) } catch { toast.error(t("aiCopilot.errorGenerating")) } finally { setCopying(false) } } return ( {t("aiCopilot.summaryTitle")} {t("aiCopilot.summaryTitle")} {loading ? ( {t("aiCopilot.summarizing")} ) : summary ? ( {summary} ) : ( {t("aiCopilot.noSummary")} )} void handleCopy()} disabled={loading || !summary || copying} > {copying ? t("aiCopilot.copied") : t("aiCopilot.copy")} onOpenChange(false)}> {t("common.close")} ) }