1
- import { useEffect , useState } from 'react' ;
1
+ import { useEffect , useRef , useState } from 'react' ;
2
2
3
3
import { Button } from '@osrd-project/ui-core' ;
4
4
import { ArrowDown , ArrowUp } from '@osrd-project/ui-icons' ;
@@ -7,6 +7,7 @@ import { compact } from 'lodash';
7
7
import { useTranslation } from 'react-i18next' ;
8
8
import { useSelector } from 'react-redux' ;
9
9
10
+ import { LOADER_HEIGHT } from 'applications/stdcm/consts' ;
10
11
import { useOsrdConfActions , useOsrdConfSelectors } from 'common/osrdContext' ;
11
12
import useInfraStatus from 'modules/pathfinding/hooks/useInfraStatus' ;
12
13
import { Map } from 'modules/trainschedule/components/ManageTrainSchedule' ;
@@ -19,7 +20,7 @@ import StdcmDestination from './StdcmDestination';
19
20
import StdcmLinkedPathSearch from './StdcmLinkedPathSearch' ;
20
21
import StdcmOrigin from './StdcmOrigin' ;
21
22
import useStaticPathfinding from '../../hooks/useStaticPathfinding' ;
22
- import type { StdcmConfigErrors } from '../../types' ;
23
+ import type { LoaderStatus , StdcmConfigErrors } from '../../types' ;
23
24
import StdcmSimulationParams from '../StdcmSimulationParams' ;
24
25
import StdcmVias from './StdcmVias' ;
25
26
import { ArrivalTimeTypes , StdcmConfigErrorTypes } from '../../types' ;
@@ -49,6 +50,7 @@ const StdcmConfig = ({
49
50
cancelStdcmRequest,
50
51
} : StdcmConfigProps ) => {
51
52
const { t } = useTranslation ( 'stdcm' ) ;
53
+ const launchButtonRef = useRef < HTMLDivElement > ( null ) ;
52
54
53
55
const { infra } = useInfraStatus ( ) ;
54
56
const dispatch = useAppDispatch ( ) ;
@@ -77,9 +79,23 @@ const StdcmConfig = ({
77
79
const pathfinding = useStaticPathfinding ( infra ) ;
78
80
79
81
const [ formErrors , setFormErrors ] = useState < StdcmConfigErrors > ( ) ;
82
+ const [ loaderStatus , setLoaderStatus ] = useState < LoaderStatus > ( ) ;
80
83
81
84
const disabled = isPending || retainedSimulationIndex > - 1 ;
82
85
86
+ useEffect ( ( ) => {
87
+ if ( ! isPending || ! launchButtonRef . current ) {
88
+ setLoaderStatus ( undefined ) ;
89
+ } else {
90
+ const { top } = launchButtonRef . current . getBoundingClientRect ( ) ;
91
+ const windowHeight = window . innerHeight ;
92
+ setLoaderStatus ( {
93
+ status : windowHeight - top - 32 > LOADER_HEIGHT ? 'absolute' : 'sticky' ,
94
+ firstLaunch : true ,
95
+ } ) ;
96
+ }
97
+ } , [ isPending ] ) ;
98
+
83
99
const startSimulation = ( ) => {
84
100
const isPathfindingFailed = ! ! pathfinding && pathfinding . status !== 'success' ;
85
101
const formErrorsStatus = checkStdcmConfigErrors (
@@ -176,15 +192,16 @@ const StdcmConfig = ({
176
192
className = { cx ( 'stdcm-launch-request' , {
177
193
'wizz-effect' : pathfinding ?. status !== 'success' || formErrors ,
178
194
} ) }
195
+ ref = { launchButtonRef }
179
196
>
180
- { showBtnToLaunchSimulation && (
181
- < Button
182
- data-testid = "launch-simulation-button"
183
- className = { cx ( { 'fade-out' : isPending } ) }
184
- label = { t ( 'simulation.getSimulation' ) }
185
- onClick = { startSimulation }
186
- />
187
- ) }
197
+ < Button
198
+ data-testid = "launch-simulation-button"
199
+ className = { cx ( {
200
+ 'fade-out' : ! showBtnToLaunchSimulation ,
201
+ } ) }
202
+ label = { t ( 'simulation.getSimulation' ) }
203
+ onClick = { startSimulation }
204
+ />
188
205
{ formErrors && (
189
206
< StdcmWarningBox
190
207
errorInfos = { formErrors }
@@ -194,7 +211,14 @@ const StdcmConfig = ({
194
211
) }
195
212
</ div >
196
213
197
- { isPending && < StdcmLoader cancelStdcmRequest = { cancelStdcmRequest } /> }
214
+ { loaderStatus && (
215
+ < StdcmLoader
216
+ cancelStdcmRequest = { cancelStdcmRequest }
217
+ loaderStatus = { loaderStatus }
218
+ setLoaderStatus = { setLoaderStatus }
219
+ launchButtonRef = { launchButtonRef }
220
+ />
221
+ ) }
198
222
</ div >
199
223
</ div >
200
224
</ div >
0 commit comments