Pit-Lane-Project / frontend /Dashboard /src /components /PositionChangePanel.tsx
Interact-ai's picture
Upload folder using huggingface_hub
267266e verified
Raw History Blame Contribute Delete
6.75 kB
import React, { useMemo } from 'react';
import { AreaChart, Area, XAxis, YAxis, CartesianGrid, ResponsiveContainer } from 'recharts';
import type { DriverPrediction, Driver, RacePhase } from '../types';
interface PositionChangePanelProps {
driver: Driver | null;
prediction: DriverPrediction | null;
racePhase: RacePhase;
}
export const PositionChangePanel: React.FC<PositionChangePanelProps> = ({ driver, prediction, racePhase }) => {
const chartData = useMemo(() => {
if (!prediction || !prediction.positionHistory || prediction.positionHistory.length === 0) {
return [];
}
return prediction.positionHistory.map((pos, idx) => ({
lap: idx + 1,
position: pos
}));
}, [prediction]);
const renderBadge = () => {
if (racePhase === 'finished') {
return (
<div className="flex items-center gap-1.5 bg-zinc-950 dark:bg-zinc-800 border border-zinc-950 dark:border-zinc-700 px-2 py-1 rounded-full shadow-sm">
<svg width="10" height="10" viewBox="0 0 12 12" className="opacity-90">
<rect x="0" y="0" width="4" height="4" fill="#fff" />
<rect x="4" y="0" width="4" height="4" fill="#52525b" />
<rect x="8" y="0" width="4" height="4" fill="#fff" />
<rect x="0" y="4" width="4" height="4" fill="#52525b" />
<rect x="4" y="4" width="4" height="4" fill="#fff" />
<rect x="8" y="4" width="4" height="4" fill="#52525b" />
<rect x="0" y="8" width="4" height="4" fill="#fff" />
<rect x="4" y="8" width="4" height="4" fill="#52525b" />
<rect x="8" y="8" width="4" height="4" fill="#fff" />
</svg>
<span className="text-[9px] font-sans font-bold text-white tracking-widest uppercase">Finished</span>
</div>
);
}
return (
<div className="bg-racing-red text-white text-[9px] font-bold px-1.5 py-0.5 rounded uppercase tracking-wider animate-pulse flex items-center gap-1 shadow-sm">
<div className="w-1.5 h-1.5 bg-white rounded-full"></div>
Live
</div>
);
};
const CustomDot = (props: any) => {
const { cx, cy, payload, index } = props;
const isFirst = index === 0;
const isLast = index === chartData.length - 1;
if (isFirst || isLast) {
return (
<g>
<circle cx={cx} cy={cy} r={4} fill="var(--color-racing-red)" stroke="#fff" strokeWidth={2} />
<text
x={cx}
y={cy - 12}
textAnchor="middle"
className="fill-zinc-900 dark:fill-zinc-100"
fontSize="10"
fontWeight="bold"
fontFamily="sans-serif"
>
P{payload.position}
</text>
</g>
);
}
return <circle cx={cx} cy={cy} r={2.5} fill="var(--color-racing-red)" />;
};
return (
<div className="bg-white dark:bg-zinc-900 rounded-xl p-4 shadow-sm border border-zinc-200 dark:border-zinc-800 flex flex-col relative overflow-hidden flex-1 min-h-[200px] transition-colors duration-200">
<div className="absolute top-0 left-0 w-full h-1 flex">
<div className="h-full flex-1 bg-[#009246]"></div>
<div className="h-full flex-1 bg-white dark:bg-zinc-700"></div>
<div className="h-full flex-1 bg-[#CE2B37]"></div>
</div>
<div className="flex items-center justify-between mb-4 mt-1 shrink-0">
<div className="flex items-center gap-3">
<h2 className="font-display font-bold text-sm text-zinc-950 dark:text-zinc-50 uppercase tracking-wide whitespace-nowrap">
Position Change
</h2>
{driver && (
<div className="flex items-center gap-1.5 bg-zinc-100 dark:bg-zinc-800 px-2 py-1 rounded-full shrink-0">
<div
className="w-1.5 h-1.5 rounded-full shrink-0"
style={{ backgroundColor: driver.team.color }}
/>
<span className="text-[10px] font-sans font-bold text-zinc-700 dark:text-zinc-300 uppercase tracking-wider">
{driver.lastName}
</span>
</div>
)}
</div>
{racePhase !== 'pre-race' && renderBadge()}
</div>
<div className="flex-1 w-full min-h-0 relative -ml-4">
{racePhase === 'pre-race' ? (
<div className="absolute inset-0 flex flex-col items-center justify-center text-center pl-4">
<span className="text-2xl mb-2">⏱</span>
<span className="text-xs font-sans text-zinc-500 dark:text-zinc-400 font-medium">Chart will build when race starts</span>
</div>
) : chartData.length === 0 ? (
<div className="absolute inset-0 flex flex-col items-center justify-center text-center pl-4">
<span className="text-xs font-sans text-zinc-500 dark:text-zinc-400 font-medium">Waiting for first lap...</span>
</div>
) : (
<ResponsiveContainer width="100%" height="100%">
<AreaChart data={chartData} margin={{ top: 15, right: 15, left: -10, bottom: 0 }}>
<defs>
<linearGradient id="colorRed" x1="0" y1="0" x2="0" y2="1">
<stop offset="5%" stopColor="var(--color-racing-red)" stopOpacity={0.25} />
<stop offset="95%" stopColor="var(--color-racing-red)" stopOpacity={0} />
</linearGradient>
</defs>
<CartesianGrid strokeDasharray="3 3" vertical={false} stroke="currentColor" className="text-zinc-100 dark:text-zinc-800" />
<XAxis
dataKey="lap"
axisLine={false}
tickLine={false}
tick={{ fontSize: 10, fill: '#71717a' }}
domain={['dataMin', 'dataMax']}
type="number"
minTickGap={20}
/>
<YAxis
domain={[20, 1]}
reversed={true}
axisLine={false}
tickLine={false}
tick={{ fontSize: 10, fill: '#71717a' }}
ticks={[1, 5, 10, 15, 20]}
width={35}
/>
<Area
type="monotone"
dataKey="position"
stroke="var(--color-racing-red)"
strokeWidth={2}
fillOpacity={1}
fill="url(#colorRed)"
dot={<CustomDot />}
isAnimationActive={false}
/>
</AreaChart>
</ResponsiveContainer>
)}
</div>
</div>
);
};