"use client"; import Link from "next/link"; import { useEffect, useRef, useState } from "react"; import type { ActivityItem, RunningView } from "@/lib/activity-types"; import { JobProgressBar, JobProgressText } from "@/components/ui/job-progress"; import { jobsHref } from "@/lib/hrefs"; // --------------------------------------------------------------------------- // What is running, on home (canvas `.running`; it was the rail's foot until // the redesign's L4 left the rail navigation only): umtool's own and the // editor's, with spend. The label row is "Running N" and the day's API spend // in Meter; then a row per job (lib/running.mjs chooses them) -- kind, target, // progress, queued or running, and a generation job's spend -- each linking to // its piece when it has one, else to the editor's /jobs; then the folded rest // ("+ 140 queued · 3 lanes"). // // Three different things to say when there are no rows: the editor did not // answer, the editor's WORKER_TOKEN is not set (it answered 503), and nothing // is running. They are never the same sentence. // // The server renders the last view it has (or umtool's own jobs alone); this // asks GET /api/running on mount, then every POLL_MS while the tab is visible, // and not at all while it is hidden. One request at a time. // --------------------------------------------------------------------------- export const POLL_MS = 8000; const usd = (n: number) => `$${n.toFixed(2)}`; function Row({ item: i }: { item: ActivityItem }) { const queued = i.state === "queued"; const spend = i.generation && typeof i.spend === "number" ? usd(i.spend) : null; const pct = !queued && typeof i.meter?.fraction === "number" ? `${Math.round(i.meter.fraction * 100)}%` : null; // The right cell: a generation job's spend (Meter), else how far it is, else // where it waits. The words of its progress go on the line under it. const right = spend ? ( {spend} ) : queued ? ( ) : pct ? ( {pct} ) : null; const words = !queued && i.meter?.text ? i.meter : null; const body = ( <> {i.kindLabel ?? i.label} {right ?? } {(i.target || words) && ( {i.target} {i.target && words ? " · " : null} {words && } )} {!queued && } ); const cls = "grid grid-cols-[minmax(0,1fr)_auto] gap-x-2 gap-y-0.5 text-[12px] text-text-2 no-underline hover:text-text"; const title = [i.kindLabel ?? i.label, i.target, i.meter?.text, i.state].filter(Boolean).join(" · "); return (
  • {i.external ? ( {body} ) : ( {body} )}
  • ); } export default function RunningList({ initial }: { initial: RunningView }) { const [view, setView] = useState(initial); const inflight = useRef(false); useEffect(() => { let live = true; const tick = async () => { if (inflight.current || document.visibilityState === "hidden") return; inflight.current = true; try { const r = await fetch("/api/running", { cache: "no-store" }); const next = r.ok ? ((await r.json()) as RunningView) : null; if (live && next && Array.isArray(next.rows)) setView(next); } catch { // the next tick asks again; the last view stays drawn } finally { inflight.current = false; } }; void tick(); const timer = window.setInterval(tick, POLL_MS); const onVisible = () => { if (document.visibilityState === "visible") void tick(); }; document.addEventListener("visibilitychange", onVisible); return () => { live = false; window.clearInterval(timer); document.removeEventListener("visibilitychange", onVisible); }; }, []); const { rows, editor, more, today } = view; const state = editor.reason ?? (editor.pending ? "pending" : "ok"); return (

    Running {" "} {view.running} {today && ( {usd(today.usd)} today )}

    {rows.length > 0 && (
      {rows.map((i) => ( ))}
    )} {editor.text ? (

    {editor.text}

    ) : rows.length === 0 ? (

    {editor.pending ? "Asking the editor…" : "Nothing running"}

    ) : null} {more.text && ( {rows.length ? more.text : more.text.replace(/^\+ /, "")} )}
    ); }