"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 ? (
{editor.text}
) : rows.length === 0 ? ({editor.pending ? "Asking the editor…" : "Nothing running"}
) : null} {more.text && ( {rows.length ? more.text : more.text.replace(/^\+ /, "")} )}