-
Notifications
You must be signed in to change notification settings - Fork 35
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
fix mapillary infinite loop and improve ui #2455
Open
CollinBeczak
wants to merge
7
commits into
main
Choose a base branch
from
CollinBeczak/EnhanceTaskMap
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+668
−350
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
586f907
fix mapillary infinite loop
CollinBeczak 3df12fc
remove unused functionlaity
CollinBeczak 7ab51f3
formatting
CollinBeczak 8cd5695
formatting
CollinBeczak a2f816b
fix formatting
CollinBeczak e0baa79
fix conditions and update tests
CollinBeczak 1c01739
fix tests
CollinBeczak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
import { createRef, useState, useEffect, Component } from 'react' | ||
import React, { useState, useEffect, useRef } from 'react' | ||
import PropTypes from 'prop-types' | ||
import { LayerGroup, Marker, Popup } from 'react-leaflet' | ||
import { LayerGroup, Marker, Popup, useMap } from 'react-leaflet' | ||
import L from 'leaflet' | ||
import _map from 'lodash/map' | ||
import { Viewer } from 'mapillary-js' | ||
import resolveConfig from 'tailwindcss/resolveConfig' | ||
import { getAccessToken } from '../../../services/Mapillary/Mapillary' | ||
import tailwindConfig from '../../../tailwind.config.js' | ||
import { FormattedMessage } from 'react-intl' | ||
import messages from './Messages.js' | ||
|
||
const colors = resolveConfig(tailwindConfig).theme.colors | ||
const imageCache = new Map(); | ||
|
||
/** | ||
* ImageMarkerLayer renders a layer of positioned image markers that, on hover, | ||
|
@@ -25,20 +28,31 @@ const colors = resolveConfig(tailwindConfig).theme.colors | |
*/ | ||
const ImageMarkerLayer = props => { | ||
const [imageMarkers, setImageMarkers] = useState([]) | ||
const map = useMap() | ||
|
||
const { images, markerColor = colors["blue-leaflet"], imageAlt, imageClicked, icon, mrLayerId, mrLayerLabel, style } = props | ||
|
||
const {images, markerColor, imageAlt, imageClicked, icon, mrLayerId, mrLayerLabel, style} = props | ||
useEffect(() => { | ||
setImageMarkers( | ||
buildImageMarkers( | ||
images, | ||
icon ? icon : circleIcon(markerColor), | ||
imageClicked, | ||
imageAlt, | ||
mrLayerId, | ||
mrLayerLabel | ||
try { | ||
if (!map.getPane('mapillaryPopups')) { | ||
map.createPane('mapillaryPopups') | ||
map.getPane('mapillaryPopups').style.zIndex = 700 | ||
} | ||
|
||
setImageMarkers( | ||
buildImageMarkers( | ||
images, | ||
icon ? icon : circleIcon(markerColor), | ||
markerColor, | ||
imageClicked, | ||
mrLayerId, | ||
mrLayerLabel | ||
) | ||
) | ||
) | ||
}, [images, markerColor, imageAlt, imageClicked, icon, mrLayerId, mrLayerLabel]) | ||
} catch (error) { | ||
console.error('Error creating map markers:', error) | ||
} | ||
}, [images, markerColor, imageClicked, icon, mrLayerId, mrLayerLabel, map]) | ||
|
||
return ( | ||
<LayerGroup style={style}> | ||
|
@@ -50,85 +64,129 @@ const ImageMarkerLayer = props => { | |
ImageMarkerLayer.propTypes = { | ||
layerKey: PropTypes.string, | ||
images: PropTypes.arrayOf(PropTypes.shape({ | ||
key: PropTypes.string, | ||
url: PropTypes.string, | ||
position: PropTypes.object, | ||
})), | ||
imageClicked: PropTypes.func, | ||
key: PropTypes.string.isRequired, | ||
url: PropTypes.string.isRequired, | ||
position: PropTypes.shape({ | ||
lat: PropTypes.number.isRequired, | ||
lon: PropTypes.number.isRequired, | ||
}).isRequired, | ||
})).isRequired, | ||
imageClicked: PropTypes.func.isRequired, | ||
imageAlt: PropTypes.string, | ||
buildIcon: PropTypes.func, | ||
markerColor: PropTypes.string, | ||
} | ||
|
||
class MapillaryViewer extends Component { | ||
containerRef = createRef(); | ||
const MapillaryViewer = ({ initialImageKey }) => { | ||
const containerRef = useRef() | ||
|
||
componentDidMount() { | ||
this.viewer = new Viewer({ | ||
accessToken: getAccessToken(), | ||
container: this.containerRef.current, | ||
imageId: this.props.initialImageKey, | ||
component: { cover: false }, | ||
}) | ||
} | ||
useEffect(() => { | ||
if (!initialImageKey) { | ||
console.error('Initial image key is null or undefined') | ||
return | ||
} | ||
|
||
let viewer | ||
try { | ||
const accessToken = getAccessToken(); | ||
if (!accessToken) { | ||
console.error('Access token is not available'); | ||
return; | ||
} | ||
|
||
componentWillUnmount() { | ||
if (this.viewer) { | ||
this.viewer.remove(); | ||
if (imageCache.has(initialImageKey)) { | ||
viewer = imageCache.get(initialImageKey); | ||
} else { | ||
viewer = new Viewer({ | ||
accessToken: accessToken, | ||
container: containerRef.current, | ||
imageId: initialImageKey, | ||
}); | ||
imageCache.set(initialImageKey, viewer); | ||
} | ||
} catch (error) { | ||
console.error('Error initializing Mapillary viewer:', error) | ||
} | ||
} | ||
|
||
shouldComponentUpdate(nextProps) { | ||
return nextProps.initialImageKey !== this.props.initialImageKey | ||
} | ||
return () => { | ||
try { | ||
if (viewer) { | ||
viewer.remove() | ||
} | ||
} catch (error) { | ||
console.error('Error removing Mapillary viewer:', error) | ||
} | ||
} | ||
}, [initialImageKey]) | ||
|
||
render() { | ||
return ( | ||
<div className="mr-p-2 mr-pt-4 mr-relative"> | ||
<div ref={this.containerRef} id="mapillary-viewer" style={{ width: 335, height: 263 }}></div> | ||
</div> | ||
) | ||
} | ||
return ( | ||
<div className="mr-p-2 mr-pt-4 mr-relative"> | ||
<div ref={containerRef} id="mapillary-viewer" className="mr-w-full mr-h-64"></div> | ||
</div> | ||
) | ||
} | ||
|
||
const buildImageMarkers = (images, icon, imageClicked, imageAlt, layerId, layerLabel) => { | ||
if (!images || images.length === 0) { | ||
const buildImageMarkers = (images, icon, markerColor, imageClicked, layerId, layerLabel) => { | ||
try { | ||
if (!images || images.length === 0) { | ||
return [] | ||
} | ||
|
||
return _map(images, imageInfo => { | ||
if (!imageInfo || (!imageInfo.position?.lat || !imageInfo.position?.lon) && (!imageInfo.lat || !imageInfo.lon)) { | ||
console.error(`Invalid position for image key: ${imageInfo?.key}`, imageInfo) | ||
return null | ||
} | ||
|
||
if (!imageInfo.url) { | ||
console.error(`Invalid URL for image key: ${imageInfo.key}`, imageInfo) | ||
return null | ||
} | ||
|
||
return ( | ||
<Marker | ||
key={imageInfo.key} | ||
mrLayerId={layerId} | ||
mrLayerLabel={layerLabel} | ||
position={[imageInfo.position?.lat || imageInfo.lat, imageInfo.position?.lon || imageInfo.lon]} | ||
icon={icon} | ||
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. This line has a similar bug to what I described above; if |
||
> | ||
<Popup pane="mapillaryPopups" maxWidth="351px" offset={[0, 5]}> | ||
<div style={{ width: 351, marginTop: 5 }}> | ||
<MapillaryViewer initialImageKey={imageInfo.key} /> | ||
</div> | ||
<div | ||
style={{ | ||
width: '100%', | ||
textAlign: 'center', | ||
color: markerColor, | ||
cursor: 'pointer', | ||
fontSize: '1.25rem', | ||
padding: '8px', | ||
transition: 'background-color 0.3s, color 0.3s' | ||
}} | ||
onClick={() => { | ||
imageClicked(imageInfo.key) | ||
}} | ||
onMouseEnter={e => { | ||
e.currentTarget.style.backgroundColor = markerColor | ||
e.currentTarget.style.color = 'white' | ||
}} | ||
onMouseLeave={e => { | ||
e.currentTarget.style.backgroundColor = 'transparent' | ||
e.currentTarget.style.color = markerColor | ||
}} | ||
> | ||
<FormattedMessage {...messages.openView} /> | ||
</div> | ||
</Popup> | ||
</Marker> | ||
) | ||
}).filter(Boolean) | ||
} catch (error) { | ||
console.error('Error building image markers:', error) | ||
return [] | ||
} | ||
|
||
return _map(images, imageInfo => { | ||
return ( | ||
<Marker | ||
key={imageInfo.key} | ||
mrLayerId={layerId} | ||
mrLayerLabel={layerLabel} | ||
position={[imageInfo.lat, imageInfo.lon]} | ||
icon={icon} | ||
onMouseover={({target}) => target.openPopup()} | ||
eventHandlers={{ | ||
click: () => { | ||
imageClicked ? imageClicked(imageInfo.key) : null | ||
}, | ||
}} | ||
> | ||
<Popup maxWidth="351px" offset={ [0.5, -5]}> | ||
<div style={{ width: 351, marginTop: 20 }}> | ||
<MapillaryViewer | ||
key={Date.now()} | ||
initialImageKey={imageInfo.key} | ||
onClose={() => null} | ||
/> | ||
</div> | ||
<div | ||
className="mr-w-full mr-text-center mr-text-green mr-cursor-pointer mr-text-lg" | ||
onClick={() => imageClicked(imageInfo.key)} | ||
> | ||
Enlarge | ||
</div> | ||
</Popup> | ||
</Marker> | ||
) | ||
}) | ||
} | ||
|
||
const circleIcon = (color = colors["blue-leaflet"]) => { | ||
|
28 changes: 28 additions & 0 deletions
28
src/components/EnhancedMap/ImageMarkerLayer/ImageMarkerLayer.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from 'react' | ||
import { render } from '@testing-library/react' | ||
import { MapContainer } from 'react-leaflet' | ||
import ImageMarkerLayer from './ImageMarkerLayer' | ||
|
||
describe('ImageMarkerLayer Component', () => { | ||
beforeAll(() => { | ||
jest.spyOn(console, 'error').mockImplementation(() => {}) | ||
}) | ||
|
||
afterAll(() => { | ||
console.error.mockRestore() | ||
}) | ||
|
||
const mockImages = [ | ||
{ key: 'image1', url: 'http://example.com/image1.jpg', position: { lat: 1, lon: 1 } }, | ||
{ key: 'image2', url: 'http://example.com/image2.jpg', position: { lat: 2, lon: 2 } }, | ||
] | ||
|
||
it('renders without crashing', () => { | ||
const { container } = render( | ||
<MapContainer> | ||
<ImageMarkerLayer images={mockImages} /> | ||
</MapContainer> | ||
) | ||
expect(container).toBeInTheDocument() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { defineMessages } from 'react-intl' | ||
|
||
/** | ||
* Internationalized messages for use with FitWorldControl | ||
*/ | ||
export default defineMessages({ | ||
openView: { | ||
id: "ImafeMarkerLayer.view", | ||
defaultMessage: "Open 3D View" | ||
}, | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from 'react' | ||
import { render } from '@testing-library/react' | ||
import MapillaryViewer from './MapillaryViewer' | ||
|
||
jest.mock('mapillary-js', () => { | ||
return { | ||
Viewer: jest.fn().mockImplementation(() => ({ | ||
setImageId: jest.fn(), | ||
remove: jest.fn(), | ||
})), | ||
} | ||
}) | ||
|
||
describe('MapillaryViewer Component', () => { | ||
beforeAll(() => { | ||
jest.spyOn(console, 'error').mockImplementation(() => {}) | ||
}) | ||
|
||
afterAll(() => { | ||
console.error.mockRestore() | ||
}) | ||
|
||
it('renders without crashing', () => { | ||
const { container } = render(<MapillaryViewer initialImageKey="abc123" />) | ||
expect(container).toBeInTheDocument() | ||
}) | ||
}) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This check has a minor bug, because you're coercing a number (
imageInfo.position?.lat
) to a boolean, which doesn't do what you want if the number == 0. In most cases you want to explicitly compare the value withundefined
instead (e.g. `imageInfo.position?.lat !== undefined)It's also pretty hard to read this condition because of its complexity; I'd suggest maybe extracting some of the logic into a
getLatLng(imageInfo) -> [number, number]?
function and then making your conditionif (!imageInfo || !getLatLng(imageInfo))
.