Skip to content

Commit 3208bf8

Browse files
tariknzCopilot
andauthored
feat: hide FasterCarsFromBehind during Lone Qualify (#44)
Co-authored-by: Copilot <[email protected]>
1 parent e1183a9 commit 3208bf8

File tree

3 files changed

+106
-54
lines changed

3 files changed

+106
-54
lines changed
Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
import { Meta, StoryObj } from '@storybook/react-vite';
2-
import { FasterCarsFromBehind } from './FasterCarsFromBehind';
2+
import { FasterCarsFromBehind, FasterCarsFromBehindDisplay } from './FasterCarsFromBehind';
33
import { TelemetryDecorator } from '@irdashies/storybook';
44

55
export default {
6-
component: FasterCarsFromBehind,
7-
parameters: {
8-
controls: {
9-
exclude: ['telemetryPath'],
10-
}
6+
component: FasterCarsFromBehindDisplay,
7+
argTypes: {
8+
classColor: {
9+
options: [undefined, 0xffda59, 0x33ceff, 0xff5888, 0xae6bff, 0xffffff],
10+
control: { type: 'select' },
11+
},
12+
percent: {
13+
control: { type: 'range', min: 0, max: 100 },
14+
},
1115
}
12-
} as Meta<typeof FasterCarsFromBehind>;
16+
} as Meta<typeof FasterCarsFromBehindDisplay>;
1317

14-
type Story = StoryObj<typeof FasterCarsFromBehind>;
18+
type Story = StoryObj<typeof FasterCarsFromBehindDisplay>;
1519

1620
export const Primary: Story = {
21+
render: () => <FasterCarsFromBehind />,
1722
decorators: [TelemetryDecorator('/test-data/1747384033336')],
1823
};
24+
25+
export const Display: Story = {
26+
args: {
27+
name: 'Tom Wilson',
28+
distance: -1.0,
29+
percent: 50,
30+
classColor: 0xffda59,
31+
},
32+
};
Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,55 @@
1-
import { useMemo } from 'react';
2-
import { useAutoAnimate } from '@formkit/auto-animate/react';
3-
import { useCarBehind } from './hooks/useCarBehind';
4-
import { useFasterCarsSettings } from './hooks/useFasterCarsSettings';
5-
6-
export const FasterCarsFromBehind = () => {
7-
const settings = useFasterCarsSettings();
8-
const carBehind = useCarBehind({distanceThreshold: settings?.distanceThreshold });
9-
const [parent] = useAutoAnimate();
10-
11-
const layout = useMemo(() => {
12-
const hidden = carBehind.name === null || carBehind.name == undefined ? 'hidden' : '';
13-
const animate = carBehind.distance > -0.3 ? 'animate-pulse' : '';
14-
const red = carBehind.percent;
15-
const green = 100 - carBehind.percent;
16-
17-
return { hidden, animate, red, green };
18-
}, [
19-
carBehind.name,
20-
carBehind.distance,
21-
carBehind.percent
22-
]);
23-
24-
return (
25-
<div className={`w-full flex justify-between rounded-sm p-1 pb-2 font-bold relative ${layout.hidden} ${layout.animate} ${carBehind.background}`}
26-
ref={parent}>
27-
<div className="rounded-sm bg-gray-700 p-1">{carBehind.name}</div>
28-
<div className="rounded-sm bg-gray-700 p-1">{carBehind.distance}</div>
29-
<div className={`absolute bottom-0 left-0 rounded-b-sm bg-white h-1 flex-none`} style={{width: carBehind.percent+'%', backgroundColor: `rgb(${layout.red}%, ${layout.green}%, 0%)`}}></div>
30-
</div>
31-
);
32-
};
1+
import { useCurrentSessionType } from '@irdashies/context';
2+
import { useCarBehind } from './hooks/useCarBehind';
3+
import { useFasterCarsSettings } from './hooks/useFasterCarsSettings';
4+
import { getTailwindStyle } from '@irdashies/utils/colors';
5+
6+
export interface FasterCarsFromBehindProps {
7+
name?: string;
8+
distance?: number;
9+
percent?: number;
10+
classColor?: number;
11+
}
12+
13+
export const FasterCarsFromBehind = () => {
14+
const settings = useFasterCarsSettings();
15+
const sessionType = useCurrentSessionType();
16+
const carBehind = useCarBehind({
17+
distanceThreshold: settings?.distanceThreshold,
18+
});
19+
20+
if (sessionType === 'Lone Qualify') {
21+
return null;
22+
}
23+
24+
return <FasterCarsFromBehindDisplay {...carBehind} />;
25+
};
26+
27+
export const FasterCarsFromBehindDisplay = ({
28+
name,
29+
distance,
30+
percent,
31+
classColor,
32+
}: FasterCarsFromBehindProps) => {
33+
if (!name) {
34+
return null;
35+
}
36+
37+
const animate = distance && distance > -0.3 ? 'animate-pulse' : '';
38+
const red = percent || 0;
39+
const green = 100 - (percent || 0);
40+
const background = getTailwindStyle(classColor).classHeader;
41+
42+
return (
43+
<div className={`w-full flex justify-between rounded-sm p-1 pb-2 font-bold relative ${background} ${animate}`}>
44+
<div className="rounded-sm bg-gray-700 p-1">{name}</div>
45+
<div className="rounded-sm bg-gray-700 p-1">{distance}</div>
46+
<div
47+
className={`absolute bottom-0 left-0 rounded-b-sm bg-white h-1 flex-none`}
48+
style={{
49+
width: `${percent ?? 0}%`,
50+
backgroundColor: `rgb(${red}%, ${green}%, 0%)`,
51+
}}
52+
></div>
53+
</div>
54+
);
55+
};
Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
import { useMemo } from 'react';
22
import { useDriverRelatives } from '../../Standings/hooks/useDriverRelatives';
3-
import { getTailwindStyle } from '@irdashies/utils/colors';
43

5-
export const useCarBehind = ({ distanceThreshold }:{ distanceThreshold?: number }) => {
4+
export const useCarBehind = ({
5+
distanceThreshold,
6+
}: {
7+
distanceThreshold?: number;
8+
}) => {
69
const drivers = useDriverRelatives({ buffer: 1 });
710
const carBehind = drivers[2];
8-
const myCar = drivers[1];
11+
const myCar = drivers[1];
912
const threshold = distanceThreshold ?? -3;
10-
11-
const background = getTailwindStyle(carBehind?.carClass?.color).classHeader;
12-
13-
const FasterCarFromBehind = useMemo(() => {
14-
const percent = parseInt((100 - (Math.abs(carBehind?.delta) / 3 * 100)).toFixed(0));
15-
16-
return { name: carBehind?.driver?.name, distance: parseFloat(carBehind?.delta.toFixed(1)), background: background, percent : percent };
17-
}, [carBehind?.delta, carBehind?.driver?.name, background]);
18-
19-
if(carBehind?.carClass?.relativeSpeed <= myCar?.carClass?.relativeSpeed || carBehind?.delta < threshold) return {name: null, distance: 0, background: null, percent: 0};
20-
return FasterCarFromBehind;
13+
const classColor = carBehind?.carClass?.color;
14+
15+
const fasterCarFromBehind = useMemo(() => {
16+
const percent = parseInt(
17+
(100 - (Math.abs(carBehind?.delta ?? 0) / 3) * 100).toFixed(0)
18+
);
19+
20+
return {
21+
name: carBehind?.driver?.name,
22+
distance: parseFloat(carBehind?.delta?.toFixed(1) ?? '0'),
23+
classColor,
24+
percent: percent,
25+
};
26+
}, [carBehind?.delta, carBehind?.driver?.name, classColor]);
27+
28+
if (
29+
carBehind?.carClass?.relativeSpeed <= myCar?.carClass?.relativeSpeed ||
30+
carBehind?.delta < threshold
31+
) {
32+
return { name: undefined, distance: 0, classColor: undefined, percent: 0 };
33+
}
34+
35+
return fasterCarFromBehind;
2136
};

0 commit comments

Comments
 (0)