Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

33.0 KB
Raw Blame
/**
 * The organization's overview, a calm dashboard: a hero naming the organization (its status
 * pill, the line of metadata and the mission folded to one line), a KPI strip — employees
 * (on desk / running / paused), the board as a segmented bar with its blocked count, today's
 * calendar, and this period's spend as the ring beside the amount with the budget bar under
 * it — and then three full-width runs, one under the other: the inbox (what names the reader,
 * what is stuck and what has landed, newest first), today's timeline with each instance's
 * outcome, and the budget alerts. Each reading is stated once: the spend lives in its KPI cell
 * alone, and nothing on this page is clickable as a whole — no card, no row, no heading. Every
 * jump is a named control: in the three lists the row's title is the link (a text button, the
 * rest of the row inert), and where there is no title to click — a KPI cell, the counts under
 * the board bar — a named corner button carries it. The controls inside a card stay clickable,
 * and the destination is read rather than guessed.
 * The overview is the one page whose rows cross into another menu — it is the dashboard, and
 * its readings are links to where each lives: today's schedule to the calendar, a budget alert
 * to finance, an inbox line to the channel it was said in. A ticket is the exception in the
 * other direction: it opens as the detail dialog, in place, like everywhere else.
 * A brand-new organization (nobody hired, empty board) gets the three-step guide in place of
 * the sections, and the header then drops its "open the CEO's desk" button: the guide's first
 * step is that same call to action, and one screen carries a control once.
 *
 * Loading discipline: the skeleton shows only until the first response or the first error;
 * a failed refresh keeps what was last read on screen under one error line with its retry.
 */
import { useCallback, useEffect, useId, useLayoutEffect, useRef, useState } from "react";
import type { ReactNode } from "react";
import { useNavigate } from "react-router";
import type { OrgChartResponse, OrgTicketStatus, OrganizationDetail } from "@adelie/server/api";
import {
  Badge,
  Button,
  Chevron,
  EmptyState,
  GlyphIcon,
  Heading,
  ICON_GAP,
  ICON_SIZE,
  RuledSection,
  Segmented,
  Text,
  toastError,
} from "@adelie/ui";
import * as api from "../../api/endpoints";
import { S } from "../../lib/strings";
import { apiErrorText } from "../../lib/api-error";
import { formatDateTime, formatMoney, formatRelativeShort } from "../../lib/format";
import { useDocumentTitle } from "../../lib/use-document-title";
import { toneDot, toneInk, toneSurface } from "../../lib/tone";
import type { Tone } from "../../lib/tone";
import { useCompany } from "../../state/company";
import { useLocale } from "../../state/locale";
import { useLiveSessionStatuses } from "../../state/sessions";
import { useTheme } from "../../state/theme";
import { NAV_ICONS } from "../../lib/nav-icons";
import { STAT_ICONS } from "../../lib/stat-icons";
import { orgChannelPath, orgKey, orgPagePath } from "./company-nav";
import type { CompanyNavKey } from "./company-nav";
import { CHANNEL_ICON } from "./channel-sidebar";
import { OrgEmptyLine, OrgPage, OrgPageSkeleton, useOrg } from "./org-layout";
import {
  BudgetBar,
  ErrorLine,
  JumpButton,
  OrgStatusPill,
  PrincipalChip,
  SpendRing,
  TitleButton,
  principalLabel,
} from "./shared";
import { agentPrincipal } from "./principals";
import { liveEmployeeStates } from "./org-sessions";
import { timeLabel } from "./calendar-geom";
import {
  BOARD_SEGMENT_TONE,
  FIRST_STEPS,
  INBOX_FILTERS,
  TIMELINE_TONE,
  boardSummary,
  employeeCounts,
  firstSteps,
  inboxCounts,
  inboxMatches,
  inboxRows,
  missionClampedGuess,
  spendSummary,
  todaySummary,
} from "./overview-summary";
import type {
  FirstStep,
  InboxCategory,
  InboxFilter,
  InboxRow,
  InboxTarget,
  TimelineMark,
} from "./overview-summary";

