Skip to content

Commit 4b79ac0

Browse files
committed
ui-charts: fix useManchetteWithSpaceTimeChart() errors with zero waypoints
In multiple spots the hook assumed there is at least one waypoint in list. Add guards to avoid crashing with errors when that's the case. Signed-off-by: Simon Ser <[email protected]>
1 parent ddef95f commit 4b79ac0

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

front/ui/ui-charts/src/manchette/hooks/useManchetteWithSpaceTimeChart.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ const useManchetteWithSpaceTimeChart = ({
366366
// Constant scale:
367367
if (isProportional) {
368368
const baseScale = baseScales[0];
369+
if (!baseScale) return baseScales;
369370
const coefficient: number =
370371
'coefficient' in baseScale && typeof baseScale.coefficient === 'number'
371372
? baseScale.coefficient
@@ -445,10 +446,13 @@ const useManchetteWithSpaceTimeChart = ({
445446
const { manchetteContents, manchetteHeight } = useMemo(() => {
446447
const spaceScaleTree = spaceScalesToBinaryTree(spaceOrigin, spaceScales);
447448
const getSpacePixel = getSpaceToPixel(0, spaceScaleTree);
448-
const totalManchetteHeight = Math.max(
449-
getSpacePixel(spaceScales.at(-1)!.to, true) + BASE_WAYPOINT_HEIGHT,
450-
height - FOOTER_HEIGHT
451-
);
449+
let totalManchetteHeight = height - FOOTER_HEIGHT;
450+
if (spaceScales.length > 0) {
451+
totalManchetteHeight = Math.max(
452+
totalManchetteHeight,
453+
getSpacePixel(spaceScales.at(-1)!.to, true) + BASE_WAYPOINT_HEIGHT,
454+
);
455+
}
452456

453457
if (!splitPoints)
454458
return {

front/ui/ui-charts/src/manchette/utils/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ export const positionMmToKm = (position: number) => Math.round((position / 10000
77

88
export const msToS = (time: number) => time / 1000;
99

10-
export const calcTotalDistance = <T extends { position: number }>(ops: T[]) =>
11-
ops.at(-1)!.position - ops.at(0)!.position;
10+
export const calcTotalDistance = <T extends { position: number }>(ops: T[]) => {
11+
if (ops.length === 0) {
12+
return 0;
13+
}
14+
return ops.at(-1)!.position - ops.at(0)!.position;
15+
};
1216

1317
type Point = {
1418
x: number;

0 commit comments

Comments
 (0)