Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Allow returning from unsaved changes dialog

Bittifcdb67755c35
diff --git a/src/components/ProjectWorkspace.tsx b/src/components/ProjectWorkspace.tsx
index 810adff..d357a5d 100644
--- a/src/components/ProjectWorkspace.tsx
+++ b/src/components/ProjectWorkspace.tsx
@@ -79,7 +79,7 @@ export function ProjectWorkspace({ project, initialView = "timeline", runtime =
}, [config.generationJobs, project.folderPath]);
return <div className="project-shell">
- {leaving && <UnsavedProjectDialog name={config.name} busy={savingToLeave} error={saveError} onSave={() => void leave(true)} onDiscard={() => void leave(false)} />}
+ {leaving && <UnsavedProjectDialog name={config.name} busy={savingToLeave} error={saveError} onSave={() => void leave(true)} onDiscard={() => void leave(false)} onBack={() => setLeaving(false)} />}
<header className="project-topbar">
<div className="project-topbar__lead">
<button
diff --git a/src/components/UnsavedProject.integration.test.tsx b/src/components/UnsavedProject.integration.test.tsx
index a1fe66d..e3c07cf 100644
--- a/src/components/UnsavedProject.integration.test.tsx
+++ b/src/components/UnsavedProject.integration.test.tsx
@@ -26,15 +26,16 @@ it("returns immediately when the project has no unsaved changes", () => {
expect(screen.queryByRole("alertdialog")).not.toBeInTheDocument();
});
-it("offers only save or discard and waits for saving before leaving", async () => {
+it("offers back, save or discard and waits for saving before leaving", async () => {
let finish!: () => void;
const writer = vi.fn((_record: ProjectRecord) => new Promise<void>((resolve) => { finish = resolve; }));
const { onBack, session } = setup(writer);
changeName(); back();
const dialog = screen.getByRole("alertdialog", { name: "Save changes?" });
- expect(within(dialog).getAllByRole("button").map((button) => button.textContent)).toEqual(["Discard changes", "Save changes"]);
+ expect(within(dialog).getAllByRole("button").map((button) => button.textContent)).toEqual(["Back", "Discard changes", "Save changes"]);
fireEvent.click(within(dialog).getByRole("button", { name: "Save changes" }));
await waitFor(() => expect(writer).toHaveBeenCalledOnce());
+ expect(within(dialog).getByRole("button", { name: "Back" })).toBeDisabled();
expect(onBack).not.toHaveBeenCalled();
expect(writer.mock.calls[0][0].config.references[0].name).toBe("Unsaved hero");
await act(async () => finish());
@@ -42,6 +43,21 @@ it("offers only save or discard and waits for saving before leaving", async () =
expect(session.getSnapshot().dirty).toBe(false);
});
+it.each(["Back", "Escape"])("returns to editing via %s without saving or discarding changes", (action) => {
+ const { onBack, session, writer } = setup();
+ changeName(); back();
+ const dialog = screen.getByRole("alertdialog", { name: "Save changes?" });
+ if (action === "Back") fireEvent.click(within(dialog).getByRole("button", { name: "Back" }));
+ else fireEvent.keyDown(dialog, { key: "Escape" });
+ expect(screen.queryByRole("alertdialog")).not.toBeInTheDocument();
+ expect(screen.getByRole("textbox", { name: "Reference name" })).toHaveValue("Unsaved hero");
+ expect(session.getSnapshot().dirty).toBe(true);
+ expect(writer).not.toHaveBeenCalled();
+ expect(onBack).not.toHaveBeenCalled();
+ back();
+ expect(screen.getByRole("alertdialog", { name: "Save changes?" })).toBeInTheDocument();
+});
+
it("discards unsaved changes from the retained session so reopening restores saved values", async () => {
const { onBack, session, writer, queue, project } = setup();
changeName(); back();
diff --git a/src/components/UnsavedProjectDialog.tsx b/src/components/UnsavedProjectDialog.tsx
index 27b06ca..339aa86 100644
--- a/src/components/UnsavedProjectDialog.tsx
+++ b/src/components/UnsavedProjectDialog.tsx
@@ -1,19 +1,24 @@
import { Save } from "lucide-react";
import { useEffect, useRef } from "react";
-export function UnsavedProjectDialog({ name, busy, error, onSave, onDiscard }: {
+export function UnsavedProjectDialog({ name, busy, error, onSave, onDiscard, onBack }: {
name: string;
busy: boolean;
error: string | null;
onSave: () => void;
onDiscard: () => void;
+ onBack: () => void;
}) {
const dialog = useRef<HTMLDivElement>(null);
const save = useRef<HTMLButtonElement>(null);
- useEffect(() => { save.current?.focus(); }, []);
+ useEffect(() => {
+ const opener = document.activeElement as HTMLElement | null;
+ save.current?.focus();
+ return () => { if (opener?.isConnected) opener.focus(); };
+ }, []);
useEffect(() => {
const keyDown = (event: KeyboardEvent) => {
- if (event.key === "Escape") { event.preventDefault(); event.stopPropagation(); }
+ if (event.key === "Escape") { event.preventDefault(); event.stopPropagation(); if (!busy) onBack(); }
if (event.key !== "Tab") return;
const buttons = Array.from(dialog.current?.querySelectorAll<HTMLButtonElement>("button:not(:disabled)") ?? []);
if (buttons.length === 0) { event.preventDefault(); return; }
@@ -24,7 +29,7 @@ export function UnsavedProjectDialog({ name, busy, error, onSave, onDiscard }: {
};
window.addEventListener("keydown", keyDown, true);
return () => window.removeEventListener("keydown", keyDown, true);
- }, []);
+ }, [busy, onBack]);
return <div className="delete-project-backdrop">
<div ref={dialog} className="delete-project-dialog" role="alertdialog" aria-modal="true" aria-labelledby="unsaved-project-title" aria-describedby="unsaved-project-description">
<div className="delete-project-dialog__head"><h2 id="unsaved-project-title">Save changes?</h2></div>
@@ -33,6 +38,7 @@ export function UnsavedProjectDialog({ name, busy, error, onSave, onDiscard }: {
{error && <p role="alert">Couldn’t save the project: {error}</p>}
</div>
<div className="delete-project-dialog__actions">
+ <button type="button" className="secondary-button" disabled={busy} onClick={onBack}>Back</button>
<button type="button" className="secondary-button" disabled={busy} onClick={onDiscard}>Discard changes</button>
<button ref={save} type="button" className="primary-button" disabled={busy} onClick={onSave}><Save size={16} />{busy ? "Saving…" : "Save changes"}</button>
</div>