Skip to content

Commit 6c95476

Browse files
committed
front: timetable performance, use callback
Signed-off-by: Valentin Chanas <anisometropie@gmail.com>
1 parent 30fa945 commit 6c95476

File tree

1 file changed

+16
-13
lines changed
  • front/src/modules/trainschedule/components/Timetable

1 file changed

+16
-13
lines changed

front/src/modules/trainschedule/components/Timetable/Timetable.tsx

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useMemo, useState } from 'react';
1+
import { useMemo, useState, useCallback } from 'react';
22

33
import cx from 'classnames';
44
import dayjs from 'dayjs';
@@ -62,27 +62,30 @@ const Timetable = ({
6262
setShowTrainDetails(!showTrainDetails);
6363
};
6464

65-
const removeAndUnselectTrains = (trainIds: number[]) => {
65+
const removeAndUnselectTrains = useCallback((trainIds: number[]) => {
6666
removeTrains(trainIds);
6767
setSelectedTrainIds([]);
68-
};
68+
}, []);
6969

7070
const toggleConflictsListExpanded = () => {
7171
setConflictsListExpanded(!conflictsListExpanded);
7272
};
7373

74-
const handleSelectTrain = (id: number) => {
75-
const currentSelectedTrainIds = [...selectedTrainIds];
76-
const index = currentSelectedTrainIds.indexOf(id);
74+
const handleSelectTrain = useCallback(
75+
(id: number) => {
76+
const currentSelectedTrainIds = [...selectedTrainIds];
77+
const index = currentSelectedTrainIds.indexOf(id);
7778

78-
if (index === -1) {
79-
currentSelectedTrainIds.push(id);
80-
} else {
81-
currentSelectedTrainIds.splice(index, 1);
82-
}
79+
if (index === -1) {
80+
currentSelectedTrainIds.push(id);
81+
} else {
82+
currentSelectedTrainIds.splice(index, 1);
83+
}
8384

84-
setSelectedTrainIds(currentSelectedTrainIds);
85-
};
85+
setSelectedTrainIds(currentSelectedTrainIds);
86+
},
87+
[selectedTrainIds]
88+
);
8689

8790
const handleConflictClick = (conflict: Conflict) => {
8891
if (conflict.train_ids.length > 0) {

0 commit comments

Comments
 (0)