|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import type { GeneratedOutputSummary } from "@cheatcode/types"; |
| 4 | +import { useAuth } from "@clerk/nextjs"; |
| 5 | +import { useQuery } from "@tanstack/react-query"; |
| 6 | +import { Download, FileText } from "@/components/ui/icons"; |
| 7 | +import { listGeneratedOutputs } from "@/lib/api/outputs"; |
| 8 | + |
| 9 | +export default function ArtifactsPage() { |
| 10 | + return ( |
| 11 | + <section className="chat-scrollbar min-w-0 flex-1 overflow-y-auto bg-white px-4 pt-12 pb-16 text-[#1b1b1b] sm:px-6 lg:px-10"> |
| 12 | + <div className="mx-auto w-full max-w-[740px]"> |
| 13 | + <h1 className="font-bold text-[30px] leading-9 tracking-[-0.01em]">Artifacts</h1> |
| 14 | + <p className="mt-2 text-[#707070] text-[15px]"> |
| 15 | + Files your agents generated — slides, docs, spreadsheets, PDFs, and charts. |
| 16 | + </p> |
| 17 | + <ArtifactsList /> |
| 18 | + </div> |
| 19 | + </section> |
| 20 | + ); |
| 21 | +} |
| 22 | + |
| 23 | +function ArtifactsList() { |
| 24 | + const { getToken, isSignedIn } = useAuth(); |
| 25 | + const query = useQuery({ |
| 26 | + enabled: Boolean(isSignedIn), |
| 27 | + queryFn: () => listGeneratedOutputs(getToken), |
| 28 | + queryKey: ["generated-outputs"], |
| 29 | + }); |
| 30 | + |
| 31 | + if (query.isLoading) { |
| 32 | + return <p className="mt-8 text-[#a0a0a0] text-[14px]">Loading…</p>; |
| 33 | + } |
| 34 | + if (query.isError) { |
| 35 | + return ( |
| 36 | + <div className="mt-8 flex items-center gap-3"> |
| 37 | + <p className="text-[#707070] text-[14px]">Couldn’t load your artifacts.</p> |
| 38 | + <button |
| 39 | + className="rounded-full border border-[#e5e5e5] px-4 py-1.5 font-medium text-[13px] hover:bg-[#f7f7f7]" |
| 40 | + onClick={() => void query.refetch()} |
| 41 | + type="button" |
| 42 | + > |
| 43 | + Retry |
| 44 | + </button> |
| 45 | + </div> |
| 46 | + ); |
| 47 | + } |
| 48 | + const outputs = query.data ?? []; |
| 49 | + if (outputs.length === 0) { |
| 50 | + return ( |
| 51 | + <div className="mt-8 rounded-2xl border border-[#f0f0f0] bg-[#fafafa] py-12 text-center"> |
| 52 | + <p className="font-medium text-[#1b1b1b] text-[15px]">No artifacts yet.</p> |
| 53 | + <p className="mt-1 text-[#707070] text-[13px]"> |
| 54 | + Ask an agent to build a deck, doc, or spreadsheet and it’ll show up here. |
| 55 | + </p> |
| 56 | + </div> |
| 57 | + ); |
| 58 | + } |
| 59 | + return ( |
| 60 | + <ul className="mt-6 flex flex-col gap-2"> |
| 61 | + {outputs.map((output) => ( |
| 62 | + <ArtifactRow key={output.id} output={output} /> |
| 63 | + ))} |
| 64 | + </ul> |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +function formatSize(bytes: number): string { |
| 69 | + if (bytes < 1024) { |
| 70 | + return `${bytes} B`; |
| 71 | + } |
| 72 | + if (bytes < 1024 * 1024) { |
| 73 | + return `${(bytes / 1024).toFixed(0)} KB`; |
| 74 | + } |
| 75 | + return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; |
| 76 | +} |
| 77 | + |
| 78 | +function ArtifactRow({ output }: { output: GeneratedOutputSummary }) { |
| 79 | + return ( |
| 80 | + <li className="flex items-center gap-3 rounded-2xl border border-[#f0f0f0] bg-white px-4 py-3"> |
| 81 | + <FileText aria-hidden="true" className="h-5 w-5 shrink-0 text-[#a0a0a0]" /> |
| 82 | + <div className="min-w-0 flex-1"> |
| 83 | + <p className="truncate font-medium text-[#1b1b1b] text-[14px]">{output.filename}</p> |
| 84 | + <p className="text-[#a0a0a0] text-[12px]"> |
| 85 | + {output.kind} · {formatSize(output.sizeBytes)} ·{" "} |
| 86 | + {new Date(output.createdAt).toLocaleDateString()} |
| 87 | + </p> |
| 88 | + </div> |
| 89 | + <a |
| 90 | + className="inline-flex h-8 shrink-0 items-center gap-1.5 rounded-full bg-[#1b1b1b] px-3 font-medium text-[13px] text-white transition-colors hover:bg-black" |
| 91 | + download={output.filename} |
| 92 | + href={output.downloadUrl} |
| 93 | + rel="noopener noreferrer" |
| 94 | + target="_blank" |
| 95 | + > |
| 96 | + <Download aria-hidden="true" className="h-3.5 w-3.5" /> |
| 97 | + Download |
| 98 | + </a> |
| 99 | + </li> |
| 100 | + ); |
| 101 | +} |
0 commit comments