import { lazy, Suspense, useState } from "react"; import { useLocale } from "../../hooks/useLocale"; import { t } from "../../i18n/translate"; import type { TimeWindow, TimeWindowPreset } from "../../lib/time-window"; import { cn } from "../../lib/utils"; const CustomTimeWindowDialog = lazy(() => import("../CustomTimeWindowDialog").then((module) => ({ default: module.CustomTimeWindowDialog, })), ); const PRESETS: readonly { value: TimeWindowPreset; label: string }[] = [ { value: "7d", label: "7d" }, { value: "14d", label: "14d" }, { value: "30d", label: "30d" }, { value: "90d", label: "90d" }, { value: "all", label: "All" }, { value: "custom", label: "Custom" }, ]; function monthDay(timestamp: number): string { const date = new Date(timestamp); return `${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")}`; } export function TimeRangeControl({ window, preset, customFrom, customTo, onSelectPreset, onSelectCustom, }: { window: TimeWindow; preset: TimeWindowPreset; customFrom?: string; customTo?: string; onSelectPreset: (preset: TimeWindowPreset) => void; onSelectCustom: (from: string, to: string) => void; }) { useLocale(); const [customOpen, setCustomOpen] = useState(false); const bounded = window.days !== 0 && window.from != null && window.to != null; return ( <> {bounded ? ( {monthDay(window.from!)} → {monthDay(window.to!)} ) : null}