Skip to content

Commit e918114

Browse files
authored
iRating gain/loss only appears in official races (#23)
1 parent f78ee77 commit e918114

File tree

4 files changed

+92
-18
lines changed

4 files changed

+92
-18
lines changed

src/frontend/components/Standings/DriverInfoRow/DriverInfoRow.tsx

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { useMemo } from 'react';
2-
import { SpeakerHighIcon, CaretUpIcon, CaretDownIcon } from '@phosphor-icons/react';
2+
import {
3+
SpeakerHighIcon,
4+
CaretUpIcon,
5+
CaretDownIcon,
6+
MinusIcon,
7+
} from '@phosphor-icons/react';
38
import { getTailwindStyle } from '@irdashies/utils/colors';
49
import { formatTime } from '@irdashies/utils/time';
510

@@ -47,7 +52,7 @@ export const DriverInfoRow = ({
4752
const fastestTimeString = formatTime(fastestTime);
4853

4954
const iratingChangeDisplay = useMemo(() => {
50-
if (iratingChange === undefined || iratingChange === null) {
55+
if (iratingChange === undefined || isNaN(iratingChange)) {
5156
return { text: '-', color: 'text-gray-400' };
5257
}
5358
const roundedChange = Math.round(iratingChange);
@@ -58,14 +63,14 @@ export const DriverInfoRow = ({
5863
if (roundedChange > 0) {
5964
text = `${roundedChange}`;
6065
color = 'text-green-400';
61-
icon = <CaretUpIcon size={10} />;
62-
} else if (roundedChange < 0) {
63-
text = `${Math.abs(roundedChange)}`;
64-
color = 'text-red-400';
65-
icon = <CaretDownIcon size={10} />;
66+
icon = <CaretUpIcon size={10} />;
67+
} else if (roundedChange < 0) {
68+
text = `${Math.abs(roundedChange)}`;
69+
color = 'text-red-400';
70+
icon = <CaretDownIcon size={10} />;
6671
} else {
6772
text = `${roundedChange}`;
68-
icon = null;
73+
icon = <MinusIcon size={10} />;
6974
}
7075
return { text, color, icon };
7176
}, [iratingChange]);
@@ -109,12 +114,14 @@ export const DriverInfoRow = ({
109114
</div>
110115
</td>
111116
<td>{badge}</td>
112-
{iratingChange !== undefined && <td className={`px-2 text-left ${iratingChangeDisplay.color}`}>
113-
<span className="flex items-center gap-0.5">
114-
{iratingChangeDisplay.icon}
115-
{iratingChangeDisplay.text}
116-
</span>
117-
</td>}
117+
{iratingChange !== undefined && (
118+
<td className={`px-2 text-left ${iratingChangeDisplay.color}`}>
119+
<span className="flex items-center gap-0.5">
120+
{iratingChangeDisplay.icon}
121+
{iratingChangeDisplay.text}
122+
</span>
123+
</td>
124+
)}
118125
<td className={`px-2`}>{delta?.toFixed(1)}</td>
119126
<td className={`px-2 ${hasFastestTime ? 'text-purple-400' : ''}`}>
120127
{fastestTimeString}

src/frontend/components/Standings/hooks/useDriverStandings.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
useDriverCarIdx,
44
useSessionDrivers,
55
useSessionFastestLaps,
6+
useSessionIsOfficial,
67
useSessionPositions,
78
useSessionQualifyingResults,
89
useSessionType,
@@ -28,6 +29,7 @@ export const useDriverStandings = ({ buffer }: { buffer: number }) => {
2829
const carIdxOnPitRoad = useTelemetry<boolean[]>('CarIdxOnPitRoad');
2930
const carIdxTrackSurface = useTelemetry('CarIdxTrackSurface');
3031
const radioTransmitCarIdx = useTelemetry('RadioTransmitCarIdx');
32+
const isOfficial = useSessionIsOfficial();
3133

3234
const standingsWithGain = useMemo(() => {
3335
const initialStandings = createDriverStandings(
@@ -49,11 +51,12 @@ export const useDriverStandings = ({ buffer }: { buffer: number }) => {
4951
}
5052
);
5153
const groupedByClass = groupStandingsByClass(initialStandings);
52-
54+
5355
// Calculate iRating changes for race sessions
54-
const augmentedGroupedByClass = sessionType === 'Race'
55-
? augmentStandingsWithIRating(groupedByClass)
56-
: groupedByClass;
56+
const augmentedGroupedByClass =
57+
sessionType === 'Race' && isOfficial
58+
? augmentStandingsWithIRating(groupedByClass)
59+
: groupedByClass;
5760

5861
return sliceRelevantDrivers(augmentedGroupedByClass, { buffer });
5962
}, [
@@ -67,6 +70,7 @@ export const useDriverStandings = ({ buffer }: { buffer: number }) => {
6770
positions,
6871
fastestLaps,
6972
sessionType,
73+
isOfficial,
7074
buffer,
7175
]);
7276

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { renderHook } from '@testing-library/react';
2+
import { describe, it, expect } from 'vitest';
3+
import { useSessionIsOfficial, useSessionStore } from './SessionStore';
4+
import type { Session } from '@irdashies/types';
5+
6+
describe('SessionStore', () => {
7+
describe('useSessionIsOfficial', () => {
8+
it('should return true when session is official', () => {
9+
const mockSession = {
10+
WeekendInfo: {
11+
Official: 1,
12+
},
13+
} as unknown as Session;
14+
15+
useSessionStore.getState().setSession(mockSession);
16+
17+
const { result } = renderHook(() => useSessionIsOfficial());
18+
expect(result.current).toBe(true);
19+
});
20+
21+
it('should return false when session is not official', () => {
22+
const mockSession = {
23+
WeekendInfo: {
24+
Official: 0,
25+
},
26+
} as unknown as Session;
27+
28+
useSessionStore.getState().setSession(mockSession);
29+
30+
const { result } = renderHook(() => useSessionIsOfficial());
31+
expect(result.current).toBe(false);
32+
});
33+
34+
it('should return false when session is null', () => {
35+
useSessionStore.getState().setSession(null as unknown as Session);
36+
37+
const { result } = renderHook(() => useSessionIsOfficial());
38+
expect(result.current).toBe(false);
39+
});
40+
41+
it('should return false when WeekendInfo is undefined', () => {
42+
const mockSession = {
43+
SessionInfo: {},
44+
CameraInfo: {},
45+
RadioInfo: {},
46+
DriverInfo: {},
47+
SplitTimeInfo: {},
48+
QualifyResultsInfo: {},
49+
} as unknown as Session;
50+
useSessionStore.getState().setSession(mockSession);
51+
52+
const { result } = renderHook(() => useSessionIsOfficial());
53+
expect(result.current).toBe(false);
54+
});
55+
});
56+
});

src/frontend/context/SessionStore/SessionStore.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ export const useSessionLaps = (sessionNum: number | undefined) =>
5858
)?.SessionLaps
5959
);
6060

61+
export const useSessionIsOfficial = () =>
62+
useStore(
63+
useSessionStore,
64+
(state) =>
65+
!!state.session?.WeekendInfo?.Official
66+
);
67+
6168
export const useDriverCarIdx = () =>
6269
useStore(useSessionStore, (state) => state.session?.DriverInfo?.DriverCarIdx);
6370

0 commit comments

Comments
 (0)