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..2ef1b051f9 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 @@ -242,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)); @@ -253,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/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..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 @@ -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([ + 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-27", - "2026-10-28", - "2026-10-29", - "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,42 @@ 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, 4]); + }); +}); + +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 -> 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("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); - } - } + 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", () => { + 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, 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 cb03db5999..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 @@ -52,9 +52,10 @@ 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 - * always offset 0. + * Index of the first visible day within the week. Narrow windows page through + * 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, @@ -62,15 +63,47 @@ 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.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 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, + direction: 1 | -1, + visibleDayCount: number, +): Dayjs => { + const weekStart = sundayWeekViewStart(start); + const offset = start.diff(weekStart, "day"); + const lastPage = WEEK_DAY_COUNT - visibleDayCount; + if (offset % visibleDayCount !== 0 && offset !== lastPage) { + return start.add(direction * visibleDayCount, "day"); + } + if (direction === 1) { + return offset === lastPage + ? weekStart.add(WEEK_DAY_COUNT, "day") + : weekStart.add(Math.min(offset + visibleDayCount, lastPage), "day"); + } + if (offset === 0) { + return weekStart.subtract(visibleDayCount, "day"); + } + return weekStart.add( + computeVisibleWindowOffset({ anchorIndex: offset - 1, visibleDayCount }), + "day", + ); +}; + /** * First visible day for a partial-week window that still includes `day`. * Anchors partial-week scrolling so the anchor day stays visible. @@ -102,22 +135,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", ); });