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.
This commit is contained in:
2 files changed
+55
-38
No files matched your search
@@ -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 {
|
||||
</PopoverTrigger>
|
||||
<PopoverContent align="end" :collision-padding="8" class="w-[calc(100vw-1rem)] max-w-none p-0 sm:w-96">
|
||||
<Alert v-if="failed" variant="destructive" class="px-3 py-6 text-center"><AlertDescription>Alerts could not be loaded.</AlertDescription></Alert>
|
||||
<Alert v-if="dismissError" variant="destructive" class="px-3 py-2"><AlertDescription>Dismiss failed: {{ dismissError }}</AlertDescription></Alert>
|
||||
<Empty v-else-if="!groups.length" class="border-0"><EmptyHeader><EmptyTitle>No alerts</EmptyTitle></EmptyHeader></Empty>
|
||||
<div v-else class="relative max-h-[calc(100dvh-6rem)] overflow-y-auto">
|
||||
<Tooltip>
|
||||
|
||||
+53
-37
@@ -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<OperatorAlert[]>([]);
|
||||
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<OperatorAlert[]>([]);
|
||||
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 };
|
||||
}
|
||||
Reference in new issue
Block a user