peppa/apps/web/components/TimezoneField.tsx
2026-11-15 14:47:50 +05:00

37 lines
1.4 KiB
TypeScript

"use client";
import { useEffect, useState } from "react";
import { Input, Label } from "@/components/ui/input";
import { useT } from "@/components/I18n";
/** IANA timezone for quiet hours and the digest, with «как в браузере» (PEP-35). */
export function TimezoneField({ value }: { value: string }) {
const t = useT();
const [tz, setTz] = useState(value);
const [browser, setBrowser] = useState<string | null>(null);
const [zones, setZones] = useState<string[]>([]);
// Client-only: the server's and the browser's lists may differ (no hydration mismatch).
useEffect(() => {
setBrowser(Intl.DateTimeFormat().resolvedOptions().timeZone);
setZones(typeof Intl.supportedValuesOf === "function" ? Intl.supportedValuesOf("timeZone") : []);
}, []);
return (
<div className="flex flex-wrap items-end gap-2">
<Label className="w-56">
{t("Таймзона")}
<Input name="timezone" value={tz} onChange={(e) => setTz(e.target.value)} list="peppa-timezones" />
</Label>
<datalist id="peppa-timezones">
{zones.map((z) => (
<option key={z} value={z} />
))}
</datalist>
{browser && browser !== tz && (
<button type="button" className="mb-2 text-sm text-primary" onClick={() => setTz(browser)}>
{t("как в браузере ({browser})", { browser })}
</button>
)}
</div>
);
}