import { exportChatAction } from "@/app/api/chat/actions"; import { LinkIcon, Loader } from "lucide-react"; import { useRouter } from "next/navigation"; import { useCallback, useState } from "react"; import { toast } from "sonner"; import { safe } from "ts-safe"; import { Button } from "ui/button"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from "ui/dialog"; import { useTranslations } from "next-intl"; type Props = { threadId: string; onExport?: () => void; open?: boolean; onOpenChange?: (open: boolean) => void; children?: React.ReactNode; }; export function ChatExportPopup(props: Props) { const router = useRouter(); const t = useTranslations(); const [isExporting, setIsExporting] = useState(false); const handleExport = useCallback(() => { setIsExporting(true); safe(() => exportChatAction({ threadId: props.threadId, }), ) .watch(() => setIsExporting(false)) .ifOk((exportId) => { const link = `${window.location.origin}/export/${exportId}`; navigator.clipboard.writeText(link).then(() => { toast.success(t("Chat.Thread.linkCopied")); }); router.push(`/export/${exportId}`); }) .ifFail((error) => { toast.error(error.message || "Failed to export chat"); }) .unwrap(); }, [props.threadId, router]); return ( {props.children} {t("Chat.Thread.sharePublicLink")} {t("Chat.Thread.sharePublicLinkDescription")}
{`${window.location.origin}/export/...`}
); }