"use client"; import { useEffect, useState } from "react"; import { Button } from "@/components/ui/button"; import type { LibraryTopic } from "@/lib/library/load"; // ORGANISE, PUBLISH AND THE ACCOUNTS of the Posts library page (lane // x-trending-publish). Every press is a POST to /api/library/*, which is the // editor's action written as the operator's; nothing here writes a library's // file or asks X anything. // // rename the topic's name; its id stays // merge this topic INTO another: there is no unmerge, so it is asked // twice, and the second press says so // move the posts a press would judge (the selection, else the post or // its thread) into another topic // publish the posts the operator KEPT, into an ordinary channel on no // site: the first press says exactly what would be written // accounts who wrote the library's posts; "Archive this account" makes // -x on no site, with sync off, and asks X nothing type Run = (what: () => Promise) => Promise; export async function send(path: string, body: unknown): Promise> { const r = await fetch(path, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }); const j = (await r.json().catch(() => ({}))) as Record; if (!r.ok) throw new Error(typeof j.error === "string" ? j.error : `HTTP ${r.status}`); return j; } const field = "rounded-[3px] border border-seam-2 bg-panel px-2 py-[3px] text-[12.5px] text-text"; export function bytesSaid(n: number): string { if (n < 1024) return `${n} B`; if (n < 1024 * 1024) return `${(n / 1024).toFixed(1)} KB`; if (n < 1024 * 1024 * 1024) return `${(n / 1024 / 1024).toFixed(1)} MB`; return `${(n / 1024 / 1024 / 1024).toFixed(2)} GB`; } type PublishPlan = { channel: string; creates: boolean; kept: number; posts: number; already: number; files: number; bytes: number; textOnly?: string[]; noLongerKept?: string[]; // Where it goes public, who moved what into the topic, and what no longer matches its capture. sites?: string[]; movedIn?: Array<{ id: string; by: "agent" | "operator"; from: string }>; mismatched?: Array<{ id: string; name: string }>; inFlight?: boolean; confirm: string; sentence: string; }; export function TopicOrganise({ lib, topic, topics, off, run, go, when }: { lib: string; topic: LibraryTopic; topics: LibraryTopic[]; off: boolean; run: Run; go: (topicId: string) => void; when: (iso: string) => string }) { const [title, setTitle] = useState(topic.title); const others = topics.filter((t) => t.id !== topic.id); const [into, setInto] = useState(""); const [merging, setMerging] = useState(false); const [plan, setPlan] = useState(null); const [channel, setChannel] = useState(""); const target = others.find((t) => t.id === into) ?? null; const published = topic.published; const kept = topic.counts?.keep ?? 0; const dryRun = (slug: string) => run(async () => { const r = (await send("/api/library/publish", { lib, topic: topic.id, dryRun: true, ...(slug ? { channel: slug } : {}) })) as unknown as PublishPlan; setPlan(r); setChannel(r.channel); }); return (
{others.length > 0 && ( <> )}
{merging && target && (

Merge “{topic.title}” into “{target.title}”? Its {topic.posts} post{topic.posts === 1 ? "" : "s"} and {topic.sources.length} trending page{topic.sources.length === 1 ? "" : "s"} move there and “{topic.title}” is gone. The judgements are untouched. This cannot be undone: there is no unmerge. {topic.watch && target.watch && topic.watch.on && target.watch.on && !topic.watch.ended && !target.watch.ended ? ` Both are watched: the watch of “${target.title}” stands with its own terms, and this topic's watch is gone.` : topic.watch && topic.watch.on && !topic.watch.ended ? ` This topic's watch becomes the watch of “${target.title}”.` : ""}

)}
{published ? `Published into the channel “${published.channel}”: ${published.posts} post${published.posts === 1 ? "" : "s"}, last ${when(published.lastAt)}.` : "Not published."}
{plan && (

{plan.sentence}

channel {plan.channel} {plan.creates ? " (new, on no site)" : ""} · {plan.posts} post{plan.posts === 1 ? "" : "s"} to add · {plan.files} file{plan.files === 1 ? "" : "s"} · {bytesSaid(plan.bytes)} · {plan.already} there already {(plan.textOnly ?? []).length ? ` · ${(plan.textOnly ?? []).length} as words alone` : ""} {(plan.noLongerKept ?? []).length ? ` · ${(plan.noLongerKept ?? []).length} no longer kept, left` : ""} {(plan.movedIn ?? []).length ? ` · ${(plan.movedIn ?? []).length} moved here (${(plan.movedIn ?? []).filter((m) => m.by === "agent").length} by an agent)` : ""} {(plan.mismatched ?? []).length ? ` · ${(plan.mismatched ?? []).length} file${(plan.mismatched ?? []).length === 1 ? "" : "s"} not copied` : ""}

{(plan.sites ?? []).length > 0 && (plan.posts > 0 || plan.files > 0) && (

On {(plan.sites ?? []).length === 1 ? "the site" : "the sites"} {(plan.sites ?? []).join(", ")}: public at the next build.

)} {plan.creates && !published && (
)}
{(plan.posts > 0 || plan.files > 0 || plan.creates) && ( )}
)}
); } /** The bar's move: the posts a press would judge, into another topic. */ export function MoveControl({ lib, topics, here, targets, off, run }: { lib: string; topics: LibraryTopic[]; here: string | null; targets: Array<{ id: string } | { thread: string }>; off: boolean; run: Run }) { const [to, setTo] = useState(""); const choices = topics.filter((t) => t.id !== here); if (topics.length < 2) return null; const ids = targets.flatMap((t) => ("id" in t ? [t.id] : [])); const thread = targets.length === 1 && "thread" in targets[0] ? targets[0].thread : null; return ( Move to: ); } type Account = { handle: string; name?: string; posts: number; kept: number; keptByOperator?: number; topics?: string[]; archivedAs?: string[] }; export function AccountsPanel({ lib, topic, topics, off, run }: { lib: string; topic: string | null; topics: LibraryTopic[]; off: boolean; run: Run }) { const [open, setOpen] = useState(false); const [state, setState] = useState<{ accounts: Account[]; total: number; more: number } | { sentence: string } | null>(null); const [asking, setAsking] = useState(null); const [made, setMade] = useState>({}); const names = new Map(topics.map((t) => [t.id, t.title])); useEffect(() => { if (!open) return; let live = true; setState(null); fetch(`/api/library/accounts?lib=${encodeURIComponent(lib)}${topic ? `&topic=${encodeURIComponent(topic)}` : ""}`, { cache: "no-store" }) .then((r) => r.json()) .then((j: { ok?: boolean; accounts?: Account[]; total?: number; more?: number; sentence?: string; error?: string }) => { if (!live) return; setState(j.ok ? { accounts: j.accounts ?? [], total: j.total ?? 0, more: j.more ?? 0 } : { sentence: j.sentence ?? j.error ?? "The accounts could not be read." }); }) .catch((err) => live && setState({ sentence: `The accounts could not be read: ${err instanceof Error ? err.message : String(err)}` })); return () => { live = false; }; }, [open, lib, topic]); return (
setOpen((e.currentTarget as HTMLDetailsElement).open)}> Accounts{topic ? " in this topic" : " in the library"} {open && !state &&

Reading the accounts…

} {state && "sentence" in state && (

{state.sentence}

)} {state && "accounts" in state && ( <>

{state.total} account{state.total === 1 ? "" : "s"} {state.more ? `, the first ${state.accounts.length} shown` : ""}. Archiving one makes its own X channel, on no site, with sync off: nothing is asked of X until you turn its sync on or fetch it in the editor.

    {state.accounts.map((a) => { const archived = made[a.handle] ? [made[a.handle]] : (a.archivedAs ?? []); return (
  • @{a.handle} {a.name ? {a.name} : null} {a.posts} post{a.posts === 1 ? "" : "s"} · {a.kept} kept{topic ? "" : ` · ${(a.topics ?? []).map((t) => names.get(t) ?? t).join(", ")}`} {archived.length > 0 ? ( archived as {archived.join(", ")} ) : asking === a.handle ? ( Makes the channel “{a.handle.toLowerCase()}-x”, on no site, with sync off. Nothing is asked of X. ) : ( )}
  • ); })}
)}
); }