Affect-Atlas / code /frontend /apps /web /src /components /timeline-panel.tsx
Jiali9's picture
Publish Affect-Atlas manuscript, figures, and demo source
891fa77 verified
Raw History Blame Contribute Delete
10.9 kB
import { useTranslation } from 'react-i18next'
import { Info } from 'lucide-react'
import { Alert, AlertDescription, AlertTitle } from '@workspace/ui/components/alert'
import { Badge } from '@workspace/ui/components/badge'
import { Button } from '@workspace/ui/components/button'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@workspace/ui/components/card'
import type { Prediction, TimelinePoint } from '@/lib/api'
import { downloadPrediction, formatDelta, formatScore, formatSeconds, nearestWindow, scoreDomain } from '@/lib/timeline'
interface Props {
result: Prediction | null
currentTime: number
selectedIndex: number | null
onSelect: (point: TimelinePoint) => void
}
export function TimelinePanel({ result, currentTime, selectedIndex, onSelect }: Props) {
const { t } = useTranslation()
const points = result?.timeline?.points ?? []
const selected = points.find(point => point.index === selectedIndex) ?? null
const active = nearestWindow(points, currentTime)
const [low, high] = scoreDomain(points)
const duration = Math.max(1, result?.duration_seconds ?? 1, ...points.map(point => point.end_seconds))
const x = (time: number) => 72 + time / duration * 876
const y = (score: number) => 260 - (score - low) / (high - low) * 220
const curves = [
{ key: 'valence' as const, label: t('window.valenceCurve'), color: 'var(--chart-valence)' },
{ key: 'arousal' as const, label: t('window.arousalCurve'), color: 'var(--chart-arousal)' }
]
return (
<section aria-label={t('window.title')} className="timeline-section">
<Card className="panel timeline-panel">
<CardHeader>
<div className="result-heading">
<div>
<div className="section-number">{t('window.sectionNumber')}</div>
<CardTitle>{t('window.title')}</CardTitle>
</div>
<Badge variant="outline">{t('window.badge')}</Badge>
</div>
<CardDescription>{t('window.desc')}</CardDescription>
</CardHeader>
<CardContent>
<Alert className="timeline-notice">
<Info />
<AlertTitle>{t('window.alertTitle')}</AlertTitle>
<AlertDescription>
{t('window.alertDesc')}
{result?.timeline && <p>{t('window.methodNote')}</p>}
</AlertDescription>
</Alert>
{!points.length ? (
<div className="timeline-empty">
{result
? result.timeline
? t('window.emptyNoData')
: t('window.emptyOldResult')
: t('window.emptyWaiting')}
</div>
) : (
<>
<div className="timeline-toolbar">
<div className="curve-legend">
{curves.map(curve => (
<span key={curve.key}>
<i style={{ background: curve.color }} />
{curve.label}
</span>
))}
</div>
<span>{t('window.playbackPos', { time: formatSeconds(currentTime) })}</span>
</div>
{points.length === 1 && (
<p className="single-window-note">{t('window.singleWindowNote')}</p>
)}
<div className="timeline-chart-scroll">
<svg
viewBox="0 0 1000 325"
role="img"
aria-label={t('window.chartAriaLabel')}
className="timeline-chart"
>
{[0, 1, 2, 3, 4].map(tick => {
const score = low + (high - low) * tick / 4
return (
<g key={tick}>
<line
x1="72"
x2="948"
y1={y(score)}
y2={y(score)}
stroke="var(--chart-grid)"
/>
<text
x="60"
y={y(score) + 4}
textAnchor="end"
className="tick"
fill="var(--ocean-text-muted)"
>
{score.toFixed(2)}
</text>
<text
x={x(duration * tick / 4)}
y="284"
textAnchor="middle"
className="tick"
fill="var(--ocean-text-muted)"
>
{(duration * tick / 4).toFixed(1)}
</text>
</g>
)
})}
{selected && (
<rect
x={x(selected.start_seconds)}
y="40"
width={Math.max(1, x(selected.end_seconds) - x(selected.start_seconds))}
height="220"
fill="var(--chart-arousal)"
opacity=".15"
/>
)}
{curves.map(curve => (
<g key={curve.key}>
{points.length > 1 && (
<polyline
points={points.map(point => `${x(point.center_seconds)},${y(point[curve.key])}`).join(' ')}
fill="none"
stroke={curve.color}
strokeWidth="2.5"
/>
)}
{points.map(point => (
<circle
key={point.index}
cx={x(point.center_seconds)}
cy={y(point[curve.key])}
r={point.index === selectedIndex ? 7 : 5}
fill={curve.color}
stroke="var(--pearl)"
strokeWidth="2"
onClick={() => onSelect(point)}
className="curve-point"
>
<title>
{t('window.windowLabel', { index: point.index + 1 })} 路{' '}
{formatSeconds(point.start_seconds)}鈥搟formatSeconds(point.end_seconds)} 路{' '}
{curve.label} {point[curve.key]}
</title>
</circle>
))}
</g>
))}
<line
x1={x(Math.max(0, Math.min(duration, currentTime)))}
x2={x(Math.max(0, Math.min(duration, currentTime)))}
y1="32"
y2="260"
stroke="var(--chart-arousal)"
strokeWidth="1.5"
strokeDasharray="4 4"
opacity="0.7"
data-testid="playhead"
/>
<text x="510" y="315" textAnchor="middle" className="axis" fill="var(--ocean-text-muted)">
{t('window.axisTime')}
</text>
<text
x="18"
y="150"
textAnchor="middle"
transform="rotate(-90 18 150)"
className="axis"
fill="var(--ocean-text-muted)"
>
{t('window.axisScore')}
</text>
</svg>
</div>
<p className="window-help">{t('window.helpText')}</p>
<div className="window-list" aria-label={t('window.title')}>
{points.map(point => (
<Button
key={point.index}
variant="outline"
aria-pressed={selectedIndex === point.index}
onClick={() => onSelect(point)}
className={`window-button ${active?.index === point.index ? 'playing-window' : ''}`}
>
<span>
{t('window.windowLabel', { index: point.index + 1 })} 路{' '}
{formatSeconds(point.start_seconds)}鈥搟formatSeconds(point.end_seconds)}
</span>
<small>
V {formatScore(point.valence)} 路 A {formatScore(point.arousal)}
{active?.index === point.index ? ` 路 ${t('window.playingIn')}` : ''}
</small>
</Button>
))}
</div>
{selected && result && (
<div className="window-detail" aria-live="polite">
<strong>{t('window.selectedWindow', { index: selected.index + 1 })}</strong>
<p>
{t('window.windowStart')} {formatSeconds(selected.start_seconds)} 路{' '}
{t('window.windowEnd')} {formatSeconds(selected.end_seconds)} 路{' '}
{t('window.windowCenter')} {formatSeconds(selected.center_seconds)} 路{' '}
{t('window.windowBins')} {selected.feature_bins}
</p>
<div className="metric-grid">
{curves.map(curve => (
<div className="metric" key={curve.key}>
<span>{curve.label}</span>
<strong title={t('result.tooltipOriginal', { value: selected[curve.key] })}>
{formatScore(selected[curve.key])}
</strong>
<p title={t('result.tooltipOriginal', { value: selected[curve.key] - result[curve.key] })}>
{t('window.relativeTo', { delta: formatDelta(selected[curve.key], result[curve.key]) })}
</p>
</div>
))}
</div>
</div>
)}
{(low < -1 || high > 1) && (
<p className="range-explanation">{t('window.rangeNote')}</p>
)}
</>
)}
{result && (
<div className="export-actions">
<Button variant="outline" onClick={() => downloadPrediction(result, 'json')}>
{t('window.exportJson')}
</Button>
<Button
variant="outline"
disabled={!points.length}
onClick={() => downloadPrediction(result, 'csv')}
>
{t('window.exportCsv')}
</Button>
<span>{t('window.exportNote')}</span>
</div>
)}
</CardContent>
</Card>
</section>
)
}