Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

🐛 Fix hydration mismatch on the new poll duration picker (#2830)

Luke Vella4c5bffcab34c
diff --git a/apps/web/public/locales/en/app.json b/apps/web/public/locales/en/app.json
index d1c96b3aa..e2953766d 100644
--- a/apps/web/public/locales/en/app.json
+++ b/apps/web/public/locales/en/app.json
@@ -240,7 +240,10 @@
"duplicatePollDescription": "Quickly create similar polls from existing ones",
"duplicateTitleDescription": "Hint: Give your new poll a unique title",
"duplicateTitleLabel": "Title",
+ "durationHours": "{count, plural, one {#h} other {#h}}",
+ "durationHoursMinutes": "{hours, plural, one {#h} other {#h}} {minutes, plural, one {#m} other {#m}}",
"durationLabel": "Duration",
+ "durationMinutes": "{count, plural, one {#m} other {#m}}",
"edit": "Edit",
"editDetails": "Edit details",
"editDetailsDescription": "Change the details of your event.",
diff --git a/apps/web/src/app/[locale]/(space)/settings/event-types/event-types-settings-page.tsx b/apps/web/src/app/[locale]/(space)/settings/event-types/event-types-settings-page.tsx
index 335c6586c..eb79c9b94 100644
--- a/apps/web/src/app/[locale]/(space)/settings/event-types/event-types-settings-page.tsx
+++ b/apps/web/src/app/[locale]/(space)/settings/event-types/event-types-settings-page.tsx
@@ -45,8 +45,7 @@ import { DeleteEventTypeDialog } from "@/features/event-types/components/delete-
import { EditEventTypeDialog } from "@/features/event-types/components/edit-event-type-dialog";
import type { EventTypeDTO } from "@/features/event-types/types";
import { Trans, useTranslation } from "@/i18n/client";
-import { formatDuration } from "@/lib/datetime/format";
-import { useLocale } from "@/lib/locale/client";
+import { Duration } from "@/lib/datetime/duration";
import type { LocationType } from "@/lib/location";
import { trpc } from "@/trpc/client";
@@ -96,7 +95,6 @@ function EventTypeCard({
onEdit: () => void;
onDelete: () => void;
}) {
- const { locale } = useLocale();
const { t } = useTranslation();
return (
<div className="flex flex-col overflow-hidden rounded-2xl border border-card-border bg-card">
@@ -141,7 +139,7 @@ function EventTypeCard({
<div className="flex flex-wrap gap-1">
<Badge>
<ClockIcon className="mr-1 -ml-0.5 size-4" />
- <span>{formatDuration(duration, locale)}</span>
+ <Duration minutes={duration} />
</Badge>
{capacity !== null ? (
<Badge>
diff --git a/apps/web/src/features/poll/components/forms/poll-options-form/month-calendar/month-calendar.tsx b/apps/web/src/features/poll/components/forms/poll-options-form/month-calendar/month-calendar.tsx
index a45577db5..f924e0849 100644
--- a/apps/web/src/features/poll/components/forms/poll-options-form/month-calendar/month-calendar.tsx
+++ b/apps/web/src/features/poll/components/forms/poll-options-form/month-calendar/month-calendar.tsx
@@ -30,7 +30,8 @@ import {
import { useHeadlessDatePicker } from "@/components/headless-date-picker";
import type { NewEventData } from "@/features/poll/components/forms/types";
import { Trans, useTranslation } from "@/i18n/client";
-import { useDateTime, useDateTimeConfig } from "@/lib/datetime/client";
+import { useDateTimeConfig } from "@/lib/datetime/client";
+import { Duration } from "@/lib/datetime/duration";
import { formatDateParts } from "@/lib/datetime/format";
import { dayjs } from "@/lib/dayjs";
import {
@@ -52,7 +53,6 @@ const MonthCalendar: React.FunctionComponent<DateTimePickerProps> = ({
}) => {
const { t } = useTranslation();
const { locale } = useDateTimeConfig();
- const { formatDuration } = useDateTime();
// Time-based options are the default. With no options yet the selected
// duration drives the mode (0 = all-day) so the first selection creates the
// right kind of option; once options exist their type is the source of truth.
@@ -189,7 +189,7 @@ const MonthCalendar: React.FunctionComponent<DateTimePickerProps> = ({
>
{durationOptions.map((minutes) => (
<RadioCardsItem key={minutes} value={String(minutes)}>
- {formatDuration(minutes)}
+ <Duration minutes={minutes} />
</RadioCardsItem>
))}
<RadioCardsItem data-testid="all-day-option" value="all-day">
diff --git a/apps/web/src/features/poll/components/forms/poll-options-form/month-calendar/time-picker.tsx b/apps/web/src/features/poll/components/forms/poll-options-form/month-calendar/time-picker.tsx
index a83036f0c..eaef46bb7 100644
--- a/apps/web/src/features/poll/components/forms/poll-options-form/month-calendar/time-picker.tsx
+++ b/apps/web/src/features/poll/components/forms/poll-options-form/month-calendar/time-picker.tsx
@@ -8,7 +8,8 @@ import {
import * as React from "react";
import { useTranslation } from "@/i18n/client";
-import { useDateTime, useDateTimeConfig } from "@/lib/datetime/client";
+import { useDateTimeConfig } from "@/lib/datetime/client";
+import { Duration } from "@/lib/datetime/duration";
import { formatDateTime } from "@/lib/datetime/format";
import { dayjs } from "@/lib/dayjs";
@@ -26,7 +27,6 @@ const TimePicker: React.FunctionComponent<TimePickerProps> = ({
after,
}) => {
const { locale, timeFormat } = useDateTimeConfig();
- const { formatDuration } = useDateTime();
const { t } = useTranslation();
const [open, setOpen] = React.useState(false);
@@ -82,9 +82,10 @@ const TimePicker: React.FunctionComponent<TimePickerProps> = ({
<div className="flex items-center gap-2">
<span>{formatTime(selected)}</span>
{after ? (
- <span className="text-muted-foreground text-sm">
- {formatDuration(dayjs(selected).diff(after, "minute"))}
- </span>
+ <Duration
+ className="text-muted-foreground text-sm"
+ minutes={dayjs(selected).diff(after, "minute")}
+ />
) : null}
</div>
) : (
@@ -101,9 +102,10 @@ const TimePicker: React.FunctionComponent<TimePickerProps> = ({
<div className="flex items-center gap-2">
<span>{formatTime(option)}</span>
{after ? (
- <span className="text-muted-foreground text-sm">
- {formatDuration(dayjs(option).diff(after, "minute"))}
- </span>
+ <Duration
+ className="text-muted-foreground text-sm"
+ minutes={dayjs(option).diff(after, "minute")}
+ />
) : null}
</div>
</SelectItem>
@@ -114,9 +116,10 @@ const TimePicker: React.FunctionComponent<TimePickerProps> = ({
<div className="flex items-center gap-2">
<span>{formatTime(dayjs(value).toDate())}</span>
{after ? (
- <span className="text-muted-foreground text-sm">
- {formatDuration(dayjs(value).diff(after, "minute"))}
- </span>
+ <Duration
+ className="text-muted-foreground text-sm"
+ minutes={dayjs(value).diff(after, "minute")}
+ />
) : null}
</div>
</SelectItem>
diff --git a/apps/web/src/lib/datetime/client.tsx b/apps/web/src/lib/datetime/client.tsx
index 46c68b391..7f06ee501 100644
--- a/apps/web/src/lib/datetime/client.tsx
+++ b/apps/web/src/lib/datetime/client.tsx
@@ -6,7 +6,6 @@ import type { DateTimePreset } from "@/lib/datetime/format";
import {
formatDateTime as baseFormatDateTime,
formatDateTimeRange as baseFormatDateTimeRange,
- formatDuration as baseFormatDuration,
formatRelativeTime,
} from "@/lib/datetime/format";
import { getLocaleDefaults, getWeekdayNames } from "@/lib/datetime/locales";
@@ -111,7 +110,6 @@ export function useDateTime() {
showTimeZone: opts?.showTimeZone,
}),
toRelativeTime: (value: DateInput) => formatRelativeTime(value, locale),
- formatDuration: (minutes: number) => baseFormatDuration(minutes, locale),
weekdays: () => getWeekdayNames(locale),
}),
[locale, timeZone, timeFormat],
diff --git a/apps/web/src/lib/datetime/duration.tsx b/apps/web/src/lib/datetime/duration.tsx
new file mode 100644
index 000000000..f26bcdd15
--- /dev/null
+++ b/apps/web/src/lib/datetime/duration.tsx
@@ -0,0 +1,51 @@
+"use client";
+
+import { Trans } from "@/i18n/client";
+
+// Durations render from translation keys rather than Intl.DurationFormat:
+// Intl output varies with the engine's ICU build, which desyncs server and
+// client HTML, and emails need translated duration strings anyway.
+export function Duration({
+ minutes,
+ className,
+}: {
+ minutes: number;
+ className?: string;
+}) {
+ const hours = Math.floor(minutes / 60);
+ const mins = minutes % 60;
+
+ if (hours && mins) {
+ return (
+ <span className={className}>
+ <Trans
+ i18nKey="durationHoursMinutes"
+ defaults="{hours, plural, one {#h} other {#h}} {minutes, plural, one {#m} other {#m}}"
+ values={{ hours, minutes: mins }}
+ />
+ </span>
+ );
+ }
+
+ if (hours) {
+ return (
+ <span className={className}>
+ <Trans
+ i18nKey="durationHours"
+ defaults="{count, plural, one {#h} other {#h}}"
+ values={{ count: hours }}
+ />
+ </span>
+ );
+ }
+
+ return (
+ <span className={className}>
+ <Trans
+ i18nKey="durationMinutes"
+ defaults="{count, plural, one {#m} other {#m}}"
+ values={{ count: mins }}
+ />
+ </span>
+ );
+}
diff --git a/apps/web/tests/i18n.spec.ts b/apps/web/tests/i18n.spec.ts
index 883ab3254..79da3c17a 100644
--- a/apps/web/tests/i18n.spec.ts
+++ b/apps/web/tests/i18n.spec.ts
@@ -3,8 +3,24 @@ import test, { expect } from "@playwright/test";
test("should show correct language if supported", async ({ browser }) => {
const context = await browser.newContext({ locale: "de" });
const page = await context.newPage();
+ // Locale-sensitive Intl formatting must not differ between the server and
+ // the browser — a mismatch makes React throw away the server HTML.
+ const hydrationErrors: string[] = [];
+ page.on("pageerror", (error) => {
+ if (error.message.includes("Hydration failed")) {
+ hydrationErrors.push(error.message);
+ }
+ });
await page.goto("/new");
await expect(page.locator("text=Titel")).toBeVisible();
+ // A pill only reacts to clicks once hydration is done, so a successful
+ // selection proves the page hydrated before we assert on errors.
+ await page.getByTestId("all-day-option").click();
+ await expect(page.getByTestId("all-day-option")).toHaveAttribute(
+ "data-checked",
+ "",
+ );
+ expect(hydrationErrors).toEqual([]);
});
test("should default to english", async ({ browser }) => {