"use client"; import { useCallback, useEffect, useState } from "react"; import { badgeVariants } from "@/components/ui/badge"; import { buttonVariants } from "@/components/ui/button"; import { cn } from "@/lib/utils"; // --------------------------------------------------------------------------- // ADD POST in Play the cut (UI notes item 13): the chain `pnpm ops // capture-posts` → `umtool posts add` → `umtool look deck.posts` in one // panel (/api/report/post-cards). Search a channel's posts archive; Capture // one through the EDITOR's capture-posts (paced there); Add it to the clip on // screen (`posts add`'s own write) and see its card on that clip as the cut // plays. Each post's WARNING comes from its own data -- a quoted post, a // repost, media -- since its screenshot shows them; the cure is a CROP (the // post's own shotMaxHeight: its top that tall, never panned). // --------------------------------------------------------------------------- type Hit = { id: string; date: string | null; author: string | null; text: string; url: string | null; warnings: string[]; captured: boolean; inCut: boolean; }; type CutPost = { id: string; date: string | null; text: string; attachTo: string | null; shotMaxHeight: number | null; hide: boolean; warnings: string[] }; type Base = { channels: { slug: string; suggested: boolean }[]; posts: CutPost[]; token: string | null }; async function postJson(body: Record) { const r = await fetch("/api/report/post-cards", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }); const j = (await r.json().catch(() => ({}))) as Record; return { ok: r.ok, status: r.status, json: j }; } export default function AddPost({ project, clip, onAdded, }: { project: string; /** The clip on screen: what Add attaches the post to (null: a card is on screen). */ clip: string | null; /** A post was added or cropped: the view is read again, the deck recomposed, the card shown on its clip. */ onAdded: (post: { id: string; attachTo: string | null }) => void; }) { const [open, setOpen] = useState(false); const [base, setBase] = useState(null); const [channel, setChannel] = useState(""); const [q, setQ] = useState(""); const [hits, setHits] = useState(null); const [total, setTotal] = useState(0); const [busy, setBusy] = useState(null); const [error, setError] = useState(null); const [said, setSaid] = useState(null); const [crop, setCrop] = useState>({}); const [capturing, setCapturing] = useState<{ jobId: string; id: string; line: string } | null>(null); const loadBase = useCallback(async () => { const r = await fetch(`/api/report/post-cards?project=${encodeURIComponent(project)}`, { cache: "no-store" }).catch(() => null); const j = r ? ((await r.json().catch(() => null)) as (Base & { error?: string }) | null) : null; if (!r?.ok || !j) { setError(j?.error ?? "the posts could not be read"); return; } setBase(j); setChannel((c) => c || j.channels[0]?.slug || ""); }, [project]); useEffect(() => { if (open && !base) void loadBase(); }, [open, base, loadBase]); const search = useCallback(async () => { if (!channel) return; setBusy("search"); setError(null); try { const r = await fetch(`/api/report/post-cards?project=${encodeURIComponent(project)}&channel=${encodeURIComponent(channel)}&q=${encodeURIComponent(q)}`, { cache: "no-store" }); const j = (await r.json().catch(() => ({}))) as { hits?: Hit[]; total?: number; archive?: boolean; error?: string }; if (!r.ok) setError(j.error ?? `HTTP ${r.status}`); else if (j.archive === false) setError(`${channel} has no posts archive`); setHits(j.hits ?? []); setTotal(j.total ?? 0); } finally { setBusy(null); } }, [channel, project, q]); // A capture in flight: the editor's job, followed until it ends; then the search again (its shot is there). useEffect(() => { if (!capturing) return; const t = setInterval(async () => { const r = await fetch(`/api/report/post-cards?project=${encodeURIComponent(project)}&job=${encodeURIComponent(capturing.jobId)}`, { cache: "no-store" }).catch(() => null); if (!r?.ok) return; const j = (await r.json()) as { status: string; ended: boolean; tail: string[] }; setCapturing((c) => (c ? { ...c, line: `${j.status}${j.tail.length ? ` — ${j.tail[j.tail.length - 1]}` : ""}` } : c)); if (j.ended) { setSaid(`capture of ${capturing.id}: ${j.status}${j.tail.length ? ` — ${j.tail[j.tail.length - 1]}` : ""}`); setCapturing(null); await search(); } }, 1000); return () => clearInterval(t); }, [capturing, project, search]); const capture = async (id: string) => { setBusy(id); setError(null); try { const r = await postJson({ project, op: "capture", channel, ids: [id] }); if (!r.ok || !r.json.jobId) setError(String(r.json.error ?? `HTTP ${r.status}`)); else setCapturing({ jobId: String(r.json.jobId), id, line: "queued" }); } finally { setBusy(null); } }; const add = async (h: Hit) => { setBusy(h.id); setError(null); setSaid(null); try { const c = crop[h.id]?.trim(); const r = await postJson({ project, op: "add", channel, id: h.id, ...(clip ? { attach: clip } : {}), ...(c ? { crop: Number(c) } : {}) }); if (!r.ok) { setError(String(r.json.error ?? `HTTP ${r.status}`)); return; } const post = r.json.post as { id: string; attachTo?: string }; setSaid(`${r.json.created ? "added" : "replaced"} ${post.id}${post.attachTo ? ` on ${post.attachTo}` : ""} — its card plays at the clip's end`); await loadBase(); await search(); onAdded({ id: post.id, attachTo: post.attachTo ?? null }); } finally { setBusy(null); } }; const saveCrop = async (p: CutPost) => { setBusy(p.id); setError(null); try { const v = (crop[`cut:${p.id}`] ?? (p.shotMaxHeight ? String(p.shotMaxHeight) : "")).trim(); const r = await postJson({ project, op: "crop", post: p.id, crop: v ? Number(v) : null, token: base?.token ?? null }); if (!r.ok) { setError(String(r.json.error ?? `HTTP ${r.status}`)); return; } setSaid(v ? `${p.id} cropped to its top ${v} px` : `${p.id} uncropped`); await loadBase(); onAdded({ id: p.id, attachTo: p.attachTo }); } finally { setBusy(null); } }; return (
{open && (
setQ(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") void search(); }} placeholder="words in the post (empty: the newest)" className="min-w-48 flex-1 rounded border border-seam-2 bg-ground px-1.5 py-0.5 text-[11.5px] text-text" /> {clip ? `adds to ${clip}, the clip on screen` : "a card is on screen: the post rides on the clip its date picks"}
{error && (

{error}

)} {said &&

{said}

} {capturing &&

capturing {capturing.id} through the editor (paced): {capturing.line}

} {hits && ( <>

{total} post{total === 1 ? "" : "s"} in {channel} {total > hits.length ? `, the newest ${hits.length} shown` : ""}

    {hits.map((h) => (
  • {h.date?.slice(0, 10)} {h.author && @{h.author.replace(/^@/, "")}} {h.inCut && in the cut} {h.captured ? "captured" : "no screenshot yet"} {h.url && ( the post ↗ )}

    {h.text}

    {h.warnings.map((w, i) => (

    ⚠ {w}

    ))}
    {!h.captured ? ( ) : ( <> )}
  • ))}
)} {base && base.posts.length > 0 && (

posts in the cut — {base.posts.length}

    {base.posts.map((p) => (
  • {p.id} {p.attachTo ? `on ${p.attachTo}` : "on the clip its date picks"} {p.text} {p.warnings.map((w, i) => ( ⚠ {w} {p.shotMaxHeight ? ` (cropped to ${p.shotMaxHeight} px)` : ""} ))}
  • ))}
)}
)}
); }