'use client' import React, { useState, useCallback, useRef, useEffect } from 'react' import { motion, AnimatePresence } from 'motion/react' import { Play, Pause, SkipBack, SkipForward, ChevronDown, ChevronUp, RotateCcw } from 'lucide-react' import { useBrainstormSnapshots } from '@/hooks/use-brainstorm' import { useLanguage } from '@/lib/i18n' interface SnapshotIdea { id: string title: string waveNumber: number positionX: number | null positionY: number | null parentIdeaId: string | null noveltyScore: number | null createdByType: string | null status: string } interface Snapshot { id: string step: number label: string | null ideaGraph: string createdAt: string } interface PlaybackBarProps { sessionId: string onSnapshotSelect: (ideas: SnapshotIdea[]) => void onExitPlayback: () => void } export function PlaybackBar({ sessionId, onSnapshotSelect, onExitPlayback }: PlaybackBarProps) { const { t } = useLanguage() const { data: snapshots } = useBrainstormSnapshots(sessionId) const [isOpen, setIsOpen] = useState(false) const [currentStep, setCurrentStep] = useState(-1) const [isPlaying, setIsPlaying] = useState(false) const playIntervalRef = useRef | null>(null) const parsedSnapshots: (Snapshot & { ideas: SnapshotIdea[] })[] = React.useMemo(() => { return (snapshots || []).map((s: Snapshot) => ({ ...s, ideas: JSON.parse(s.ideaGraph) as SnapshotIdea[], })) }, [snapshots]) const handleStepChange = useCallback((step: number) => { if (step === -1) { setCurrentStep(-1) onExitPlayback() return } const snapshot = parsedSnapshots[step] if (snapshot) { setCurrentStep(step) onSnapshotSelect(snapshot.ideas) } }, [parsedSnapshots, onSnapshotSelect, onExitPlayback]) const togglePlay = useCallback(() => { if (isPlaying) { setIsPlaying(false) if (playIntervalRef.current) clearInterval(playIntervalRef.current) return } setIsPlaying(true) const startStep = currentStep === -1 ? 0 : currentStep + 1 let step = startStep playIntervalRef.current = setInterval(() => { if (step >= parsedSnapshots.length) { setIsPlaying(false) if (playIntervalRef.current) clearInterval(playIntervalRef.current) return } handleStepChange(step) step++ }, 1500) }, [isPlaying, currentStep, parsedSnapshots.length, handleStepChange]) useEffect(() => { return () => { if (playIntervalRef.current) clearInterval(playIntervalRef.current) } }, []) if (!parsedSnapshots || parsedSnapshots.length === 0) return null const isLive = currentStep === -1 return (
)} {isOpen ? : }
{isOpen && (
handleStepChange(parseInt(e.target.value))} className="w-full h-1.5 bg-border rounded-full appearance-none cursor-pointer accent-orange-500" />
{t('brainstorm.liveStatus')} {t('brainstorm.playbackStepsCount', { count: parsedSnapshots.length })}
{parsedSnapshots.map((s, idx) => ( ))}
)}
) }