-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathPathfinding.tsx
131 lines (116 loc) · 4.77 KB
/
Pathfinding.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { Alert, CheckCircle, Stop } from '@osrd-project/ui-icons';
import cx from 'classnames';
import { isEqual } from 'lodash';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
import InfraLoadingState from 'applications/operationalStudies/components/Scenario/InfraLoadingState';
import type { ManageTrainSchedulePathProperties } from 'applications/operationalStudies/types';
import infraLogo from 'assets/pictures/components/tracks.svg';
import { Spinner } from 'common/Loaders';
import { useOsrdConfSelectors } from 'common/osrdContext';
import { usePathfinding } from 'modules/pathfinding/hooks/usePathfinding';
import { isPathStepInvalid } from 'modules/pathfinding/utils';
import { conditionalStringConcat, formatKmValue } from 'utils/strings';
import { InfraHardError, InfraSoftError } from './InfraError';
type PathfindingProps = {
pathProperties?: ManageTrainSchedulePathProperties;
setPathProperties: (pathProperties?: ManageTrainSchedulePathProperties) => void;
};
const Pathfinding = ({ pathProperties, setPathProperties }: PathfindingProps) => {
const { t } = useTranslation(['operationalStudies/manageTrainSchedule']);
const { getOrigin, getDestination, getPathSteps } = useOsrdConfSelectors();
const pathSteps = useSelector(getPathSteps);
const hasInvalidPathStep = pathSteps.some((pathStep) => isPathStepInvalid(pathStep));
const origin = useSelector(getOrigin, isEqual);
const destination = useSelector(getDestination, isEqual);
const { getRollingStockID } = useOsrdConfSelectors();
const rollingStockId = useSelector(getRollingStockID);
const {
pathfindingState,
infraInfos: { infra, reloadCount },
} = usePathfinding(setPathProperties, pathProperties);
const missingElements = conditionalStringConcat([
[!origin, t('origin')],
[!destination, t('destination')],
[!rollingStockId, t('rollingstock')],
]);
const isPathFindingActive = Object.values(pathfindingState).every(
(state) => state === false || state === ''
);
return (
<div className="pathfinding-state-main-container flex-grow-1">
{infra && infra.state !== 'CACHED' && (
<div className="content infra-loading">
<img src={infraLogo} alt="Infra logo" className="infra-logo" />
<div>{t('infraLoading')}</div>
<InfraLoadingState infra={infra} />
</div>
)}
{infra && infra.state === 'TRANSIENT_ERROR' && <InfraSoftError reloadCount={reloadCount} />}
{infra && infra.state === 'ERROR' && <InfraHardError />}
{!pathfindingState.error &&
!pathfindingState.running &&
pathfindingState.done &&
origin &&
destination &&
!hasInvalidPathStep && (
<div className="content pathfinding-done">
<span className="lead" data-testid="result-pathfinding-done">
<CheckCircle />
</span>
<span className="flex-grow-1">{t('pathfindingDone')}</span>
<small className="text-secondary" data-testid="result-pathfinding-distance">
{destination.positionOnPath &&
formatKmValue(destination.positionOnPath, 'millimeters')}
</small>
</div>
)}
{!pathProperties && isPathFindingActive ? (
<div
data-testid="pathfinding-no-state"
className={cx('content pathfinding-none', { 'mt-2': infra && infra.state !== 'CACHED' })}
>
{t('pathfindingNoState')}
</div>
) : (
<>
{(pathfindingState.error || hasInvalidPathStep) && (
<div
className={cx('content pathfinding-error', {
'mt-2': infra && infra.state !== 'CACHED',
})}
>
<span className="lead">
<Stop />
</span>
<span className="flex-grow-1">
{hasInvalidPathStep
? t('InvalidTrainScheduleStep')
: t('pathfindingError', { errorMessage: t(pathfindingState.error) })}
</span>
</div>
)}
{pathfindingState.missingParam && (
<div className="content missing-params">
<span className="lead">
<Alert />
</span>
<span data-testid="missing-params-info" className="flex-grow-1">
{t('pathfindingMissingParams', { missingElements })}
</span>
</div>
)}
{pathfindingState.running && (
<div className="content pathfinding-loading">
<span className="lead">
<Spinner />
</span>
<span className="flex-grow-1">{t('pathfindingInProgress')}</span>
</div>
)}
</>
)}
</div>
);
};
export default Pathfinding;