181 lines
8.4 KiB
TypeScript
181 lines
8.4 KiB
TypeScript
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<HTMLDivElement>(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 (
|
|
<div style={{
|
|
flex: '0 0 auto', display: 'flex', alignItems: 'center', gap: 12,
|
|
margin: '0 6px 6px', padding: '9px 14px', borderRadius: 12,
|
|
background: tokens.panelBgAlt, border: `1px solid ${tokens.border}`,
|
|
backdropFilter: 'blur(8px)', userSelect: 'none',
|
|
}}>
|
|
{/* 回放控制 */}
|
|
<button
|
|
onClick={() => setPlaying(p => !p)}
|
|
className="ghost-btn"
|
|
title={playing ? t.net.tlPause : t.net.tlPlay}
|
|
style={{
|
|
display: 'flex', alignItems: 'center', gap: 5, flexShrink: 0, cursor: 'pointer',
|
|
padding: '4px 10px', borderRadius: 8, fontSize: 12, fontWeight: 700,
|
|
color: playing ? tokens.onAccent : tokens.accent,
|
|
background: playing ? tokens.accent : tokens.accentBg,
|
|
border: `1px solid ${tokens.borderAccent}`,
|
|
}}
|
|
>
|
|
{playing ? <Square size={11} /> : <Play size={11} />}
|
|
{playing ? t.net.tlPause : t.net.tlPlay}
|
|
</button>
|
|
|
|
{/* 轨道:12 根健康度柱 + 游标 */}
|
|
<div
|
|
ref={trackRef}
|
|
style={{ position: 'relative', flex: 1, height: 42, cursor: 'ew-resize', touchAction: 'none', minWidth: 0 }}
|
|
onPointerDown={e => {
|
|
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; }}
|
|
>
|
|
{/* 柱阵列 */}
|
|
<div style={{ position: 'absolute', inset: '0 0 12px', display: 'flex', gap: PERIOD_COUNT > 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 (
|
|
<div key={i} title={`回放健康度 ${health}`} style={{ flex: 1, display: 'flex', flexDirection: 'column', justifyContent: 'flex-end', alignItems: 'stretch' }}>
|
|
<div style={{
|
|
height: h, borderRadius: 3,
|
|
background: col,
|
|
opacity: i === period ? 0.95 : passed ? 0.42 : 0.16,
|
|
boxShadow: i === period ? `0 0 10px ${col}88` : 'none',
|
|
transition: 'opacity 0.25s ease, height 0.4s ease, box-shadow 0.25s ease',
|
|
}} />
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
{/* 游标发光线 */}
|
|
<div style={{
|
|
position: 'absolute', top: 0, bottom: 10, width: 2, borderRadius: 2,
|
|
left: `calc(${((period + 0.5) / PERIOD_COUNT) * 100}% - 1px)`,
|
|
background: `linear-gradient(180deg, ${tokens.accent}, ${tokens.info})`,
|
|
boxShadow: `0 0 10px ${tokens.accentGlow}`,
|
|
transition: 'left 0.28s cubic-bezier(0.22,1,0.36,1)', pointerEvents: 'none',
|
|
}}>
|
|
<span style={{
|
|
position: 'absolute', top: -3, left: '50%', transform: 'translateX(-50%)',
|
|
width: 9, height: 9, borderRadius: '50%', background: tokens.accent,
|
|
boxShadow: `0 0 8px ${tokens.accentGlow}`, border: `2px solid ${tokens.panelBgAlt}`,
|
|
}} />
|
|
</div>
|
|
{/* 日刻度 */}
|
|
<div style={{ position: 'absolute', left: 0, right: 0, bottom: -2, height: 12, overflow: 'hidden' }}>
|
|
{shownTicks.map((i) => (
|
|
<span key={i} style={{
|
|
position: 'absolute', top: 0, whiteSpace: 'nowrap', fontSize: 9.5, lineHeight: '12px',
|
|
left: i === 0 ? 0 : i === PERIOD_COUNT - 1 ? '100%' : `${((i + 0.5) / PERIOD_COUNT) * 100}%`,
|
|
transform: `translateX(${i === 0 ? '0' : i === PERIOD_COUNT - 1 ? '-100%' : '-50%'})`,
|
|
fontVariantNumeric: 'tabular-nums',
|
|
color: i === period ? tokens.accent : tokens.textMuted,
|
|
opacity: i === period ? 1 : 0.55, fontWeight: i === period ? 800 : 400,
|
|
}}>{periodShortLabel(i)}</span>
|
|
))}
|
|
</div>
|
|
</div>
|
|
|
|
{/* 当前期读数 */}
|
|
<div style={{ display: 'flex', alignItems: 'center', gap: 7, flexShrink: 0 }}>
|
|
<button
|
|
onClick={() => onPeriod(Math.max(0, period - 1))}
|
|
className="ghost-btn"
|
|
style={{ display: 'grid', placeItems: 'center', width: 22, height: 22, borderRadius: 6, cursor: 'pointer', color: tokens.textSub, border: `1px solid ${tokens.border}`, background: 'transparent' }}
|
|
><ChevronLeft size={13} /></button>
|
|
<span style={{
|
|
display: 'flex', alignItems: 'center', gap: 5, fontSize: 13.5, fontWeight: 800,
|
|
color: tokens.accent, fontVariantNumeric: 'tabular-nums', fontFamily: 'ui-monospace, monospace',
|
|
minWidth: 86, justifyContent: 'center',
|
|
}}>
|
|
<Radio size={11} />{cur}
|
|
</span>
|
|
<button
|
|
onClick={() => onPeriod(Math.min(PERIOD_COUNT - 1, period + 1))}
|
|
className="ghost-btn"
|
|
style={{ display: 'grid', placeItems: 'center', width: 22, height: 22, borderRadius: 6, cursor: 'pointer', color: tokens.textSub, border: `1px solid ${tokens.border}`, background: 'transparent' }}
|
|
><ChevronRight size={13} /></button>
|
|
<button
|
|
onClick={() => { setPlaying(false); onPeriod(CURRENT_PERIOD); }}
|
|
className="ghost-btn"
|
|
disabled={period === CURRENT_PERIOD}
|
|
style={{
|
|
padding: '3px 10px', borderRadius: 8, fontSize: 11.5, fontWeight: 700, cursor: period === CURRENT_PERIOD ? 'default' : 'pointer',
|
|
color: period === CURRENT_PERIOD ? tokens.textMuted : tokens.accent,
|
|
border: `1px solid ${period === CURRENT_PERIOD ? tokens.border : tokens.borderAccent}`,
|
|
background: period === CURRENT_PERIOD ? 'transparent' : tokens.accentBg,
|
|
whiteSpace: 'nowrap',
|
|
}}
|
|
>{t.net.tlBack}</button>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|