Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(calendar): stop a journal emptying its grid to redraw the same events
Elian Doran369ead06f25a
diff --git a/apps/client/src/widgets/collections/calendar/index.tsx b/apps/client/src/widgets/collections/calendar/index.tsxindex e277137926..4a63cfb1b9 100644--- a/apps/client/src/widgets/collections/calendar/index.tsx+++ b/apps/client/src/widgets/collections/calendar/index.tsx@@ -38,7 +38,7 @@ import GhostPopover from "./GhostPopover";import { openCalendarContextMenu } from "./context_menu";import { CalendarSelection, EventDraft } from "./selection";import { buildEvents, buildEventsForCalendar } from "./event_builder";-import { formatDateToLocalISO, formatTimeToLocalISO, isAttributeChangeAffecting, isValidDuration, parseDurationSeconds, parseStartEndDateFromEvent, parseStartEndTimeFromEvent } from "./utils";+import { formatDateToLocalISO, formatTimeToLocalISO, isAttributeChangeAffecting, isBranchChangeAffecting, isValidDuration, parseDurationSeconds, parseStartEndDateFromEvent, parseStartEndTimeFromEvent } from "./utils";interface CalendarViewData {@@ -146,12 +146,12 @@ export default function CalendarView({ note, noteIds }: ViewModeProps<CalendarVi// Worked out once and handed to both the grid and the tap that makes an event of one of its// slots (see draftFromDateClick), so that the two cannot come to disagree on a slot's length.const effectiveSlotDuration = isValidDuration(slotDuration) ? slotDuration : DEFAULT_SLOT_DURATION;- const eventBuilder = useMemo(() => {- if (!isCalendarRoot) {- return async () => await buildEvents(noteIds);- }- return async (e: EventSourceFuncInfo) => await buildEventsForCalendar(note, e);- }, [isCalendarRoot, noteIds]);+ // Memoized apart, and each on only what it reads: FullCalendar knows a source by the identity+ // of the function, and a new one empties the grid before it fetches.+ const rootEventBuilder = useMemo(() =>+ async (e: EventSourceFuncInfo) => await buildEventsForCalendar(note, e), [note]);+ const collectionEventBuilder = useMemo(() => async () => await buildEvents(noteIds), [noteIds]);+ const eventBuilder = isCalendarRoot ? rootEventBuilder : collectionEventBuilder;const plugins = usePlugins(isEditable, isCalendarRoot);const locale = useLocale();@@ -303,7 +303,15 @@ export default function CalendarView({ note, noteIds }: ViewModeProps<CalendarViif (noteId) drawnNoteIds.add(String(noteId));}- if (isAttributeChangeAffecting(loadResults.getAttributeRows(parentComponent?.componentId), drawnNoteIds)) {+ // A note filed anywhere in the journal may be a chip on a calendar root, which draws its+ // day notes and their children rather than a list of ids (see isBranchChangeAffecting).+ // Only for a root: a collection's own source is built from the ids, so it is renewed as+ // they are, and asking here as well would fetch the same events twice.+ const isFilingAffecting = isCalendarRoot+ && isBranchChangeAffecting(loadResults.getBranchRows(), note.noteId, drawnNoteIds);++ if (isFilingAffecting+ || isAttributeChangeAffecting(loadResults.getAttributeRows(parentComponent?.componentId), drawnNoteIds)) {// Defer execution after the load results are processed so that the event builder has the updated data to work with.setTimeout(() => api.refetchEvents(), 0);return; // early return since we'll refresh the events anywaydiff --git a/apps/client/src/widgets/collections/calendar/utils.spec.ts b/apps/client/src/widgets/collections/calendar/utils.spec.tsindex 9bac1764b7..4c08a094d9 100644--- a/apps/client/src/widgets/collections/calendar/utils.spec.ts+++ b/apps/client/src/widgets/collections/calendar/utils.spec.ts@@ -1,8 +1,8 @@import { describe, expect, it } from "vitest";-import { AttributeRow } from "../../../services/load_results";+import { AttributeRow, BranchRow } from "../../../services/load_results";import { buildNote } from "../../../test/easy-froca";-import { formatDateToLocalISO, formatTimeToLocalISO, getMonthsInDateRange, isAttributeChangeAffecting, offsetDate, parseStartEndDateFromEvent, parseStartEndTimeFromEvent } from "./utils";+import { formatDateToLocalISO, formatTimeToLocalISO, getMonthsInDateRange, isAttributeChangeAffecting, isBranchChangeAffecting, offsetDate, parseStartEndDateFromEvent, parseStartEndTimeFromEvent } from "./utils";/*** A moment built in the machine's own zone, which is what the calendar hands these functions: a@@ -177,3 +177,33 @@ describe("isAttributeChangeAffecting", () => {expect(isAttributeChangeAffecting([], [ "drawn" ])).toBe(false);});});++describe("isBranchChangeAffecting", () => {+ /** What a reload hands over for one branch; only where it puts the note is read. */+ function filed(parentNoteId: string): BranchRow {+ return { branchId: `${parentNoteId}_child`, componentId: "some-component", parentNoteId };+ }++ it("answers for a note filed under the calendar itself, which is where a new year lands", () => {+ expect(isBranchChangeAffecting([ filed("journal") ], "journal", [])).toBe(true);+ });++ it("answers for a note filed under one the calendar already knows of", () => {+ // The day note a click on an empty day makes: filed under a month the calendar knows, and+ // itself unknown until the events are built again.+ expect(isBranchChangeAffecting([ filed("month") ], "journal", [ "year", "month" ])).toBe(true);+ });++ it("answers where only the outermost of several new notes hangs off something known", () => {+ // A click on a day of a month that has never been opened makes all three at once, and the+ // day's own parent is as new as the day is.+ const rows = [ filed("journal"), filed("year"), filed("month") ];++ expect(isBranchChangeAffecting(rows, "journal", [])).toBe(true);+ });++ it("keeps quiet for a note filed somewhere else entirely, and for no filing at all", () => {+ expect(isBranchChangeAffecting([ filed("elsewhere") ], "journal", [ "year", "month" ])).toBe(false);+ expect(isBranchChangeAffecting([], "journal", [ "year" ])).toBe(false);+ });+});diff --git a/apps/client/src/widgets/collections/calendar/utils.ts b/apps/client/src/widgets/collections/calendar/utils.tsindex fe251cee19..b19347da51 100644--- a/apps/client/src/widgets/collections/calendar/utils.ts+++ b/apps/client/src/widgets/collections/calendar/utils.ts@@ -3,7 +3,7 @@ import { DateSelectInfo, EventApi } from "fullcalendar";import FNote from "../../../entities/fnote";import attributes from "../../../services/attributes";import froca from "../../../services/froca";-import { AttributeRow } from "../../../services/load_results";+import { AttributeRow, BranchRow } from "../../../services/load_results";export function parseStartEndDateFromEvent(e: DateSelectInfo | EventApi) {const startDate = formatDateToLocalISO(e.start);@@ -94,6 +94,25 @@ export function isAttributeChangeAffecting(attributeRows: AttributeRow[], noteId});}+/**+ * Whether a note has been filed somewhere a calendar root draws from, and so asks for the events to+ * be built again — a day note made by a click on an empty day, a note dropped under one afterwards,+ * or either of them taken away.+ *+ * Asked of the branches rather than of the ids because a root builds its events from the calendar+ * note and the range it is showing, not from a list: the day note just made is in no list yet, and+ * the list the collection keeps is a refresh behind at this point anyway. Anywhere within the+ * journal counts, a day note being reachable only through the year and month notes above it —+ * whichever of the three the reload happens to name, the branch of the outermost new one hangs off+ * something already known.+ */+export function isBranchChangeAffecting(branchRows: BranchRow[], rootNoteId: string, noteIds: Iterable<string>) {+ const drawnNoteIds = new Set(noteIds);++ return branchRows.some(({ parentNoteId }) =>+ parentNoteId === rootNoteId || (!!parentNoteId && drawnNoteIds.has(parentNoteId)));+}+/** The labels the calendar draws an event by, each of which a note may rename for itself via the* matching `#calendar:` label (see {@link getCustomisableLabel}). */export type EventLabelName = "startDate" | "endDate" | "startTime" | "endTime" | "recurrence";