Lutra/ui/src/commands/modals.ts
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

113 lines
3.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* The modal rule (M1.15): at most one modal is open at a time.
*
* - **Exclusive modals:** the command palette, the go-to popup (M2.6), the
* app menu (M3.9), the settings dialog and the close question of a dirty tab. Opening one closes the one that is open
* (the close question closes as Cancel). Each reports itself with
* {@link useModalsStore}'s `opened` and `closed`; the close question is
* followed from the editor store, so the editor needs no code for it.
* - **The restore dialog is queued:** it never closes another modal and is
* never closed by one. While an exclusive modal is open it is hidden, and
* it shows again, with its list intact, when that modal closes. Its
* offer stays until the user decides, so it must not be lost to a
* palette opened by an agent.
* - **The palette is a launcher:** running any command other than
* `palette.open` closes it first (see `store.ts` › `execute`), so a
* `uiCommand` from the control channel never leaves it open behind
* what the command shows. The go-to popup is a launcher too: any command
* but the go-to open commands closes it. So is the app menu (M3.9): any
* command but `menu.open` closes it.
* - **Navigation (M3.5):** the definition picker and the inline rename field
* are exclusive modals too. So is the branches popup (M4.5), and so are
* "Clone Repository…" and "Manage Remotes…" (M4.10), the merge
* dialog (M4.8) and the stash dialog (M4.9), the deployment servers
* dialog (G17), the sync preview (C9 D2), the theme picker (C9 T1) and
* the Local History dialog (C10 A2).
*/
import { defineStore } from "pinia";
import { ref, watch } from "vue";
import { useEditorStore } from "../stores/editor";
/** The exclusive modals. */
export type ModalId =
| "palette"
| "goto"
| "menu"
| "settings"
| "closePrompt"
| "navPicker"
| "rename"
| "branches"
| "clone"
| "remotes"
| "deployment"
| "deploySync"
| "merge"
| "stash"
| "themes"
| "localHistory";
/** Store of the open exclusive modal. */
export const useModalsStore = defineStore("modals", () => {
const editor = useEditorStore();
/** The exclusive modal that is open, or null. */
const current = ref<ModalId | null>(null);
/** Closes {@link current}; set while one is open. */
let closeCurrent: (() => void) | null = null;
/**
* Forgets `id` as the open modal; the modal closed itself.
*
* @param id - the modal.
*/
function closed(id: ModalId): void {
if (current.value === id) {
current.value = null;
closeCurrent = null;
}
}
/**
* Closes `id` if it is the open modal.
*
* @param id - the modal.
*/
function dismiss(id: ModalId): void {
if (current.value !== id) {
return;
}
const close = closeCurrent;
closed(id);
close?.();
}
/**
* Records `id` as the open modal and closes the one open before.
* Call it before the modal takes the focus, so the closed modal gives
* the focus back first.
*
* @param id - the modal that opens.
* @param close - closes it when another modal opens.
*/
function opened(id: ModalId, close: () => void): void {
if (current.value !== null && current.value !== id) {
dismiss(current.value);
}
current.value = id;
closeCurrent = close;
}
watch(
() => editor.closePrompt !== null,
(asking) => {
if (asking) {
opened("closePrompt", () => void editor.resolveClose("cancel"));
} else {
closed("closePrompt");
}
},
{ immediate: true },
);
return { current, opened, closed, dismiss };
});