import { useEffect, useRef } from 'react' import { Play, Pause, SkipBack, SkipForward, History } from 'lucide-react' import { usePlaybackStore, REPLAY_TICKS, REPLAY_START, REPLAY_END } from '@/store/playbackStore' function formatTick(iso: string): string { const d = new Date(iso) return d.toLocaleString('zh-CN', { month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', hour12: false, }) } export function TimelinePanel() { const { isReplayMode, playbackTime, isPlaying, speedSecPerTick, setReplayMode, setPlaybackTime, setIsPlaying, stepForward, stepBack, setSpeed, } = usePlaybackStore() const timerRef = useRef | null>(null) useEffect(() => { if (!isPlaying || !isReplayMode) { if (timerRef.current) { clearInterval(timerRef.current) timerRef.current = null } return } timerRef.current = setInterval(() => { const current = usePlaybackStore.getState().playbackTime const i = REPLAY_TICKS.indexOf(current) if (i >= REPLAY_TICKS.length - 1) { setIsPlaying(false) return } setPlaybackTime(REPLAY_TICKS[i + 1]) }, speedSecPerTick * 1000) return () => { if (timerRef.current) clearInterval(timerRef.current) } }, [isPlaying, isReplayMode, speedSecPerTick, setPlaybackTime, setIsPlaying]) const index = REPLAY_TICKS.indexOf(playbackTime) const value = index >= 0 ? index : REPLAY_TICKS.length - 1 const handleSliderChange = (e: React.ChangeEvent) => { const i = parseInt(e.target.value, 10) setPlaybackTime(REPLAY_TICKS[i]) } return (
{isReplayMode && ( <>
{formatTick(REPLAY_START)} {formatTick(playbackTime)} {formatTick(REPLAY_END)}
)}
) }