fix(ui): count what needs a person from served reads (#2145)

The shell's attention badge counted the task board's summary, which the binary
does not serve, so #2121 removed the badge with the board and nothing said, on
every page, how much work waits on a person.

The count now comes from the two operations that answer it: pending approval
requests and parked runs. The rule lives in ui/src/lib/needs-attention.ts, so
the dashboard's list and the shell's badge agree on what waiting means, and the
count is read through a small store so both read it once.
This commit is contained in:
sam committed 2026-11-27 19:37:46 +11:00
1 parent 77e291fdfc
commit 61d486fdc0
5 files changed
+82 -2

No files matched your search

@@ -10,13 +10,20 @@ import {
DropdownMenuItem, DropdownMenuItem,
DropdownMenuTrigger, DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"; } from "@/components/ui/dropdown-menu";
import { Badge } from "@/components/ui/badge";
import { activeNavPath, navTree, type NavEntry } from "@/lib/nav"; import { activeNavPath, navTree, type NavEntry } from "@/lib/nav";
import { navIcons } from "@/lib/nav-icons"; import { navIcons } from "@/lib/nav-icons";
import { useAttentionStore } from "@/stores/attention";
import { useLiveResource } from "@/stores/session";
import { useHoverOpen } from "./hover-open"; import { useHoverOpen } from "./hover-open";
const props = defineProps<{ hidden: string[]; link?: string }>(); const props = defineProps<{ hidden: string[]; link?: string }>();
const route = useRoute(); 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(() => const links = computed(() =>
navTree(props.hidden).flatMap((node): NavEntry[] => (node.kind === "link" ? [node.entry] : node.items)), navTree(props.hidden).flatMap((node): NavEntry[] => (node.kind === "link" ? [node.entry] : node.items)),
); );
@@ -40,6 +47,7 @@ const hover = useHoverOpen();
<DropdownMenuTrigger v-else as-child> <DropdownMenuTrigger v-else as-child>
<Button variant="ghost" size="sm" class="gap-1 px-2" aria-label="Sections" @pointerenter="hover.enterTrigger" @pointerleave="hover.leaveTrigger"> <Button variant="ghost" size="sm" class="gap-1 px-2" aria-label="Sections" @pointerenter="hover.enterTrigger" @pointerleave="hover.leaveTrigger">
<span>{{ current?.label ?? route.meta.label ?? "Sections" }}</span> <span>{{ current?.label ?? route.meta.label ?? "Sections" }}</span>
<Badge v-if="activePath !== '/' && attention.count" variant="warn" class="tabular-nums" :aria-label="`${attention.count} need attention`">{{ attention.count }}</Badge>
<ChevronDown class="size-4" aria-hidden="true" /> <ChevronDown class="size-4" aria-hidden="true" />
</Button> </Button>
</DropdownMenuTrigger> </DropdownMenuTrigger>
@@ -48,6 +56,7 @@ const hover = useHoverOpen();
<RouterLink :to="entry.path" :aria-current="entry.path === activePath ? 'page' : undefined"> <RouterLink :to="entry.path" :aria-current="entry.path === activePath ? 'page' : undefined">
<component :is="navIcons[entry.path]" v-if="navIcons[entry.path]" aria-hidden="true" /> <component :is="navIcons[entry.path]" v-if="navIcons[entry.path]" aria-hidden="true" />
{{ entry.label }} {{ entry.label }}
<Badge v-if="entry.path === '/' && attention.count" variant="warn" class="ml-auto tabular-nums" :aria-label="`${attention.count} need attention`">{{ attention.count }}</Badge>
</RouterLink> </RouterLink>
</DropdownMenuItem> </DropdownMenuItem>
</DropdownMenuContent> </DropdownMenuContent>
+3 -2
View File
@@ -10,6 +10,7 @@ import { Section } from "@/components/ui/section";
import { Timestamp } from "@/components/ui/timestamp"; import { Timestamp } from "@/components/ui/timestamp";
import { api } from "@/lib/api"; import { api } from "@/lib/api";
import { usePoll, useRead } from "@/lib/use-read"; import { usePoll, useRead } from "@/lib/use-read";
import { waitsOnPerson } from "@/lib/needs-attention";
import { useLiveResource } from "@/stores/session"; import { useLiveResource } from "@/stores/session";
/** What waits on a person: approval requests, and runs a step parked. Each /** What waits on a person: approval requests, and runs a step parked. Each
@@ -35,7 +36,7 @@ interface Waiting {
const waiting = computed<Waiting[]>(() => [ const waiting = computed<Waiting[]>(() => [
...(approvals.data.value ?? []) ...(approvals.data.value ?? [])
.filter((a) => a.state === "pending") .filter((a) => waitsOnPerson(a, undefined))
.map((a) => ({ .map((a) => ({
key: `approval:${a.id}`, key: `approval:${a.id}`,
badge: "Approval", badge: "Approval",
@@ -45,7 +46,7 @@ const waiting = computed<Waiting[]>(() => [
at: a.requested_at, at: a.requested_at,
})), })),
...(runs.data.value ?? []) ...(runs.data.value ?? [])
.filter((r) => r.state === "parked") .filter((r) => waitsOnPerson(undefined, r))
.map((r) => ({ .map((r) => ({
key: `run:${r.id}`, key: `run:${r.id}`,
badge: "Parked", badge: "Parked",
+23
View File
@@ -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<Approval, "state">, run?: Pick<Run, "state">): boolean {
return approval?.state === "pending" || run?.state === "parked";
}
/** needsAttention counts what waits: pending approvals plus parked runs. */
export function needsAttention(
approvals: Pick<Approval, "state">[] | undefined,
runs: Pick<Run, "state">[] | undefined,
): number {
const pending = (approvals ?? []).filter((a) => waitsOnPerson(a, undefined)).length;
const parked = (runs ?? []).filter((r) => waitsOnPerson(undefined, r)).length;
return pending + parked;
}
+22
View File
@@ -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<void> {
// 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 };
});
+25
View File
@@ -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);
});