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 (
{previewed?.longLabel ?? ''}
setIdx(Number(e.target.value))} onMouseUp={() => commit(idx)} onTouchEnd={() => commit(idx)} onKeyUp={() => commit(idx)} className="w-full cursor-pointer accent-emerald-600" aria-label="Select period" />
{sorted.map((p, i) => { const stride = Math.max(1, Math.ceil(sorted.length / 8)); const visible = i === 0 || i === last || i === idx || i % stride === 0; return ( {p.label} ); })}
); }