"use client"; import { useEffect, useState } from "react"; // A count beside a rail place (canvas rail: "Inbox 7", "Work 14"), in Meter. // Fetched after the page is up (GET /api/rail, memoised server-side), so no // page waits on the inbox to render its rail. The two places share one request // per page load. Nothing is shown until it answers, and nothing on a failure: // a missing count is better than a wrong one. type Counts = { inbox: number; work: number }; let inflight: { at: number; p: Promise } | null = null; function counts(): Promise { if (inflight && Date.now() - inflight.at < 2000) return inflight.p; const p = fetch("/api/rail", { cache: "no-store" }) .then((r) => (r.ok ? (r.json() as Promise) : null)) .catch(() => null); inflight = { at: Date.now(), p }; return p; } /** `dock`: the same count in the phone dock (PhoneDock.tsx), under its own attribute so the rail's stays the one `data-rail-count`. */ export default function RailCount({ which, dock = false }: { which: keyof Counts; dock?: boolean }) { const [n, setN] = useState(null); useEffect(() => { let live = true; void counts().then((c) => { if (live && c && typeof c[which] === "number") setN(c[which]); }); return () => { live = false; }; }, [which]); if (n === null) return null; if (dock) return ( ); return ( ); }