import { useEffect, useRef, useState } from 'react'; import { ChevronLeft, ChevronRight, Play, Radio, Square } from 'lucide-react'; import type { ThemeTokens } from '../../context/ThemeContext'; import type { I18n } from '../../i18n/locales'; import { CURRENT_PERIOD, PERIOD_COUNT, periodLabel, periodShortLabel } from '../../lib/oeeTime'; import { replayHealth } from '../../lib/replay'; /** * BI 式时间轴滑块:拖动 / 点击刻度 / 自动回放,全图按游标时点重取值与状态。 * 背景柱不再借用经营指标 OV,而是由「回放健康度」驱动: * 与中央六页签、左栏流程、抬头同属一条 2025-11 → 2026-10 的质量曲线。 */ interface Props { period: number; onPeriod: (p: number) => void; tokens: ThemeTokens; t: I18n; } export default function MetricTimeline({ period, onPeriod, tokens, t }: Props) { const [playing, setPlaying] = useState(false); const trackRef = useRef(null); const draggingRef = useRef(false); const periodRef = useRef(period); periodRef.current = period; const onPeriodRef = useRef(onPeriod); onPeriodRef.current = onPeriod; useEffect(() => { if (!playing) return; const id = window.setInterval(() => { onPeriodRef.current((periodRef.current + 1) % PERIOD_COUNT); }, 850); return () => window.clearInterval(id); }, [playing]); const posToPeriod = (clientX: number) => { const el = trackRef.current; if (!el) return periodRef.current; const r = el.getBoundingClientRect(); const f = (clientX - r.left) / Math.max(1, r.width); return Math.max(0, Math.min(PERIOD_COUNT - 1, Math.floor(f * PERIOD_COUNT))); }; const [trackW, setTrackW] = useState(0); useEffect(() => { const el = trackRef.current; if (!el) return; const ro = new ResizeObserver(([e]) => setTrackW(e.contentRect.width)); ro.observe(el); return () => ro.disconnect(); }, []); // 日轴刻度按轨道实宽抽稀(每签约 34px),当期刻度恒显示,避开其两侧的常规刻度 const step = Math.max(1, Math.ceil((PERIOD_COUNT * 40) / Math.max(1, trackW))); const shownTicks = Array.from({ length: PERIOD_COUNT }, (_, i) => i).filter( (i) => i === period || (i % step === 0 && Math.abs(i - period) >= step), ); const cur = periodLabel(period); return (
{/* 回放控制 */} {/* 轨道:12 根健康度柱 + 游标 */}
{ draggingRef.current = true; setPlaying(false); try { e.currentTarget.setPointerCapture(e.pointerId); } catch { /* 合成指针下降级为无捕获拖动 */ } onPeriod(posToPeriod(e.clientX)); }} onPointerMove={e => { if (draggingRef.current) onPeriod(posToPeriod(e.clientX)); }} onPointerUp={() => { draggingRef.current = false; }} onPointerCancel={() => { draggingRef.current = false; }} > {/* 柱阵列 */}
40 ? 1 : 3 }}> {Array.from({ length: PERIOD_COUNT }, (_, i) => { const health = replayHealth(i); const passed = i <= period; const h = 12 + Math.max(0.2, Math.min(1, health / 100)) * 24; const col = health >= 85 ? tokens.success : health >= 70 ? tokens.warning : tokens.danger; return (
); })}
{/* 游标发光线 */}
{/* 日刻度 */}
{shownTicks.map((i) => ( {periodShortLabel(i)} ))}
{/* 当前期读数 */}
{cur}
); }