From 20f44cc64f29208d000de6b5042baee7aff84c69 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Fri, 5 Dec 2025 09:36:21 +0200 Subject: [PATCH] chore(react/launch_bar): reimplement week notes --- apps/client/src/widgets/buttons/calendar.ts | 35 +------------------ .../src/widgets/launch_bar/Calendar.tsx | 21 +++++++---- .../src/widgets/launch_bar/CalendarWidget.tsx | 25 +++++++++++++ 3 files changed, 41 insertions(+), 40 deletions(-) diff --git a/apps/client/src/widgets/buttons/calendar.ts b/apps/client/src/widgets/buttons/calendar.ts index 2e8728dc2..4406bd2b7 100644 --- a/apps/client/src/widgets/buttons/calendar.ts +++ b/apps/client/src/widgets/buttons/calendar.ts @@ -46,28 +46,6 @@ export default class CalendarWidget extends RightDropdownButtonWidget { this.manageFirstDayOfWeek(); this.initWeekCalculation(); - // Week click - this.$dropdownContent.on("click", ".calendar-week-number", async (ev) => { - if (!this.weekNoteEnable) { - return; - } - - const week = $(ev.target).closest(".calendar-week-number").attr("data-calendar-week-number"); - - if (week) { - const note = await dateNoteService.getWeekNote(week); - - if (note) { - appContext.tabManager.getActiveContext()?.setNote(note.noteId); - this.dropdown?.hide(); - } else { - toastService.showError(t("calendar.cannot_find_week_note")); - } - } - - ev.stopPropagation(); - }); - // Handle click events for the entire calendar widget this.$dropdownContent.on("click", (e) => { const $target = $(e.target); @@ -85,16 +63,6 @@ export default class CalendarWidget extends RightDropdownButtonWidget { }); } - private async getWeekNoteEnable() { - const noteId = await server.get(`search/${encodeURIComponent('#calendarRoot')}`); - if (noteId.length === 0) { - this.weekNoteEnable = false; - return; - } - const noteAttributes = await server.get(`notes/${noteId}/attributes`); - this.weekNoteEnable = noteAttributes.some(a => a.name === 'enableWeekNote'); - } - initWeekCalculation() { this.weekCalculationOptions = { firstWeekType: options.getInt("firstWeekOfYear") || 0, @@ -109,11 +77,10 @@ export default class CalendarWidget extends RightDropdownButtonWidget { } createWeekNumber(weekNumber: number) { - const weekNoteId = this.date.local().format('YYYY-') + 'W' + String(weekNumber).padStart(2, '0'); + let $newWeekNumber; if (this.weekNoteEnable) { - $newWeekNumber = $("").addClass("calendar-date"); if (this.weekNotes.includes(weekNoteId)) { $newWeekNumber.addClass("calendar-date-exists").attr("data-href", `#root/${weekNoteId}`); } diff --git a/apps/client/src/widgets/launch_bar/Calendar.tsx b/apps/client/src/widgets/launch_bar/Calendar.tsx index 5ddf56f9c..b058dd8c5 100644 --- a/apps/client/src/widgets/launch_bar/Calendar.tsx +++ b/apps/client/src/widgets/launch_bar/Calendar.tsx @@ -30,6 +30,7 @@ export interface CalendarArgs { todaysDate: Dayjs; activeDate: Dayjs | null; onDateClicked(date: string, e: TargetedMouseEvent): void; + onWeekClicked?: (week: string, e: TargetedMouseEvent) => void; } export default function Calendar(args: CalendarArgs) { @@ -76,7 +77,7 @@ function PreviousMonthDays({ date, info: { dates, weekNumbers }, ...args }: { da return ( <> - + {dates.map(date => )} ) @@ -96,7 +97,7 @@ function CurrentMonthDays({ date, firstDayOfWeekISO, ...args }: { date: Dayjs, f while (dateCursor.month() === currentMonth) { const weekNumber = getWeekNumber(dateCursor, firstDayOfWeekISO); if (dateCursor.isoWeekday() === firstDayOfWeekISO) { - items.push() + items.push() } items.push() @@ -140,10 +141,18 @@ function CalendarDay({ date, dateNotesForMonth, className, activeDate, todaysDat ); } -function CalendarWeek({ weekNumber }: { weekNumber: number }) { - return ( - {weekNumber} - ) +function CalendarWeek({ date, weekNumber, onWeekClicked }: { weekNumber: number } & Pick) { + if (onWeekClicked) { + const weekNoteId = date.local().format('YYYY-') + 'W' + String(weekNumber).padStart(2, '0'); + return ( + onWeekClicked(weekNoteId, e)} + >{weekNumber} + ) + } + + return ({weekNumber}); } export function getMonthInformation(date: Dayjs, firstDayISO: number, firstDayOfWeekISO: number) { diff --git a/apps/client/src/widgets/launch_bar/CalendarWidget.tsx b/apps/client/src/widgets/launch_bar/CalendarWidget.tsx index 9a89b2507..617dd8c80 100644 --- a/apps/client/src/widgets/launch_bar/CalendarWidget.tsx +++ b/apps/client/src/widgets/launch_bar/CalendarWidget.tsx @@ -12,6 +12,7 @@ import FormTextBox from "../react/FormTextBox"; import toast from "../../services/toast"; import date_notes from "../../services/date_notes"; import { Dropdown } from "bootstrap"; +import search from "../../services/search"; const MONTHS = [ t("calendar.january"), @@ -33,6 +34,19 @@ export default function CalendarWidget({ launcherNote }: { launcherNote: FNote } const [ calendarArgs, setCalendarArgs ] = useState>(); const [ date, setDate ] = useState(); const dropdownRef = useRef(null); + const [ enableWeekNotes, setEnableWeekNotes ] = useState(false); + const calendarRootRef = useRef(); + + async function checkEnableWeekNotes() { + if (!calendarRootRef.current) { + const notes = await search.searchForNotes("#calendarRoot"); + if (!notes.length) return; + calendarRootRef.current = notes[0]; + } + + if (!calendarRootRef.current) return; + setEnableWeekNotes(calendarRootRef.current.hasLabel("enableWeekNote")); + } return ( { + const note = await date_notes.getWeekNote(week); + if (note) { + appContext.tabManager.getActiveContext()?.setNote(note.noteId); + dropdownRef.current?.hide(); + } else { + toast.showError(t("calendar.cannot_find_week_note")); + } + e.stopPropagation(); + } : undefined} {...calendarArgs} /> }