/** How many of today's instances the timeline shows before pointing at the calendar. */
const TIMELINE_ROWS = 6;

/** The mark that leads an inbox row: the channel hash for what was said, the board's glyph for a ticket. */
const INBOX_ICON: Record<InboxCategory, string> = {
  mention: CHANNEL_ICON,
  blocked: NAV_ICONS.orgTickets,
  done: NAV_ICONS.orgTickets,
};

/**
 * A row of a section: full width, the content decides the rest. The row is inert — it carries
 * no click of its own and no hover wash that would imply one; where it leads somewhere, its
 * title is the only thing that goes there.
 */
const rowClass = "flex w-full items-center gap-2 rounded-md px-2 py-1.5 text-left text-sm";

/** A small tone-marked count: the dot, the label, the number. */
function ToneCount({ tone, label, value }: { tone: Tone; label: string; value: number }) {
  return (
    <span className={`inline-flex items-center ${ICON_GAP.tight}`} data-tooltip={label}>
      <span className={`block h-1.5 w-1.5 rounded-full ${toneDot[tone]}`} />
      <span>{label}</span>
      <span className="font-semibold tabular-nums text-gray-700 dark:text-gray-200">{value}</span>
    </span>
  );
}

/** The bar segments' fills: the ticket status badges' tones, with done as the heavier neutral its badge wears. */
const BOARD_FILL: Record<OrgTicketStatus, string> = {
  proposed: toneDot.muted,
  in_progress: toneDot.busy,
  review: toneDot.attention,
  done: "bg-gray-500 dark:bg-gray-400",
  rejected: toneDot.danger,
};

/** The label of a timeline mark: the calendar's own outcome names, plus "upcoming". */
function markLabel(mark: TimelineMark): string {
  return mark === "upcoming"
    ? S.company.overview.upcoming
    : (S.company.calendarOutcomes[mark] ?? mark);
}

/**
 * The label row of a summary block: its name, and the one button that opens the page the
 * block summarizes. Every KPI cell wears it, so the corner button sits in the same place in
 * all of them.
 */
function SummaryLabel({
  label,
  jump,
  onJump,
}: {
  label: string;
  /** The corner button's name — where it goes, e.g. "Open the org chart". */
  jump: string;
  onJump: () => void;
}) {
  return (
    <div className="flex items-start justify-between gap-2">
      <Text variant="eyebrow" as="span" className="block">
        {label}
      </Text>
      <JumpButton label={jump} onClick={onJump} />
    </div>
  );
}

/**
 * One cell of the KPI strip: its label with the corner button, the headline number, and the
 * detail line under it. The cell is a plain card rather than a link — several cells carry
 * their own controls in the detail, which a card-wide click would swallow, and a card that is
 * silently clickable never says where the click lands.
 */
function KpiCell({
  label,
  value,
  detail,
  jump,
  onJump,
}: {
  label: string;
  value: ReactNode;
  detail: ReactNode;
  jump: string;
  onJump: () => void;
}) {
  return (
    <div className="flex min-h-28 flex-col justify-between bg-white px-4 py-3 dark:bg-gray-950">
      <div>
        <SummaryLabel label={label} jump={jump} onJump={onJump} />
        <span className="mt-1 block text-2xl font-semibold leading-none tabular-nums">{value}</span>
      </div>
      <div className="mt-2 flex flex-wrap items-center gap-x-3 gap-y-1 text-xs text-gray-500 dark:text-gray-400">
        {detail}
      </div>
    </div>
  );
}

/**
 * The organization's mission under the hero's metadata line: a label, one clamped line, and
 * the chevron that opens it. Missions run to paragraphs, and a hero that prints one whole
 * pushes the dashboard itself below the fold. Collapsed on every visit — the mission is read
 * once, not tracked — so nothing is persisted.
 *
 * Whether the toggle is offered at all is a measurement (`scrollHeight > clientHeight` on the
 * clamped paragraph, re-taken on resize), seeded by a guess from the text so the toggle does
 * not flicker in after the first paint. The measurement only holds while collapsed: unclamped,
 * the two heights are equal and re-measuring would take the toggle away mid-read.
 */
