Skip to content

Build your personal knowledge base with Trilium Notes

fix(calendar): reset the date draft on another note or a failed write

Elian Doranb1f57bf112a2
diff --git a/apps/client/src/widgets/collections/calendar/EventDatesEditor.spec.tsx b/apps/client/src/widgets/collections/calendar/EventDatesEditor.spec.tsx
index ec954914b4..710afd582a 100644
--- a/apps/client/src/widgets/collections/calendar/EventDatesEditor.spec.tsx
+++ b/apps/client/src/widgets/collections/calendar/EventDatesEditor.spec.tsx
@@ -110,6 +110,32 @@ describe("EventDatesEditor", () => {
expect(setLabel).toHaveBeenLastCalledWith(note.noteId, "endTime", "18:00");
});
+ it("drops a pending edit when it shows another event with the same hours", () => {
+ mount(buildNote({ title: "First", "#startDate": "2026-06-05", "#startTime": "13:00", "#endTime": "14:30" }));
+ commitValue(timeInputs()[1], "15:00");
+
+ const second = buildNote({ title: "Second", "#startDate": "2026-06-05", "#startTime": "13:00", "#endTime": "14:30" });
+ mount(second);
+ expect(timeInputs()[1].value).toBe("14:30");
+
+ commitValue(timeInputs()[0], "16:00");
+ expect(setLabel).toHaveBeenLastCalledWith(second.noteId, "endTime", "17:30");
+ });
+
+ it("shows the stored hours again after a failed write, so the edit can be retried", async () => {
+ const note = buildNote({ title: "Meeting", "#startDate": "2026-06-05", "#startTime": "13:00", "#endTime": "14:30" });
+ mount(note);
+ setLabel.mockRejectedValueOnce(new Error("offline"));
+
+ commitValue(timeInputs()[1], "15:00");
+ await act(() => new Promise((resolve) => setTimeout(resolve)));
+ expect(timeInputs()[1].value).toBe("14:30");
+
+ commitValue(timeInputs()[1], "15:00");
+ expect(setLabel).toHaveBeenCalledTimes(2);
+ expect(setLabel).toHaveBeenLastCalledWith(note.noteId, "endTime", "15:00");
+ });
+
it("shows the day an event actually ends on, and writes it once an edit is made", () => {
// Stored as ending at 08:00 on its own start day, which the calendar draws as the next day.
const note = buildNote({
diff --git a/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsx b/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsx
index e6d84ff873..f1917e78fb 100644
--- a/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsx
+++ b/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsx
@@ -1,5 +1,5 @@
import { ComponentChildren } from "preact";
-import { useEffect, useState } from "preact/hooks";
+import { useEffect, useRef, useState } from "preact/hooks";
import FNote from "../../../entities/fnote";
import { t } from "../../../services/i18n";
@@ -23,17 +23,22 @@ export default function EventDatesEditor({ note, repeats }: {
const [ endTime, setEndTime ] = useEventLabel(note, "endTime");
// The labels change only when the server echoes a write, so the last edit is kept here and the
- // next edit is compared against it. A label change from the server replaces it.
+ // next edit is compared against it. Another note, a label change from the server or a failed
+ // write replaces it with the stored labels.
const stored: EventDraft = { startDate: startDate ?? "", endDate, startTime, endTime };
+ const storedRef = useRef(stored);
+ storedRef.current = stored;
const [ value, setValue ] = useState(stored);
- useEffect(() => setValue(stored), [ startDate, endDate, startTime, endTime ]);
+ useEffect(() => setValue(stored), [ note, startDate, endDate, startTime, endTime ]);
const onChange = (next: EventDraft) => {
setValue(next);
- if (next.startDate !== value.startDate) setStartDate(next.startDate);
- if ((next.endDate ?? null) !== (value.endDate ?? null)) setEndDate(next.endDate ?? null);
- if ((next.startTime ?? null) !== (value.startTime ?? null)) setStartTime(next.startTime ?? null);
- if ((next.endTime ?? null) !== (value.endTime ?? null)) setEndTime(next.endTime ?? null);
+ const writes: Promise<unknown>[] = [];
+ if (next.startDate !== value.startDate) writes.push(setStartDate(next.startDate));
+ if ((next.endDate ?? null) !== (value.endDate ?? null)) writes.push(setEndDate(next.endDate ?? null));
+ if ((next.startTime ?? null) !== (value.startTime ?? null)) writes.push(setStartTime(next.startTime ?? null));
+ if ((next.endTime ?? null) !== (value.endTime ?? null)) writes.push(setEndTime(next.endTime ?? null));
+ Promise.all(writes).catch(() => setValue(storedRef.current));
};
return (
diff --git a/apps/client/src/widgets/collections/calendar/hooks.ts b/apps/client/src/widgets/collections/calendar/hooks.ts
index 366018275d..c77a0b95cc 100644
--- a/apps/client/src/widgets/collections/calendar/hooks.ts
+++ b/apps/client/src/widgets/collections/calendar/hooks.ts
@@ -10,21 +10,17 @@ import { EVENT_LABELS, EventLabelName } from "./utils";
* on the grid does (see changeEvent in api.ts). Clearing clears the stock label too: a stale value
* left there would come back through the builder's fallback the moment the renamed one went.
*/
-export function useEventLabel(note: FNote, defaultName: EventLabelName): [string | null, (value: string | null) => void] {
+export function useEventLabel(note: FNote, defaultName: EventLabelName): [string | null, (value: string | null) => Promise<unknown>] {
const [ customName ] = useNoteLabel(note, `calendar:${defaultName}`);
const [ customValue, setCustomValue ] = useNoteLabelByName(note, customName || defaultName);
const [ stockValue, setStockValue ] = useNoteLabelByName(note, defaultName);
const value = (customName ? customValue : null) || stockValue || null;
const setValue = (newValue: string | null) => {
- if (customName) {
- setCustomValue(newValue);
- if (newValue === null) {
- setStockValue(null);
- }
- } else {
- setStockValue(newValue);
+ if (!customName) {
+ return setStockValue(newValue);
}
+ return Promise.all([ setCustomValue(newValue), newValue === null && setStockValue(null) ]);
};
return [ value, setValue ];
diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx
index 60f19185e9..3d15ed08e9 100644
--- a/apps/client/src/widgets/react/hooks.tsx
+++ b/apps/client/src/widgets/react/hooks.tsx
@@ -761,7 +761,7 @@ export function useNoteLabel(note: FNote | undefined | null, labelName: FilterLa
* themselves, e.g. a label a `#calendar:startDate` renaming points at. Prefer {@link useNoteLabel}
* wherever the name is a builtin one, as it holds the name to the declared vocabulary.
*/
-export function useNoteLabelByName(note: FNote | undefined | null, labelName: string): [string | null | undefined, (newValue: string | null | undefined) => void] {
+export function useNoteLabelByName(note: FNote | undefined | null, labelName: string): [string | null | undefined, (newValue: string | null | undefined) => Promise<unknown>] {
const [ , setLabelValue ] = useState<string | null | undefined>();
useEffect(() => setLabelValue(note?.getLabelValue(labelName) ?? null), [ note, labelName ]);
@@ -778,12 +778,12 @@ export function useNoteLabelByName(note: FNote | undefined | null, labelName: st
}
});
- const setter = useCallback((value: string | null | undefined) => {
+ const setter = useCallback(async (value: string | null | undefined) => {
if (note) {
if (value !== null) {
- attributes.setLabel(note.noteId, labelName, value);
+ await attributes.setLabel(note.noteId, labelName, value);
} else {
- attributes.removeOwnedLabelByName(note, labelName);
+ await attributes.removeOwnedLabelByName(note, labelName);
}
}
}, [note, labelName]);