import { useEffect, useState } from 'react'; import type { PeriodOption } from './time-range'; interface Props { periods: PeriodOption[]; activeKey: string; onPick: (key: string) => void; } export function PeriodSlider({ periods, activeKey, onPick }: Props) { const sorted = [...periods].reverse(); const activeIdx = Math.max(0, sorted.findIndex((p) => p.key === activeKey)); const last = sorted.length - 1; const [idx, setIdx] = useState(activeIdx); useEffect(() => { setIdx(activeIdx); }, [activeIdx]); const previewed = sorted[idx] ?? sorted[activeIdx]; const commit = (nextIdx: number) => { const period = sorted[nextIdx]; if (period && period.key !== activeKey) onPick(period.key); }; return (