"use client"; import { useState } from "react"; import { FileText, Hash, Mail, Loader2, Copy, Sparkles, Download } from "lucide-react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { repurposeAction } from "@/app/(app)/episodes/actions"; type Format = "blog" | "social_thread" | "newsletter"; type Content = { title: string; body: string } | null; const FORMATS: { key: Format; label: string; icon: React.ComponentType<{ className?: string }> }[] = [ { key: "blog", label: "Blog post", icon: FileText }, { key: "social_thread", label: "Social thread", icon: Hash }, { key: "newsletter", label: "Newsletter", icon: Mail }, ]; function wordCount(text: string): number { const t = text.trim(); return t ? t.split(/\s+/).length : 0; } function slugify(s: string): string { return s.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 60) || "content"; } function downloadMarkdown(filename: string, markdown: string) { const blob = new Blob([markdown], { type: "text/markdown;charset=utf-8" }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = filename; document.body.appendChild(a); a.click(); a.remove(); URL.revokeObjectURL(url); } export function RepurposeClient({ episodeId, initial, }: { episodeId: string; initial: Record; }) { const [content, setContent] = useState>(initial); const [busy, setBusy] = useState(null); async function generate(format: Format) { setBusy(format); const res = await repurposeAction(episodeId, format); setBusy(null); if (!res.ok || !res.content) { toast.error(res.error ?? "Could not generate"); return; } setContent((prev) => ({ ...prev, [format]: res.content! })); toast.success("Generated"); } function copy(text: string) { navigator.clipboard.writeText(text).then(() => toast.success("Copied to clipboard")); } return (
{FORMATS.map((f) => { const c = content[f.key]; return ( {f.label} {c ? (

{c.title}

{wordCount(c.body).toLocaleString()} words ยท {c.body.length.toLocaleString()} chars
{c.body}
) : (

Turn this episode into a {f.label.toLowerCase()}.

)}
); })}
); }