Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

front: replace some type assertions with non-null assertions #8822

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions front/src/applications/editor/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ const MapUnplugged = ({
feature.properties.id !== toolState.hovered?.id
) {
partialToolState.hovered = {
id: feature.properties?.id as string,
id: feature.properties.id,
type: LAYER_TO_EDITOAST_DICT[feature.sourceLayer as Layer],
renderedEntity: feature,
};
} else if (feature.sourceLayer === 'errors') {
partialToolState.hovered = {
id: feature.properties?.obj_id as string,
id: feature.properties.obj_id,
type: feature.properties?.obj_type,
renderedEntity: feature,
error: feature.properties as InfraError,
Expand Down Expand Up @@ -255,23 +255,20 @@ const MapUnplugged = ({
: e;
if (toolState.hovered && activeTool.onClickEntity) {
if (toolState.hovered.type) {
getEntity(
infraID as number,
toolState.hovered.id,
toolState.hovered.type,
dispatch
).then((entity) => {
if (activeTool.onClickEntity) {
// Those features lack a proper "geometry", and have a "_geometry"
// instead. This fixes it:
entity = {
...entity,
// eslint-disable-next-line no-underscore-dangle,@typescript-eslint/no-explicit-any
geometry: entity.geometry || (entity as any)._geometry,
};
activeTool.onClickEntity(entity, eventWithFeature, extendedContext);
getEntity(infraID!, toolState.hovered.id, toolState.hovered.type, dispatch).then(
(entity) => {
if (activeTool.onClickEntity) {
// Those features lack a proper "geometry", and have a "_geometry"
// instead. This fixes it:
entity = {
...entity,
// eslint-disable-next-line no-underscore-dangle,@typescript-eslint/no-explicit-any
geometry: entity.geometry || (entity as any)._geometry,
};
activeTool.onClickEntity(entity, eventWithFeature, extendedContext);
}
}
});
);
}
}
if (activeTool.onClickMap) {
Expand Down
4 changes: 2 additions & 2 deletions front/src/applications/editor/components/EntitySumUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,10 @@ const EntitySumUp = ({ entity, id, objType, classes, status, error }: EntitySumU
setState({ type: 'loading' });

if (!entity) {
entity = await getEntity(infraID as number, id, objType as EditoastType, dispatch);
entity = await getEntity(infraID!, id, objType as EditoastType, dispatch);
}

const additionalEntities = await getAdditionalEntities(infraID as number, entity, dispatch);
const additionalEntities = await getAdditionalEntities(infraID!, entity, dispatch);
setState({
type: 'ready',
entity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const InfraErrorCorrectorModal = () => {

const { data: infraAutoFixes, isLoading } =
osrdEditoastApi.endpoints.getInfraByInfraIdAutoFixes.useQuery(
{ infraId: infraId as number },
{ infraId: infraId! },
{ skip: !infraId }
);

Expand Down
17 changes: 3 additions & 14 deletions front/src/applications/editor/components/LayersModal.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import { useEffect, useMemo, useState } from 'react';

import {
groupBy,
mapKeys,
mapValues,
sum,
isString,
isArray,
uniq,
isNil,
concat,
compact,
} from 'lodash';
import { groupBy, mapKeys, mapValues, sum, isString, isArray, uniq, concat, compact } from 'lodash';
import { useTranslation } from 'react-i18next';
import { GiElectric, GiUnplugged } from 'react-icons/gi';
import { MdSpeed } from 'react-icons/md';
Expand Down Expand Up @@ -80,8 +69,8 @@ const LayersModal = ({ initialLayers, selection, frozenLayers, onChange }: Layer

const { data: speedLimitTagsByInfraId } =
osrdEditoastApi.endpoints.getInfraByInfraIdSpeedLimitTags.useQuery(
{ infraId: infraID as number },
{ skip: isNil(infraID) }
{ infraId: infraID! },
{ skip: !infraID }
);
const { data: speedLimitTags } = osrdEditoastApi.endpoints.getSpeedLimitTags.useQuery();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ export const FormComponent = (props: FieldProps) => {
// Get the distance of the geometry
const distance = useMemo(() => {
if (!isNil(formContext.length)) {
return formContext.length as number;
return formContext.length!;
}
if (formContext.geometry?.type === 'LineString') {
return getLineStringDistance(formContext.geometry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,22 @@ const PointEditionLeftPanel = <Entity extends EditorEntity>({

useEffect(() => {
const firstLoading = trackState.type === 'idle';
const trackId = state.entity.properties.track as string | undefined;
const trackId: string | undefined = state.entity.properties.track;

if (trackId && trackState.id !== trackId) {
setTrackState({ type: 'isLoading', id: trackId });
getEntity<TrackSectionEntity>(infraID as number, trackId, 'TrackSection', dispatch).then(
(track) => {
setTrackState({ type: 'ready', id: trackId, track });
getEntity<TrackSectionEntity>(infraID!, trackId, 'TrackSection', dispatch).then((track) => {
setTrackState({ type: 'ready', id: trackId, track });

if (!firstLoading) {
const { position } = state.entity.properties;
const turfPosition =
(position * length(track, { units: 'meters' })) / track.properties.length;
const point = along(track, turfPosition, { units: 'meters' });
if (!firstLoading) {
const { position } = state.entity.properties;
const turfPosition =
(position * length(track, { units: 'meters' })) / track.properties.length;
const point = along(track, turfPosition, { units: 'meters' });

setState({ ...state, entity: { ...state.entity, geometry: point.geometry } });
}
setState({ ...state, entity: { ...state.entity, geometry: point.geometry } });
}
);
});
}
}, [infraID, setState, state, state.entity.properties.track, trackState.id, trackState.type]);

Expand Down
28 changes: 13 additions & 15 deletions front/src/applications/editor/tools/pointEdition/tool-factory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,21 +168,19 @@ function getPointEditionTool<T extends EditorPoint>({
newEntity.properties = newEntity.properties || {};
newEntity.properties.track = nearestPoint.trackSectionID;

getEntity(infraID as number, newEntity.properties.track, 'TrackSection', dispatch).then(
(track) => {
const distanceAlongTrack = calculateDistanceAlongTrack(
track as TrackSectionEntity,
newEntity.geometry as Point
);
newEntity.properties.position = distanceAlongTrack;
getEntity(infraID!, newEntity.properties.track, 'TrackSection', dispatch).then((track) => {
const distanceAlongTrack = calculateDistanceAlongTrack(
track as TrackSectionEntity,
newEntity.geometry as Point
);
newEntity.properties.position = distanceAlongTrack;

setState({
...state,
entity: newEntity,
nearestPoint: null,
});
}
);
setState({
...state,
entity: newEntity,
nearestPoint: null,
});
});
}
},
onMove(e, { setState, state }) {
Expand Down Expand Up @@ -247,7 +245,7 @@ function getPointEditionTool<T extends EditorPoint>({

if (typeof trackId !== 'string') return;

getEntity(infraID as number, trackId, 'TrackSection', dispatch).then((track) => {
getEntity(infraID!, trackId, 'TrackSection', dispatch).then((track) => {
const dbPosition = entity.properties.position;
const computedPosition = nearestPointOnLine(
(track as Feature<LineString>).geometry,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ const RangeEditionLeftPanel = () => {

const { data: voltages } = osrdEditoastApi.endpoints.getInfraByInfraIdVoltages.useQuery(
{
infraId: infraID as number,
infraId: infraID!,
},
{ skip: !infraID }
);

const { data: speedLimitTagsByInfraId } =
osrdEditoastApi.endpoints.getInfraByInfraIdSpeedLimitTags.useQuery(
{
infraId: infraID as number,
infraId: infraID!,
},
{ skip: !infraID }
);
Expand Down Expand Up @@ -190,7 +190,7 @@ const RangeEditionLeftPanel = () => {
optionsState: { type: 'loading' },
});
const routesAndNodesPositions = await getRoutesFromSwitch({
infraId: infraID as number,
infraId: infraID!,
body,
}).unwrap();
const { routes, available_node_positions } = routesAndNodesPositions;
Expand All @@ -206,7 +206,7 @@ const RangeEditionLeftPanel = () => {
setSwitchesRouteCandidates(routes);
setAvailableSwitchesPositions(available_node_positions);
const trackRangesResults = await getTrackRangesByRoutes({
infraId: infraID as number,
infraId: infraID!,
routes: routes.join(','),
}).unwrap();
const newRouteElements = makeRouteElements(trackRangesResults, routes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,17 @@ export const ElectrificationEditionLayers = () => {
),
}));

getEntities<TrackSectionEntity>(
infraID as number,
missingTrackIDs,
'TrackSection',
dispatch
).then((res) => {
setState((s) => ({
...s,
trackSectionsCache: {
...s.trackSectionsCache,
...mapValues(res, (track) => ({ type: 'success', track }) as TrackState),
},
}));
});
getEntities<TrackSectionEntity>(infraID!, missingTrackIDs, 'TrackSection', dispatch).then(
(res) => {
setState((s) => ({
...s,
trackSectionsCache: {
...s.trackSectionsCache,
...mapValues(res, (track) => ({ type: 'success', track }) as TrackState),
},
}));
}
);
}
}, [entity.properties?.track_ranges]);

Expand All @@ -121,20 +118,17 @@ export const ElectrificationEditionLayers = () => {
},
}));

getEntity<TrackSectionEntity>(
infraID as number,
hoveredItem.id,
'TrackSection',
dispatch
).then((track) => {
setState((s) => ({
...s,
trackSectionsCache: {
...s.trackSectionsCache,
[hoveredItem.id]: { type: 'success', track },
},
}));
});
getEntity<TrackSectionEntity>(infraID!, hoveredItem.id, 'TrackSection', dispatch).then(
(track) => {
setState((s) => ({
...s,
trackSectionsCache: {
...s.trackSectionsCache,
[hoveredItem.id]: { type: 'success', track },
},
}));
}
);
}
}, [hoveredItem]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,17 @@ export const SpeedSectionEditionLayers = () => {
),
}));

getEntities<TrackSectionEntity>(
infraID as number,
missingTrackIDs,
'TrackSection',
dispatch
).then((res) => {
setState((s) => ({
...s,
trackSectionsCache: {
...s.trackSectionsCache,
...mapValues(res, (track) => ({ type: 'success', track }) as TrackState),
},
}));
});
getEntities<TrackSectionEntity>(infraID!, missingTrackIDs, 'TrackSection', dispatch).then(
(res) => {
setState((s) => ({
...s,
trackSectionsCache: {
...s.trackSectionsCache,
...mapValues(res, (track) => ({ type: 'success', track }) as TrackState),
},
}));
}
);
}
}, [entity.properties?.track_ranges]);

Expand All @@ -191,20 +188,17 @@ export const SpeedSectionEditionLayers = () => {
},
}));

getEntity<TrackSectionEntity>(
infraID as number,
hoveredItem.id,
'TrackSection',
dispatch
).then((track) => {
setState((s) => ({
...s,
trackSectionsCache: {
...s.trackSectionsCache,
[hoveredItem.id]: { type: 'success', track },
},
}));
});
getEntity<TrackSectionEntity>(infraID!, hoveredItem.id, 'TrackSection', dispatch).then(
(track) => {
setState((s) => ({
...s,
trackSectionsCache: {
...s.trackSectionsCache,
[hoveredItem.id]: { type: 'success', track },
},
}));
}
);
}
}, [hoveredItem]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,15 @@ function getRangeEditionTool<T extends EditorRange>({
if (interactionState.type === 'selectSwitch') {
if (feature.sourceLayer && LAYERS_SET.has(feature.sourceLayer)) {
const newHoveredItem = {
id: feature.properties?.id as string,
id: feature.properties.id,
type: LAYER_TO_EDITOAST_DICT[feature.sourceLayer as Layer],
renderedEntity: feature,
};
if (!isEqual(newHoveredItem, hoveredItem)) {
if (feature.sourceLayer === 'switches') {
setState({
hovered: {
id: feature.properties?.id as string,
id: feature.properties.id,
type: LAYER_TO_EDITOAST_DICT[feature.sourceLayer as Layer],
renderedEntity: feature,
},
Expand Down Expand Up @@ -376,8 +376,8 @@ function getRangeEditionTool<T extends EditorRange>({
itemType: 'PSLSign',
position: hoveredExtremity.geometry.coordinates,
track: trackState.track,
signIndex: feature.properties?.speedSectionSignIndex as number,
signType: feature.properties?.speedSectionSignType as string,
signIndex: feature.properties?.speedSectionSignIndex,
signType: feature.properties?.speedSectionSignType,
};
if (!isEqual(newHoveredItem, hoveredItem))
setState({
Expand All @@ -387,7 +387,7 @@ function getRangeEditionTool<T extends EditorRange>({
// Handle hovering EditorEntity elements:
else if (feature.sourceLayer && LAYERS_SET.has(feature.sourceLayer)) {
const newHoveredItem = {
id: feature.properties?.id as string,
id: feature.properties.id,
type: LAYER_TO_EDITOAST_DICT[feature.sourceLayer as Layer],
renderedEntity: feature,
};
Expand Down
Loading
Loading