function MissionFold({ mission }: { mission: string }) {
  const [expanded, setExpanded] = useState(false);
  const [overflows, setOverflows] = useState(() => missionClampedGuess(mission));
  const textRef = useRef<HTMLParagraphElement>(null);
  const bodyId = useId();
  useLayoutEffect(() => {
    if (expanded) return;
    const el = textRef.current;
    if (el === null) return;
    // A sub-pixel line height leaves the two heights a fraction apart on a text that fits.
    const measure = () => setOverflows(el.scrollHeight > el.clientHeight + 1);
    measure();
    window.addEventListener("resize", measure);
    return () => window.removeEventListener("resize", measure);
  }, [expanded, mission]);
  if (mission.trim() === "") return null;
  const toggle = expanded ? S.company.overview.collapse : S.company.overview.expand;
  return (
    <div className="mt-3 max-w-3xl">
      <Text variant="eyebrow">{S.company.overview.mission}</Text>
      <div className="mt-0.5 flex items-baseline gap-3">
        <p
          ref={textRef}
          id={bodyId}
          className={`min-w-0 flex-1 text-sm text-gray-600 dark:text-gray-300 ${
            expanded ? "whitespace-pre-line" : "line-clamp-1"
          }`}
        >
          {mission}
        </p>
        {overflows && (
          <button
            type="button"
            onClick={() => setExpanded((v) => !v)}
            data-tooltip={toggle}
            aria-expanded={expanded}
            aria-controls={bodyId}
            className={`inline-flex shrink-0 items-center ${ICON_GAP.tight} rounded text-xs text-gray-500 transition-colors duration-150 hover:text-gray-800 dark:text-gray-400 dark:hover:text-gray-200`}
          >
            {toggle}
            <Chevron open={expanded} size={ICON_SIZE.chevronDense} />
          </button>
        )}
      </div>
    </div>
  );
}

/**
 * An inbox row's state dot: attention while the row needs the reader, muted once it is only
 * news. Nothing here rests on the colour alone — every row wears the chip that names its
 * category beside it.
 */
function InboxDot({ row }: { row: InboxRow }) {
  return (
    <span className="flex shrink-0 items-center">
      <span aria-hidden className={`block h-1.5 w-1.5 rounded-full ${toneDot[row.tone]}`} />
    </span>
  );
}

/** One step of the first-steps guide: its number (a check once done), title, what to do, and the button that does it. */
function StepCard({
  index,
  done,
  current,
  title,
  body,
  action,
}: {
  index: number;
  done: boolean;
  current: boolean;
  title: string;
  body: string;
  action: ReactNode;
}) {
  return (
    <li
      className={`flex flex-col gap-2 rounded-md border p-4 ${
        current ? "border-gray-300 dark:border-gray-700" : "border-gray-200 dark:border-gray-800"
      }`}
    >
      <span className={`flex items-center ${ICON_GAP.menu}`}>
        <span
          className={`flex h-6 w-6 shrink-0 items-center justify-center rounded-full text-xs font-semibold ${
            done
              ? toneSurface.success
              : current
                ? "bg-gray-900 text-white dark:bg-gray-100 dark:text-gray-900"
                : "bg-gray-100 text-gray-500 dark:bg-gray-800 dark:text-gray-400"
          }`}
        >
          {done ? <GlyphIcon d={STAT_ICONS.check} size={ICON_SIZE.inlineGlyph} /> : index}
        </span>
        <span className="text-sm font-medium">{title}</span>
      </span>
      <p className="flex-1 text-xs text-gray-500 dark:text-gray-400">{body}</p>
      <span className="flex items-center justify-between gap-2">
        {done ? (
          <span className={`text-xs ${toneInk.success}`}>{S.company.overview.stepDone}</span>
        ) : (
          <span />
        )}
        {action}
      </span>
    </li>
  );
}

