-
-
Notifications
You must be signed in to change notification settings - Fork 6
feat: Show Mapbox NavigationControl in DrawingMode and RealTimeMode #373
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ export const Mapbox: React.FC<{ position?: { latitude: number; longitude: number | |
| const map = useRef<mapboxgl.Map | null>(null) | ||
| const { setMap } = useMap() | ||
| const drawRef = useRef<MapboxDraw | null>(null) | ||
| const navigationControlRef = useRef<mapboxgl.NavigationControl | null>(null) | ||
| const rotationFrameRef = useRef<number | null>(null) | ||
| const polygonLabelsRef = useRef<{ [id: string]: mapboxgl.Marker }>({}) | ||
| const lineLabelsRef = useRef<{ [id: string]: mapboxgl.Marker }>({}) | ||
|
|
@@ -352,6 +353,24 @@ export const Mapbox: React.FC<{ position?: { latitude: number; longitude: number | |
| // Handle geolocation setup based on mode | ||
| setupGeolocationWatcher() | ||
|
|
||
| // Add navigation control for DrawingMode or RealTimeMode | ||
| if (mapType === MapToggleEnum.DrawingMode || mapType === MapToggleEnum.RealTimeMode) { | ||
| if (!navigationControlRef.current) { | ||
| navigationControlRef.current = new mapboxgl.NavigationControl() | ||
| if (map.current) { | ||
| map.current.addControl(navigationControlRef.current, 'top-left') | ||
| } | ||
| } | ||
| } else { | ||
| // Remove navigation control in FreeMode | ||
| if (navigationControlRef.current) { | ||
| if (map.current) { | ||
| map.current.removeControl(navigationControlRef.current) | ||
| } | ||
| navigationControlRef.current = null | ||
| } | ||
| } | ||
|
Comment on lines
+356
to
+372
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chainNavigationControl is not added on initial mount for Drawing/RealTime modes The if (!map.current || !initializedRef.current) returnYou also removed the unconditional
To ensure the control shows up immediately in those modes, initialize it in the map map.current.on('load', () => {
if (!map.current) return
setMap(map.current)
…
if (mapType === MapToggleEnum.DrawingMode) {
setupDrawingTools()
}
+
+ // Ensure NavigationControl is present on initial load for Drawing/RealTime modes
+ if (mapType === MapToggleEnum.DrawingMode || mapType === MapToggleEnum.RealTimeMode) {
+ if (!navigationControlRef.current) {
+ navigationControlRef.current = new mapboxgl.NavigationControl()
+ map.current.addControl(navigationControlRef.current, 'top-left')
+ }
+ }
…
})The existing Also applies to: 431-431 I'll help verify this review comment by examining the actual code structure. Let me generate shell scripts to investigate the implementation. 🏁 Script executed: #!/bin/bash
# Find and display the mapbox-map.tsx file structure
fd -t f "mapbox-map.tsx" --exec cat {} \;Length of output: 22680 🏁 Script executed: #!/bin/bash
# Check specific line ranges mentioned in the review
# Lines 356-372 for the useEffect and context around line 431
fd -t f "mapbox-map.tsx" --exec sed -n '340,450p' {} \;Length of output: 4046 🏁 Script executed: #!/bin/bash
# Search for NavigationControl usage and initialization
rg -nC5 "NavigationControl" --type=tsxLength of output: 27 🏁 Script executed: #!/bin/bash
# Search for map initialization and 'load' event handlers
rg -nC5 "map\.current\.on\s*\(\s*['\"]load" --type=tsxLength of output: 27 Add NavigationControl initialization to map load handler Your review is correct. The NavigationControl won't appear on initial mount when
To fix this, initialize NavigationControl in the map // Ensure NavigationControl is present on initial load for Drawing/RealTime modes
if (mapType === MapToggleEnum.DrawingMode || mapType === MapToggleEnum.RealTimeMode) {
if (!navigationControlRef.current) {
navigationControlRef.current = new mapboxgl.NavigationControl()
map.current.addControl(navigationControlRef.current, 'top-left')
}
}The existing 🤖 Prompt for AI Agents |
||
|
|
||
| // Handle draw controls based on mode | ||
| if (mapType === MapToggleEnum.DrawingMode) { | ||
| // If switching to drawing mode, setup drawing tools | ||
|
|
@@ -409,7 +428,7 @@ export const Mapbox: React.FC<{ position?: { latitude: number; longitude: number | |
| preserveDrawingBuffer: true | ||
| }) | ||
|
|
||
| map.current.addControl(new mapboxgl.NavigationControl(), 'top-left') | ||
|
|
||
|
|
||
| // Register event listeners | ||
| map.current.on('moveend', captureMapCenter) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Reset
navigationControlRefon unmount to avoid stale control instance across remountsnavigationControlRefis only updated inside the map-type effect; it’s never cleared in the unmount/teardown paths. If the component unmounts while inDrawingModeorRealTimeMode, the ref will still point at aNavigationControlinstance on the next mount. Because the add logic is guarded withif (!navigationControlRef.current), this can prevent a control from ever being (re)attached to the new map instance.Add explicit cleanup so the ref is nulled and (optionally) the control is removed before
map.current.remove():You’ll want equivalent handling in the duplicate cleanup effect at lines 578-615, or (better) dedupe those cleanups and have a single teardown path that also clears
navigationControlRef.Also applies to: 481-511, 578-615
I'll verify the concerns in this review by examining the actual code structure.
🏁 Script executed:
Length of output: 21799
Ensure
navigationControlRefis cleared in all cleanup paths to prevent stale control references on remountThe two cleanup effects (lines 481–511 and 578–615) do not reset
navigationControlRef, allowing a stale reference to persist across component remounts. If the component unmounts while inDrawingModeorRealTimeMode, the ref will retain the oldNavigationControlinstance. On remount, the guardif (!navigationControlRef.current)will be false, preventing a fresh control from being added to the new map.Add cleanup before
map.current.remove()in both effects:if (navigationControlRef.current) { try { map.current.removeControl(navigationControlRef.current) } catch (e) { console.log('Navigation control already removed') } navigationControlRef.current = null }Consolidate the two cleanup effects into a single teardown path if possible to avoid maintaining duplicate cleanup logic.
🤖 Prompt for AI Agents