Skip to content

Scheduling infrastructure for absolutely everyone.

#4533: Show correct time format on booking page (#4771)

Jeroen Reumkens6d45e1c7c707
diff --git a/apps/web/components/booking/pages/AvailabilityPage.tsx b/apps/web/components/booking/pages/AvailabilityPage.tsx
index af43d3b9552..6267299791f 100644
--- a/apps/web/components/booking/pages/AvailabilityPage.tsx
+++ b/apps/web/components/booking/pages/AvailabilityPage.tsx
@@ -24,8 +24,7 @@ import useTheme from "@calcom/lib/hooks/useTheme";
import notEmpty from "@calcom/lib/notEmpty";
import { getRecurringFreq } from "@calcom/lib/recurringStrings";
import { collectPageParameters, telemetryEventTypes, useTelemetry } from "@calcom/lib/telemetry";
-import { detectBrowserTimeFormat } from "@calcom/lib/timeFormat";
-import { localStorage } from "@calcom/lib/webstorage";
+import { detectBrowserTimeFormat, getIs24hClockFromLocalStorage } from "@calcom/lib/timeFormat";
import { trpc } from "@calcom/trpc/react";
import { Icon } from "@calcom/ui/Icon";
import DatePicker from "@calcom/ui/v2/modules/booker/DatePicker";
@@ -225,7 +224,7 @@ function TimezoneDropdown({
const [isTimeOptionsOpen, setIsTimeOptionsOpen] = useState(false);
useEffect(() => {
- handleToggle24hClock(localStorage.getItem("timeOption.is24hClock") === "true");
+ handleToggle24hClock(!!getIs24hClockFromLocalStorage());
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
diff --git a/apps/web/lib/clock.ts b/apps/web/lib/clock.ts
index 1fd0b42273b..dde33133d65 100644
--- a/apps/web/lib/clock.ts
+++ b/apps/web/lib/clock.ts
@@ -1,6 +1,10 @@
// handles logic related to user clock display using 24h display / timeZone options.
import dayjs from "@calcom/dayjs";
-import { isBrowserLocale24h } from "@calcom/lib/timeFormat";
+import {
+ getIs24hClockFromLocalStorage,
+ isBrowserLocale24h,
+ setIs24hClockInLocalStorage,
+} from "@calcom/lib/timeFormat";
import { localStorage } from "@calcom/lib/webstorage";
interface TimeOptions {
@@ -20,8 +24,8 @@ const initClock = () => {
return;
}
// This only sets browser locale if there's no preference on localStorage.
- if (!localStorage.getItem("timeOption.is24hClock")) set24hClock(isBrowserLocale24h());
- timeOptions.is24hClock = localStorage.getItem("timeOption.is24hClock") === "true";
+ if (getIs24hClockFromLocalStorage() === null) set24hClock(isBrowserLocale24h());
+ timeOptions.is24hClock = !!getIs24hClockFromLocalStorage();
timeOptions.inviteeTimeZone = localStorage.getItem("timeOption.preferredTimeZone") || dayjs.tz.guess();
};
@@ -32,7 +36,7 @@ const is24h = (is24hClock?: boolean) => {
};
const set24hClock = (is24hClock: boolean) => {
- localStorage.setItem("timeOption.is24hClock", is24hClock.toString());
+ setIs24hClockInLocalStorage(is24hClock);
timeOptions.is24hClock = is24hClock;
};
diff --git a/apps/web/pages/settings/profile.tsx b/apps/web/pages/settings/profile.tsx
index dd181b581ed..20a65a73376 100644
--- a/apps/web/pages/settings/profile.tsx
+++ b/apps/web/pages/settings/profile.tsx
@@ -17,6 +17,7 @@ import TimezoneSelect, { ITimezone } from "react-timezone-select";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import showToast from "@calcom/lib/notification";
+import { setIs24hClockInLocalStorage } from "@calcom/lib/timeFormat";
import prisma from "@calcom/prisma";
import { TRPCClientErrorLike } from "@calcom/trpc/client";
import { trpc } from "@calcom/trpc/react";
@@ -229,7 +230,7 @@ function SettingsView(props: ComponentProps<typeof Settings> & { localeProp: str
// Write time format to localStorage if available
// Embed isn't applicable to profile pages. So ignore the rule
// eslint-disable-next-line @calcom/eslint/avoid-web-storage
- window.localStorage.setItem("timeOption.is24hClock", selectedTimeFormat.value === 12 ? "false" : "true");
+ setIs24hClockInLocalStorage(selectedTimeFormat.value === 24);
// TODO: Add validation
diff --git a/apps/web/pages/success.tsx b/apps/web/pages/success.tsx
index 3c09ddac3fe..d8e9f84117d 100644
--- a/apps/web/pages/success.tsx
+++ b/apps/web/pages/success.tsx
@@ -26,7 +26,7 @@ import { useLocale } from "@calcom/lib/hooks/useLocale";
import useTheme from "@calcom/lib/hooks/useTheme";
import { getEveryFreqFor } from "@calcom/lib/recurringStrings";
import { collectPageParameters, telemetryEventTypes, useTelemetry } from "@calcom/lib/telemetry";
-import { isBrowserLocale24h } from "@calcom/lib/timeFormat";
+import { getIs24hClockFromLocalStorage, isBrowserLocale24h } from "@calcom/lib/timeFormat";
import { localStorage } from "@calcom/lib/webstorage";
import prisma from "@calcom/prisma";
import Button from "@calcom/ui/Button";
@@ -203,7 +203,7 @@ export default function Success(props: SuccessProps) {
// TODO: Add payment details
});
setDate(date.tz(localStorage.getItem("timeOption.preferredTimeZone") || dayjs.tz.guess()));
- setIs24h(!!localStorage.getItem("timeOption.is24hClock"));
+ setIs24h(!!getIs24hClockFromLocalStorage());
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [eventType, needsConfirmation]);
diff --git a/apps/web/pages/v2/settings/my-account/general.tsx b/apps/web/pages/v2/settings/my-account/general.tsx
index de75677cc2b..d2881f42dc5 100644
--- a/apps/web/pages/v2/settings/my-account/general.tsx
+++ b/apps/web/pages/v2/settings/my-account/general.tsx
@@ -3,6 +3,7 @@ import { useMemo } from "react";
import { useForm, Controller } from "react-hook-form";
import { useLocale } from "@calcom/lib/hooks/useLocale";
+import { setIs24hClockInLocalStorage } from "@calcom/lib/timeFormat";
import { trpc } from "@calcom/trpc/react";
import { Button } from "@calcom/ui/v2/core/Button";
import Meta from "@calcom/ui/v2/core/Meta";
@@ -110,6 +111,8 @@ const GeneralView = ({ localeProp }: GeneralViewProps) => {
<Form
form={formMethods}
handleSubmit={(values) => {
+ setIs24hClockInLocalStorage(values.timeFormat.value === 24);
+
mutation.mutate({
...values,
locale: values.locale.value,
diff --git a/apps/web/pages/v2/success.tsx b/apps/web/pages/v2/success.tsx
index 991b20bed06..fb0d39852b6 100644
--- a/apps/web/pages/v2/success.tsx
+++ b/apps/web/pages/v2/success.tsx
@@ -26,7 +26,7 @@ import { useLocale } from "@calcom/lib/hooks/useLocale";
import useTheme from "@calcom/lib/hooks/useTheme";
import { getEveryFreqFor } from "@calcom/lib/recurringStrings";
import { collectPageParameters, telemetryEventTypes, useTelemetry } from "@calcom/lib/telemetry";
-import { isBrowserLocale24h } from "@calcom/lib/timeFormat";
+import { getIs24hClockFromLocalStorage, isBrowserLocale24h } from "@calcom/lib/timeFormat";
import { localStorage } from "@calcom/lib/webstorage";
import prisma from "@calcom/prisma";
import Button from "@calcom/ui/Button";
@@ -201,7 +201,7 @@ export default function Success(props: SuccessProps) {
// TODO: Add payment details
});
setDate(date.tz(localStorage.getItem("timeOption.preferredTimeZone") || dayjs.tz.guess()));
- setIs24h(!!localStorage.getItem("timeOption.is24hClock"));
+ setIs24h(!!getIs24hClockFromLocalStorage());
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [eventType, needsConfirmation]);
diff --git a/packages/features/ee/payments/components/PaymentPage.tsx b/packages/features/ee/payments/components/PaymentPage.tsx
index 6c997063ea3..41c8ab6ebfe 100644
--- a/packages/features/ee/payments/components/PaymentPage.tsx
+++ b/packages/features/ee/payments/components/PaymentPage.tsx
@@ -11,7 +11,7 @@ import { sdkActionManager, useIsEmbed } from "@calcom/embed-core/embed-iframe";
import { WEBSITE_URL } from "@calcom/lib/constants";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import useTheme from "@calcom/lib/hooks/useTheme";
-import { isBrowserLocale24h } from "@calcom/lib/timeFormat";
+import { getIs24hClockFromLocalStorage, isBrowserLocale24h } from "@calcom/lib/timeFormat";
import { Icon } from "@calcom/ui/Icon";
import type { PaymentPageProps } from "../pages/payment";
@@ -26,7 +26,7 @@ const PaymentPage: FC<PaymentPageProps> = (props) => {
useEffect(() => {
let embedIframeWidth = 0;
setDate(date.tz(localStorage.getItem("timeOption.preferredTimeZone") || dayjs.tz.guess()));
- setIs24h(!!localStorage.getItem("timeOption.is24hClock"));
+ setIs24h(!!getIs24hClockFromLocalStorage());
if (isEmbed) {
requestAnimationFrame(function fixStripeIframe() {
// HACK: Look for stripe iframe and center position it just above the embed content
diff --git a/packages/lib/timeFormat.ts b/packages/lib/timeFormat.ts
index 4b8a4e4be6e..9b1533c8206 100644
--- a/packages/lib/timeFormat.ts
+++ b/packages/lib/timeFormat.ts
@@ -6,12 +6,24 @@
*/
import { localStorage } from "@calcom/lib/webstorage";
+const is24hLocalstorageKey = "timeOption.is24hClock";
+
+export const setIs24hClockInLocalStorage = (is24h: boolean) =>
+ localStorage.setItem(is24hLocalstorageKey, is24h.toString());
+
+export const getIs24hClockFromLocalStorage = () => {
+ const is24hFromLocalstorage = localStorage.getItem(is24hLocalstorageKey);
+ if (is24hFromLocalstorage === null) return null;
+
+ return is24hFromLocalstorage === "true";
+};
+
export const isBrowserLocale24h = () => {
- const localStorageTimeFormat = localStorage.getItem("timeOption.is24hClock");
+ const localStorageTimeFormat = getIs24hClockFromLocalStorage();
// If time format is already stored in the browser then retrieve and return early
- if (localStorageTimeFormat === "true") {
+ if (localStorageTimeFormat === true) {
return true;
- } else if (localStorageTimeFormat === "false") {
+ } else if (localStorageTimeFormat === false) {
return false;
}
@@ -19,10 +31,10 @@ export const isBrowserLocale24h = () => {
if (typeof window !== "undefined" && navigator) locale = window.navigator?.language;
if (!new Intl.DateTimeFormat(locale, { hour: "numeric" }).format(0).match(/M/)) {
- localStorage.setItem("timeOption.is24hClock", "false");
+ setIs24hClockInLocalStorage(false);
return false;
} else {
- localStorage.setItem("timeOption.is24hClock", "true");
+ setIs24hClockInLocalStorage(true);
return true;
}
};