import { Badge, Banner, Text, Tooltip } from "@cloudhal/ui";
import { ClockClockwise, ClockCountdown, Stop } from "@phosphor-icons/react";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { useEffect } from "react";
import { useI18n } from "../i18n/index.tsx";
import { ownModeOf } from "../lib/agents.ts";
import { type AgentKeyElevation, type AgentKeySummary, api } from "../lib/api.ts";
import type { PendingElevation } from "../lib/approvals.ts";
import {
activeElevation,
formatDuration,
formatUntil,
nextElevationEnd,
remainingLabel,
remainingMinutes,
} from "../lib/elevation.ts";
import {
DEFAULT_EXTEND_MINUTES,
type ExtendTarget,
endsWithin,
extendTargetOfKey,
} from "../lib/notifications.ts";
import { ModeBadge } from "./AgentModes.tsx";
import { AsksForBadge } from "./ElevationRequest.tsx";
import { ElevationGoneError, useEndElevation, useExtendElevation } from "./elevation-actions.ts";
import { useAgentModeRules } from "./useAgentModeRules.ts";
export { ElevationGoneError, useEndElevation, useExtendElevation };
/** Keys with an elevation in force at `now`, with that elevation. */
export function elevatedKeys(
keys: readonly AgentKeySummary[] | undefined,
now: number,
): Array<{ key: AgentKeySummary; elevation: AgentKeyElevation }> {
return (keys ?? []).flatMap((key) => {
const elevation = activeElevation({ mode: ownModeOf(key), elevation: key.elevation }, now);
return elevation ? [{ key, elevation }] : [];
});
}
/** Who granted an elevation, as the person reads it: "you" for their own grants. */
function useGrantedBy(): (actorId: string) => string {
const { t } = useI18n();
const me = useQuery({ queryKey: ["me"], queryFn: api.me, staleTime: 5 * 60_000 });
return (actorId) =>
actorId && actorId === me.data?.actor.id ? t("agents.elevation.byYou") : actorId || "—";
}
/**
* The key list's Mode cell (DESIGN.md §2g). While an elevation runs: the elevated `ModeBadge`, an
* outline timer badge "42 min left" whose tooltip says from which mode, until when, by whom and
* why, and the key's own mode underneath. Otherwise just the own mode. `now` ticks from the page.
* When the key's agent waits for an elevation it asked for, "Asks for Auto" links to Approvals.
*/
export function KeyModeCell({
agentKey,
now,
asks,
}: {
agentKey: AgentKeySummary;
now: number;
/** The agent's pending elevation request, if any (ADR-0016 addendum). */
asks?: PendingElevation | undefined;
}) {
const { t, locale } = useI18n();
const grantedBy = useGrantedBy();
const { noticeMinutes } = useAgentModeRules();
const own = ownModeOf(agentKey);
const elevation = activeElevation({ mode: own, elevation: agentKey.elevation }, now);
if (!elevation)
return asks ? (
) : (
);
const ownName = t(`agents.modes.names.${own}`);
return (
{/* One line even in a narrow table: the table scrolls sideways instead. */}
{t("agents.elevation.tooltip", {
from: ownName,
time: formatUntil(elevation.until, locale, { now }),
who: grantedBy(elevation.grantedBy),
})}
{elevation.reason && (
{agentAsked(elevation.reason)
? t("agents.elevation.requestedReason", {
reason: agentAsked(elevation.reason) ?? "",
})
: `“${elevation.reason}”`}
)}
}
>
{/* Inside the notice window the timer turns to a warning, like the bell's item. */}
}
>
{remainingLabel(t, locale, elevation.until, now)}
{t("agents.elevation.ownMode", { mode: ownName })}
{asks &&
}
);
}
/**
* A grant made from an agent's request stores the reason as `requested by the agent: …`
* (packages/api routes/approvals.ts); returns the agent's own words, or undefined otherwise.
*/
function agentAsked(reason: string): string | undefined {
const m = /^requested by the agent:\s*(.*)$/s.exec(reason);
return m ? (m[1] ?? "").trim() || "…" : undefined;
}
/** Refetches the key list the moment the first elevation runs out, so the list flips back. */
export function useElevationExpiry(keys: readonly AgentKeySummary[] | undefined) {
const qc = useQueryClient();
useEffect(() => {
const wait = nextElevationEnd(
(keys ?? []).map((k) => ({ mode: ownModeOf(k), elevation: k.elevation })),
);
if (wait === undefined) return;
// A second after the end, so the server no longer reports it; capped to setTimeout's range.
const id = setTimeout(
() => qc.invalidateQueries({ queryKey: ["agent-keys"] }),
Math.min(wait + 1000, 2_147_483_647),
);
return () => clearTimeout(id);
}, [keys, qc]);
}
/**
* Agents page summary while any key is elevated: "1 agent key is elevated", one line per key
* ("Desk: Edits → Auto until 14:30") and "End now". Alert tone when one of them is in Auto. Keys
* inside the notice window (ADR-0016 addendum) carry an "Ends in 8 min" badge, and the banner
* offers "Extend 1 hour" for them next to "End now" (`useExtendElevation`, like the bell).
*/
export function ElevatedKeysBanner({
keys,
now,
noticeMinutes,
onEnd,
ending,
onExtend,
extending,
}: {
keys: readonly AgentKeySummary[] | undefined;
now: number;
/** Minutes before its end that a key counts as ending soon. */
noticeMinutes: number;
onEnd: (keys: AgentKeySummary[]) => void;
ending: boolean;
onExtend: (targets: ExtendTarget[]) => void;
extending: boolean;
}) {
const { t, locale } = useI18n();
const elevated = elevatedKeys(keys, now);
if (elevated.length === 0) return null;
const count = elevated.length;
const endingSoon = elevated.flatMap(({ key, elevation }) => {
const target = endsWithin(elevation.until, noticeMinutes, now)
? extendTargetOfKey(key, now)
: undefined;
return target ? [target] : [];
});
const extendBy = t(`notifications.extend.presets.${DEFAULT_EXTEND_MINUTES}`);
return (
e.elevation.mode === "auto") ? "alert" : "default"}
// Two actions do not fit next to the text on a phone: let them wrap under it.
className="[&>div]:flex-wrap"
icon={}
title={t("agents.elevation.bannerTitle", { count })}
description={
{elevated.map(({ key, elevation }) => (
{t("agents.elevation.bannerItem", {
name: key.name ?? t("agents.unnamed"),
from: t(`agents.modes.names.${ownModeOf(key)}`),
to: t(`agents.modes.names.${elevation.mode}`),
time: formatUntil(elevation.until, locale, { now }),
})}
{endingSoon.some((e) => e.keyId === key.id) && (
}>
{t("agents.elevation.endsIn", {
time: formatDuration(
Math.max(1, remainingMinutes(elevation.until, now)),
locale,
"short",
),
})}
)}
))}
}
action={
<>
{endingSoon.length > 0 && (
}
loading={extending}
onClick={() => onExtend(endingSoon)}
>
{endingSoon.length === 1 // i18n-ignore: one key's action vs "all", not a form
? t("notifications.extend.button", { duration: extendBy })
: t("agents.elevation.extendAll", { duration: extendBy })}
)}
}
loading={ending}
onClick={() => onEnd(elevated.map((e) => e.key))}
>
{
// i18n-ignore: ending one key vs all keys, not a grammatical form
count === 1 ? t("agents.elevation.endNow") : t("agents.elevation.endAll")
}
>
}
/>
);
}