From a88224fb8a48158e1ef640760b4fb0191d8dd352 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 4 Dec 2026 13:00:30 +0000 Subject: [PATCH 1/2] fix(web): keep narrow week windows aligned to sunday Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HmwVQGfsHYrmcJ8Ff6cGAg --- .../src/views/Week/hooks/useWeek.test.tsx | 18 +++- .../src/views/Week/hooks/useWeek.ts | 7 +- .../views/Week/util/week-window.util.test.ts | 84 +++++++++---------- .../src/views/Week/util/week-window.util.ts | 56 ++++++++----- e2e/navigation/go-to-date.spec.ts | 6 +- 5 files changed, 93 insertions(+), 78 deletions(-) diff --git a/apps/calendar-web/src/views/Week/hooks/useWeek.test.tsx b/apps/calendar-web/src/views/Week/hooks/useWeek.test.tsx index b6993495c0..d9f185f2dc 100644 --- a/apps/calendar-web/src/views/Week/hooks/useWeek.test.tsx +++ b/apps/calendar-web/src/views/Week/hooks/useWeek.test.tsx @@ -78,6 +78,16 @@ describe("useWeek", () => { expect(result.current.component.isCurrentWeek).toBe(true); }); + it("defaults a partial-week anchor to Sunday when today fits", () => { + const today = dayjs("2026-10-29", DATE_FORMAT); // Thursday + + const { result } = renderHook(() => useWeek(today, 5)); + + expect(result.current.component.startOfView.format(DATE_FORMAT)).toBe( + "2026-10-25", + ); + }); + it("defaults the anchor so today stays visible on partial-week widths", () => { const today = dayjs("2026-10-30", DATE_FORMAT); // Friday @@ -86,11 +96,11 @@ describe("useWeek", () => { expect( result.current.component.weekDays.map((d) => d.format(DATE_FORMAT)), ).toEqual([ - "2026-10-27", - "2026-10-28", - "2026-10-29", "2026-10-30", "2026-10-31", + "2026-11-01", + "2026-11-02", + "2026-11-03", ]); expect(result.current.component.isCurrentWeek).toBe(true); }); @@ -208,7 +218,7 @@ describe("useWeek", () => { expect(mockNavigate).toHaveBeenCalledWith({ to: "/week/$dateString", - params: { dateString: "2026-10-27" }, + params: { dateString: "2026-10-30" }, }); }); diff --git a/apps/calendar-web/src/views/Week/hooks/useWeek.ts b/apps/calendar-web/src/views/Week/hooks/useWeek.ts index 408bf2ff64..e90c9d8455 100644 --- a/apps/calendar-web/src/views/Week/hooks/useWeek.ts +++ b/apps/calendar-web/src/views/Week/hooks/useWeek.ts @@ -10,6 +10,7 @@ import { useEffectiveTimeZone } from "@web/timezone/effective-timezone.store"; import { computeVisibleWindowOffset, currentWeekWindowStart, + pagedWindowStart, WEEK_DAY_COUNT, weekEventQueryRange, weekEventsQueryWindow, @@ -89,11 +90,11 @@ export const useWeek = ( // Defer until the current range has settled so first paint is not competing // with adjacent Sync drains. const previousStart = useMemo( - () => start.subtract(visibleDayCount, "day"), + () => pagedWindowStart(start, -1, visibleDayCount), [start, visibleDayCount], ); const nextStart = useMemo( - () => start.add(visibleDayCount, "day"), + () => pagedWindowStart(start, 1, visibleDayCount), [start, visibleDayCount], ); const previous = useMemo( @@ -135,7 +136,7 @@ export const useWeek = ( const pageWindow = useCallback( (direction: 1 | -1, source: WeekNavigationSource) => { navigationSourceRef.current = source; - setAnchor(start.add(direction * visibleDayCount, "day")); + setAnchor(pagedWindowStart(start, direction, visibleDayCount)); }, [setAnchor, start, visibleDayCount], ); diff --git a/apps/calendar-web/src/views/Week/util/week-window.util.test.ts b/apps/calendar-web/src/views/Week/util/week-window.util.test.ts index 68c2b80fa9..a693bb4392 100644 --- a/apps/calendar-web/src/views/Week/util/week-window.util.test.ts +++ b/apps/calendar-web/src/views/Week/util/week-window.util.test.ts @@ -10,12 +10,12 @@ import { setPinnedTimeZone, } from "@web/timezone/effective-timezone.store"; import { - anchorDateForWindowOffset, computeVisibleDayCount, computeVisibleWindowOffset, currentWeekWindowStart, isAllDayEventInVisibleDays, isTimedEventInVisibleDays, + pagedWindowStart, WEEK_DAY_COUNT, weekEventQueryRange, weekEventsQueryWindow, @@ -59,22 +59,18 @@ describe("computeVisibleDayCount", () => { }); describe("currentWeekWindowStart", () => { - it("keeps Friday visible in a five-day window (staging journey viewport)", () => { - const friday = dayjs("2026-10-30T09:00:00"); - const start = currentWeekWindowStart(friday, 5); + it("starts a narrow window on Sunday when the day fits", () => { + const thursday = dayjs("2026-10-29T09:00:00"); + expect(currentWeekWindowStart(thursday, 5).format("YYYY-MM-DD")).toBe( + "2026-10-25", + ); + }); - expect(start.format("YYYY-MM-DD")).toBe("2026-10-27"); - expect( - [...Array(5)].map((_, index) => - start.add(index, "day").format("YYYY-MM-DD"), - ), - ).toEqual([ - "2026-10-27", - "2026-10-28", - "2026-10-29", + it("moves to the week's next page when the day is past the first", () => { + const friday = dayjs("2026-10-30T09:00:00"); + expect(currentWeekWindowStart(friday, 5).format("YYYY-MM-DD")).toBe( "2026-10-30", - "2026-10-31", - ]); + ); }); it("uses Sunday at full week width", () => { @@ -105,7 +101,7 @@ describe("weekViewDateString", () => { it("keeps the picked day visible in a narrow window", () => { showWindow(0, 3); - expect(weekViewDateString(weekDay(5))).toBe("2026-07-02"); + expect(weekViewDateString(weekDay(5))).toBe("2026-07-01"); }); }); @@ -118,38 +114,34 @@ describe("computeVisibleWindowOffset", () => { } }); - it("centers the anchor day", () => { - expect( - computeVisibleWindowOffset({ anchorIndex: 3, visibleDayCount: 3 }), - ).toBe(2); // Tue..Thu around Wed + it("steps through the week from Sunday", () => { + const offsets = [0, 1, 2, 3, 4, 5, 6].map((anchorIndex) => + computeVisibleWindowOffset({ anchorIndex, visibleDayCount: 3 }), + ); + expect(offsets).toEqual([0, 0, 0, 3, 3, 3, 6]); }); +}); - it("clamps at the week boundaries", () => { - expect( - computeVisibleWindowOffset({ anchorIndex: 0, visibleDayCount: 3 }), - ).toBe(0); - expect( - computeVisibleWindowOffset({ anchorIndex: 6, visibleDayCount: 3 }), - ).toBe(4); - }); - - it("round-trips with anchorDateForWindowOffset", () => { - for (let count = 1; count <= 7; count++) { - const maxOffset = WEEK_DAY_COUNT - count; - for (let offset = 0; offset <= maxOffset; offset++) { - const anchor = anchorDateForWindowOffset({ - weekStart, - windowOffset: offset, - visibleDayCount: count, - }); - expect( - computeVisibleWindowOffset({ - anchorIndex: anchor.diff(weekStart, "day"), - visibleDayCount: count, - }), - ).toBe(offset); - } - } +describe("pagedWindowStart", () => { + const page = (date: string, direction: 1 | -1, count: number) => + pagedWindowStart(dayjs(date), direction, count).format("YYYY-MM-DD"); + + it("keeps a six-day window starting on Sunday week after week", () => { + // Sun Dec 6 -> Sat Dec 12 (last page) -> Sun Dec 13 + expect(page("2026-12-06", 1, 6)).toBe("2026-12-12"); + expect(page("2026-12-12", 1, 6)).toBe("2026-12-13"); + expect(page("2026-12-13", -1, 6)).toBe("2026-12-12"); + expect(page("2026-12-12", -1, 6)).toBe("2026-12-06"); + }); + + it("pages a full week by seven days", () => { + expect(page("2026-12-06", 1, 7)).toBe("2026-12-13"); + expect(page("2026-12-06", -1, 7)).toBe("2026-11-29"); + }); + + it("keeps a manual shift when paging", () => { + expect(page("2026-12-07", 1, 6)).toBe("2026-12-13"); + expect(page("2026-12-07", -1, 6)).toBe("2026-12-01"); }); }); diff --git a/apps/calendar-web/src/views/Week/util/week-window.util.ts b/apps/calendar-web/src/views/Week/util/week-window.util.ts index cb03db5999..a4c107be24 100644 --- a/apps/calendar-web/src/views/Week/util/week-window.util.ts +++ b/apps/calendar-web/src/views/Week/util/week-window.util.ts @@ -52,8 +52,9 @@ export const computeVisibleDayCount = ( ); /** - * Index of the first visible day within the week. The window centers on the - * anchor day and clamps to the week boundaries, so a full-width window is + * Index of the first visible day within the week. Narrow windows page through + * the week from Sunday in visible-day-count steps, so the window starts on + * Sunday whenever that keeps the anchor day visible. A full-width window is * always offset 0. */ export const computeVisibleWindowOffset = ({ @@ -62,10 +63,37 @@ export const computeVisibleWindowOffset = ({ }: { anchorIndex: number; visibleDayCount: number; -}) => { - const maxOffset = Math.max(WEEK_DAY_COUNT - visibleDayCount, 0); - const centered = anchorIndex - Math.floor((visibleDayCount - 1) / 2); - return Math.min(Math.max(centered, 0), maxOffset); +}) => Math.floor(anchorIndex / visibleDayCount) * visibleDayCount; + +/** + * First visible day after paging one window forward or back. Week-aligned + * windows step through the week's pages and snap to the next or previous + * Sunday, so paging never drifts off the week start. A window the user shifted + * by hand (Shift+J/K) keeps its alignment and moves by the visible day count. + */ +export const pagedWindowStart = ( + start: Dayjs, + direction: 1 | -1, + visibleDayCount: number, +): Dayjs => { + const weekStart = sundayWeekViewStart(start); + const offset = start.diff(weekStart, "day"); + if (offset % visibleDayCount !== 0) { + return start.add(direction * visibleDayCount, "day"); + } + if (direction === 1) { + return offset + visibleDayCount < WEEK_DAY_COUNT + ? start.add(visibleDayCount, "day") + : weekStart.add(WEEK_DAY_COUNT, "day"); + } + if (offset > 0) { + return start.subtract(visibleDayCount, "day"); + } + const lastPageOffset = computeVisibleWindowOffset({ + anchorIndex: WEEK_DAY_COUNT - 1, + visibleDayCount, + }); + return weekStart.subtract(WEEK_DAY_COUNT - lastPageOffset, "day"); }; /** Sunday 00:00 for the calendar week containing `day` (matches Day.js `startOf("week")`). */ @@ -102,22 +130,6 @@ export const weekViewDateString = (day: Dayjs): string => { ); }; -/** - * The anchor day that makes {@link computeVisibleWindowOffset} produce the - * given offset (the center day of that window). Used by paging navigation to - * express "move the window to this offset" in anchor terms. - */ -export const anchorDateForWindowOffset = ({ - weekStart, - windowOffset, - visibleDayCount, -}: { - weekStart: Dayjs; - windowOffset: number; - visibleDayCount: number; -}) => - weekStart.add(windowOffset + Math.floor((visibleDayCount - 1) / 2), "day"); - type EventDates = Pick; /** diff --git a/e2e/navigation/go-to-date.spec.ts b/e2e/navigation/go-to-date.spec.ts index 90da2192ce..69d2b3618c 100644 --- a/e2e/navigation/go-to-date.spec.ts +++ b/e2e/navigation/go-to-date.spec.ts @@ -23,7 +23,7 @@ async function goToDateFromPalette( await page.keyboard.press("Enter"); await expect(search).toHaveCount(0); // The window stays on the calendar week instead of starting on the picked - // day; the 5-column test viewport centers the day within that week. + // day; the 5-column test viewport starts on Sunday when the day fits. await expect(page).toHaveURL(new RegExp(`/week/${windowStart}`)); await expect.poll(() => getVisibleDayDates(page)).toContain(expectedDate); await expect( @@ -54,7 +54,7 @@ test("G accepts slash-separated ISO-style dates", async ({ page }) => { "2026/10/29", "Go to Thu, Oct 29, 2026", "2026-10-29", - "2026-10-27", + "2026-10-25", "Showing week of Thursday, October 29, 2026", ); }); @@ -67,7 +67,7 @@ test("G accepts month-first dates without a year", async ({ page }) => { "10/29", "Go to Thu, Oct 29, 2026", "2026-10-29", - "2026-10-27", + "2026-10-25", "Showing week of Thursday, October 29, 2026", ); }); From 9532610757316fe66b792677aa37eab5c7317c5b Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 4 Dec 2026 13:12:02 +0000 Subject: [PATCH 2/2] fix(web): keep narrow week pages inside the calendar week Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HmwVQGfsHYrmcJ8Ff6cGAg --- .../src/views/Week/hooks/useWeek.test.tsx | 12 ++--- .../views/Week/util/week-window.util.test.ts | 28 +++++++---- .../src/views/Week/util/week-window.util.ts | 47 ++++++++++--------- 3 files changed, 50 insertions(+), 37 deletions(-) diff --git a/apps/calendar-web/src/views/Week/hooks/useWeek.test.tsx b/apps/calendar-web/src/views/Week/hooks/useWeek.test.tsx index d9f185f2dc..2ef1b051f9 100644 --- a/apps/calendar-web/src/views/Week/hooks/useWeek.test.tsx +++ b/apps/calendar-web/src/views/Week/hooks/useWeek.test.tsx @@ -96,11 +96,11 @@ describe("useWeek", () => { expect( result.current.component.weekDays.map((d) => d.format(DATE_FORMAT)), ).toEqual([ + "2026-10-27", + "2026-10-28", + "2026-10-29", "2026-10-30", "2026-10-31", - "2026-11-01", - "2026-11-02", - "2026-11-03", ]); expect(result.current.component.isCurrentWeek).toBe(true); }); @@ -218,7 +218,7 @@ describe("useWeek", () => { expect(mockNavigate).toHaveBeenCalledWith({ to: "/week/$dateString", - params: { dateString: "2026-10-30" }, + params: { dateString: "2026-10-27" }, }); }); @@ -252,7 +252,7 @@ describe("useWeek", () => { expect(mockNavigate).not.toHaveBeenCalled(); }); - it("pages by the number of visible days", () => { + it("pages within the calendar week, ending on Saturday", () => { mockParams.dateString = "2026-05-20"; const today = dayjs("2026-05-20", DATE_FORMAT); const { result } = renderHook(() => useWeek(today, 3)); @@ -263,7 +263,7 @@ describe("useWeek", () => { expect(mockNavigate).toHaveBeenCalledWith({ to: "/week/$dateString", - params: { dateString: "2026-05-23" }, + params: { dateString: "2026-05-21" }, }); }); diff --git a/apps/calendar-web/src/views/Week/util/week-window.util.test.ts b/apps/calendar-web/src/views/Week/util/week-window.util.test.ts index a693bb4392..5d3b462a8d 100644 --- a/apps/calendar-web/src/views/Week/util/week-window.util.test.ts +++ b/apps/calendar-web/src/views/Week/util/week-window.util.test.ts @@ -66,10 +66,10 @@ describe("currentWeekWindowStart", () => { ); }); - it("moves to the week's next page when the day is past the first", () => { + it("ends the window on Saturday when the day is past the first page", () => { const friday = dayjs("2026-10-30T09:00:00"); expect(currentWeekWindowStart(friday, 5).format("YYYY-MM-DD")).toBe( - "2026-10-30", + "2026-10-27", ); }); @@ -118,7 +118,7 @@ describe("computeVisibleWindowOffset", () => { const offsets = [0, 1, 2, 3, 4, 5, 6].map((anchorIndex) => computeVisibleWindowOffset({ anchorIndex, visibleDayCount: 3 }), ); - expect(offsets).toEqual([0, 0, 0, 3, 3, 3, 6]); + expect(offsets).toEqual([0, 0, 0, 3, 3, 3, 4]); }); }); @@ -127,11 +127,19 @@ describe("pagedWindowStart", () => { pagedWindowStart(dayjs(date), direction, count).format("YYYY-MM-DD"); it("keeps a six-day window starting on Sunday week after week", () => { - // Sun Dec 6 -> Sat Dec 12 (last page) -> Sun Dec 13 - expect(page("2026-12-06", 1, 6)).toBe("2026-12-12"); - expect(page("2026-12-12", 1, 6)).toBe("2026-12-13"); - expect(page("2026-12-13", -1, 6)).toBe("2026-12-12"); - expect(page("2026-12-12", -1, 6)).toBe("2026-12-06"); + // Sun Dec 6 -> Mon Dec 7 (last page, ends Saturday) -> Sun Dec 13 + expect(page("2026-12-06", 1, 6)).toBe("2026-12-07"); + expect(page("2026-12-07", 1, 6)).toBe("2026-12-13"); + expect(page("2026-12-13", -1, 6)).toBe("2026-12-07"); + expect(page("2026-12-07", -1, 6)).toBe("2026-12-06"); + }); + + it("steps a three-day window through the week", () => { + expect(page("2026-12-06", 1, 3)).toBe("2026-12-09"); + expect(page("2026-12-09", 1, 3)).toBe("2026-12-10"); + expect(page("2026-12-10", 1, 3)).toBe("2026-12-13"); + expect(page("2026-12-10", -1, 3)).toBe("2026-12-09"); + expect(page("2026-12-09", -1, 3)).toBe("2026-12-06"); }); it("pages a full week by seven days", () => { @@ -140,8 +148,8 @@ describe("pagedWindowStart", () => { }); it("keeps a manual shift when paging", () => { - expect(page("2026-12-07", 1, 6)).toBe("2026-12-13"); - expect(page("2026-12-07", -1, 6)).toBe("2026-12-01"); + expect(page("2026-12-07", 1, 5)).toBe("2026-12-12"); + expect(page("2026-12-07", -1, 5)).toBe("2026-12-02"); }); }); diff --git a/apps/calendar-web/src/views/Week/util/week-window.util.ts b/apps/calendar-web/src/views/Week/util/week-window.util.ts index a4c107be24..31e739655c 100644 --- a/apps/calendar-web/src/views/Week/util/week-window.util.ts +++ b/apps/calendar-web/src/views/Week/util/week-window.util.ts @@ -53,9 +53,9 @@ export const computeVisibleDayCount = ( /** * Index of the first visible day within the week. Narrow windows page through - * the week from Sunday in visible-day-count steps, so the window starts on - * Sunday whenever that keeps the anchor day visible. A full-width window is - * always offset 0. + * the calendar week from Sunday in visible-day-count steps, with the last page + * ending on Saturday, so a window starts on Sunday whenever that keeps the + * anchor day visible. A full-width window is always offset 0. */ export const computeVisibleWindowOffset = ({ anchorIndex, @@ -63,13 +63,21 @@ export const computeVisibleWindowOffset = ({ }: { anchorIndex: number; visibleDayCount: number; -}) => Math.floor(anchorIndex / visibleDayCount) * visibleDayCount; +}) => + Math.min( + Math.floor(anchorIndex / visibleDayCount) * visibleDayCount, + WEEK_DAY_COUNT - visibleDayCount, + ); + +/** Sunday 00:00 for the calendar week containing `day` (matches Day.js `startOf("week")`). */ +export const sundayWeekViewStart = (day: Dayjs) => day.startOf("week"); /** * First visible day after paging one window forward or back. Week-aligned - * windows step through the week's pages and snap to the next or previous - * Sunday, so paging never drifts off the week start. A window the user shifted - * by hand (Shift+J/K) keeps its alignment and moves by the visible day count. + * windows step through the pages of {@link computeVisibleWindowOffset} and + * cross into the next or previous week at its Sunday or last page, so paging + * never drifts off the week start. A window the user shifted by hand + * (Shift+J/K) keeps its alignment and moves by the visible day count. */ export const pagedWindowStart = ( start: Dayjs, @@ -78,27 +86,24 @@ export const pagedWindowStart = ( ): Dayjs => { const weekStart = sundayWeekViewStart(start); const offset = start.diff(weekStart, "day"); - if (offset % visibleDayCount !== 0) { + const lastPage = WEEK_DAY_COUNT - visibleDayCount; + if (offset % visibleDayCount !== 0 && offset !== lastPage) { return start.add(direction * visibleDayCount, "day"); } if (direction === 1) { - return offset + visibleDayCount < WEEK_DAY_COUNT - ? start.add(visibleDayCount, "day") - : weekStart.add(WEEK_DAY_COUNT, "day"); + return offset === lastPage + ? weekStart.add(WEEK_DAY_COUNT, "day") + : weekStart.add(Math.min(offset + visibleDayCount, lastPage), "day"); } - if (offset > 0) { - return start.subtract(visibleDayCount, "day"); + if (offset === 0) { + return weekStart.subtract(visibleDayCount, "day"); } - const lastPageOffset = computeVisibleWindowOffset({ - anchorIndex: WEEK_DAY_COUNT - 1, - visibleDayCount, - }); - return weekStart.subtract(WEEK_DAY_COUNT - lastPageOffset, "day"); + return weekStart.add( + computeVisibleWindowOffset({ anchorIndex: offset - 1, visibleDayCount }), + "day", + ); }; -/** Sunday 00:00 for the calendar week containing `day` (matches Day.js `startOf("week")`). */ -export const sundayWeekViewStart = (day: Dayjs) => day.startOf("week"); - /** * First visible day for a partial-week window that still includes `day`. * Anchors partial-week scrolling so the anchor day stays visible.