export function OverviewPage() {
  const { projectId, orgId, org } = useOrg();
  const navigate = useNavigate();
  const company = useCompany();
  const { currency } = useTheme();
  const { locale } = useLocale();
  const liveStatuses = useLiveSessionStatuses();
  useDocumentTitle(org ? `${org.name} · ${S.nav.org.overview}` : S.nav.org.overview);
  const [detail, setDetail] = useState<OrganizationDetail | null>(null);
  const [chart, setChart] = useState<OrgChartResponse | null>(null);
  /** The whole calendar's size, read only while the organization is fresh (today's slice says nothing about next week). */
  const [calendarCount, setCalendarCount] = useState<number | null>(null);
  const [error, setError] = useState<string | null>(null);
  const [openingDesk, setOpeningDesk] = useState(false);
  /** The inbox chip in force; local to the visit, as a filter over one section should be. */
  const [inboxFilter, setInboxFilter] = useState<InboxFilter>("all");

  // Another organization's data must not linger while this one loads.
  useEffect(() => {
    setDetail(null);
    setChart(null);
    setCalendarCount(null);
    setError(null);
  }, [projectId, orgId]);

  const load = useCallback(async () => {
    try {
      const [d, c] = await Promise.all([
        api.getOrganization(projectId, orgId),
        api.getOrgChart(projectId, orgId),
      ]);
      setDetail(d);
      setChart(c);
      setError(null);
      if (
        firstSteps({
          employeeCount: c.employees.length,
          boardTotal: boardSummary(d.board).total,
          ceoDeskOpened: d.ceoDeskSessionId !== undefined,
          calendarCount: 0,
        }).fresh
      ) {
        // Best effort: an unreadable calendar only leaves the third step unticked.
        try {
          setCalendarCount((await api.listOrgCalendar(projectId, orgId)).events.length);
        } catch {
          setCalendarCount(null);
        }
      }
    } catch (e) {
      setError(apiErrorText(e));
    }
  }, [projectId, orgId]);

  // Every event family moves something on this page: reload on any of them.
  const { messages, tickets, runs, budget } = company.versions;
  useEffect(() => {
    void load();
  }, [load, messages, tickets, runs, budget]);

  const page = (key: CompanyNavKey, query = "") =>
    navigate(`${orgPagePath(projectId, orgId, key)}${query}`);
  /**
   * Where an inbox row leads: a ticket opens as the dialog over this page, while what was said
   * in a channel opens the channel — a conversation is a place, not a detail.
   */
  const openInboxRow = (target: InboxTarget) => {
    if (target.kind === "ticket") company.openTicket(projectId, orgId, target.ticketId);
    else navigate(orgChannelPath(projectId, orgId, target.channelId));
  };

  const openCeoDesk = async () => {
    if (chart === null || openingDesk) return;
    setOpeningDesk(true);
    try {
      const desk = await api.getOrgDesk(projectId, orgId, chart.ceoAgentId);
      navigate(`/chat/${desk.sessionId}`);
    } catch (e) {
      toastError(apiErrorText(e));
    } finally {
      setOpeningDesk(false);
    }
  };

  const title = S.nav.org.overview;
  const info = S.company.overview.info;

  if (detail === null || chart === null) {
    return (
      <OrgPage title={title} info={info}>
        {error !== null ? (
          <EmptyState
            title={error}
            action={<Button onClick={() => void load()}>{S.common.retry}</Button>}
          />
        ) : (
          <OrgPageSkeleton />
        )}
      </OrgPage>
    );
  }

  // Who is actually working: the session list's live statuses, since the chart's own `state`
  // only moves on an organization event and a run ending publishes none.
  const counts = employeeCounts(
    chart.employees,
    liveEmployeeStates(
      chart.employees,
      company.orgSessions.get(orgKey(projectId, orgId)),
      liveStatuses,
    ),
  );
  const board = boardSummary(detail.board);
  const today = todaySummary(detail.today);
  const spend = spendSummary(detail.spend);
  const steps = firstSteps({
    employeeCount: chart.employees.length,
    boardTotal: board.total,
    ceoDeskOpened: detail.ceoDeskSessionId !== undefined,
    calendarCount: calendarCount ?? 0,
  });
  const names = new Map(chart.employees.map((e) => [e.agentId, e.name]));
  const inbox = inboxRows({
    inbox: detail.inbox,
    names: (principal) => principalLabel(principal, names),
  });
  const inboxTotals = inboxCounts(inbox);
  const visibleInbox = inbox.filter((row) => inboxMatches(row, inboxFilter));
  const ceoName = names.get(chart.ceoAgentId) ?? chart.ceoAgentId;

  const deskButton = (variant: "primary" | "secondary", size: "sm" | "md") => (
    <Button
      size={size}
      variant={variant}
      disabled={openingDesk}
      onClick={() => void openCeoDesk()}
      title={`${ceoName} · ${S.company.openDesk}`}
    >
      {openingDesk ? S.company.openingDesk : S.company.overview.openCeoDesk}
    </Button>
  );

  const stepAction = (step: FirstStep, current: boolean) => {
    const variant = current ? "primary" : "secondary";
    if (step === "ceo") return deskButton(variant, "sm");
    if (step === "hire") {
      return (
        <Button size="sm" variant={variant} onClick={() => page("chart")}>
          {S.company.overview.goToChart}
        </Button>
      );
    }
    return (
      <Button size="sm" variant={variant} onClick={() => page("calendar")}>
        {S.company.overview.goToCalendar}
      </Button>
    );
  };
  const stepText: Record<FirstStep, { title: string; body: string }> = {
    ceo: { title: S.company.overview.stepCeoTitle, body: S.company.overview.stepCeoBody },
    hire: { title: S.company.overview.stepHireTitle, body: S.company.overview.stepHireBody },
    schedule: {
      title: S.company.overview.stepScheduleTitle,
      body: S.company.overview.stepScheduleBody,
    },
  };

  const spendDetail =
    spend.budget === null
      ? S.company.noBudget
      : spend.remaining !== null && spend.remaining < 0
        ? S.company.overview.overBudget(formatMoney(-spend.remaining, currency))
        : S.company.overview.budgetLeft(formatMoney(spend.remaining ?? 0, currency));

  return (
    // While the first-steps guide is on screen its step 1 IS the "open the CEO's desk"
    // button, so the header does not draw a second one.
    <OrgPage
      title={title}
      info={info}
      {...(steps.fresh ? {} : { actions: deskButton("secondary", "sm") })}
    >
      {error !== null && (
        <ErrorLine
          message={S.company.overview.refreshFailed}
          detail={error}
          onRetry={() => void load()}
          className="mb-4"
        />
      )}

      {/* Hero: name and state, the metadata line, and the mission folded to one line. The
          period's spend is not repeated here — it is the last cell of the KPI strip. */}
      <header className="min-w-0 border-b border-gray-200 pb-5 dark:border-gray-800">
        <div className="flex flex-wrap items-center gap-2">
          {/* The organization's name on the h1 rung, one outline level under the page title. */}
          <Heading level={1} as="h2">
            {detail.name}
          </Heading>
          <OrgStatusPill org={detail} />
        </div>
        <p className="mt-1.5 text-xs text-gray-500 dark:text-gray-400">
          {S.company.overview.createdBy(detail.createdBy)} ·{" "}
          {S.company.overview.employeesCount(counts.total)} ·{" "}
          {S.company.overview.period(detail.spend.period)}
        </p>
        <MissionFold mission={detail.mission} />
      </header>

      {/* KPI strip: four cells ruled by hairlines, each with the button that opens its page. */}
      <div className="mt-5 grid grid-cols-2 gap-px overflow-hidden rounded-md border border-gray-200 bg-gray-200 lg:grid-cols-4 dark:border-gray-800 dark:bg-gray-800">
        <KpiCell
          label={S.company.overview.employees}
          value={counts.total}
          jump={S.company.overview.openChart}
          onJump={() => page("chart")}
          detail={
            <>
              <ToneCount tone="success" label={S.company.overview.onDesk} value={counts.onDesk} />
              <ToneCount tone="busy" label={S.company.overview.running} value={counts.running} />
              <ToneCount tone="attention" label={S.company.overview.paused} value={counts.paused} />
            </>
          }
        />
        <KpiCell
          label={S.company.overview.board}
          value={board.open}
          jump={S.company.overview.openBoard}
          onJump={() => page("tickets")}
          detail={
            <span className="block w-full">
              <span
                className="flex h-1.5 w-full overflow-hidden rounded-full bg-gray-100 dark:bg-gray-800"
                role="img"
                aria-label={`${S.company.overview.openTickets} ${board.open} · ${S.company.overview.boardTotal(board.total)}`}
              >
                {board.segments
                  .filter((seg) => seg.count > 0)
                  .map((seg) => (
                    <span
                      key={seg.status}
                      data-tooltip={`${S.company.tickets.columns[seg.status] ?? seg.status} ${seg.count}`}
                      className={`block h-full ${BOARD_FILL[seg.status]}`}
                      style={{ width: `${seg.share * 100}%` }}
                    />
                  ))}
              </span>
              {/* The counts under the bar are the controls, not the bar and not the cell: each
                  is a small button that opens the board filtered to what it counts, and its
                  tooltip says so rather than leaving the reader to guess. */}
              <span className="mt-2 flex flex-wrap items-center gap-x-2 gap-y-1">
                {board.segments.map((seg) => (
                  <button
                    key={seg.status}
                    type="button"
                    data-tooltip={S.company.overview.openColumn(
                      S.company.tickets.columns[seg.status] ?? seg.status,
                    )}
                    // The hint says what the click opens, which the count above it cannot.
                    data-tooltip-extra
                    onClick={() => page("tickets", `?column=${seg.status}`)}
                    className={`inline-flex items-center ${ICON_GAP.tight} rounded px-1 text-xs transition-colors duration-150 hover:bg-gray-100 dark:hover:bg-gray-800`}
                  >
                    <span
                      className={`block h-1.5 w-1.5 rounded-full ${BOARD_FILL[seg.status]}`}
                      aria-hidden
                    />
                    {S.company.tickets.columns[seg.status] ?? seg.status}
                    <span className="font-semibold tabular-nums text-gray-700 dark:text-gray-200">
                      {seg.count}
                    </span>
                  </button>
                ))}
                {/* The blocked count is one more label in this row, not a pill: it is read
                    beside the five column counts and has to weigh the same as they do. */}
                <button
                  type="button"
                  data-tooltip={S.company.overview.openColumn(S.company.overview.blocked)}
                  onClick={() => page("tickets", "?blocked=1")}
                  className={`inline-flex items-center ${ICON_GAP.tight} rounded px-1 text-xs transition-colors duration-150 hover:bg-gray-100 dark:hover:bg-gray-800`}
                >
                  <span
                    className={`block h-1.5 w-1.5 rounded-full ${
                      detail.blockedTickets > 0 ? toneDot.attention : toneDot.muted
                    }`}
                    aria-hidden
                  />
                  {S.company.overview.blocked}
                  <span className="font-semibold tabular-nums text-gray-700 dark:text-gray-200">
                    {detail.blockedTickets}
                  </span>
                </button>
              </span>
            </span>
          }
        />
        <KpiCell
          label={S.company.overview.today}
          value={today.total}
          jump={S.company.overview.openCalendar}
          onJump={() => page("calendar")}
          detail={
            today.total === 0 ? (
              <span>{S.company.overview.todayEmpty}</span>
            ) : (
              <>
                {today.fired > 0 && (
                  <ToneCount tone="success" label={markLabel("fired")} value={today.fired} />
                )}
                {today.queued > 0 && (
                  <ToneCount tone="attention" label={markLabel("queued")} value={today.queued} />
                )}
                {today.failed > 0 && (
                  <ToneCount tone="danger" label={S.company.overview.failed} value={today.failed} />
                )}
                {today.paused > 0 && (
                  <ToneCount tone="muted" label={markLabel("paused")} value={today.paused} />
                )}
                <ToneCount
                  tone="attention"
                  label={S.company.overview.upcoming}
                  value={today.upcoming}
                />
              </>
            )
          }
        />
        <KpiCell
          label={S.company.overview.spend}
          value={
            <span className={`flex items-center ${ICON_GAP.card}`}>
              {/* Small: the cell is a quarter of the strip, and the ring is the picture of a
                  number already spelled out beside it. */}
              <SpendRing
                size={40}
                cost={spend.cost}
                currency={currency}
                {...(spend.budget !== null ? { budget: spend.budget } : {})}
                {...(spend.ratio !== null ? { ratio: spend.ratio } : {})}
              />
              <span className="min-w-0 truncate">{formatMoney(spend.cost, currency)}</span>
            </span>
          }
          jump={S.company.overview.openFinance}
          onJump={() => page("finance")}
          detail={
            <span className="block w-full">
              <BudgetBar
                cost={spend.cost}
                currency={currency}
                compact
                {...(spend.budget !== null ? { budget: spend.budget } : {})}
                {...(spend.ratio !== null ? { ratio: spend.ratio } : {})}
              />
              <span className="mt-1.5 block">{spendDetail}</span>
            </span>
          }
        />
      </div>

      {steps.fresh ? (
        <RuledSection
          title={S.company.overview.firstStepsTitle}
          info={S.company.overview.firstStepsInfo}
          className="mt-8"
        >
          <ol className="grid grid-cols-1 gap-4 md:grid-cols-3">
            {FIRST_STEPS.map((step, i) => (
              <StepCard
                key={step}
                index={i + 1}
                done={steps.done[step]}
                current={steps.next === step}
                title={stepText[step].title}
                body={stepText[step].body}
                action={stepAction(step, steps.next === step)}
              />
            ))}
          </ol>
        </RuledSection>
      ) : (
        <>
          {/* The inbox: everything that needs the reader, newest first. */}
          <RuledSection
            title={S.company.overview.inbox}
            info={S.company.overview.inboxInfo}
            count={inbox.length}
            className="mt-8"
            actions={
              <div role="group" aria-label={S.company.overview.inbox}>
                <Segmented
                  cols={4}
                  value={inboxFilter}
                  onChange={setInboxFilter}
                  options={INBOX_FILTERS.map((f) => ({
                    value: f,
                    label: `${S.company.overview.inboxFilters[f]} ${inboxTotals[f]}`,
                  }))}
                />
              </div>
            }
          >
            {visibleInbox.length === 0 ? (
              <OrgEmptyLine>{S.company.overview.inboxEmpty}</OrgEmptyLine>
            ) : (
              <ul className="space-y-1">
                {visibleInbox.map((row) => (
                  <li key={row.key} className={rowClass}>
                    <span
                      className={`flex w-20 shrink-0 items-center ${ICON_GAP.tight} text-xs text-gray-500 dark:text-gray-400`}
                    >
                      <GlyphIcon d={INBOX_ICON[row.category]} size={ICON_SIZE.rowLead} />
                      <span className="truncate">
                        {S.company.overview.inboxCategories[row.category]}
                      </span>
                    </span>
                    <InboxDot row={row} />
                    <TitleButton
                      className="flex-1 truncate"
                      title={
                        row.target.kind === "ticket"
                          ? S.company.overview.openTicket
                          : S.company.overview.openChannel
                      }
                      onClick={() => openInboxRow(row.target)}
                    >
                      {row.title}
                    </TitleButton>
                    {row.detail !== undefined && (
                      <span className="hidden max-w-40 shrink-0 truncate text-xs text-gray-500 sm:inline dark:text-gray-400">
                        {row.detail}
                      </span>
                    )}
                    <span
                      className="w-20 shrink-0 text-right text-xs tabular-nums text-gray-400 dark:text-gray-500"
                      {...(row.time !== null
                        ? { "data-tooltip": formatDateTime(row.time), "data-tooltip-extra": true }
                        : {})}
                    >
                      {row.time === null ? "—" : formatRelativeShort(row.time, locale)}
                    </span>
                  </li>
                ))}
              </ul>
            )}
          </RuledSection>

          {/* Today's timeline: a dot per instance on a rule, in the tone of its outcome. */}
          {/* No jump of its own: the KPI cell above carries the one button to the calendar,
              and every row's title opens it too. */}
          <RuledSection title={S.company.overview.today} count={today.total} className="mt-8">
            {today.entries.length === 0 ? (
              <OrgEmptyLine>{S.company.overview.todayEmpty}</OrgEmptyLine>
            ) : (
              <ol className="ml-1.5 border-l border-gray-200 dark:border-gray-800">
                {today.entries.slice(0, TIMELINE_ROWS).map((entry) => {
                  const tone = TIMELINE_TONE[entry.mark];
                  return (
                    <li key={entry.key} className="relative pl-4">
                      <span
                        aria-hidden
                        className={`absolute -left-1 top-3 block h-1.5 w-1.5 rounded-full ${toneDot[tone]}`}
                      />
                      <div className={`${rowClass} px-1.5`}>
                        <span className="w-11 shrink-0 font-mono text-xs tabular-nums text-gray-500 dark:text-gray-400">
                          {entry.at === null ? "—" : timeLabel(entry.at)}
                        </span>
                        <TitleButton
                          className="flex-1 truncate"
                          title={S.company.overview.openCalendar}
                          onClick={() => page("calendar")}
                        >
                          {entry.title}
                        </TitleButton>
                        <span className="hidden shrink-0 text-xs text-gray-500 sm:inline-flex dark:text-gray-400">
                          <PrincipalChip principal={agentPrincipal(entry.agentId)} names={names} />
                        </span>
                        <span className={`shrink-0 text-xs ${toneInk[tone]}`}>
                          {markLabel(entry.mark)}
                        </span>
                      </div>
                    </li>
                  );
                })}
                {today.entries.length > TIMELINE_ROWS && (
                  <li className="pl-4 pt-1 text-xs text-gray-400 dark:text-gray-500">
                    {S.company.overview.timelineMore(today.entries.length - TIMELINE_ROWS)}
                  </li>
                )}
              </ol>
            )}
          </RuledSection>

          {/* Budget alerts: who, warned or paused, when. */}
          <RuledSection
            title={S.company.overview.alerts}
            count={detail.alerts.length}
            className="mt-8"
          >
            {detail.alerts.length === 0 ? (
              <OrgEmptyLine>{S.company.overview.alertsEmpty}</OrgEmptyLine>
            ) : (
              <ul className="space-y-1">
                {detail.alerts.map((a) => (
                  <li key={`${a.agentId}/${a.period}`} className={rowClass}>
                    <TitleButton
                      className="flex-1 truncate"
                      title={S.company.overview.openFinance}
                      onClick={() => page("finance")}
                    >
                      <PrincipalChip principal={agentPrincipal(a.agentId)} names={names} />
                    </TitleButton>
                    {a.pausedAt !== undefined ? (
                      <Badge tone="danger">{S.company.finance.paused}</Badge>
                    ) : (
                      <Badge tone="attention">{S.company.finance.warned}</Badge>
                    )}
                    <span className="shrink-0 text-xs text-gray-500 dark:text-gray-400">
                      {formatRelativeShort(a.pausedAt ?? a.warnedAt ?? "", locale)}
                    </span>
                  </li>
                ))}
              </ul>
            )}
          </RuledSection>
        </>
      )}
    </OrgPage>
  );
}