fix(ui): stage history restore as a draft applied from the save bar

This commit is contained in:
sam committed 2026-10-11 11:35:02 +11:00
1 parent 1adde88512
commit c1f358e87d
4 files changed
+86 -67

No files matched your search

+13 -4
View File
@@ -20,8 +20,8 @@ import { filterHistory, historyEmptyTitle } from "./history";
/**
* Every settings change, one row per changed field, newest first. Restore
* puts a resource back to the version before a change, as an ordinary
* replace, so the restore is itself recorded here.
* stages the version before a change as a draft; applying it from the save
* bar is an ordinary replace, so the restore is itself recorded here.
*/
const store = useControlPlaneStore();
const { catalog } = storeToRefs(store);
@@ -30,6 +30,7 @@ const router = useRouter();
const entries = ref<AuditEntry[]>([]);
const error = ref("");
const restoreError = ref("");
const expanded = ref<number | null>(null);
const titles = computed(() => new Map(catalog.value.map((r) => [r.kind, r.title])));
@@ -98,8 +99,15 @@ async function restoreBefore(entry: AuditEntry): Promise<void> {
const previous = (await store.history(entry.record_key)).find(
(revision) => revision.version === entry.version - 1,
);
if (previous) await store.replace(entry.record_key, previous.value);
if (!previous) {
restoreError.value = "That earlier version is no longer available.";
return;
}
restoreError.value = "";
store.stageRestore(entry.record_key, previous.value);
}
const restorable = (entry: AuditEntry) =>
entry.version > 1 && store.genericResources.some((r) => r.kind === entry.record_key);
</script>
<template>
@@ -134,6 +142,7 @@ async function restoreBefore(entry: AuditEntry): Promise<void> {
</Select>
</div>
<p v-if="restoreError" class="mb-2 text-sm text-danger" role="alert">{{ restoreError }}</p>
<p v-if="error" class="text-sm text-danger" role="alert">{{ error }}</p>
<div v-else class="overflow-x-auto rounded-lg border border-border bg-card">
<table class="w-full text-[13px]">
@@ -180,7 +189,7 @@ async function restoreBefore(entry: AuditEntry): Promise<void> {
</td>
<td class="px-2 py-1.5 text-right">
<Button
v-if="entry.version > 1"
v-if="restorable(entry)"
variant="ghost"
size="sm"
:disabled="store.stateFor(entry.record_key).saving"
+10
View File
@@ -334,6 +334,15 @@ export const useControlPlaneStore = defineStore("control-plane", () => {
drafts[kind] = { base: resource, value: cloneControlPlaneValue(resource.value) };
}
/** Loads an earlier version into the draft and opens the review, so a restore
* is applied the same deliberate way an edit is. */
function stageRestore(kind: string, value: unknown): void {
const resource = stateFor(kind).resource;
if (!resource) return;
drafts[kind] = { base: resource, value: cloneControlPlaneValue(value) };
reviewing.value = true;
}
// The diff for every draft, computed once per change. The save bar, the
// inline "was old" hint, the inline errors and the route guard all read
// this one map, so no consumer forks its own notion of what is dirty.
@@ -559,6 +568,7 @@ export const useControlPlaneStore = defineStore("control-plane", () => {
issuesFor,
drafts,
resetDraft,
stageRestore,
saveDrafts,
catalog,
catalogError,