Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 44 additions & 39 deletions app/src/routes/canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -828,45 +828,50 @@ const CanvasRoute: VoidComponent = () => {
}
}}
/>
<ConfirmDialog
open={discardDialogOpen()}
onOpenChange={(open) => {
setDiscardDialogOpen(open);
if (!open) setPendingSelectedId(undefined);
}}
title={
activeEditor()?.api.error()
? "This note didn't save"
: "Discard unsaved changes?"
}
description={
activeEditor()?.api.error()
? "Your latest changes aren't saved. If you stay, this note keeps retrying. If you leave now, they will be lost."
: "Your unsaved changes will be lost if you leave this note."
}
confirmLabel={
activeEditor()?.api.error() ? "Leave" : "Discard changes"
}
cancelLabel={activeEditor()?.api.error() ? "Stay" : "Keep editing"}
onConfirm={() => {
setSelectedId(pendingSelectedId());
setPendingSelectedId(undefined);
}}
/>
<SimpleDialog
open={saveInProgressDialogOpen()}
onOpenChange={setSaveInProgressDialogOpen}
title="Note is saving"
description="Wait for this save to finish before closing or opening another note."
footer={
<Button
variant="solid"
onClick={() => setSaveInProgressDialogOpen(false)}
>
Keep editing
</Button>
}
/>
{/* Mounted on demand so their portals land above the panel's. */}
<Show when={discardDialogOpen()}>
<ConfirmDialog
open={discardDialogOpen()}
onOpenChange={(open) => {
setDiscardDialogOpen(open);
if (!open) setPendingSelectedId(undefined);
}}
title={
activeEditor()?.api.error()
? "This note didn't save"
: "Discard unsaved changes?"
}
description={
activeEditor()?.api.error()
? "Your latest changes aren't saved. If you stay, this note keeps retrying. If you leave now, they will be lost."
: "Your unsaved changes will be lost if you leave this note."
}
confirmLabel={
activeEditor()?.api.error() ? "Leave" : "Discard changes"
}
cancelLabel={activeEditor()?.api.error() ? "Stay" : "Keep editing"}
onConfirm={() => {
setSelectedId(pendingSelectedId());
setPendingSelectedId(undefined);
}}
/>
</Show>
<Show when={saveInProgressDialogOpen()}>
<SimpleDialog
open={saveInProgressDialogOpen()}
onOpenChange={setSaveInProgressDialogOpen}
title="Note is saving"
description="Wait for this save to finish before closing or opening another note."
footer={
<Button
variant="solid"
onClick={() => setSaveInProgressDialogOpen(false)}
>
Keep editing
</Button>
}
/>
</Show>
</Show>
</Box>
);
Expand Down
48 changes: 37 additions & 11 deletions app/tests/e2e/canvas-note-panel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { waitForAppReady } from "./helpers/readiness";
import { holdNextServerAction, settled } from "./helpers/server-actions";

// Regressions on the real /canvas route and its keyed note panel.
// PR #44: closing the panel (close button, backdrop, or Escape) used to drop
// unsaved edits and could close mid-save; Escape inside the guard dialog must
// not close the panel.
// PR #44: leaving the note (close button, Escape, or opening another note) used
// to drop unsaved edits and could happen mid-save; Escape inside the guard
// dialog must not close the panel. The panel is non-modal, so it has no
// backdrop and the map and search stay usable beside it.
// PR #43: a disposed panel viewer must not report its late failure over the
// note that replaced it.
//
Expand All @@ -37,6 +38,19 @@ const openFromRail = async (page: Page, title: string) => {
await page.waitForLoadState("networkidle");
};

// DocumentEditor saves 1.5s after the last keystroke, which races any case that
// needs an unsaved draft. This drops that one timer; if the delay changes, the
// unsaved case fails on its dialog or its no-save check.
const AUTOSAVE_DELAY_MS = 1500;
const holdAutosave = (page: Page) =>
page.evaluate((autosaveMs) => {
const schedule = window.setTimeout.bind(window);
window.setTimeout = ((handler: TimerHandler, ms?: number, ...args) =>
ms === autosaveMs
? 0
: schedule(handler, ms, ...args)) as typeof window.setTimeout;
}, AUTOSAVE_DELAY_MS);

const typeIntoEditor = async (page: Page, text: string) => {
await editor(page).click();
await expect(editor(page)).toBeFocused();
Expand All @@ -48,8 +62,13 @@ const typeIntoEditor = async (page: Page, text: string) => {
).toBeVisible();
};

const clickBackdrop = (page: Page) =>
panel(page).click({ position: { x: 10, y: 200 } });
// The panel covers the rail, so search is how to ask for another note by name:
// Enter on a single match opens it.
const openFromSearch = async (page: Page, title: string) => {
const search = page.getByRole("searchbox", { name: /^Search notes/ });
await search.fill(title);
await search.press("Enter");
};

test.describe("canvas note panel", () => {
// The canvas reads the newest UMAP run, so these tests run one at a time in
Expand Down Expand Up @@ -88,6 +107,9 @@ test.describe("canvas note panel", () => {
});

test("unsaved edits ask before the panel closes", async ({ page }) => {
// In Chromium, `request.postData()` is null for server-action requests
// unless the page has a route, so the no-save check below needs one.
await page.route("**/_server*", (route) => route.continue());
const posts: string[] = [];
page.on("request", (request) => {
if (request.method() === "POST" && request.url().includes("/_server")) {
Expand All @@ -98,6 +120,7 @@ test.describe("canvas note panel", () => {
});

await openFromRail(page, title);
await holdAutosave(page);
await typeIntoEditor(page, " unsaved canvas edit");

await panel(page).getByRole("button", { name: "Close panel" }).click();
Expand All @@ -118,14 +141,15 @@ test.describe("canvas note panel", () => {
await expect(panel(page)).toBeVisible();
await expect(editor(page)).toContainText("unsaved canvas edit");

await clickBackdrop(page);
await openFromSearch(page, otherTitle);
await expect(discardDialog(page)).toBeVisible();
await discardDialog(page)
.getByRole("button", { name: "Discard changes" })
.click();
await expect(panel(page)).toBeHidden();
await expect(editor(page)).toContainText(`Body of ${otherTitle}`);

await openFromRail(page, title);
await openFromSearch(page, title);
await expect(editor(page)).toContainText(`Body of ${title}`);
await expect(editor(page)).not.toContainText("unsaved canvas edit");
expect(posts, "discarding must not save the edit").toEqual([]);
});
Expand Down Expand Up @@ -153,9 +177,10 @@ test.describe("canvas note panel", () => {
page,
}) => {
await openFromRail(page, title);
await holdAutosave(page);
await typeIntoEditor(page, " saved canvas edit");

const save = await holdNextServerAction(page, "updateDoc", [noteId]);
const save = await holdNextServerAction(page, "saveDocContent", [noteId]);
await panel(page)
.getByRole("button", { name: "Save note changes" })
.click();
Expand All @@ -169,7 +194,7 @@ test.describe("canvas note panel", () => {
await expect(savingDialog(page)).toBeHidden();
await expect(panel(page)).toBeVisible();

await clickBackdrop(page);
await openFromSearch(page, otherTitle);
await expect(savingDialog(page)).toBeVisible();

save.release();
Expand All @@ -182,7 +207,8 @@ test.describe("canvas note panel", () => {
await expect(discardDialog(page)).toBeHidden();
await expect(panel(page)).toBeHidden();

await openFromRail(page, title);
// The rail still lists the search above, so reopen through search too.
await openFromSearch(page, title);
await expect(editor(page)).toContainText("saved canvas edit");
});

Expand Down
Loading