From 9ca9545847effdd90418b01caae8b19b7c2872f6 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 7 Nov 2026 00:19:31 +1100 Subject: [PATCH] fix(ui): alert state is shared; a failed dismiss keeps the alert and says why (#1747) use-alerts.ts now holds the alerts/failed refs at module scope (wired once), so the bell and the dashboard card see the same list. dismiss removes the alert only after the server confirms; on failure it stays and the reason shows in the bell. dismissAll reports any rejection and reloads. --- ui/src/alerts/AlertsBell.vue | 3 +- ui/src/alerts/use-alerts.ts | 90 +++++++++++++++++++++--------------- 2 files changed, 55 insertions(+), 38 deletions(-) diff --git a/ui/src/alerts/AlertsBell.vue b/ui/src/alerts/AlertsBell.vue index 91df947ab..c791f9c3d 100644 --- a/ui/src/alerts/AlertsBell.vue +++ b/ui/src/alerts/AlertsBell.vue @@ -13,7 +13,7 @@ import { Timestamp } from "@/components/ui/timestamp"; import { groupBySeverity, type AlertSeverity, type OperatorAlert } from "./alerts"; import { useAlerts } from "./use-alerts"; -const { alerts, failed, dismiss, dismissAll } = useAlerts(); +const { alerts, failed, dismissError, dismiss, dismissAll } = useAlerts(); const open = ref(false); const groups = computed(() => groupBySeverity(alerts.value)); @@ -43,6 +43,7 @@ function failedDeliveries(alert: OperatorAlert): number { Alerts could not be loaded. + Dismiss failed: {{ dismissError }} No alerts
diff --git a/ui/src/alerts/use-alerts.ts b/ui/src/alerts/use-alerts.ts index 2ca15d918..354da0bc6 100644 --- a/ui/src/alerts/use-alerts.ts +++ b/ui/src/alerts/use-alerts.ts @@ -6,50 +6,66 @@ import { useSessionStore } from "@/stores/session"; import type { OperatorAlert } from "./alerts"; /** - * The open alerts, following the event stream: the store records every alert - * change as an event, so the newest alert event's id moving means the list - * moved. A reconnect re-reads, since events may have been missed. + * The open alerts, shared by every caller (the bell and the dashboard card + * must not hold separate copies): the refs live at module scope, like + * captures/state.ts, and the stream wiring is registered once. The store + * records every alert change as an event, so the newest alert event's id + * moving means the list moved. A reconnect re-reads, since events may have + * been missed. */ -export function useAlerts() { - const alerts = ref([]); - const failed = ref(false); - // Loads overlap when alert events arrive close together; only the newest - // may land, or an older list overwrites a newer one. - let latest = 0; +const alerts = ref([]); +const failed = ref(false); +const dismissError = ref(""); +// Loads overlap when alert events arrive close together; only the newest +// may land, or an older list overwrites a newer one. +let latest = 0; +let wired = false; - async function load() { - const seq = ++latest; - try { - const next = (await api.alerts<{ alerts: OperatorAlert[] }>()).alerts; - if (seq !== latest) return; - alerts.value = next; - failed.value = false; - } catch { - if (seq === latest) failed.value = true; - } +async function load() { + const seq = ++latest; + try { + const next = (await api.alerts<{ alerts: OperatorAlert[] }>()).alerts; + if (seq !== latest) return; + alerts.value = next; + failed.value = false; + } catch { + if (seq === latest) failed.value = true; } +} - async function dismiss(alert: OperatorAlert) { - latest++; +async function dismiss(alert: OperatorAlert) { + latest++; + dismissError.value = ""; + try { + await api.dismissAlert(alert.id); alerts.value = alerts.value.filter((a) => a.id !== alert.id); - try { - await api.dismissAlert(alert.id); - } catch { - await load(); - } + } catch (err) { + // The dismiss failed: the alert stays and the reason is shown. + dismissError.value = String((err as Error).message || err); + await load(); } +} - async function dismissAll() { - const ids = alerts.value.map((a) => a.id); - alerts.value = []; - const results = await Promise.allSettled(ids.map((id) => api.dismissAlert(id))); - if (results.some((r) => r.status === "rejected")) await load(); +async function dismissAll() { + const ids = alerts.value.map((a) => a.id); + dismissError.value = ""; + const results = await Promise.allSettled(ids.map((id) => api.dismissAlert(id))); + const rejected = results.find((r) => r.status === "rejected"); + if (rejected && rejected.status === "rejected") { + dismissError.value = String((rejected.reason as Error)?.message || rejected.reason); + await load(); + return; } + alerts.value = []; +} - const { activity, connectionRevision } = storeToRefs(useSessionStore()); - watch(() => activity.value.find((event) => event.kind === "alert")?.id, load); - watch(connectionRevision, load); - onMounted(load); - - return { alerts, failed, dismiss, dismissAll }; +export function useAlerts() { + if (!wired) { + wired = true; + const { activity, connectionRevision } = storeToRefs(useSessionStore()); + watch(() => activity.value.find((event) => event.kind === "alert")?.id, load); + watch(connectionRevision, load); + onMounted(load); + } + return { alerts, failed, dismissError, dismiss, dismissAll }; }