Files
FabledScribe/frontend/src/components/VersionHistorySection.vue
T
bvandeusenandClaude Opus 5 a8b2040216
CI & Build / Python lint (push) Successful in 5s
CI & Build / Plugin hooks (push) Successful in 9s
CI & Build / TypeScript typecheck (push) Successful in 34s
CI & Build / integration (push) Successful in 35s
CI & Build / Python tests (push) Successful in 1m20s
CI & Build / Build & push image (push) Successful in 40s
feat(rules): the edit history is visible in the slide-over (#3243, milestone 323 step 4)
Rule 27: a history nobody can read is not shipped. `RuleHistoryPanel.vue`
sits below the fields in `RuleEditorSlideOver`, where a rule is read in full
— not on the list row, where a history entry point would compete with the
row's job.

REUSE, DECIDED FIELD BY FIELD RATHER THAN ALL AT ONCE.

DiffView.vue is reused unchanged: it takes DiffLine[] and nothing
note-shaped.

HistoryPanel.vue is NOT, and its props are the reason — noteId +
currentBody, a NoteVersion carrying tags and pin columns, a fetch of
/api/notes/…, a restore emit, pin/unpin buttons. Rules have no tags, no
pins, and deliberately no restore, and a rule's text is EIGHT fields rather
than one body, which changes the reader's question from "what changed" to
"which fields moved". Recorded here rather than forked silently, per #3207.

THE FORK THAT WAS ALREADY THERE. The LCS walk existed three times —
privately in useAssist.ts, and again inside HistoryPanel.vue and
VersionHistorySection.vue — character-identical apart from quote style,
because computeDiff was never exported. Rather than add a fourth copy, it
moves to utils/diff.ts and the three become imports; the extraction was
verified equivalent to all three before anything was deleted. DiffLine is
re-exported from useAssist so its existing importers are untouched.

WHAT A ROW SHOWS: when, and which fields moved. A version holds the text the
edit REPLACED, so the edit is the step from a row to the next NEWER state —
the row above it, or, for the newest row, the rule as it stands now.
Comparing against the row below would attribute every change to the wrong
edit. A field nobody has fetched yet reads as neither changed nor unchanged.

An edit that touched verify_with is badged "check reset", because that edit
silently cleared verified_at (milestone 312) and put the rule back at the
top of the staleness sweep — a moment visible nowhere else.

The badge is a 12% color-mix TINT, not solid `--fs-warning`.
`--fs-warning-fg` is defined in theme.css as "warning TEXT on a warning
tint", so painting it over the solid token is exactly the same-hue contrast
failure #3141 records. Every var() the component references resolves against
theme.css, checked before pushing.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-30 12:52:07 -04:00

240 lines
5.5 KiB
Vue

<script setup lang="ts">
import { ref, computed } from "vue";
import { apiGet } from "@/api/client";
import DiffView from "@/components/DiffView.vue";
import { computeDiff, type DiffLine } from "@/utils/diff";
interface NoteVersion {
id: number;
note_id: number;
title: string;
tags: string[];
body?: string;
created_at: string;
}
const props = defineProps<{
noteId: number;
currentBody: string;
}>();
const emit = defineEmits<{
(e: "restore", body: string, tags: string[]): void;
}>();
const expanded = ref(false);
const view = ref<"list" | "diff">("list");
const versions = ref<NoteVersion[]>([]);
const selectedVersion = ref<NoteVersion | null>(null);
const loading = ref(false);
const loadingDetail = ref(false);
const diff = computed<DiffLine[]>(() => {
if (!selectedVersion.value?.body) return [];
return computeDiff(props.currentBody, selectedVersion.value.body);
});
function formatDate(iso: string): string {
const d = new Date(iso);
const now = new Date();
const isToday = d.toDateString() === now.toDateString();
if (isToday) {
return d.toLocaleTimeString(undefined, { hour: "2-digit", minute: "2-digit" });
}
return d.toLocaleString(undefined, {
month: "short", day: "numeric",
hour: "2-digit", minute: "2-digit",
});
}
async function toggle() {
expanded.value = !expanded.value;
if (expanded.value && versions.value.length === 0) {
await loadVersions();
}
}
async function loadVersions() {
loading.value = true;
try {
const data = await apiGet<{ versions: NoteVersion[] }>(
`/api/notes/${props.noteId}/versions`
);
versions.value = data.versions;
} catch {
// silent
} finally {
loading.value = false;
}
}
async function selectVersion(v: NoteVersion) {
if (v.body === undefined) {
loadingDetail.value = true;
try {
const full = await apiGet<NoteVersion>(
`/api/notes/${props.noteId}/versions/${v.id}`
);
const listItem = versions.value.find((x) => x.id === v.id);
if (listItem) listItem.body = full.body;
selectedVersion.value = full;
} catch {
// silent
} finally {
loadingDetail.value = false;
}
} else {
selectedVersion.value = v;
}
view.value = "diff";
}
function back() {
view.value = "list";
selectedVersion.value = null;
}
function restore() {
if (!selectedVersion.value?.body) return;
emit("restore", selectedVersion.value.body, selectedVersion.value.tags ?? []);
back();
}
</script>
<template>
<div class="vh-section">
<button class="vh-header" @click="toggle">
<span class="vh-title">Version History</span>
<span class="vh-chevron">{{ expanded ? "▴" : "▾" }}</span>
</button>
<div v-if="expanded" class="vh-body">
<!-- List view -->
<template v-if="view === 'list'">
<div v-if="loading" class="vh-empty">Loading...</div>
<div v-else-if="!versions.length" class="vh-empty">No snapshots yet.</div>
<div
v-for="v in versions"
:key="v.id"
class="vh-item"
@click="selectVersion(v)"
>
{{ formatDate(v.created_at) }}
</div>
</template>
<!-- Diff view -->
<template v-else>
<div class="vh-diff-actions">
<button class="vh-btn-back" @click="back"> Back</button>
<button
class="vh-btn-restore"
:disabled="!selectedVersion?.body"
@click="restore"
>Restore</button>
</div>
<div class="vh-diff-wrap">
<div v-if="loadingDetail" class="vh-empty">Loading...</div>
<DiffView v-else :diff="diff" />
</div>
</template>
</div>
</div>
</template>
<style scoped>
.vh-section {
border-top: 1px solid var(--fs-border-color);
}
.vh-header {
display: flex;
align-items: center;
justify-content: space-between;
width: 100%;
padding: 0.55rem 0.75rem;
background: none;
border: none;
cursor: pointer;
font-family: inherit;
text-align: left;
}
.vh-header:hover { background: var(--fs-surface-raised); }
.vh-title {
font-size: 0.75rem;
font-weight: 700;
color: var(--fs-text-secondary);
text-transform: uppercase;
letter-spacing: 0.05em;
}
.vh-chevron {
font-size: 0.7rem;
color: var(--fs-text-tertiary);
}
.vh-body {
padding: 0 0 0.5rem;
}
.vh-empty {
padding: 0.5rem 0.75rem;
font-size: 0.8rem;
color: var(--fs-text-tertiary);
}
.vh-item {
padding: 0.35rem 0.75rem;
font-size: 0.8rem;
color: var(--fs-text-primary);
cursor: pointer;
font-family: monospace;
border-left: 2px solid transparent;
}
.vh-item:hover {
background: var(--fs-surface-raised);
border-left-color: var(--fs-accent);
}
.vh-diff-actions {
display: flex;
gap: 0.4rem;
padding: 0.4rem 0.75rem 0.35rem;
}
.vh-btn-back {
background: none;
border: 1px solid var(--fs-border-color);
border-radius: var(--fs-radius-sm);
padding: 0.25rem 0.6rem;
font-size: 0.78rem;
color: var(--fs-text-secondary);
cursor: pointer;
font-family: inherit;
}
.vh-btn-back:hover { border-color: var(--fs-accent); color: var(--fs-accent); }
.vh-btn-restore {
background: var(--fs-action-primary);
border: none;
border-radius: var(--fs-radius-sm);
padding: 0.25rem 0.6rem;
font-size: 0.78rem;
color: var(--fs-text-on-action);
cursor: pointer;
font-family: inherit;
}
.vh-btn-restore:disabled { opacity: 0.5; cursor: default; }
.vh-diff-wrap {
padding: 0 0.5rem 0.25rem;
max-height: 420px;
overflow-y: auto;
display: flex;
flex-direction: column;
}
</style>