diff --git a/ui/src/components/topbar/SectionCrumb.vue b/ui/src/components/topbar/SectionCrumb.vue index 1f862b434..070521111 100644 --- a/ui/src/components/topbar/SectionCrumb.vue +++ b/ui/src/components/topbar/SectionCrumb.vue @@ -10,13 +10,20 @@ import { DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; +import { Badge } from "@/components/ui/badge"; import { activeNavPath, navTree, type NavEntry } from "@/lib/nav"; import { navIcons } from "@/lib/nav-icons"; +import { useAttentionStore } from "@/stores/attention"; +import { useLiveResource } from "@/stores/session"; import { useHoverOpen } from "./hover-open"; const props = defineProps<{ hidden: string[]; link?: string }>(); const route = useRoute(); +// The badge counts what waits on a person on every page, not only the dashboard. +const attention = useAttentionStore(); +void attention.load(); +useLiveResource(null, () => void attention.load(), 5000); const links = computed(() => navTree(props.hidden).flatMap((node): NavEntry[] => (node.kind === "link" ? [node.entry] : node.items)), ); @@ -40,6 +47,7 @@ const hover = useHoverOpen(); @@ -48,6 +56,7 @@ const hover = useHoverOpen(); diff --git a/ui/src/dashboard/NeedsAttentionCard.vue b/ui/src/dashboard/NeedsAttentionCard.vue index e07ab1bc0..2599a2b1f 100644 --- a/ui/src/dashboard/NeedsAttentionCard.vue +++ b/ui/src/dashboard/NeedsAttentionCard.vue @@ -10,6 +10,7 @@ import { Section } from "@/components/ui/section"; import { Timestamp } from "@/components/ui/timestamp"; import { api } from "@/lib/api"; import { usePoll, useRead } from "@/lib/use-read"; +import { waitsOnPerson } from "@/lib/needs-attention"; import { useLiveResource } from "@/stores/session"; /** What waits on a person: approval requests, and runs a step parked. Each @@ -35,7 +36,7 @@ interface Waiting { const waiting = computed(() => [ ...(approvals.data.value ?? []) - .filter((a) => a.state === "pending") + .filter((a) => waitsOnPerson(a, undefined)) .map((a) => ({ key: `approval:${a.id}`, badge: "Approval", @@ -45,7 +46,7 @@ const waiting = computed(() => [ at: a.requested_at, })), ...(runs.data.value ?? []) - .filter((r) => r.state === "parked") + .filter((r) => waitsOnPerson(undefined, r)) .map((r) => ({ key: `run:${r.id}`, badge: "Parked", diff --git a/ui/src/lib/needs-attention.ts b/ui/src/lib/needs-attention.ts new file mode 100644 index 000000000..5001ebe81 --- /dev/null +++ b/ui/src/lib/needs-attention.ts @@ -0,0 +1,23 @@ +/** + * What waits on a person. + * + * Two served reads answer it: an approval request that is still pending, and a + * run a step parked. The shell counts them and the dashboard lists them, so the + * rule lives here rather than in either. + */ +import type { Approval, Run } from "./api"; + +/** waitsOnPerson reports whether one read answered something that waits. */ +export function waitsOnPerson(approval?: Pick, run?: Pick): boolean { + return approval?.state === "pending" || run?.state === "parked"; +} + +/** needsAttention counts what waits: pending approvals plus parked runs. */ +export function needsAttention( + approvals: Pick[] | undefined, + runs: Pick[] | undefined, +): number { + const pending = (approvals ?? []).filter((a) => waitsOnPerson(a, undefined)).length; + const parked = (runs ?? []).filter((r) => waitsOnPerson(undefined, r)).length; + return pending + parked; +} diff --git a/ui/src/stores/attention.ts b/ui/src/stores/attention.ts new file mode 100644 index 000000000..56eebed6a --- /dev/null +++ b/ui/src/stores/attention.ts @@ -0,0 +1,22 @@ +import { defineStore } from "pinia"; +import { ref } from "vue"; + +import { api } from "../lib/api.ts"; +import { needsAttention } from "../lib/needs-attention.ts"; + +/** How much waits on a person, as the shell badges it on every page. Read from + * the two operations that answer it, so a page shows the count without loading + * the dashboard. */ +export const useAttentionStore = defineStore("attention", () => { + const count = ref(0); + + async function load(): Promise { + // A failed read keeps the last count: the badge is a hint, and a blink to + // zero would read as nothing waiting. + const [approvals, runs] = await Promise.all([api.approvals().catch(() => undefined), api.runs().catch(() => undefined)]); + if (approvals === undefined && runs === undefined) return; + count.value = needsAttention(approvals ?? [], runs ?? []); + } + + return { count, load }; +}); diff --git a/ui/test/needs-attention.test.ts b/ui/test/needs-attention.test.ts new file mode 100644 index 000000000..5a5bee4d6 --- /dev/null +++ b/ui/test/needs-attention.test.ts @@ -0,0 +1,25 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +import { needsAttention, waitsOnPerson } from "../src/lib/needs-attention.ts"; + +// Guards #2145: the shell used to count what needs a person from the task +// board's summary, which the binary does not serve, so the count went with the +// board. It now counts the two things the binary does serve. +test("what waits on a person is a pending approval or a parked run", () => { + const approval = (state: string) => ({ state }) as never; + const run = (state: string) => ({ state }) as never; + assert.equal(waitsOnPerson(approval("pending"), undefined), true); + assert.equal(waitsOnPerson(approval("approved"), undefined), false); + assert.equal(waitsOnPerson(approval("refused"), undefined), false); + assert.equal(waitsOnPerson(undefined, run("parked")), true); + assert.equal(waitsOnPerson(undefined, run("running")), false); +}); + +test("the count adds approvals and runs, and reads nothing as zero", () => { + const approvals = [{ state: "pending" }, { state: "pending" }, { state: "approved" }] as never[]; + const runs = [{ state: "parked" }, { state: "running" }, { state: "succeeded" }] as never[]; + assert.equal(needsAttention(approvals, runs), 3); + assert.equal(needsAttention([], []), 0); + assert.equal(needsAttention(undefined, undefined), 0); +});