2021-06-20 14:19:41 +00:00
|
|
|
// handles logic related to user clock display using 24h display / timeZone options.
|
2021-08-19 12:27:01 +00:00
|
|
|
import dayjs from "dayjs";
|
|
|
|
import utc from "dayjs/plugin/utc";
|
|
|
|
import timezone from "dayjs/plugin/timezone";
|
2021-06-20 14:37:33 +00:00
|
|
|
|
2021-08-19 12:27:01 +00:00
|
|
|
dayjs.extend(utc);
|
|
|
|
dayjs.extend(timezone);
|
2021-06-20 14:19:41 +00:00
|
|
|
|
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 = () => {
|
|
|
|
if (typeof localStorage === "undefined" || isInitialized) {
|
|
|
|
return;
|
|
|
|
}
|
2021-08-19 12:27:01 +00:00
|
|
|
timeOptions.is24hClock = localStorage.getItem("timeOption.is24hClock") === "true";
|
|
|
|
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) => {
|
2021-08-19 12:27:01 +00:00
|
|
|
localStorage.setItem("timeOption.is24hClock", is24hClock.toString());
|
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 };
|