peppa/apps/web/components/TimezoneField.tsx
2026-11-14 05:10:20 +05:00

35 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client";
import { useEffect, useState } from "react";
import { Input, Label } from "@/components/ui/input";
/** IANA timezone for quiet hours and the digest, with «как в браузере» (PEP-35). */
export function TimezoneField({ value }: { value: string }) {
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">
Таймзона
<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)}>
как в браузере ({browser})
</button>
)}
</div>
);
}