import React, { useEffect, useMemo, useState } from 'react'; import { Play, Pause } from 'lucide-react'; import { cn } from '../lib/utils'; import { useStore } from '../store/useStore'; export function FrameTimeline() { const frames = useStore((state) => state.frames); const currentProject = useStore((state) => state.currentProject); const currentFrameIndex = useStore((state) => state.currentFrameIndex); const setCurrentFrame = useStore((state) => state.setCurrentFrame); const [isPlaying, setIsPlaying] = useState(false); const totalFrames = frames.length; const currentFrame = totalFrames > 0 ? currentFrameIndex + 1 : 0; const playbackFps = useMemo(() => { const fps = currentProject?.parse_fps || currentProject?.original_fps || 12; return Math.min(Math.max(fps, 1), 30); }, [currentProject?.original_fps, currentProject?.parse_fps]); useEffect(() => { if (!isPlaying || totalFrames <= 1) return; const timer = window.setTimeout(() => { if (currentFrameIndex >= totalFrames - 1) { setIsPlaying(false); return; } setCurrentFrame(currentFrameIndex + 1); }, 1000 / playbackFps); return () => window.clearTimeout(timer); }, [currentFrameIndex, isPlaying, playbackFps, setCurrentFrame, totalFrames]); useEffect(() => { if (totalFrames === 0) { setIsPlaying(false); } }, [totalFrames]); // show frames around current frame const frameWindow = 20; const displayIndices = totalFrames > 0 ? Array.from({ length: 41 }, (_, i) => currentFrameIndex - frameWindow + i) : []; return (