import { useCallback, useRef } from 'react' import { useTranslation } from 'react-i18next' import type { TimeRange } from '../../api/crowdsecDashboard' interface DashboardTimeRangeSelectorProps { value: TimeRange onChange: (range: TimeRange) => void } const RANGES: TimeRange[] = ['1h', '6h', '24h', '7d', '30d'] const RANGE_LABELS: Record = { '1h': '1H', '6h': '6H', '24h': '24H', '7d': '7D', '30d': '30D', } export function DashboardTimeRangeSelector({ value, onChange }: DashboardTimeRangeSelectorProps) { const { t } = useTranslation() const buttonRefs = useRef<(HTMLButtonElement | null)[]>([]) const selectedIndex = RANGES.indexOf(value) const handleKeyDown = useCallback( (e: React.KeyboardEvent) => { let nextIndex: number switch (e.key) { case 'ArrowRight': case 'ArrowDown': nextIndex = (selectedIndex + 1) % RANGES.length break case 'ArrowLeft': case 'ArrowUp': nextIndex = (selectedIndex - 1 + RANGES.length) % RANGES.length break case 'Home': nextIndex = 0 break case 'End': nextIndex = RANGES.length - 1 break default: return } e.preventDefault() onChange(RANGES[nextIndex]) buttonRefs.current[nextIndex]?.focus() }, [selectedIndex, onChange], ) return (
{RANGES.map((range, i) => ( ))}
) }