import { useState, type ReactNode } from "react";
import { Link } from "react-router-dom";
import { artifactsApi, videosApi } from "../api/endpoints";
import type { Artifact, Citation, Segment } from "../api/types";
import { useJob } from "../hooks/useJob";
import { Markdown } from "./Markdown";
import { ConfirmButton, errorMessage, formatDate, formatDuration, JobProgress, StatusPill } from "./ui";
export function CitationList({ citations, onCite }: { citations: Citation[]; onCite?: (c: Citation) => void }) {
if (!citations.length) return null;
return (
{citations.map((c) => (
onCite?.(c)}>
{c.title}
{" "}
lines {c.line_start}
{c.line_end > c.line_start ? ` to ${c.line_end}` : ""}
{c.span.length > 220 ? `${c.span.slice(0, 220)}...` : c.span}
))}
);
}
function Transcript({ segments, onSeek }: { segments: Segment[]; onSeek: (t: number) => void }) {
return (
{segments.map((s, i) => (
onSeek(s.start)}>
{formatDuration(s.start) || "0:00"}
{s.speaker === "host_b" ? "B" : "A"}
{s.text}
{s.sources.length > 0 && [{s.sources.map((r) => r.title ?? r.path).join("; ")}] }
))}
);
}
/** The player, image or text of a finished artifact (nothing while it is still being made). */
export function Body({ artifact, onCite }: { artifact: Artifact; onCite?: (c: Citation) => void }) {
const [showTranscript, setShowTranscript] = useState(false);
const [audioEl, setAudioEl] = useState(null);
const c = artifact.content;
if (artifact.status !== "ready") return null;
switch (artifact.type) {
case "summary":
return (
{(c.themes as string[] | undefined)?.length ? (
{(c.themes as string[]).map((t) => (
{t}
))}
) : null}
);
case "audio_overview":
return (
{artifact.url &&
}
{c.segments?.length > 0 && (
<>
setShowTranscript((v) => !v)}>
{showTranscript ? "Hide transcript" : `Show transcript (${c.segments.length} lines)`}
{showTranscript && (
{
if (audioEl) {
audioEl.currentTime = t;
void audioEl.play();
}
}}
/>
)}
>
)}
);
case "video":
if (artifact.provider === "blog2video") {
return (
{artifact.url && }
Open editor
);
}
return artifact.url ? (
) : null;
case "quote_card":
return artifact.url ? (
) : null;
case "carousel":
return (
{artifact.slide_urls.map((u, i) => (
))}
);
case "launch_kit":
return (
{c.claims?.length ?? 0} claims, {c.hooks?.length ?? 0} hooks, posts for 4 platforms, SEO pack and carousel.
Open Launch Kit
);
default:
return null;
}
}
/** Any generated artifact: live progress while it runs, then the right player or preview. */
export function ArtifactCard({
artifact: initial,
onCite,
onRemoved,
onChanged,
actions,
}: {
artifact: Artifact;
onCite?: (c: Citation) => void;
onRemoved?: (id: string) => void;
onChanged?: (a: Artifact) => void;
actions?: (a: Artifact) => ReactNode;
}) {
const [artifact, setArtifact] = useState(initial);
const [error, setError] = useState(null);
const job = useJob(artifact.job, () => {
artifactsApi.get(artifact.id).then((a) => {
setArtifact(a);
onChanged?.(a);
});
});
const running = job && (job.status === "queued" || job.status === "running");
const failedMessage = artifact.status === "failed" ? (artifact.content.error as string | undefined) ?? job?.error : null;
return (
{running && job && }
{failedMessage && {failedMessage}
}
{error && {error}
}
{actions?.(artifact)}
{artifact.download_url && (
Download
)}
{artifact.status === "failed" && (
{
setError(null);
try {
setArtifact(await artifactsApi.retry(artifact.id));
} catch (e) {
setError(errorMessage(e));
}
}}
>
Retry
)}
{onRemoved && (
{
// A blog2video video is also removed from blog2video.
await (artifact.provider === "blog2video" ? videosApi.remove(artifact.id) : artifactsApi.remove(artifact.id));
onRemoved(artifact.id);
}}
>
Delete
)}
);
}