import { useRef, useState } from 'react' import { useTranslation } from 'react-i18next' import { Info } from 'lucide-react' import { Alert, AlertDescription, AlertTitle } from '@workspace/ui/components/alert' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@workspace/ui/components/card' import type { Prediction, TimelinePoint } from '@/lib/api' import { formatSeconds, formatScore } from '@/lib/timeline' interface Props { result: Prediction | null currentTime: number onSeek: (time: number) => void lang?: 'zh' | 'en' // kept for compatibility but now uses i18next directly } interface Tooltip { x: number y: number time: number arousal: number visible: boolean } export function EmotionTimelineChart({ result, currentTime, onSeek }: Props) { const { t } = useTranslation() const points = result?.timeline?.points ?? [] const svgRef = useRef(null) const [tooltip, setTooltip] = useState({ x: 0, y: 0, time: 0, arousal: 0, visible: false }) // Calculate dimensions const duration = Math.max(1, result?.duration_seconds ?? 1, ...points.map(p => p.end_seconds)) const viewBox = { width: 1000, height: 300 } const margin = { left: 60, right: 40, top: 30, bottom: 50 } const graphWidth = viewBox.width - margin.left - margin.right const graphHeight = viewBox.height - margin.top - margin.bottom // Get Arousal data range const arousalValues = points.map(p => p.arousal) const minArousal = arousalValues.length ? Math.min(-1, ...arousalValues) : -1 const maxArousal = arousalValues.length ? Math.max(1, ...arousalValues) : 1 const arousalRange = maxArousal - minArousal // Transform functions const xScale = (time: number) => margin.left + (time / duration) * graphWidth const yScale = (arousal: number) => margin.top + graphHeight - ((arousal - minArousal) / arousalRange) * graphHeight // Generate smooth curve path const generateCurvePath = () => { if (points.length === 0) return '' if (points.length === 1) { const x = xScale(points[0].center_seconds) const y = yScale(points[0].arousal) return `M ${x} ${y}` } let path = '' for (let i = 0; i < points.length; i++) { const p = points[i] const x = xScale(p.center_seconds) const y = yScale(p.arousal) if (i === 0) { path += `M ${x} ${y}` } else { // Quadratic Bezier curve for smooth connection const prevP = points[i - 1] const prevX = xScale(prevP.center_seconds) const prevY = yScale(prevP.arousal) const ctrlX = (prevX + x) / 2 const ctrlY = (prevY + y) / 2 path += ` Q ${ctrlX} ${ctrlY} ${x} ${y}` } } return path } // Generate gradient fill path (closed area) const generateFillPath = () => { if (points.length === 0) return '' let path = generateCurvePath() const lastX = xScale(points[points.length - 1].center_seconds) const bottomY = margin.top + graphHeight path += ` L ${lastX} ${bottomY}` if (points.length > 0) { const firstX = xScale(points[0].center_seconds) path += ` L ${firstX} ${bottomY} Z` } return path } // Handle mouse move for tooltip const handleMouseMove = (e: React.MouseEvent) => { if (!svgRef.current || points.length === 0) return const rect = svgRef.current.getBoundingClientRect() const mouseX = e.clientX - rect.left const mouseY = e.clientY - rect.top // Convert mouse position to SVG coordinates const svgX = (mouseX / rect.width) * viewBox.width const svgY = (mouseY / rect.height) * viewBox.height // Check if within graph area if ( svgX < margin.left || svgX > margin.left + graphWidth || svgY < margin.top || svgY > margin.top + graphHeight ) { setTooltip(prev => ({ ...prev, visible: false })) return } // Find nearest point let nearestPoint: TimelinePoint | null = null let minDist = Infinity for (const p of points) { const pX = xScale(p.center_seconds) const dist = Math.abs(pX - svgX) if (dist < minDist && dist < 30) { minDist = dist nearestPoint = p } } if (nearestPoint) { const pX = xScale(nearestPoint.center_seconds) const pY = yScale(nearestPoint.arousal) setTooltip({ x: pX, y: pY, time: nearestPoint.center_seconds, arousal: nearestPoint.arousal, visible: true, }) } else { setTooltip(prev => ({ ...prev, visible: false })) } } const handleMouseLeave = () => { setTooltip(prev => ({ ...prev, visible: false })) } // Handle click to seek const handleClick = (e: React.MouseEvent) => { if (!svgRef.current) return const rect = svgRef.current.getBoundingClientRect() const mouseX = e.clientX - rect.left const svgX = (mouseX / rect.width) * viewBox.width if (svgX < margin.left || svgX > margin.left + graphWidth) return const seekTime = ((svgX - margin.left) / graphWidth) * duration onSeek(Math.max(0, Math.min(duration, seekTime))) } // Remove old labels object since we now use t() directly // Current playhead position const playheadX = xScale(Math.max(0, Math.min(duration, currentTime))) const playheadY = margin.top + graphHeight // Find arousal value at current time (interpolated or from nearest point) const getCurrentArousal = () => { if (points.length === 0) return null if (points.length === 1) return points[0].arousal for (let i = 0; i < points.length - 1; i++) { if (currentTime >= points[i].center_seconds && currentTime <= points[i + 1].center_seconds) { const ratio = (currentTime - points[i].center_seconds) / (points[i + 1].center_seconds - points[i].center_seconds) return points[i].arousal + ratio * (points[i + 1].arousal - points[i].arousal) } } return points[points.length - 1].arousal } const currentArousal = getCurrentArousal() const currentArousalY = currentArousal !== null ? yScale(currentArousal) : playheadY return (
04 / {t('timeline.title')}
{t('timeline.title')}
{t('timeline.desc')}
{t('timeline.arousal')} {t('timeline.helpText')} {points.length === 0 ? (
{result ? result.timeline ? t('timeline.empty') : t('timeline.waiting') : t('timeline.waiting')}
) : (
{/* Grid lines */} {[0, 1, 2, 3, 4].map(i => { const arousal = minArousal + (arousalRange * i) / 4 const y = yScale(arousal) return ( {arousal.toFixed(2)} ) })} {/* Time labels */} {[0, 1, 2, 3, 4].map(i => { const time = (duration * i) / 4 const x = xScale(time) return ( {time.toFixed(1)}s ) })} {/* Y-axis */} {/* X-axis */} {/* Arousal curve fill */} {points.length > 0 && ( )} {/* Arousal curve */} {points.length > 0 && ( )} {/* Data points */} {points.map(point => ( ))} {/* Playhead line */} {/* Playhead dot at curve intersection */} {currentArousal !== null && ( )} {/* Tooltip */} {tooltip.visible && ( {formatSeconds(tooltip.time)} A: {formatScore(tooltip.arousal)} )} {/* Axis labels */} {t('timeline.time')} {t('timeline.arousal')}
)} {points.length > 0 && (

{t('timeline.clickHelp')}

)}
) }