// 主机监控时间范围选择器 — 预设 + 自定义 // Logan.Li import { useState, useRef, useEffect } from 'react'; import { Calendar, ChevronDown, Timer } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { TIME_RANGE_PRESETS, type HostMonitorTimeRange, type TimeRangePresetId, } from './types'; import { presetToRange, toDatetimeLocalValue, formatRangeSummary } from './timeRange'; import { useTranslation } from 'react-i18next'; interface Props { value: HostMonitorTimeRange; onChange: (range: HostMonitorTimeRange) => void; } export function HostTimeRangePicker({ value, onChange }: Props) { const { t } = useTranslation(); const [customOpen, setCustomOpen] = useState(false); const now = new Date(); const [customStart, setCustomStart] = useState(toDatetimeLocalValue(new Date(now.getTime() - 3600_000))); const [customEnd, setCustomEnd] = useState(toDatetimeLocalValue(now)); const popRef = useRef(null); useEffect(() => { const handler = (e: MouseEvent) => { if (popRef.current && !popRef.current.contains(e.target as Node)) setCustomOpen(false); }; document.addEventListener('mousedown', handler); return () => document.removeEventListener('mousedown', handler); }, []); const selectPreset = (id: TimeRangePresetId) => { if (id === 'custom') return; onChange(presetToRange(id)); setCustomOpen(false); }; const applyCustom = () => { const start = new Date(customStart); const end = new Date(customEnd); if (Number.isNaN(start.getTime()) || Number.isNaN(end.getTime())) return; const s = start.getTime() <= end.getTime() ? start : end; const e = start.getTime() <= end.getTime() ? end : start; onChange({ preset: 'custom', label: t('aiops.providerCustom'), start: s.toISOString(), end: e.toISOString(), buckets: Math.min(120, Math.max(36, Math.ceil((e.getTime() - s.getTime()) / 60_000 / 2))), }); setCustomOpen(false); }; return (
时间范围
{TIME_RANGE_PRESETS.map((p) => ( ))}
{customOpen && (

{t('monitor.lbl.unlimited')}

setCustomStart(e.target.value)} className="h-8 text-xs dg-input mt-0.5" />
setCustomEnd(e.target.value)} className="h-8 text-xs dg-input mt-0.5" />
)}
{formatRangeSummary(value)}
); }