Lutra/ui/src/components/history/LocalHistoryDialog.vue
danny d2344bf216 A2: disk-conflict banner, overwrite question and Local History dialog
- Banner inside the editor pane for a dirty tab whose file changed
  (Reload (discard mine) / Keep mine / Compare) or was deleted (Keep as
  unsaved / Close); state in stores/diskConflicts.ts, the old notice is gone.
- Save and save all ask Overwrite / Cancel when the engine refuses with
  disk_changed.
- Diff tabs against the disk text (file.readDisk) and a Local History
  entry (history.read); "Revert to This" in the caption.
- Show Local History dialog (history.show: tree context menu, palette,
  File menu) with Show Diff and an undoable Revert to This.
- History failures warn once until a snapshot works again.
- Docs: docs/local-history.md, G2 in gaps.md, ui-parity, architecture.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 12:55:04 +01:00

413 lines
10 KiB
Vue

<script setup lang="ts">
/**
* The "Show Local History" dialog (C10 A2), centred over the window: the
* snapshots of one file, newest first, with time, reason and size.
*
* - **List:** Up/Down, Home and End move; Enter or "Show Diff" opens the
* entry against the current text in a diff tab and closes the dialog;
* "Revert to This" puts the entry's text into the buffer (undoable,
* unsaved). A failure shows under the list.
* - **Command:** the dialog supplies the UI command `history.show`,
* enabled while `history.list` is (an open project).
* - **Closing:** Escape, Cancel or a click outside; the focus goes back
* where it was. The dialog is an exclusive modal (`commands/modals.ts`).
*/
import { History } from "@lucide/vue";
import { nextTick, onBeforeUnmount, onMounted, ref, watch } from "vue";
import { useModalsStore } from "../../commands/modals";
import { useCommandsStore } from "../../commands/store";
import { formatDateTime, t } from "../../i18n";
import { baseName } from "../../stores/editor";
import { SHOW_HISTORY_COMMAND, useLocalHistoryStore, type HistoryEntry, type HistoryReason } from "./localHistory";
const history = useLocalHistoryStore();
const modals = useModalsStore();
const commands = useCommandsStore();
/** The list. */
const list = ref<HTMLElement | null>(null);
/** The dialog box. */
const dialogBox = ref<HTMLElement | null>(null);
/** The element that had the focus before the dialog opened. */
let returnFocus: HTMLElement | null = null;
/**
* The DOM id of an entry's row.
*
* @param index - its index in the list.
*/
function rowId(index: number): string {
return `lt-history-${String(index)}`;
}
/**
* The size of an entry for the list.
*
* @param entry - the entry.
*/
function sizeText(entry: HistoryEntry): string {
return entry.size < 1024
? t("history.sizeBytes", { size: entry.size })
: t("history.sizeKib", { size: Math.round(entry.size / 102.4) / 10 });
}
/** The label of each reason. */
const REASON_TEXT: Record<HistoryReason, () => string> = {
save: () => t("history.reason.save"),
beforeSave: () => t("history.reason.beforeSave"),
externalChange: () => t("history.reason.externalChange"),
beforeReload: () => t("history.reason.beforeReload"),
deleted: () => t("history.reason.deleted"),
refactoring: () => t("history.reason.refactoring"),
git: () => t("history.reason.git"),
beforeRevert: () => t("history.reason.beforeRevert"),
};
/**
* The text of a reason for the list.
*
* @param reason - why the snapshot was taken.
*/
function reasonText(reason: HistoryReason): string {
return REASON_TEXT[reason]();
}
/** Closes the dialog. */
function close(): void {
if (history.open) {
history.hide();
}
}
watch(
() => history.open,
(isOpen) => {
if (isOpen) {
const active = document.activeElement;
returnFocus = active instanceof HTMLElement ? active : null;
modals.opened("localHistory", close);
} else {
modals.closed("localHistory");
returnFocus?.focus();
returnFocus = null;
}
},
);
watch(
() => [history.focusRequest, history.entries] as const,
async () => {
await nextTick();
if ((history.entries?.length ?? 0) > 0) {
list.value?.focus();
} else {
dialogBox.value?.focus();
}
},
);
/** Keeps the selected row in view. */
function scrollSelected(): void {
void nextTick(() => {
const row = list.value?.querySelector<HTMLElement>(`#${rowId(history.selected)}`);
if (row && typeof row.scrollIntoView === "function") {
row.scrollIntoView({ block: "nearest" });
}
});
}
/**
* Escape closes the dialog.
*
* @param event - the key press.
*/
function onDialogKey(event: KeyboardEvent): void {
if (event.key === "Escape") {
event.preventDefault();
event.stopPropagation();
close();
}
}
/**
* Keyboard handling of the list.
*
* @param event - the key press.
*/
function onListKey(event: KeyboardEvent): void {
const count = history.entries?.length ?? 0;
const moves: Record<string, number> = { ArrowDown: 1, ArrowUp: -1, Home: -count, End: count };
const delta = moves[event.key];
const entry = history.current;
if (delta !== undefined) {
history.move(delta);
scrollSelected();
} else if (event.key === "Enter" && entry) {
void history.showDiff(entry);
} else {
return;
}
event.preventDefault();
event.stopPropagation();
}
/** Withdraws the command handler; set while mounted. */
let withdraw: (() => void) | null = null;
onMounted(() => {
withdraw = commands.provide(SHOW_HISTORY_COMMAND, {
run: (params) => history.showCommand(params),
enabled: () => commands.isEnabled("history.list"),
});
});
onBeforeUnmount(() => {
withdraw?.();
withdraw = null;
});
</script>
<template>
<div
v-if="history.open"
class="local-history"
data-test="local-history"
@mousedown.self.prevent="close()"
>
<div
ref="dialogBox"
class="local-history__dialog"
tabindex="-1"
role="dialog"
aria-modal="true"
aria-labelledby="lt-history-title"
@keydown="onDialogKey"
>
<div class="local-history__header">
<History
:size="14"
aria-hidden="true"
/>
<h2
id="lt-history-title"
class="local-history__title"
>
{{ t("history.title", { name: baseName(history.path) }) }}
</h2>
<span class="local-history__path">{{ history.path }}</span>
</div>
<p
v-if="history.entries === null || history.entries.length === 0"
class="local-history__hint"
role="status"
data-test="history-hint"
>
{{ history.entries === null ? (history.loading ? t("history.loading") : "") : t("history.empty") }}
</p>
<ul
v-show="(history.entries?.length ?? 0) > 0"
ref="list"
class="local-history__list"
role="listbox"
tabindex="0"
:aria-label="t('history.listLabel')"
:aria-activedescendant="rowId(history.selected)"
data-test="history-list"
@keydown="onListKey"
>
<li
v-for="(entry, i) in history.entries"
:id="rowId(i)"
:key="entry.id"
class="local-history__row"
:class="{ 'local-history__row--selected': history.selected === i }"
role="option"
:aria-selected="history.selected === i"
:data-id="entry.id"
data-test="history-row"
@click="history.selected = i"
@dblclick="history.showDiff(entry)"
>
<span class="local-history__time">{{ formatDateTime(entry.time) }}</span>
<span class="local-history__reason">{{ reasonText(entry.reason) }}</span>
<span class="local-history__size">{{ sizeText(entry) }}</span>
</li>
</ul>
<p
v-if="history.error !== null"
class="local-history__error"
role="alert"
data-test="history-error"
>
{{ history.error }}
</p>
<div class="local-history__buttons">
<button
type="button"
class="local-history__button"
data-test="history-cancel"
@click="close()"
>
{{ t("common.cancel") }}
</button>
<button
type="button"
class="local-history__button"
:disabled="history.busy || history.current === null"
data-test="history-revert"
@click="history.current && history.revert(history.path, history.current.id)"
>
{{ t("history.revert") }}
</button>
<button
type="button"
class="local-history__button local-history__button--primary"
:disabled="history.current === null"
data-test="history-diff"
@click="history.current && history.showDiff(history.current)"
>
{{ t("history.showDiff") }}
</button>
</div>
</div>
</div>
</template>
<style scoped>
.local-history {
position: fixed;
inset: 0;
display: flex;
align-items: flex-start;
justify-content: center;
padding-top: 12vh;
z-index: 30;
}
.local-history__dialog {
width: min(560px, 92vw);
max-height: 70vh;
display: flex;
flex-direction: column;
gap: 6px;
padding: 10px;
background: var(--lt-bg);
color: var(--lt-fg);
border: 1px solid var(--lt-border);
border-radius: 6px;
outline: none;
overflow: hidden;
}
.local-history__header {
display: flex;
align-items: center;
gap: 6px;
min-width: 0;
color: var(--lt-fg-muted);
}
.local-history__title {
margin: 0;
font-size: 1em;
font-weight: 600;
color: var(--lt-fg);
white-space: nowrap;
}
.local-history__path {
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.local-history__hint {
margin: 0;
color: var(--lt-fg-muted);
}
.local-history__error {
margin: 0;
white-space: pre-wrap;
color: var(--lt-error);
}
.local-history__list {
flex: 1;
min-height: 0;
margin: 0;
padding: 2px 0;
list-style: none;
overflow-y: auto;
border: 1px solid var(--lt-border);
border-radius: var(--lt-radius, 4px);
outline: none;
}
.local-history__list:focus-visible {
box-shadow: inset 0 0 0 1px var(--lt-accent);
}
.local-history__row {
display: flex;
align-items: center;
gap: 12px;
padding: 3px 8px;
white-space: nowrap;
cursor: default;
}
.local-history__row:hover {
background: color-mix(in srgb, var(--lt-fg) 6%, transparent);
}
.local-history__row--selected,
.local-history__row--selected:hover {
background: var(--lt-editor-selection-bg);
}
.local-history__time {
flex: none;
font-family: var(--lt-font-mono);
}
.local-history__reason {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
}
.local-history__size {
flex: none;
color: var(--lt-fg-muted);
}
.local-history__buttons {
display: flex;
justify-content: flex-end;
gap: 8px;
}
.local-history__button {
padding: 4px 12px;
font: inherit;
color: var(--lt-fg);
background: var(--lt-editor-bg);
border: 1px solid var(--lt-border);
border-radius: 4px;
}
.local-history__button--primary {
border-color: var(--lt-accent);
}
.local-history__button:focus-visible {
outline: 1px solid var(--lt-accent);
}
.local-history__button:disabled {
opacity: 0.5;
}
</style>