2021-06-20 14:19:41 +00:00
|
|
|
// handles logic related to user clock display using 24h display / timeZone options.
|
2022-06-28 20:40:58 +00:00
|
|
|
import dayjs from "@calcom/dayjs";
|
2022-09-30 12:45:28 +00:00
|
|
|
import {
|
|
|
|
getIs24hClockFromLocalStorage,
|
|
|
|
isBrowserLocale24h,
|
|
|
|
setIs24hClockInLocalStorage,
|
|
|
|
} from "@calcom/lib/timeFormat";
|
2022-05-09 18:12:47 +00:00
|
|
|
import { localStorage } from "@calcom/lib/webstorage";
|
|
|
|
|
2021-08-19 12:27:01 +00:00
|
|
|
interface TimeOptions {
|
|
|
|
is24hClock: boolean;
|
|
|
|
inviteeTimeZone: string;
|
|
|
|
}
|
2021-06-20 14:19:41 +00:00
|
|
|
|
|
|
|
const timeOptions: TimeOptions = {
|
|
|
|
is24hClock: false,
|
2021-08-19 12:27:01 +00:00
|
|
|
inviteeTimeZone: "",
|
|
|
|
};
|
2021-06-20 14:19:41 +00:00
|
|
|
|
2021-08-19 12:27:01 +00:00
|
|
|
const isInitialized = false;
|
2021-06-20 14:19:41 +00:00
|
|
|
|
|
|
|
const initClock = () => {
|
2022-05-09 18:12:47 +00:00
|
|
|
if (isInitialized) {
|
2021-06-20 14:19:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-23 12:37:15 +00:00
|
|
|
// This only sets browser locale if there's no preference on localStorage.
|
2022-09-30 12:45:28 +00:00
|
|
|
if (getIs24hClockFromLocalStorage() === null) set24hClock(isBrowserLocale24h());
|
|
|
|
timeOptions.is24hClock = !!getIs24hClockFromLocalStorage();
|
2021-08-19 12:27:01 +00:00
|
|
|
timeOptions.inviteeTimeZone = localStorage.getItem("timeOption.preferredTimeZone") || dayjs.tz.guess();
|
|
|
|
};
|
2021-06-20 14:19:41 +00:00
|
|
|
|
|
|
|
const is24h = (is24hClock?: boolean) => {
|
|
|
|
initClock();
|
2021-08-19 12:27:01 +00:00
|
|
|
if (typeof is24hClock !== "undefined") set24hClock(is24hClock);
|
2021-06-20 14:19:41 +00:00
|
|
|
return timeOptions.is24hClock;
|
2021-08-19 12:27:01 +00:00
|
|
|
};
|
2021-06-20 14:19:41 +00:00
|
|
|
|
|
|
|
const set24hClock = (is24hClock: boolean) => {
|
2022-09-30 12:45:28 +00:00
|
|
|
setIs24hClockInLocalStorage(is24hClock);
|
2021-06-20 14:19:41 +00:00
|
|
|
timeOptions.is24hClock = is24hClock;
|
2021-08-19 12:27:01 +00:00
|
|
|
};
|
2021-06-20 14:19:41 +00:00
|
|
|
|
|
|
|
function setTimeZone(selectedTimeZone: string) {
|
2021-08-19 12:27:01 +00:00
|
|
|
localStorage.setItem("timeOption.preferredTimeZone", selectedTimeZone);
|
2021-06-20 14:19:41 +00:00
|
|
|
timeOptions.inviteeTimeZone = selectedTimeZone;
|
|
|
|
}
|
|
|
|
|
|
|
|
const timeZone = (selectedTimeZone?: string) => {
|
|
|
|
initClock();
|
2021-08-19 12:27:01 +00:00
|
|
|
if (selectedTimeZone) setTimeZone(selectedTimeZone);
|
2021-06-20 14:19:41 +00:00
|
|
|
return timeOptions.inviteeTimeZone;
|
2021-08-19 12:27:01 +00:00
|
|
|
};
|
2021-06-20 14:19:41 +00:00
|
|
|
|
2021-08-19 12:27:01 +00:00
|
|
|
export { is24h, timeZone };
|