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: unify PathStep and SuggestedOP matching #9289

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import ModalBodySNCF from 'common/BootstrapSNCF/ModalSNCF/ModalBodySNCF';
import ModalFooterSNCF from 'common/BootstrapSNCF/ModalSNCF/ModalFooterSNCF';
import ModalHeaderSNCF from 'common/BootstrapSNCF/ModalSNCF/ModalHeaderSNCF';
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import { isVia } from 'modules/pathfinding/utils';
import { isVia, matchPathStepAndOp } from 'modules/pathfinding/utils';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import { useAppDispatch } from 'store';
import { formatUicToCi } from 'utils/strings';
Expand All @@ -35,10 +35,7 @@ const ModalSuggestedVias = ({ suggestedVias }: ModalSuggestedViasProps) => {
);

const removeViaFromPath = (op: SuggestedOP) => {
const updatedPathSteps = compact(pathSteps).filter(
(step) =>
('uic' in step && step.uic !== op.uic) || ('track' in step && step.track !== op.track)
);
const updatedPathSteps = compact(pathSteps).filter((step) => !matchPathStepAndOp(step, op));
dispatch(
updatePathSteps({
pathSteps: updatedPathSteps,
Expand Down
43 changes: 26 additions & 17 deletions front/src/modules/pathfinding/utils.ts
Synar marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,20 @@ export const formatSuggestedOperationalPoints = (
coordinates: getPointCoordinates(geometry, pathLength, op.position),
}));

export const matchPathStepAndOp = (step: PathStep, op: SuggestedOP) => {
export const matchPathStepAndOp = (
step: PathStep,
op: Pick<SuggestedOP, 'opId' | 'uic' | 'ch' | 'trigram' | 'track' | 'offsetOnTrack'>
) => {
if ('operational_point' in step) {
return step.operational_point === op.opId;
Synar marked this conversation as resolved.
Show resolved Hide resolved
}
// We match the kp in case two OPs have the same uic+ch (can happen when the
// infra is imported)
if ('uic' in step) {
return step.uic === op.uic && step.ch === op.ch && step.kp === op.kp;
return step.uic === op.uic && step.ch === op.ch;
}
if ('trigram' in step) {
return step.trigram === op.trigram && step.ch === op.ch && step.kp === op.kp;
return step.trigram === op.trigram && step.ch === op.ch;
}
// TODO: we abuse the PathStep.id field here, the backend also sets it to an
// ID which has nothing to do with OPs
return step.id === op.opId;
return step.track === op.track && step.offset === op.offsetOnTrack;
};

export const getPathfindingQuery = ({
Expand Down Expand Up @@ -159,15 +158,22 @@ export const upsertPathStepsInOPs = (ops: SuggestedOP[], pathSteps: PathStep[]):

export const pathStepMatchesOp = (
pathStep: PathStep,
op: Pick<SuggestedOP, 'uic' | 'ch' | 'kp' | 'name' | 'opId'>,
op: Pick<
SuggestedOP,
'opId' | 'uic' | 'ch' | 'trigram' | 'track' | 'offsetOnTrack' | 'name' | 'kp'
>,
withKP = false
) =>
('uic' in pathStep &&
'ch' in pathStep &&
emersion marked this conversation as resolved.
Show resolved Hide resolved
pathStep.uic === op.uic &&
pathStep.ch === op.ch &&
(withKP ? pathStep.kp === op.kp : pathStep.name === op.name)) ||
pathStep.id === op.opId;
) => {
if (!matchPathStepAndOp(pathStep, op)) {
// TODO: we abuse the PathStep.id field here, the backend also sets it to an
// ID which has nothing to do with OPs
return pathStep.id === op.opId;
}
if ('uic' in pathStep) {
return withKP ? pathStep.kp === op.kp : pathStep.name === op.name;
}
return true;
};

/**
* Check if a suggested operational point is a via.
Expand All @@ -179,6 +185,9 @@ export const pathStepMatchesOp = (
*/
export const isVia = (
vias: PathStep[],
op: Pick<SuggestedOP, 'uic' | 'ch' | 'kp' | 'name' | 'opId'>,
op: Pick<
SuggestedOP,
'opId' | 'uic' | 'ch' | 'trigram' | 'track' | 'offsetOnTrack' | 'name' | 'kp'
>,
{ withKP = false } = {}
) => vias.some((via) => pathStepMatchesOp(via, op, withKP));
20 changes: 17 additions & 3 deletions front/src/modules/timesStops/helpers/utils.ts
Synar marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ import {
type TimesStopsInputRow,
} from '../types';

const matchPathStepAndOpWithKP = (step: PathStep, op: SuggestedOP) => {
if (!matchPathStepAndOp(step, op)) {
// TODO: we abuse the PathStep.id field here, the backend also sets it to an
// ID which has nothing to do with OPs
return step.id === op.opId;
}
// We match the kp in case two OPs have the same uic+ch (can happen when the
// infra is imported)
if ('uic' in step || 'trigram' in step) {
return step.kp === op.kp;
}
return true;
};

export const formatSuggestedViasToRowVias = (
operationalPoints: (SuggestedOP & { isWaypoint?: boolean })[],
pathSteps: PathStep[],
Expand All @@ -43,7 +57,7 @@ export const formatSuggestedViasToRowVias = (
// to move it to the first position
const origin = pathSteps[0];
const originIndexInOps = origin
? operationalPoints.findIndex((op) => matchPathStepAndOp(origin, op))
? operationalPoints.findIndex((op) => matchPathStepAndOpWithKP(origin, op))
: -1;
if (originIndexInOps !== -1) {
[formattedOps[0], formattedOps[originIndexInOps]] = [
Expand All @@ -55,7 +69,7 @@ export const formatSuggestedViasToRowVias = (
// Ditto: destination should be last
const dest = pathSteps[pathSteps.length - 1];
const destIndexInOps = dest
? operationalPoints.findIndex((op) => matchPathStepAndOp(dest, op))
? operationalPoints.findIndex((op) => matchPathStepAndOpWithKP(dest, op))
: -1;
if (destIndexInOps !== -1) {
const lastOpIndex = formattedOps.length - 1;
Expand All @@ -66,7 +80,7 @@ export const formatSuggestedViasToRowVias = (
}

return formattedOps.map((op, i) => {
const pathStep = pathSteps.find((step) => matchPathStepAndOp(step, op));
const pathStep = pathSteps.find((step) => matchPathStepAndOpWithKP(step, op));
const { name } = pathStep || op;
const objectToUse = tableType === TableType.Input ? pathStep : op;

Expand Down
Loading