Skip to content

Commit e154693

Browse files
committed
Fix filtredNominalVoltages
Signed-off-by: Ayoub LABIDI <ayoub.labidi@protonmail.com>
1 parent 5f3c48e commit e154693

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

src/components/network/network-map-panel.tsx

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import SelectionCreationPanel from './selection-creation-panel/selection-creatio
6969
import { useEquipmentMenu } from '../../hooks/use-equipment-menu';
7070
import useEquipmentDialogs from 'hooks/use-equipment-dialogs';
7171
import { getNominalVoltageColor } from 'utils/colors';
72+
import { getNominalVoltageIntervalName } from './utils/nominal-voltage-filter-utils';
7273

7374
const INITIAL_POSITION = [0, 0] as const;
7475
const INITIAL_ZOOM = 9;
@@ -184,9 +185,8 @@ export const NetworkMapPanel = forwardRef<NetworkMapPanelRef, NetworkMapPanelPro
184185

185186
const { snackError } = useSnackMessage();
186187

187-
const [filteredNominalVoltages, setFilteredNominalVoltages] = useState<number[]>(
188-
mapState?.filteredNominalVoltages ?? []
189-
);
188+
const [filteredNominalVoltages, setFilteredNominalVoltages] = useState<number[]>([]);
189+
const [hasInitializedFilters, setHasInitializedFilters] = useState(false);
190190
const [geoData, setGeoData] = useState<GeoData>();
191191
const geoDataRef = useRef<any>();
192192

@@ -861,6 +861,7 @@ export const NetworkMapPanel = forwardRef<NetworkMapPanelRef, NetworkMapPanelPro
861861
if (previousCurrentRootNetworkUuid && previousCurrentRootNetworkUuid !== currentRootNetworkUuid) {
862862
setInitialized(false);
863863
setIsRootNodeGeoDataLoaded(false);
864+
setHasInitializedFilters(false);
864865
dispatch(resetMapEquipment());
865866
return;
866867
}
@@ -1246,23 +1247,45 @@ export const NetworkMapPanel = forwardRef<NetworkMapPanelRef, NetworkMapPanelPro
12461247
);
12471248

12481249
// Set up filteredNominalVoltages once at map initialization
1249-
// TODO: how do we must manage case where voltages change (like when changing node), as filters are already initialized?
12501250
const nominalVoltagesFromMapEquipments = mapEquipments?.getNominalVoltages();
1251+
12511252
useEffect(() => {
1253+
// Only initialize once when mapEquipments are loaded
12521254
if (
12531255
nominalVoltagesFromMapEquipments !== undefined &&
12541256
nominalVoltagesFromMapEquipments.length > 0 &&
1255-
filteredNominalVoltages.length === 0 &&
1256-
!mapState?.filteredNominalVoltages // Only initialize if no saved map state exists
1257+
!hasInitializedFilters
12571258
) {
1258-
handleFilteredNominalVoltagesChange(nominalVoltagesFromMapEquipments);
1259+
setHasInitializedFilters(true);
1260+
1261+
// Check if we have saved state to restore
1262+
if (mapState?.filteredNominalVoltages && mapState.filteredNominalVoltages.length > 0) {
1263+
// Get intervals from saved voltages
1264+
const savedIntervals = new Set(
1265+
mapState.filteredNominalVoltages
1266+
.map((v) => getNominalVoltageIntervalName(v))
1267+
.filter((interval): interval is string => interval !== undefined)
1268+
);
1269+
1270+
// Filter current voltages by matching intervals
1271+
const voltagesMatchingIntervals = nominalVoltagesFromMapEquipments.filter((v) => {
1272+
const interval = getNominalVoltageIntervalName(v);
1273+
return interval && savedIntervals.has(interval);
1274+
});
1275+
1276+
if (voltagesMatchingIntervals.length > 0) {
1277+
// Restore voltages from matching intervals
1278+
setFilteredNominalVoltages(voltagesMatchingIntervals);
1279+
} else {
1280+
// No matching intervals found, initialize with all
1281+
setFilteredNominalVoltages(nominalVoltagesFromMapEquipments);
1282+
}
1283+
} else {
1284+
// No saved state, initialize with all voltages
1285+
setFilteredNominalVoltages(nominalVoltagesFromMapEquipments);
1286+
}
12591287
}
1260-
}, [
1261-
filteredNominalVoltages,
1262-
handleFilteredNominalVoltagesChange,
1263-
nominalVoltagesFromMapEquipments,
1264-
mapState?.filteredNominalVoltages,
1265-
]);
1288+
}, [nominalVoltagesFromMapEquipments, mapState?.filteredNominalVoltages, hasInitializedFilters]);
12661289

12671290
function renderNominalVoltageFilter() {
12681291
return (

src/components/network/nominal-voltage-filter.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,17 @@ export default function NominalVoltageFilter({
6464
const vlListValues = nominalVoltages.filter(
6565
(vnom) => getNominalVoltageIntervalName(vnom) === interval.name
6666
);
67-
return { ...interval, vlListValues, isChecked: true };
67+
// Check if all voltages in this interval are present in filteredNominalVoltages
68+
const isChecked = vlListValues.length > 0 && vlListValues.every((v) => filteredNominalVoltages.includes(v));
69+
return { ...interval, vlListValues, isChecked };
6870
});
6971
setVoltageLevelIntervals(newIntervals);
70-
}, [nominalVoltages]);
72+
}, [nominalVoltages, filteredNominalVoltages]);
7173

7274
const handleToggle = useCallback(
7375
(interval: VoltageLevelValuesInterval) => {
74-
let newFiltered: number[];
75-
76-
// we "inverse" the selection for vlListValues
77-
newFiltered = [...filteredNominalVoltages];
76+
// Toggle all voltages in this interval
77+
const newFiltered = [...filteredNominalVoltages];
7878
for (const vnom of interval.vlListValues) {
7979
const currentIndex = newFiltered.indexOf(vnom);
8080
if (currentIndex === -1) {
@@ -83,11 +83,9 @@ export default function NominalVoltageFilter({
8383
newFiltered.splice(currentIndex, 1); // previously present, we remove it
8484
}
8585
}
86-
setVoltageLevelIntervals((prev) =>
87-
prev.map((i) => (i.name === interval.name ? { ...i, isChecked: !i.isChecked } : i))
88-
);
8986

90-
onChange(newFiltered); // update filteredNominalVoltages
87+
// Update parent state - the useEffect will handle updating voltageLevelIntervals
88+
onChange(newFiltered);
9189
},
9290
[filteredNominalVoltages, onChange]
9391
);

0 commit comments

Comments
 (0)