@@ -5,12 +5,15 @@ import { useGetRouteDetailsByIdQuery } from '../../../generated/graphql';
55import { mapRouteToInfraLinksAlongRoute } from '../../../graphql' ;
66import { useAppDispatch , useAppSelector } from '../../../hooks' ;
77import {
8+ LoadingState ,
89 Mode ,
10+ Operation ,
911 selectEditedRouteData ,
1012 selectMapRouteEditor ,
1113 stopRouteEditingAction ,
1214} from '../../../redux' ;
1315import { removeRoute } from '../../../utils/map' ;
16+ import { useLoader } from '../../common/hooks/useLoader' ;
1417import { DrawControl } from '../DrawControl' ;
1518import { ACTIVE_LINE_STROKE_ID } from './editorStyles' ;
1619import {
@@ -42,16 +45,19 @@ const setCursor = (map: MapRef | undefined, drawingMode: Mode | undefined) => {
4245
4346export const DrawRouteLayer : FC = ( ) => {
4447 const drawRef = useRef < MapboxDraw | null > ( null ) ;
45- const { current : mapboxDraw } = drawRef ;
4648 const { current : map } = useMap ( ) ;
4749 const dispatch = useAppDispatch ( ) ;
50+ const { setLoadingState : setRouteDrawLoadingState } = useLoader (
51+ Operation . PrepareRouteDraw ,
52+ ) ;
4853
4954 const editedRouteData = useAppSelector ( selectEditedRouteData ) ;
5055 const { drawingMode } = useAppSelector ( selectMapRouteEditor ) ;
5156 const { creatingNewRoute } = useAppSelector ( selectMapRouteEditor ) ;
52- const shouldUseDrawingCursor =
53- drawingMode === Mode . Edit && creatingNewRoute && ! editedRouteData . geometry ;
54- setCursor ( map , shouldUseDrawingCursor ? Mode . Draw : drawingMode ) ;
57+ const isNewRouteDrawPhase =
58+ creatingNewRoute && ! editedRouteData . geometry && drawingMode !== undefined ;
59+
60+ setCursor ( map , isNewRouteDrawPhase ? Mode . Draw : drawingMode ) ;
5561
5662 const { templateRouteId } = editedRouteData ;
5763 // Fetch existing route's stops and geometry in case editing existing route
@@ -159,6 +165,44 @@ export const DrawRouteLayer: FC = () => {
159165 } ;
160166 } , [ keyDown ] ) ;
161167
168+ // Check that draw is ready before removing loader in new route draw phase and allowing user to start drawing
169+ useEffect ( ( ) => {
170+ const mapInstance = map ?. getMap ( ) ;
171+ const handleDrawLoadingState = ( ) => {
172+ const hasDrawRef = ! ! drawRef . current ;
173+ const hasDrawHotSource = ! ! mapInstance ?. getSource ( 'mapbox-gl-draw-hot' ) ;
174+ const hasDrawColdSource = ! ! mapInstance ?. getSource ( 'mapbox-gl-draw-cold' ) ;
175+
176+ const isDrawReady = hasDrawRef && hasDrawHotSource && hasDrawColdSource ;
177+
178+ setRouteDrawLoadingState (
179+ isDrawReady ? LoadingState . NotLoading : LoadingState . HighPriority ,
180+ ) ;
181+ } ;
182+
183+ if ( isNewRouteDrawPhase ) {
184+ setRouteDrawLoadingState ( LoadingState . HighPriority ) ;
185+ if ( mapInstance ) {
186+ // Check if drawing is ready
187+ handleDrawLoadingState ( ) ;
188+
189+ // Re-check readiness when the map receives new data.
190+ mapInstance . on ( 'sourcedata' , handleDrawLoadingState ) ;
191+
192+ // Re-check readiness after the map has finished updating.
193+ mapInstance . on ( 'idle' , handleDrawLoadingState ) ;
194+ }
195+ }
196+
197+ return ( ) => {
198+ if ( mapInstance ) {
199+ mapInstance . off ( 'sourcedata' , handleDrawLoadingState ) ;
200+ mapInstance . off ( 'idle' , handleDrawLoadingState ) ;
201+ }
202+ setRouteDrawLoadingState ( LoadingState . NotLoading ) ;
203+ } ;
204+ } , [ isNewRouteDrawPhase , map , setRouteDrawLoadingState ] ) ;
205+
162206 // If we don't have metadata, we should not render <DrawControl>
163207 // useControl hook inside <DrawControl> do not rerender correctly and have an incorrect state
164208 if ( ! editedRouteData . metaData ) {
@@ -184,7 +228,7 @@ export const DrawRouteLayer: FC = () => {
184228 const onModeChange = ( ) => {
185229 // Disables all other modes when editing
186230 if ( drawingMode === Mode . Edit ) {
187- mapboxDraw ?. changeMode ( 'direct_select' , {
231+ drawRef . current ?. changeMode ( 'direct_select' , {
188232 featureId : SNAPPING_LINE_LAYER_ID ,
189233 } ) ;
190234 }
0 commit comments