Skip to content

Build your personal knowledge base with Trilium Notes

fix(calendar): let a ghost be asked again where its note could not be made

Elian Dorane6d720cabf74
diff --git a/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsx b/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsx
new file mode 100644
index 0000000000..3fd3ad9fa5
--- /dev/null
+++ b/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsx
@@ -0,0 +1,85 @@
+import { render } from "preact";
+import { act } from "preact/test-utils";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+import GhostPopover from "./GhostPopover";
+import { EventDraft } from "./selection";
+
+describe("GhostPopover", () => {
+ let container: HTMLElement;
+
+ beforeEach(() => {
+ container = document.createElement("div");
+ document.body.appendChild(container);
+ });
+
+ afterEach(() => {
+ render(null, container);
+ container.remove();
+ document.body.innerHTML = "";
+ });
+
+ const draft: EventDraft = { startDate: "2026-06-05" };
+
+ function mount(onCommit: (title: string) => Promise<void> | void) {
+ act(() => render(
+ <GhostPopover
+ draft={draft}
+ anchor={{ x: 10, y: 10 }}
+ container={container}
+ onCommit={onCommit}
+ onCancel={() => {}}
+ onDismiss={() => {}}
+ />,
+ container));
+ }
+
+ /** The form's only button, which is the one that asks for the note. */
+ const createButton = () => document.querySelector<HTMLButtonElement>(".calendar-ghost-actions button");
+ const titleInput = () => document.querySelector<HTMLInputElement>(".calendar-ghost-body input");
+
+ it("asks for the note once, however many times the ask is made before it arrives", async () => {
+ // A request still out, which is the window each further Enter would make its own note in.
+ const onCommit = vi.fn().mockReturnValue(new Promise<void>(() => {}));
+ mount(onCommit);
+
+ act(() => { createButton()?.click(); });
+ act(() => { createButton()?.click(); });
+
+ expect(onCommit).toHaveBeenCalledTimes(1);
+ // Held shut for as long as the asking lasts.
+ expect(createButton()?.disabled).toBe(true);
+ });
+
+ it("opens the form again where the note could not be made, keeping the title for another try", async () => {
+ const onCommit = vi.fn().mockRejectedValue(new Error("the server said no"));
+ mount(onCommit);
+
+ const input = titleInput();
+ if (!input) throw new Error("The ghost has no title field.");
+ input.value = "Fair";
+ act(() => { input.dispatchEvent(new Event("input", { bubbles: true })); });
+
+ await act(async () => { createButton()?.click(); });
+
+ // Nothing was created and the calendar keeps the ghost up, so the button that asks must be
+ // pressable again — otherwise the title just typed can only be discarded.
+ expect(createButton()?.disabled).toBe(false);
+ expect(titleInput()?.value).toBe("Fair");
+
+ // And a second try really does ask again.
+ await act(async () => { createButton()?.click(); });
+ expect(onCommit).toHaveBeenCalledTimes(2);
+ expect(onCommit).toHaveBeenLastCalledWith("Fair");
+ });
+
+ it("hands over the title as typed, an empty one standing for the calendar's own naming", async () => {
+ const onCommit = vi.fn().mockResolvedValue(undefined);
+ mount(onCommit);
+
+ await act(async () => { createButton()?.click(); });
+
+ // Committing blank is meaningful: the note is then named by the calendar's #titleTemplate.
+ expect(onCommit).toHaveBeenCalledWith("");
+ });
+});
diff --git a/apps/client/src/widgets/collections/calendar/GhostPopover.tsx b/apps/client/src/widgets/collections/calendar/GhostPopover.tsx
index 435ee4b558..1afe3a73dc 100644
--- a/apps/client/src/widgets/collections/calendar/GhostPopover.tsx
+++ b/apps/client/src/widgets/collections/calendar/GhostPopover.tsx
@@ -34,7 +34,9 @@ export default function GhostPopover({ draft, anchor, container, onCommit, onCan
anchor: AnchorPoint | null;
/** The calendar the range's shading is read out of (see {@link ghostAnchorRect}). */
container: HTMLElement | null;
- onCommit(title: string): void;
+ /** Answers when the note has been made, or rejects having failed to make one — the ghost stays
+ * up for a second try either way it is told (see {@link commit}). */
+ onCommit(title: string): Promise<void> | void;
/** The draft given up on: Escape, or the close button. */
onCancel(): void;
/**
@@ -60,10 +62,25 @@ export default function GhostPopover({ draft, anchor, container, onCommit, onCan
*/
const focusTitle = useCallback(() => titleRef.current?.focus(), []);
- const commit = () => {
+ /**
+ * Asks for the note, once: the form is held shut while the request is out, or each Enter falling
+ * before the note arrives would make one of its own.
+ *
+ * Held shut only for as long as the asking lasts. A request that fails leaves the draft exactly
+ * as it stood — nothing was created, and the calendar keeps the ghost up — so the form is opened
+ * again for a second try rather than stranding the title just typed behind a button that can no
+ * longer be pressed. What went wrong is already said by the request itself (see server.ts, which
+ * shows the error before it throws). Succeeded, there is nothing to reopen: the draft resolves
+ * and the ghost goes with it (see commitDraft in index.tsx).
+ */
+ const commit = async () => {
if (committing) return;
setCommitting(true);
- onCommit(title);
+ try {
+ await onCommit(title);
+ } catch {
+ setCommitting(false);
+ }
};
// The sheet answers Escape itself, being a dialog; the popover is not, so it listens.
@@ -86,7 +103,7 @@ export default function GhostPopover({ draft, anchor, container, onCommit, onCan
placeholder={t("calendar_view.draft_title_placeholder")}
onChange={setTitle}
onKeyDown={(e) => {
- if (e.key === "Enter") commit();
+ if (e.key === "Enter") void commit();
}}
/>
@@ -100,7 +117,7 @@ export default function GhostPopover({ draft, anchor, container, onCommit, onCan
kind="primary"
text={t("calendar_view.create_event")}
disabled={committing}
- onClick={commit}
+ onClick={() => void commit()}
/>
</div>
</div>
diff --git a/apps/client/src/widgets/collections/calendar/index.tsx b/apps/client/src/widgets/collections/calendar/index.tsx
index 8b3e49b62b..9cd83b9cea 100644
--- a/apps/client/src/widgets/collections/calendar/index.tsx
+++ b/apps/client/src/widgets/collections/calendar/index.tsx
@@ -362,7 +362,9 @@ export default function CalendarView({ note, noteIds }: ViewModeProps<CalendarVi
draft={selection.draft}
anchor={selection.anchor}
container={containerRef.current}
- onCommit={(title) => void commitDraft(title)}
+ /* Handed over whole rather than fired and forgotten: a creation that fails has
+ to reach the ghost, which is what opens its form again for another try. */
+ onCommit={commitDraft}
onCancel={cancelDraft}
onDismiss={dismissDraft}
/>