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): redundant refresh when adding new item
Elian Doran3e3c3e3bb4bd
diff --git a/apps/client/src/services/attributes.ts b/apps/client/src/services/attributes.tsindex 0b156dcf3a..728ff515c6 100644--- a/apps/client/src/services/attributes.ts+++ b/apps/client/src/services/attributes.ts@@ -14,13 +14,13 @@ async function addLabel(noteId: string, name: string, value: string = "", isInhe});}-export async function setLabel(noteId: string, name: string, value: string = "", isInheritable = false) {+export async function setLabel(noteId: string, name: string, value: string = "", isInheritable = false, componentId?: string) {await server.put(`notes/${noteId}/set-attribute`, {type: "label",name,value,- isInheritable- });+ isInheritable,+ }, componentId);}export async function setRelation(noteId: string, name: string, value: string = "", isInheritable = false) {diff --git a/apps/client/src/widgets/collections/calendar/api.ts b/apps/client/src/widgets/collections/calendar/api.tsindex eef391108d..65f8905e4c 100644--- a/apps/client/src/widgets/collections/calendar/api.ts+++ b/apps/client/src/widgets/collections/calendar/api.ts@@ -1,8 +1,8 @@import { CreateChildrenResponse } from "@triliumnext/commons";-import server from "../../../services/server";+import FNote from "../../../entities/fnote";import { setAttribute, setLabel } from "../../../services/attributes";-import froca from "../../../services/froca";+import server from "../../../services/server";interface NewEventOpts {title: string;@@ -10,6 +10,7 @@ interface NewEventOpts {endDate?: string | null;startTime?: string | null;endTime?: string | null;+ componentId?: string;}interface ChangeEventOpts {@@ -19,24 +20,24 @@ interface ChangeEventOpts {endTime?: string | null;}-export async function newEvent(parentNote: FNote, { title, startDate, endDate, startTime, endTime }: NewEventOpts) {+export async function newEvent(parentNote: FNote, { title, startDate, endDate, startTime, endTime, componentId }: NewEventOpts) {// Create the note.const { note } = await server.post<CreateChildrenResponse>(`notes/${parentNote.noteId}/children?target=into`, {title,content: "",type: "text"- });+ }, componentId);// Set the attributes.- setLabel(note.noteId, "startDate", startDate);+ setLabel(note.noteId, "startDate", startDate, false, componentId);if (endDate) {- setLabel(note.noteId, "endDate", endDate);+ setLabel(note.noteId, "endDate", endDate, false, componentId);}if (startTime) {- setLabel(note.noteId, "startTime", startTime);+ setLabel(note.noteId, "startTime", startTime, false, componentId);}if (endTime) {- setLabel(note.noteId, "endTime", endTime);+ setLabel(note.noteId, "endTime", endTime, false, componentId);}}diff --git a/apps/client/src/widgets/collections/calendar/index.tsx b/apps/client/src/widgets/collections/calendar/index.tsxindex 9d1721a97a..fd7669646e 100644--- a/apps/client/src/widgets/collections/calendar/index.tsx+++ b/apps/client/src/widgets/collections/calendar/index.tsx@@ -5,7 +5,7 @@ import { DateSelectArg, EventChangeArg, EventMountArg, EventSourceFuncArg, Localimport { DateClickArg } from "@fullcalendar/interaction";import { DISPLAYABLE_LOCALE_IDS } from "@triliumnext/commons";import { RefObject } from "preact";-import { useCallback, useEffect, useMemo, useRef, useState } from "preact/hooks";+import { useCallback, useContext, useEffect, useMemo, useRef, useState } from "preact/hooks";import appContext from "../../../components/app_context";import FNote from "../../../entities/fnote";@@ -17,6 +17,7 @@ import { isMobile } from "../../../services/utils";import ActionButton from "../../react/ActionButton";import Button, { ButtonGroup } from "../../react/Button";import { useNoteLabel, useNoteLabelBoolean, useResizeObserver, useSpacedUpdate, useTriliumEvent, useTriliumOption, useTriliumOptionInt } from "../../react/hooks";+import { ParentComponent } from "../../react/react_utils";import TouchBar, { TouchBarButton, TouchBarLabel, TouchBarSegmentedControl, TouchBarSpacer } from "../../react/TouchBar";import { ViewModeProps } from "../interface";import { changeEvent, newEvent } from "./api";@@ -87,6 +88,7 @@ export const LOCALE_MAPPINGS: Record<DISPLAYABLE_LOCALE_IDS, (() => Promise<{ de};export default function CalendarView({ note, noteIds }: ViewModeProps<CalendarViewData>) {+ const parentComponent = useContext(ParentComponent);const containerRef = useRef<HTMLDivElement>(null);const calendarRef = useRef<FullCalendar>(null);@@ -105,24 +107,24 @@ export default function CalendarView({ note, noteIds }: ViewModeProps<CalendarViconst eventBuilder = useMemo(() => {if (!isCalendarRoot) {return async () => await buildEvents(noteIds);- }+ }return async (e: EventSourceFuncArg) => await buildEventsForCalendar(note, e);-}, [isCalendarRoot, noteIds]);const plugins = usePlugins(isEditable, isCalendarRoot);const locale = useLocale();const { eventDidMount } = useEventDisplayCustomization(note);- const editingProps = useEditing(note, isEditable, isCalendarRoot);+ const editingProps = useEditing(note, isEditable, isCalendarRoot, parentComponent?.componentId);// React to changes.useTriliumEvent("entitiesReloaded", ({ loadResults }) => {if (loadResults.getNoteIds().some(noteId => noteIds.includes(noteId)) // note title change.- || loadResults.getAttributeRows().some((a) => noteIds.includes(a.noteId ?? ""))) // subnote change.+ || loadResults.getAttributeRows(parentComponent?.componentId).some((a) => noteIds.includes(a.noteId ?? ""))) // subnote change.{// Defer execution after the load results are processed so that the event builder has the updated data to work with.setTimeout(() => {+ console.log("Refresh");calendarRef.current?.refetchEvents();}, 0);}@@ -222,7 +224,7 @@ function useLocale() {return calendarLocale;}-function useEditing(note: FNote, isEditable: boolean, isCalendarRoot: boolean) {+function useEditing(note: FNote, isEditable: boolean, isCalendarRoot: boolean, componentId: string | undefined) {const onCalendarSelection = useCallback(async (e: DateSelectArg) => {const { startDate, endDate } = parseStartEndDateFromEvent(e);if (!startDate) return;@@ -234,8 +236,8 @@ function useEditing(note: FNote, isEditable: boolean, isCalendarRoot: boolean) {return;}- newEvent(note, { title, startDate, endDate, startTime, endTime });- }, [ note ]);+ newEvent(note, { title, startDate, endDate, startTime, endTime, componentId });+ }, [ note, componentId ]);const onEventChange = useCallback(async (e: EventChangeArg) => {const { startDate, endDate } = parseStartEndDateFromEvent(e.event);