Skip to content

Commit f6d1513

Browse files
23Kevclaude
andcommitted
refactor(gantry): use the shared speed util
Main added src/frontend/utils/units.ts as the single home for speed conversion, so the Gantry copy has to go. Deletes gantryUnits.ts and the two hooks that wrapped it. Nothing used them. Renames the setting to speedUnit with 'mph' | 'km/h' | 'auto' values, matching Battle, so it passes straight into resolveSpeedUnit with no mapping in between. Auto now falls back to mph when it cannot read iRacing's setting. That is what the shared util does. It used to fall back to km/h. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 53f31f5 commit f6d1513

7 files changed

Lines changed: 30 additions & 268 deletions

File tree

src/frontend/components/Gantry/hooks/gantryUnits.spec.ts

Lines changed: 0 additions & 121 deletions
This file was deleted.

src/frontend/components/Gantry/hooks/gantryUnits.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/frontend/components/Gantry/hooks/useGantrySettings.ts

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/frontend/components/Gantry/hooks/useGantryUnits.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/frontend/components/Settings/sections/GantrySettings.tsx

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,25 @@ import { SettingDivider } from '../components/SettingDivider';
1616
import { SettingNumberRow } from '../components/SettingNumberRow';
1717
import { SettingSelectRow } from '../components/SettingSelectRow';
1818
import {
19-
resolveGantryUnits,
20-
speedFromDisplay,
21-
speedMaxToDisplay,
22-
speedMinToDisplay,
23-
speedToDisplay,
24-
} from '../../Gantry/hooks/gantryUnits';
19+
kphFromSpeed,
20+
resolveSpeedUnit,
21+
speedFromKph,
22+
type SpeedUnit,
23+
} from '@irdashies/utils/units';
2524

2625
const SETTING_ID = 'gantry';
2726

27+
// Thresholds are stored in km/h; only the inputs convert. Bounds round inward
28+
// so a converted bound always lands back inside the stored range.
29+
const toDisplay = (kph: number, unit: SpeedUnit) =>
30+
Math.round(speedFromKph(kph, unit));
31+
const fromDisplay = (value: number, unit: SpeedUnit) =>
32+
Math.round(kphFromSpeed(value, unit));
33+
const minToDisplay = (kph: number, unit: SpeedUnit) =>
34+
Math.ceil(speedFromKph(kph, unit));
35+
const maxToDisplay = (kph: number, unit: SpeedUnit) =>
36+
Math.floor(speedFromKph(kph, unit));
37+
2838
const defaultConfig = getWidgetDefaultConfig('gantry');
2939

3040
type ThresholdKey =
@@ -153,10 +163,10 @@ export const GantrySettings = memo(() => {
153163
if (!currentDashboard) return <>Loading...</>;
154164

155165
const config = settings.config;
156-
const unitSetting = config.units ?? 'auto';
157-
const { isMetric, speedUnit } = resolveGantryUnits(unitSetting, displayUnits);
166+
const unitSetting = config.speedUnit ?? 'auto';
167+
const speedUnit = resolveSpeedUnit(unitSetting, displayUnits);
158168
// This window has no TelemetryProvider, so Auto cannot read iRacing's setting
159-
// here and falls back to km/h.
169+
// here and falls back to the shared default.
160170
const autoUnresolved = unitSetting === 'auto' && displayUnits === undefined;
161171

162172
return (
@@ -218,20 +228,20 @@ export const GantrySettings = memo(() => {
218228

219229
<SettingDivider />
220230

221-
<SettingButtonGroupRow<GantryConfig['units']>
231+
<SettingButtonGroupRow<GantryConfig['speedUnit']>
222232
title="Speed Units"
223233
description={`Units for the speed settings on the Incidents tab. Values are always saved in km/h, so switching units never changes how incidents are detected.${
224234
autoUnresolved
225-
? " Auto follows iRacing's own unit setting, which this window cannot read, so it shows km/h here. Pick km/h or mph to choose explicitly."
235+
? ` Auto follows iRacing's own unit setting, which this window cannot read, so it shows ${speedUnit} here. Pick km/h or mph to choose explicitly.`
226236
: ''
227237
}`}
228238
value={unitSetting}
229239
options={[
230240
{ label: 'Auto', value: 'auto' },
231-
{ label: 'km/h', value: 'Metric' },
232-
{ label: 'mph', value: 'Imperial' },
241+
{ label: 'km/h', value: 'km/h' },
242+
{ label: 'mph', value: 'mph' },
233243
]}
234-
onChange={(v) => handleConfigChange({ units: v })}
244+
onChange={(v) => handleConfigChange({ speedUnit: v })}
235245
/>
236246

237247
<SettingSelectRow
@@ -264,24 +274,24 @@ export const GantrySettings = memo(() => {
264274
description={field.description}
265275
value={
266276
field.isSpeed
267-
? speedToDisplay(config[field.key], isMetric)
277+
? toDisplay(config[field.key], speedUnit)
268278
: config[field.key]
269279
}
270280
min={
271281
field.isSpeed
272-
? speedMinToDisplay(field.min, isMetric)
282+
? minToDisplay(field.min, speedUnit)
273283
: field.min
274284
}
275285
max={
276286
field.isSpeed
277-
? speedMaxToDisplay(field.max, isMetric)
287+
? maxToDisplay(field.max, speedUnit)
278288
: field.max
279289
}
280290
step={1}
281291
onChange={(v) =>
282292
handleConfigChange({
283293
[field.key]: field.isSpeed
284-
? speedFromDisplay(v, isMetric)
294+
? fromDisplay(v, speedUnit)
285295
: v,
286296
} as Partial<GantryConfig>)
287297
}

src/types/defaultDashboard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ export const defaultDashboard: {
14201420
height: 1080,
14211421
},
14221422
config: {
1423-
units: 'auto',
1423+
speedUnit: 'auto',
14241424
slowSpeedThreshold: 15,
14251425
slowFrameThreshold: 10,
14261426
suddenStopFromSpeed: 80,

src/types/widgetConfigs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ export type SessionRetention = 'all' | 5 | 10 | 20;
656656

657657
export interface GantryConfig {
658658
/** Display units for speed values. Stored thresholds stay in km/h. */
659-
units: 'auto' | 'Metric' | 'Imperial';
659+
speedUnit: 'mph' | 'km/h' | 'auto';
660660
// Incident detection thresholds
661661
slowSpeedThreshold: number;
662662
slowFrameThreshold: number;

0 commit comments

Comments
 (0)