File size: 6,754 Bytes
267266e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | 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>
);
};
|