Skip to content

Commit 3fba139

Browse files
committed
fix(calendar): focus end of week when using reverse-chronological order
ensures the active date points to the last day of the period when navigating through calendar weeks in reverse order, which is the expected behavior for history and activity lists.
1 parent 903a144 commit 3fba139

5 files changed

Lines changed: 22 additions & 5 deletions

File tree

projects/client/src/lib/features/calendar/context/useCalendarPeriod.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { getLocale } from '$lib/features/i18n/index.ts';
22
import { getStartOfWeek } from '$lib/utils/date/getStartOfWeek.ts';
33
import { isCurrentWeek } from '$lib/utils/date/isCurrentWeek.ts';
4+
import { addDays } from 'date-fns/addDays';
45
import { addWeeks } from 'date-fns/addWeeks';
56
import { subWeeks } from 'date-fns/subWeeks';
67
import { map } from 'rxjs';
78
import { AnalyticsEvent } from '../../analytics/events/AnalyticsEvent.ts';
89
import { useTrack } from '../../analytics/useTrack.ts';
10+
import type { CalendarOrder } from '../models/CalendarOrder.ts';
911
import { getCalendarContext } from './getCalendarContext.ts';
1012

1113
type Direction = 'next' | 'previous';
@@ -19,8 +21,21 @@ function getNextOrPreviousPeriod(date: Date, direction: Direction) {
1921
return getStartOfWeek(targetWeek, getLocale());
2022
}
2123

24+
const LAST_DAY_OF_WEEK_OFFSET = 6;
25+
26+
function getActiveDate(weekStart: Date, order: CalendarOrder): Date {
27+
if (order === 'reverse-chronological') {
28+
return addDays(weekStart, LAST_DAY_OF_WEEK_OFFSET);
29+
}
30+
31+
return weekStart;
32+
}
33+
2234
// FIXME: calendar navigation should be reflected in url
23-
export function useCalendarPeriod() {
35+
export function useCalendarPeriod(
36+
options?: { order?: CalendarOrder },
37+
) {
38+
const { order = 'chronological' } = options ?? {};
2439
const { startDate, activeDate } = getCalendarContext();
2540
const { track } = useTrack(AnalyticsEvent.CalendarPeriod);
2641

@@ -29,7 +44,7 @@ export function useCalendarPeriod() {
2944
const date = startDate.value;
3045
const newStart = getNextOrPreviousPeriod(date, direction);
3146

32-
activeDate.next(newStart);
47+
activeDate.next(getActiveDate(newStart, order));
3348
startDate.next(newStart);
3449
};
3550

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import type { Snippet } from 'svelte';
22
import type { Calendar } from './Calendar.ts';
33
import type { CalendarNavigationProps } from './CalendarNavigationProps.ts';
4+
import type { CalendarOrder } from './CalendarOrder.ts';
45

56
export type CalendarLayoutProps<T> = {
67
calendar: Calendar<T>;
78
isLoading: boolean;
89
item: Snippet<[T]>;
910
layout?: 'list' | 'grid';
1011
empty?: Snippet;
11-
order?: 'chronological' | 'reverse-chronological';
12+
order?: CalendarOrder;
1213
} & CalendarNavigationProps;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type CalendarOrder = 'chronological' | 'reverse-chronological';

projects/client/src/lib/sections/lists/activity/ActivityPaginatedList.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
const { filterMap } = useFilter();
1212
1313
const { startDate, endDate, activeDate, next, previous, reset } =
14-
useCalendarPeriod();
14+
useCalendarPeriod({ order: 'reverse-chronological' });
1515
1616
const now = new Date();
1717

projects/client/src/lib/sections/lists/history/PersonalHistoryPaginatedList.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
const { mode }: { mode?: DiscoverMode } = $props();
1212
1313
const { startDate, endDate, activeDate, next, previous, reset } =
14-
useCalendarPeriod();
14+
useCalendarPeriod({ order: 'reverse-chronological' });
1515
1616
const historyType = $derived(toRecentlyWatchedType(mode));
1717

0 commit comments

Comments
 (0)