Skip to content

Commit def1e13

Browse files
committed
add settings page for faster cars overlay
1 parent 38c2397 commit def1e13

File tree

5 files changed

+100
-5
lines changed

5 files changed

+100
-5
lines changed

src/frontend/components/FasterCarsFromBehind/FasterCarsFromBehind.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { useMemo } from 'react';
22
import { useAutoAnimate } from '@formkit/auto-animate/react';
33
import { useCarBehind } from './hooks/useCarBehind';
4+
import { useFasterCarsSettings } from './hooks/useFasterCarsSettings';
45

56
export const FasterCarsFromBehind = () => {
6-
const carBehind = useCarBehind();
7+
const settings = useFasterCarsSettings();
8+
const carBehind = useCarBehind({distanceThreshold: settings?.distanceThreshold });
79
const [parent] = useAutoAnimate();
8-
10+
911
const layout = useMemo(() => {
1012
const hidden = carBehind.name === null || carBehind.name == undefined ? 'hidden' : '';
1113
const animate = carBehind.distance > -0.3 ? 'animate-pulse' : '';
@@ -18,7 +20,7 @@ export const FasterCarsFromBehind = () => {
1820
carBehind.distance,
1921
carBehind.percent
2022
]);
21-
23+
2224
return (
2325
<div className={`w-full flex justify-between rounded-sm p-1 pb-2 font-bold relative ${layout.hidden} ${layout.animate} ${carBehind.background}`}
2426
ref={parent}>

src/frontend/components/FasterCarsFromBehind/hooks/useCarBehind.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { useMemo } from 'react';
22
import { useDriverRelatives } from '../../Standings/hooks/useDriverRelatives';
33
import { getTailwindStyle } from '@irdashies/utils/colors';
44

5-
export const useCarBehind = () => {
5+
export const useCarBehind = ({ distanceThreshold }:{ distanceThreshold?: number }) => {
66
const drivers = useDriverRelatives({ buffer: 1 });
77
const carBehind = drivers[2];
88
const myCar = drivers[1];
9+
const threshold = distanceThreshold ?? -3;
910

1011
const background = getTailwindStyle(carBehind?.carClass?.color).classHeader;
1112

@@ -15,6 +16,6 @@ export const useCarBehind = () => {
1516
return { name: carBehind?.driver?.name, distance: parseFloat(carBehind?.delta.toFixed(1)), background: background, percent : percent };
1617
}, [carBehind?.delta, carBehind?.driver?.name, background]);
1718

18-
if(carBehind?.carClass?.relativeSpeed <= myCar?.carClass?.relativeSpeed || carBehind?.delta < -3) return {name: null, distance: 0, background: null, percent: 0};
19+
if(carBehind?.carClass?.relativeSpeed <= myCar?.carClass?.relativeSpeed || carBehind?.delta < threshold) return {name: null, distance: 0, background: null, percent: 0};
1920
return FasterCarFromBehind;
2021
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { useDashboard } from '@irdashies/context';
2+
3+
interface FasterCarsFromBehindSettings {
4+
enabled: boolean;
5+
config: {
6+
distanceThreshold: number;
7+
};
8+
}
9+
10+
export const useFasterCarsSettings = () => {
11+
const { currentDashboard } = useDashboard();
12+
13+
const settings = currentDashboard?.widgets.find(
14+
(widget) => widget.id === 'fastercarsfrombehind',
15+
)?.config;
16+
17+
// Add type guard to ensure settings matches expected shape
18+
if (settings &&
19+
typeof settings === 'object' &&
20+
'distanceThreshold' in settings &&
21+
typeof settings.distanceThreshold === 'number'
22+
) {
23+
return settings as FasterCarsFromBehindSettings['config'];
24+
}
25+
26+
return undefined;
27+
};

src/frontend/components/Settings/SettingsLayout.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { TrackMapSettings } from './sections/TrackMapSettings';
77
import { AdvancedSettings } from './sections/AdvancedSettings';
88
import { InputSettings } from './sections/InputSettings';
99
import { AboutSettings } from './sections/AboutSettings';
10+
import { FasterCarsFromBehindSettings } from './sections/FasterCarsFromBehindSettings';
1011
import { useDashboard } from '@irdashies/context';
1112
import { useState } from 'react';
1213

@@ -104,6 +105,14 @@ export const SettingsLayout = () => {
104105
Weather
105106
</Link>
106107
</li>
108+
<li>
109+
<Link
110+
to="/settings/faster-cars"
111+
className={menuItemClass('/faster-cars')}
112+
>
113+
Faster Cars
114+
</Link>
115+
</li>
107116
<li>
108117
<Link to="/settings/map" className={menuItemClass('/track-map')}>
109118
<div className="flex flex-row gap-2 items-center">
@@ -140,6 +149,7 @@ export const SettingsLayout = () => {
140149
<Route path="weather" element={<WeatherSettings />} />
141150
<Route path="map" element={<TrackMapSettings />} />
142151
<Route path="input" element={<InputSettings />} />
152+
<Route path="faster-cars" element={<FasterCarsFromBehindSettings />} />
143153
<Route path="advanced" element={<AdvancedSettings />} />
144154
<Route path="about" element={<AboutSettings />} />
145155
<Route
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { useState } from 'react';
2+
import { BaseSettingsSection } from '../components/BaseSettingsSection';
3+
import { useDashboard } from '@irdashies/context';
4+
5+
const SETTING_ID = 'fastercarsfrombehind';
6+
7+
interface FasterCarsFromBehindSettings {
8+
enabled: boolean;
9+
config: {
10+
distanceThreshold: number;
11+
};
12+
}
13+
14+
const defaultConfig: FasterCarsFromBehindSettings['config'] = {
15+
distanceThreshold: -0.3
16+
};
17+
18+
export const FasterCarsFromBehindSettings = () => {
19+
const { currentDashboard } = useDashboard();
20+
const [settings, setSettings] = useState<FasterCarsFromBehindSettings>({
21+
enabled: currentDashboard?.widgets.find(w => w.id === SETTING_ID)?.enabled ?? false,
22+
config: currentDashboard?.widgets.find(w => w.id === SETTING_ID)?.config as FasterCarsFromBehindSettings['config'] ?? defaultConfig,
23+
});
24+
25+
if (!currentDashboard) {
26+
return <>Loading...</>;
27+
}
28+
29+
return (
30+
<BaseSettingsSection
31+
title="Faster Cars From Behind Settings"
32+
description="Configure settings for the faster cars detection widget."
33+
settings={settings}
34+
onSettingsChange={setSettings}
35+
widgetId="fastercarsfrombehind"
36+
>
37+
{(handleConfigChange) => (
38+
<div className="space-y-4">
39+
<div className="space-y-2">
40+
<label className="text-slate-300">Distance Threshold</label>
41+
<input
42+
type="number"
43+
value={settings.config.distanceThreshold}
44+
onChange={(e) => handleConfigChange({
45+
distanceThreshold: parseFloat(e.target.value)
46+
})}
47+
className="w-full rounded border-gray-600 bg-gray-700 p-2 text-slate-300"
48+
step="0.1"
49+
/>
50+
</div>
51+
</div>
52+
)}
53+
</BaseSettingsSection>
54+
);
55+
};

0 commit comments

Comments
 (0)