feat(ui): revert single changes in settings and workflow review
This commit is contained in:
1 parent
f10199f0b6
commit
55deeaa88c
11 files changed
+203
-98
No files matched your search
@@ -0,0 +1,26 @@
|
||||
<script setup lang="ts">
|
||||
import { Undo2 } from "@lucide/vue";
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { formatValue, type Change } from "@/lib/changes";
|
||||
|
||||
/** One row per changed leaf, each revertable on its own, so a reviewer keeps
|
||||
* the changes they want and drops the rest before saving. */
|
||||
defineProps<{ changes: Change[]; label?: (path: string) => string }>();
|
||||
defineEmits<{ revert: [change: Change] }>();
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<ul class="divide-y divide-border font-mono text-xs">
|
||||
<li v-for="change in changes" :key="change.path" class="group flex items-start gap-2 px-3 py-2">
|
||||
<div class="min-w-0 flex-1">
|
||||
<p class="mb-1 font-sans font-medium">{{ label ? label(change.path) : change.path }}</p>
|
||||
<p class="whitespace-pre-wrap break-words text-danger"><span class="font-sans">Before: </span>{{ formatValue(change.before) }}</p>
|
||||
<p class="whitespace-pre-wrap break-words text-ok"><span class="font-sans">After: </span>{{ formatValue(change.after) }}</p>
|
||||
</div>
|
||||
<Button variant="ghost" size="icon-sm" :aria-label="`Revert ${label ? label(change.path) : change.path}`" title="Revert" @click="$emit('revert', change)">
|
||||
<Undo2 />
|
||||
</Button>
|
||||
</li>
|
||||
</ul>
|
||||
</template>
|
||||
@@ -65,3 +65,37 @@ export function formatValue(value: unknown): string {
|
||||
if (typeof value === "string") return value === "" ? '""' : value;
|
||||
return JSON.stringify(value);
|
||||
}
|
||||
|
||||
/** pathSegments splits a change path into keys, with row indexes as numbers. */
|
||||
export function pathSegments(path: string): (string | number)[] {
|
||||
return path === "" ? [] : path.split(".").map((part) => (/^\d+$/.test(part) ? Number(part) : part));
|
||||
}
|
||||
|
||||
/**
|
||||
* setAt returns a copy of doc with value written at path. An undefined value
|
||||
* removes the key or row, and a parent it leaves empty, so reverting an
|
||||
* added row deletes it rather than leaving a hole.
|
||||
*/
|
||||
export function setAt(doc: unknown, path: (string | number)[], value: unknown): unknown {
|
||||
if (path.length === 0) return value;
|
||||
const [head, ...rest] = path;
|
||||
if (typeof head === "number") {
|
||||
const rows = Array.isArray(doc) ? [...doc] : [];
|
||||
const next = setAt(rows[head], rest, value);
|
||||
if (next === undefined) rows.splice(head, 1);
|
||||
else if (rest.length === 0 && head >= rows.length) rows.push(next);
|
||||
else rows[head] = next;
|
||||
return next === undefined && rows.length === 0 ? undefined : rows;
|
||||
}
|
||||
const record = isRecord(doc) ? { ...doc } : {};
|
||||
const next = setAt(record[head], rest, value);
|
||||
if (next === undefined) delete record[head];
|
||||
else record[head] = next;
|
||||
return next === undefined && Object.keys(record).length === 0 ? undefined : record;
|
||||
}
|
||||
|
||||
/** revertChange puts one change's before value back into doc, leaving every
|
||||
* other change in place: the surgical half of accept all. */
|
||||
export function revertChange(doc: unknown, change: Change): unknown {
|
||||
return setAt(doc, pathSegments(change.path), change.before);
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
import { computed } from "vue";
|
||||
|
||||
import { useSettingsStore } from "@/stores/settings";
|
||||
import { formatValue } from "./changes";
|
||||
import { formatValue } from "@/lib/changes";
|
||||
|
||||
/**
|
||||
* The previous value of an edited field, inline at the control: "was 1m0s".
|
||||
|
||||
@@ -17,7 +17,7 @@ import {
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
|
||||
import { ago } from "@/lib/format";
|
||||
import { useSettingsStore, type AuditEntry } from "@/stores/settings";
|
||||
import { formatValue } from "./changes";
|
||||
import { formatValue } from "@/lib/changes";
|
||||
import { filterHistory, historyEmptyTitle } from "./history";
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,7 +14,8 @@ import {
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { useSettingsStore } from "@/stores/settings";
|
||||
import { diffValues, formatValue } from "./changes";
|
||||
import ChangeList from "@/components/ChangeList.vue";
|
||||
import { diffValues } from "@/lib/changes";
|
||||
import type { SettingsSchema } from "@/workflows/workflow-yaml";
|
||||
|
||||
const store = useSettingsStore();
|
||||
@@ -135,13 +136,11 @@ async function save() {
|
||||
<Badge v-if="section.restart" variant="warn">After restart</Badge>
|
||||
<Button variant="ghost" size="sm" @click="store.resetDraft(section.kind)">Revert</Button>
|
||||
</header>
|
||||
<ul class="divide-y divide-border font-mono text-xs">
|
||||
<li v-for="change in section.changes" :key="change.path" class="px-3 py-2">
|
||||
<p class="mb-1 font-sans font-medium">{{ fieldLabel(section.kind, change.path) }}</p>
|
||||
<p class="whitespace-pre-wrap break-words text-danger"><span class="font-sans">Before: </span>{{ formatValue(change.before) }}</p>
|
||||
<p class="whitespace-pre-wrap break-words text-ok"><span class="font-sans">After: </span>{{ formatValue(change.after) }}</p>
|
||||
</li>
|
||||
</ul>
|
||||
<ChangeList
|
||||
:changes="section.changes"
|
||||
:label="(path) => fieldLabel(section.kind, path)"
|
||||
@revert="(change) => store.revertDraftChange(section.kind, change)"
|
||||
/>
|
||||
<ul
|
||||
v-if="section.issues.length"
|
||||
class="space-y-0.5 border-t border-border px-3 py-2 text-xs text-danger"
|
||||
|
||||
@@ -4,7 +4,7 @@ import { useSessionStore } from "./session.ts";
|
||||
import { api } from "../lib/api.ts";
|
||||
import type { ConfigView } from "../settings/types.ts";
|
||||
|
||||
import { diffValues, saveInOrder, type Change } from "../settings/changes.ts";
|
||||
import { diffValues, revertChange, saveInOrder, type Change } from "../lib/changes.ts";
|
||||
import {
|
||||
collectIssues,
|
||||
draftValidators,
|
||||
@@ -291,6 +291,12 @@ export const useSettingsStore = defineStore("settings", () => {
|
||||
drafts[kind] = { base: resource, value: cloneControlPlaneValue(resource.value) };
|
||||
}
|
||||
|
||||
/** Undoes one change in a draft and keeps the rest. */
|
||||
function revertDraftChange(kind: string, change: Change): void {
|
||||
const draft = drafts[kind];
|
||||
if (draft) draft.value = revertChange(draft.value, change);
|
||||
}
|
||||
|
||||
/** 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 {
|
||||
@@ -500,6 +506,7 @@ export const useSettingsStore = defineStore("settings", () => {
|
||||
issuesFor,
|
||||
drafts,
|
||||
resetDraft,
|
||||
revertDraftChange,
|
||||
stageRestore,
|
||||
saveDrafts,
|
||||
catalog,
|
||||
|
||||
@@ -35,7 +35,9 @@ import WorkflowCanvas from "./WorkflowCanvas.vue";
|
||||
import WorkflowHistory from "./WorkflowHistory.vue";
|
||||
import { useWorkflowHistory } from "./workflow-history";
|
||||
import { revisionOfRun, type WorkflowRevision } from "./workflow-revisions";
|
||||
import { deleteStep, duplicateStep, insertStep, moveStep, replaceStep, stepAt, workflowField, type StepPath } from "./workflow-edit";
|
||||
import { deleteStep, duplicateStep, insertStep, moveStep, replaceStep, revertWorkflowChange, stepAt, workflowChanges, workflowField, type StepPath } from "./workflow-edit";
|
||||
import ChangeList from "@/components/ChangeList.vue";
|
||||
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
|
||||
import { useWorkflowRuns } from "./workflow-runs";
|
||||
import { useWorkflowTriggers } from "./workflow-triggers";
|
||||
import { cloneControlPlaneValue, useSettingsStore } from "@/stores/settings";
|
||||
@@ -240,6 +242,7 @@ const vocabulary = computed(() => workflows.stepTypes);
|
||||
const parsed = computed(() => parseWorkflowYaml(yaml.value, vocabulary.value));
|
||||
const stored = computed(() => collection.value.definitions.find((entry) => entry.id === selected.value)?.yaml);
|
||||
const dirty = computed(() => yaml.value !== (stored.value ?? ""));
|
||||
const changes = computed(() => (dirty.value ? workflowChanges(stored.value ?? "", yaml.value) : []));
|
||||
const view = ref("canvas");
|
||||
// What each step lacks, read the way its editor reads it.
|
||||
const problems = computed(() =>
|
||||
@@ -300,7 +303,15 @@ function syncScroll(event: Event): void {
|
||||
<span v-if="!parsed.ok" class="ml-auto max-w-md truncate text-xs text-danger" role="status" :title="validationLabel(parsed)">
|
||||
{{ validationLabel(parsed) }}
|
||||
</span>
|
||||
<span v-else-if="dirty" class="ml-auto text-xs text-fg-subtle">Unsaved changes</span>
|
||||
<Popover v-else-if="dirty">
|
||||
<PopoverTrigger as-child>
|
||||
<Button type="button" variant="ghost" size="sm" class="ml-auto text-xs text-fg-subtle">
|
||||
{{ changes.length || "" }} Unsaved {{ changes.length === 1 ? "change" : "changes" }}</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent align="end" class="max-h-96 w-96 overflow-y-auto p-0">
|
||||
<ChangeList :changes="changes" @revert="(change) => (yaml = revertWorkflowChange(yaml, change))" />
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
<span v-else class="ml-auto" />
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger as-child>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { isMap, isScalar, isSeq, parseDocument } from "yaml";
|
||||
import { diffValues, pathSegments, type Change } from "../lib/changes.ts";
|
||||
|
||||
/**
|
||||
* Canvas edits as edits to the workflow's YAML document. The text stays the
|
||||
@@ -203,3 +204,30 @@ export function setWorkflowField(source: string, key: string, value: unknown): s
|
||||
else document.set(key, document.createNode(pruned));
|
||||
return document.toString();
|
||||
}
|
||||
|
||||
/** The changes between two versions of a workflow, one per changed leaf. */
|
||||
export function workflowChanges(before: string, after: string): Change[] {
|
||||
const data = (source: string) => {
|
||||
try { return parseDocument(source).toJSON() ?? {}; } catch { return {}; }
|
||||
};
|
||||
return diffValues(data(before), data(after), "", true);
|
||||
}
|
||||
|
||||
/** Puts one change's before value back, keeping every other edit and the
|
||||
* document's comments and layout. */
|
||||
export function revertWorkflowChange(source: string, change: Change): string {
|
||||
const document = parseDocument(source);
|
||||
const path = pathSegments(change.path);
|
||||
if (change.before !== undefined) {
|
||||
document.setIn(path, document.createNode(change.before));
|
||||
return document.toString();
|
||||
}
|
||||
// Removing a row's last leaf removes the row, not just its contents.
|
||||
document.deleteIn(path);
|
||||
for (let parent = path.slice(0, -1); parent.length > 0; parent = parent.slice(0, -1)) {
|
||||
const node = document.getIn(parent, true);
|
||||
if (!((isMap(node) || isSeq(node)) && node.items.length === 0)) break;
|
||||
document.deleteIn(parent);
|
||||
}
|
||||
return document.toString();
|
||||
}
|
||||
Reference in new issue
Block a user