zc2 0.0.52

P2P compute broker with yen-denominated billing, WAL, and broker mesh support
import { useEffect, useMemo, useState } from "react";
import { agent } from "../lib/agent";
import { MACHINE, yen } from "../lib/format";
import { duration, sourceLabel, weekSummary } from "../lib/jobs";
import type { Job, Overview } from "../lib/overview";
import { Panel } from "./ui";

/** "What your machine did this week": jobs, busy time and earnings over the
 * last 7 days, from the agent's job list and the hub's daily earnings. */
export function WeeklySummary({ ov }: { ov: Overview | null }) {
  const [jobs, setJobs] = useState<Job[] | null>(null);
  useEffect(() => {
    agent.jobs().then(setJobs, () => setJobs(null));
  }, [ov?.jobs.done24h, ov?.jobs.live.length]);
  const s = useMemo(() => (jobs ? weekSummary(jobs, Date.now() - 7 * 86_400_000) : null), [jobs]);
  const earned = ov?.ledger ? ov.ledger.days.slice(-7).reduce((a, d) => a + d.earned, 0) : null;
  const best = ov?.ledger?.days.slice(-7).reduce<{ date: string; earned: number } | null>((a, d) => (d.earned > (a?.earned ?? 0) ? d : a), null);

  let line = `Waiting for this ${MACHINE}'s jobs.`;
  if (s) {
    line =
      s.jobs === 0
        ? `This ${MACHINE} ran no jobs in the last 7 days.`
        : `This ${MACHINE} ran ${s.jobs} job${s.jobs === 1 ? "" : "s"} (${s.requests.toLocaleString("en-US")} requests, ${duration(s.busyMinutes)} busy) in the last 7 days` +
          (earned !== null ? ` and earned ${yen(earned)}.` : ".");
  }
  return (
    <Panel title="What your machine did this week" aside="last 7 days">
      <p className="m-0 text-[13px] leading-snug">{line}</p>
      {s && s.bySource.length > 0 && (
        <p className="m-0 text-[12px] text-fg-dim">
          {s.bySource.map((b) => `${sourceLabel(b.source)} ${b.jobs}`).join(" · ")}
          {best && best.earned > 0 ? ` · best day ${new Date(`${best.date}T00:00:00`).toLocaleDateString("en-US", { weekday: "long" })}, ${yen(best.earned)}` : ""}
        </p>
      )}
    </Panel>
  );
}