Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🐛 Respect the user's week start preference in the week calendar (RAL-1341) (#2661)
Luke Vellafd90cd1c2fdc
diff --git a/apps/web/src/features/poll/components/forms/poll-options-form/dayjs-localizer.test.ts b/apps/web/src/features/poll/components/forms/poll-options-form/dayjs-localizer.test.tsnew file mode 100644index 000000000..2bec7fdf2--- /dev/null+++ b/apps/web/src/features/poll/components/forms/poll-options-form/dayjs-localizer.test.ts@@ -0,0 +1,64 @@+import { describe, expect, it } from "vitest";+import { dayjs } from "@/lib/dayjs";+import dayjsLocalizer from "./dayjs-localizer";++// The DateLocalizer typings omit the week unit react-big-calendar passes at+// runtime, so widen to the shape under test.+const createLocalizer = (weekStart?: number) =>+ dayjsLocalizer(dayjs, { weekStart }) as unknown as {+ startOfWeek: () => number;+ startOf: (date: Date, unit: string) => Date;+ endOf: (date: Date, unit: string) => Date;+ eq: (a: Date, b: Date, unit: string) => boolean;+ };++// 2026-07-14 is a Tuesday+const tuesday = new Date(2026, 6, 14, 12, 0, 0);++describe("dayjsLocalizer weekStart", () => {+ it("defaults to the dayjs locale week start when no weekStart is given", () => {+ const localizer = createLocalizer();+ expect(localizer.startOfWeek()).toBe(0);+ expect(localizer.startOf(tuesday, "week")).toEqual(+ new Date(2026, 6, 12, 0, 0, 0),+ );+ });++ it("starts the week on Monday when weekStart is 1", () => {+ const localizer = createLocalizer(1);+ expect(localizer.startOfWeek()).toBe(1);+ expect(localizer.startOf(tuesday, "week")).toEqual(+ new Date(2026, 6, 13, 0, 0, 0),+ );+ expect(localizer.endOf(tuesday, "week")).toEqual(+ new Date(2026, 6, 19, 23, 59, 59, 999),+ );+ });++ it("starts the week on Saturday when weekStart is 6", () => {+ const localizer = createLocalizer(6);+ expect(localizer.startOf(tuesday, "week")).toEqual(+ new Date(2026, 6, 11, 0, 0, 0),+ );+ expect(localizer.endOf(tuesday, "week")).toEqual(+ new Date(2026, 6, 17, 23, 59, 59, 999),+ );+ });++ it("keeps a date already on the week start day in place", () => {+ const localizer = createLocalizer(1);+ const monday = new Date(2026, 6, 13, 8, 30, 0);+ expect(localizer.startOf(monday, "week")).toEqual(+ new Date(2026, 6, 13, 0, 0, 0),+ );+ });++ it("compares dates by the configured week boundary", () => {+ const localizer = createLocalizer(1);+ const sunday = new Date(2026, 6, 12);+ const monday = new Date(2026, 6, 13);+ // With Monday as week start, Sunday belongs to the previous week+ expect(localizer.eq(sunday, monday, "week")).toBe(false);+ expect(localizer.eq(monday, tuesday, "week")).toBe(true);+ });+});diff --git a/apps/web/src/features/poll/components/forms/poll-options-form/dayjs-localizer.ts b/apps/web/src/features/poll/components/forms/poll-options-form/dayjs-localizer.tsindex 4e7b4e2ea..dca2728a0 100644--- a/apps/web/src/features/poll/components/forms/poll-options-form/dayjs-localizer.ts+++ b/apps/web/src/features/poll/components/forms/poll-options-form/dayjs-localizer.ts@@ -54,12 +54,30 @@ function fixUnit(unit) {return datePart;}-export default function (dayjs) {+export default function (dayjs, { weekStart } = {}) {const locale = (m, c) => (c ? m.locale(c) : m);+ function firstOfWeek() {+ if (weekStart !== undefined) {+ return weekStart;+ }+ const data = dayjs.localeData();+ return data ? data.firstDayOfWeek() : 0;+ }++ // dayjs' own startOf("week") uses the global locale's week start, so week+ // boundaries are computed manually from the configured weekStart.+ function startOfWeek(date) {+ const day = dayjs(date).startOf("day");+ return day.subtract((day.day() - firstOfWeek() + 7) % 7, "day");+ }+/*** BEGIN localized date arithmetic methods with dayjs ***/function defineComparators(a, b, unit) {const datePart = fixUnit(unit);+ if (datePart === "week") {+ return [startOfWeek(a), startOfWeek(b), "day"];+ }const dtA = datePart ? dayjs(a).startOf(datePart) : dayjs(a);const dtB = datePart ? dayjs(b).startOf(datePart) : dayjs(b);return [dtA, dtB, datePart];@@ -68,6 +86,9 @@ export default function (dayjs) {// biome-ignore lint/style/useDefaultParameterLast: Fix this laterfunction startOf(date = null, unit) {const datePart = fixUnit(unit);+ if (datePart === "week") {+ return startOfWeek(date).toDate();+ }if (datePart) {return dayjs(date).startOf(datePart).toDate();}@@ -77,6 +98,9 @@ export default function (dayjs) {// biome-ignore lint/style/useDefaultParameterLast: Fix this laterfunction endOf(date = null, unit) {const datePart = fixUnit(unit);+ if (datePart === "week") {+ return startOfWeek(date).add(6, "day").endOf("day").toDate();+ }if (datePart) {return dayjs(date).endOf(datePart).toDate();}@@ -184,17 +208,12 @@ export default function (dayjs) {return dt.minutes();}- function firstOfWeek() {- const data = dayjs.localeData();- return data ? data.firstDayOfWeek() : 0;- }-function firstVisibleDay(date) {- return dayjs(date).startOf("month").startOf("week").toDate();+ return startOfWeek(dayjs(date).startOf("month")).toDate();}function lastVisibleDay(date) {- return dayjs(date).endOf("month").endOf("week").toDate();+ return endOf(dayjs(date).endOf("month").toDate(), "week");}function visibleDays(date) {diff --git a/apps/web/src/features/poll/components/forms/poll-options-form/week-calendar.tsx b/apps/web/src/features/poll/components/forms/poll-options-form/week-calendar.tsxindex b4ba3b8f4..129d303e7 100644--- a/apps/web/src/features/poll/components/forms/poll-options-form/week-calendar.tsx+++ b/apps/web/src/features/poll/components/forms/poll-options-form/week-calendar.tsx@@ -2,7 +2,7 @@ import "react-big-calendar/lib/css/react-big-calendar.css";import "./rbc-overrides.css";import { XIcon } from "lucide-react";-import type React from "react";+import React from "react";import type { CalendarProps } from "react-big-calendar";import { Calendar } from "react-big-calendar";import { createBreakpoint } from "react-use";@@ -19,8 +19,6 @@ import dayjsLocalizer from "./dayjs-localizer";import type { DateTimeOption, DateTimePickerProps } from "./types";import { formatDateWithoutTz } from "./utils";-const localizer = dayjsLocalizer(dayjs);-const useDevice = createBreakpoint({ desktop: 720, mobile: 360 });/**@@ -37,9 +35,14 @@ const WeekCalendar: React.FunctionComponent<DateTimePickerProps> = ({duration = 60,onChangeDuration,}) => {- const { locale, timeFormat } = useDateTimeConfig();+ const { locale, timeFormat, weekStart } = useDateTimeConfig();const { formatDuration } = useDateTime();+ const localizer = React.useMemo(+ () => dayjsLocalizer(dayjs, { weekStart }),+ [weekStart],+ );+// The form works in naive local times; format in the system zone.const formatTime = (time: Date) =>formatDateTime(time, { preset: "time", locale, timeFormat });