2021-09-14 08:20:24 +00:00
|
|
|
// TODO: replace headlessui with radix-ui
|
2021-06-30 01:35:08 +00:00
|
|
|
import { Switch } from "@headlessui/react";
|
2021-09-29 21:33:18 +00:00
|
|
|
import { FC, useEffect, useState } from "react";
|
|
|
|
import TimezoneSelect, { ITimezoneOption } from "react-timezone-select";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-08-03 08:30:35 +00:00
|
|
|
import classNames from "@lib/classNames";
|
2021-10-08 11:43:48 +00:00
|
|
|
import { useLocale } from "@lib/hooks/useLocale";
|
2021-06-20 14:19:41 +00:00
|
|
|
|
2021-09-22 19:52:38 +00:00
|
|
|
import { is24h, timeZone } from "../../lib/clock";
|
|
|
|
|
2021-09-29 21:33:18 +00:00
|
|
|
type Props = {
|
|
|
|
onSelectTimeZone: (selectedTimeZone: string) => void;
|
|
|
|
onToggle24hClock: (is24hClock: boolean) => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
const TimeOptions: FC<Props> = (props) => {
|
2021-06-30 01:35:08 +00:00
|
|
|
const [selectedTimeZone, setSelectedTimeZone] = useState("");
|
2021-06-20 14:19:41 +00:00
|
|
|
const [is24hClock, setIs24hClock] = useState(false);
|
2021-10-12 13:11:33 +00:00
|
|
|
const { t } = useLocale();
|
2021-06-20 14:19:41 +00:00
|
|
|
|
2021-06-30 01:35:08 +00:00
|
|
|
useEffect(() => {
|
2021-06-20 14:19:41 +00:00
|
|
|
setIs24hClock(is24h());
|
|
|
|
setSelectedTimeZone(timeZone());
|
|
|
|
}, []);
|
|
|
|
|
2021-06-30 01:35:08 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (selectedTimeZone && timeZone() && selectedTimeZone !== timeZone()) {
|
|
|
|
props.onSelectTimeZone(timeZone(selectedTimeZone));
|
|
|
|
}
|
2021-06-20 14:19:41 +00:00
|
|
|
}, [selectedTimeZone]);
|
|
|
|
|
2021-08-19 12:36:17 +00:00
|
|
|
const handle24hClockToggle = (is24hClock: boolean) => {
|
|
|
|
setIs24hClock(is24hClock);
|
2021-06-20 14:19:41 +00:00
|
|
|
props.onToggle24hClock(is24h(is24hClock));
|
2021-08-19 12:36:17 +00:00
|
|
|
};
|
2021-06-20 14:19:41 +00:00
|
|
|
|
2021-09-29 21:33:18 +00:00
|
|
|
return selectedTimeZone !== "" ? (
|
2021-12-14 10:39:32 +00:00
|
|
|
<div className="absolute z-10 w-full px-4 py-2 bg-white border border-gray-200 rounded-sm max-w-80 dark:bg-gray-700 dark:border-0">
|
2021-09-29 21:33:18 +00:00
|
|
|
<div className="flex mb-4">
|
2021-12-14 10:39:32 +00:00
|
|
|
<div className="w-1/2 font-medium text-gray-600 dark:text-white">{t("time_options")}</div>
|
2021-09-29 21:33:18 +00:00
|
|
|
<div className="w-1/2">
|
|
|
|
<Switch.Group as="div" className="flex items-center justify-end">
|
|
|
|
<Switch.Label as="span" className="mr-3">
|
2021-12-14 10:39:32 +00:00
|
|
|
<span className="text-sm text-gray-500 dark:text-white">{t("am_pm")}</span>
|
2021-09-29 21:33:18 +00:00
|
|
|
</Switch.Label>
|
|
|
|
<Switch
|
|
|
|
checked={is24hClock}
|
|
|
|
onChange={handle24hClockToggle}
|
|
|
|
className={classNames(
|
2021-12-14 10:39:32 +00:00
|
|
|
is24hClock ? "bg-brand text-brandcontrast" : "dark:bg-gray-600 bg-gray-200",
|
2021-09-29 21:33:18 +00:00
|
|
|
"relative inline-flex flex-shrink-0 h-5 w-8 border-2 border-transparent rounded-full cursor-pointer transition-colors ease-in-out duration-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-black"
|
|
|
|
)}>
|
2021-10-08 11:43:48 +00:00
|
|
|
<span className="sr-only">{t("use_setting")}</span>
|
2021-09-29 21:33:18 +00:00
|
|
|
<span
|
|
|
|
aria-hidden="true"
|
2021-06-20 14:19:41 +00:00
|
|
|
className={classNames(
|
2021-09-29 21:33:18 +00:00
|
|
|
is24hClock ? "translate-x-3" : "translate-x-0",
|
|
|
|
"pointer-events-none inline-block h-4 w-4 rounded-full bg-white shadow transform ring-0 transition ease-in-out duration-200"
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</Switch>
|
|
|
|
<Switch.Label as="span" className="ml-3">
|
2021-12-14 10:39:32 +00:00
|
|
|
<span className="text-sm text-gray-500 dark:text-white">{t("24_h")}</span>
|
2021-09-29 21:33:18 +00:00
|
|
|
</Switch.Label>
|
|
|
|
</Switch.Group>
|
2021-06-20 14:19:41 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-09-29 21:33:18 +00:00
|
|
|
<TimezoneSelect
|
|
|
|
id="timeZone"
|
|
|
|
value={selectedTimeZone}
|
|
|
|
onChange={(tz: ITimezoneOption) => setSelectedTimeZone(tz.value)}
|
2021-12-14 10:39:32 +00:00
|
|
|
className="block w-full mt-1 mb-2 border-gray-300 rounded-md shadow-sm focus:ring-black focus:border-brand sm:text-sm"
|
2021-09-29 21:33:18 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : null;
|
2021-06-30 01:35:08 +00:00
|
|
|
};
|
2021-06-20 14:19:41 +00:00
|
|
|
|
2021-06-30 01:35:08 +00:00
|
|
|
export default TimeOptions;
|