43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
|
// handles logic related to user clock display using 24h display / timeZone options.
|
||
|
import dayjs, {Dayjs} from 'dayjs';
|
||
|
|
||
|
interface TimeOptions { is24hClock: boolean, inviteeTimeZone: string };
|
||
|
|
||
|
const timeOptions: TimeOptions = {
|
||
|
is24hClock: false,
|
||
|
inviteeTimeZone: '',
|
||
|
}
|
||
|
|
||
|
const isInitialized: boolean = false;
|
||
|
|
||
|
const initClock = () => {
|
||
|
if (typeof localStorage === "undefined" || isInitialized) {
|
||
|
return;
|
||
|
}
|
||
|
timeOptions.is24hClock = localStorage.getItem('timeOption.is24hClock') === "true";
|
||
|
timeOptions.inviteeTimeZone = localStorage.getItem('timeOption.preferredTimeZone') || dayjs.tz.guess();
|
||
|
}
|
||
|
|
||
|
const is24h = (is24hClock?: boolean) => {
|
||
|
initClock();
|
||
|
if(typeof is24hClock !== "undefined") set24hClock(is24hClock);
|
||
|
return timeOptions.is24hClock;
|
||
|
}
|
||
|
|
||
|
const set24hClock = (is24hClock: boolean) => {
|
||
|
localStorage.setItem('timeOption.is24hClock', is24hClock.toString());
|
||
|
timeOptions.is24hClock = is24hClock;
|
||
|
}
|
||
|
|
||
|
function setTimeZone(selectedTimeZone: string) {
|
||
|
localStorage.setItem('timeOption.preferredTimeZone', selectedTimeZone);
|
||
|
timeOptions.inviteeTimeZone = selectedTimeZone;
|
||
|
}
|
||
|
|
||
|
const timeZone = (selectedTimeZone?: string) => {
|
||
|
initClock();
|
||
|
if (selectedTimeZone) setTimeZone(selectedTimeZone)
|
||
|
return timeOptions.inviteeTimeZone;
|
||
|
}
|
||
|
|
||
|
export {is24h, timeZone};
|