feat(ui): unified audit page with server filters and live tail
This commit is contained in:
1 parent
2caab17461
commit
2ce773f5a5
19 files changed
+1519
-424
No files matched your search
@@ -3,13 +3,13 @@ import { History } from "@lucide/vue";
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
|
||||
/** Opens Settings > History filtered to these resource kinds. */
|
||||
/** Opens Settings > Audit filtered to these resource kinds. */
|
||||
const props = defineProps<{ kinds: string[] }>();
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Button variant="ghost" size="sm" as-child>
|
||||
<RouterLink :to="{ path: '/settings/history', query: { section: props.kinds.join(',') } }">
|
||||
<History data-icon="inline-start" /> History</RouterLink>
|
||||
<RouterLink :to="{ path: '/settings/audit', query: { section: props.kinds.join(',') } }">
|
||||
<History data-icon="inline-start" /> Audit</RouterLink>
|
||||
</Button>
|
||||
</template>
|
||||
@@ -1,207 +0,0 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref, watch } from "vue";
|
||||
import { useRoute, useRouter } from "vue-router";
|
||||
import { storeToRefs } from "pinia";
|
||||
|
||||
import PageHeader from "@/base/PageHeader.vue";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Section } from "@/components/ui/section";
|
||||
import { Timestamp } from "@/components/ui/timestamp";
|
||||
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
|
||||
import { useSettingsStore, type AuditEntry } from "@/stores/settings";
|
||||
import { formatValue } from "@/lib/changes";
|
||||
import { filterHistory, historyEmptyTitle } from "./history";
|
||||
|
||||
/**
|
||||
* Every settings change, one row per changed field, newest first. Restore
|
||||
* 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 = useSettingsStore();
|
||||
const { catalog } = storeToRefs(store);
|
||||
const route = useRoute();
|
||||
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])));
|
||||
const kinds = computed(() => catalog.value.map((r) => r.kind));
|
||||
|
||||
async function load(): Promise<void> {
|
||||
if (!kinds.value.length) return;
|
||||
try {
|
||||
entries.value = await store.audit(kinds.value);
|
||||
error.value = "";
|
||||
} catch (err) {
|
||||
error.value = String((err as Error).message || err);
|
||||
}
|
||||
}
|
||||
onMounted(store.load);
|
||||
// Any stored version moving means a save happened somewhere: re-read.
|
||||
const versions = computed(() =>
|
||||
kinds.value.map((kind) => store.stateFor(kind).resource?.version).join(","),
|
||||
);
|
||||
watch([kinds, versions], load, { immediate: true });
|
||||
|
||||
// Filters live in the query, so every History link elsewhere is a deep link.
|
||||
const ALL = "all";
|
||||
const section = computed({
|
||||
get: () => (route.query.section as string) || ALL,
|
||||
set: (value: string) => setQuery({ section: value === ALL ? undefined : value }),
|
||||
});
|
||||
const actor = computed({
|
||||
get: () => (route.query.actor as string) || ALL,
|
||||
set: (value: string) => setQuery({ actor: value === ALL ? undefined : value }),
|
||||
});
|
||||
const since = computed({
|
||||
get: () => (route.query.since as string) || ALL,
|
||||
set: (value: string) => setQuery({ since: value === ALL ? undefined : value }),
|
||||
});
|
||||
function setQuery(patch: Record<string, string | undefined>) {
|
||||
void router.replace({ query: { ...route.query, ...patch } });
|
||||
}
|
||||
|
||||
const windows: Record<string, number> = { "24h": 86_400_000, "7d": 604_800_000, "30d": 2_592_000_000 };
|
||||
const actorName = (id: string) => entries.value.find((entry) => entry.actor === id)?.actor_name || id;
|
||||
const actors = computed(() => [...new Set(entries.value.map((e) => e.actor))].sort());
|
||||
const sectionOptions = computed(() => {
|
||||
const options = catalog.value.map((r) => ({ value: r.kind, label: r.title }));
|
||||
// A deep link may name several kinds (a page that shows more than one).
|
||||
if (section.value !== ALL && !options.some((o) => o.value === section.value))
|
||||
options.unshift({
|
||||
value: section.value,
|
||||
label: section.value.split(",").map((k) => titles.value.get(k) ?? k).join(" + "),
|
||||
});
|
||||
return options;
|
||||
});
|
||||
|
||||
const rows = computed(() =>
|
||||
filterHistory(
|
||||
entries.value,
|
||||
{
|
||||
kinds: section.value === ALL ? [] : section.value.split(","),
|
||||
actor: actor.value === ALL ? undefined : actor.value,
|
||||
sinceMs: windows[since.value],
|
||||
},
|
||||
Date.now(),
|
||||
),
|
||||
);
|
||||
|
||||
async function restoreBefore(entry: AuditEntry): Promise<void> {
|
||||
const previous = (await store.history(entry.record_key)).find(
|
||||
(revision) => revision.version === entry.version - 1,
|
||||
);
|
||||
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>
|
||||
<div>
|
||||
<PageHeader title="History" />
|
||||
|
||||
<div class="mb-4 flex flex-wrap gap-2">
|
||||
<Select v-model="section">
|
||||
<SelectTrigger class="w-60" aria-label="Section">
|
||||
<SelectValue>{{ section === ALL ? "All sections" : sectionOptions.find((o) => o.value === section)?.label ?? section }}</SelectValue>
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem :value="ALL">All sections</SelectItem>
|
||||
<SelectItem v-for="o in sectionOptions" :key="o.value" :value="o.value">{{ o.label }}</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<Select v-model="actor">
|
||||
<SelectTrigger class="w-48" aria-label="Changed by"><SelectValue /></SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem :value="ALL">Anyone</SelectItem>
|
||||
<SelectItem v-for="a in actors" :key="a" :value="a">{{ actorName(a) }}</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<Select v-model="since">
|
||||
<SelectTrigger class="w-40" aria-label="When"><SelectValue /></SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem :value="ALL">Any time</SelectItem>
|
||||
<SelectItem value="24h">Last 24 hours</SelectItem>
|
||||
<SelectItem value="7d">Last 7 days</SelectItem>
|
||||
<SelectItem value="30d">Last 30 days</SelectItem>
|
||||
</SelectContent>
|
||||
</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>
|
||||
<Section v-else>
|
||||
<Table class="table-fixed">
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead class="w-20">When</TableHead>
|
||||
<TableHead class="w-36">Section</TableHead>
|
||||
<TableHead class="w-24">Field</TableHead>
|
||||
<TableHead>Change</TableHead>
|
||||
<TableHead class="w-14">Version</TableHead>
|
||||
<TableHead class="w-32">Changed by</TableHead>
|
||||
<TableHead class="w-20"><span class="sr-only">Actions</span></TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
<TableRow v-for="entry in rows" :key="entry.id" class="align-top">
|
||||
<TableCell class="text-fg-muted"><Timestamp :at="entry.at" /></TableCell>
|
||||
<TableCell class="truncate">{{ titles.get(entry.record_key) ?? entry.record_key }}</TableCell>
|
||||
<TableCell class="truncate font-mono text-xs" :title="entry.field">{{ entry.field || "whole value" }}</TableCell>
|
||||
<TableCell class="font-mono text-xs whitespace-normal">
|
||||
<button
|
||||
type="button"
|
||||
class="block w-full min-w-0 text-left"
|
||||
:aria-expanded="expanded === entry.id"
|
||||
@click="expanded = expanded === entry.id ? null : entry.id"
|
||||
>
|
||||
<span :class="expanded === entry.id ? 'break-all' : 'truncate'" class="block text-danger">− {{ formatValue(entry.old_value ?? undefined) }}</span>
|
||||
<span :class="expanded === entry.id ? 'break-all' : 'truncate'" class="block text-ok">+ {{ formatValue(entry.new_value ?? undefined) }}</span>
|
||||
</button>
|
||||
</TableCell>
|
||||
<TableCell class="font-mono text-xs">{{ entry.version }}</TableCell>
|
||||
<TableCell class="truncate" :title="`${entry.actor} · ${entry.source}`">
|
||||
{{ entry.actor_name || entry.actor }}<span class="text-fg-subtle"> · {{ entry.source }}</span>
|
||||
</TableCell>
|
||||
<TableCell class="py-1 text-right">
|
||||
<Button
|
||||
v-if="restorable(entry)"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
:disabled="store.stateFor(entry.record_key).saving"
|
||||
@click="restoreBefore(entry)"
|
||||
>Restore</Button
|
||||
>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
<TableRow v-if="!rows.length">
|
||||
<TableCell colspan="7">
|
||||
<Empty>
|
||||
<EmptyHeader>
|
||||
<EmptyTitle>{{ historyEmptyTitle(entries.length > 0) }}</EmptyTitle>
|
||||
</EmptyHeader>
|
||||
</Empty>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
</TableBody>
|
||||
</Table>
|
||||
</Section>
|
||||
</div>
|
||||
</template>
|
||||
@@ -1,39 +0,0 @@
|
||||
interface HistoryEntry {
|
||||
record_key: string;
|
||||
actor: string;
|
||||
at?: string;
|
||||
}
|
||||
|
||||
export interface HistoryFilter {
|
||||
/** Resource kinds to keep; empty or absent keeps every kind. */
|
||||
kinds?: string[];
|
||||
actor?: string;
|
||||
/** Keep entries newer than this many milliseconds before now. */
|
||||
sinceMs?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* historyEmptyTitle is what the history table's nothing-to-show row says: a
|
||||
* history with entries whose filters match nothing asks the reader to change
|
||||
* the filters, while a history with nothing recorded at all says so.
|
||||
*/
|
||||
export function historyEmptyTitle(entriesRecorded: boolean): string {
|
||||
return entriesRecorded ? "No matching changes." : "No changes recorded yet.";
|
||||
}
|
||||
|
||||
/** filterHistory keeps the audit entries that match every filter set. */
|
||||
export function filterHistory<T extends HistoryEntry>(
|
||||
entries: T[],
|
||||
filter: HistoryFilter,
|
||||
now: number,
|
||||
): T[] {
|
||||
return entries.filter((entry) => {
|
||||
if (filter.kinds?.length && !filter.kinds.includes(entry.record_key)) return false;
|
||||
if (filter.actor && entry.actor !== filter.actor) return false;
|
||||
if (filter.sinceMs !== undefined) {
|
||||
if (!entry.at) return false;
|
||||
if (now - Date.parse(entry.at) > filter.sinceMs) return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user