Skip to content

Commit 8eff37f

Browse files
authored
fix: update map position when clicking observation or track from list (#553)
Follow-up to #453 due to me misunderstanding the desired UX for how the map behaves when single-clicking on an item in the list. Now it will center the map around the selected observation/track.
1 parent 305f12e commit 8eff37f

File tree

1 file changed

+37
-76
lines changed
  • src/renderer/src/routes/app/projects/$projectId/_main-tabs

1 file changed

+37
-76
lines changed

src/renderer/src/routes/app/projects/$projectId/_main-tabs/-map-panel.tsx

Lines changed: 37 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,8 @@ export function MapPanel() {
569569
mapRef.current.removeFeatureState({ source: OBSERVATIONS_SOURCE_ID })
570570
mapRef.current.removeFeatureState({ source: TRACKS_SOURCE_ID })
571571

572+
const shouldZoomIn = mapRef.current.getZoom() < 10
573+
572574
if (document.type === 'observation') {
573575
let trackDocIdToHighlight: string | undefined
574576

@@ -579,28 +581,59 @@ export function MapPanel() {
579581
}
580582
}
581583

582-
// NOTE: Highlight the associated track as well
584+
// 1. Highlight the track that references the observation
583585
if (trackDocIdToHighlight) {
584586
mapRef.current.setFeatureState(
585587
{ source: TRACKS_SOURCE_ID, id: trackDocIdToHighlight },
586588
{ highlight: true },
587589
)
588590
}
591+
592+
const observationMatch = observationsFeatureCollection.features.find(
593+
({ properties }) => properties.docId === document.docId,
594+
)
595+
596+
// 2. Move to observation
597+
if (observationMatch) {
598+
moveMapToObservation(
599+
{
600+
coordinates: [
601+
observationMatch.geometry.coordinates[0]!,
602+
observationMatch.geometry.coordinates[1]!,
603+
],
604+
shouldZoomIn,
605+
},
606+
mapRef.current,
607+
)
608+
}
589609
} else {
610+
// 1. Highlight the track
590611
mapRef.current.setFeatureState(
591612
{ source: TRACKS_SOURCE_ID, id: document.docId },
592613
{ highlight: true },
593614
)
615+
616+
const tracksMatch = tracksFeatureCollection.features.find(
617+
({ properties }) => properties.docId === document.docId,
618+
)
619+
620+
// 2. Move to the track
621+
if (tracksMatch) {
622+
moveMapToTrack(
623+
{ trackFeature: tracksMatch, shouldZoomIn },
624+
mapRef.current,
625+
)
626+
}
594627
}
595628
},
596629
)
597630

598631
useEffect(
599632
/**
600-
* Controls map feature highlighting for when items in the list are selected
601-
* via single click.
633+
* Controls map feature highlighting for when items in the list selected via
634+
* click.
602635
*/
603-
function onUpdateFromListSingleClick() {
636+
function onUpdateFromListClick() {
604637
// NOTE: Only care about triggers from list interactions
605638
if (documentToHighlight?.from !== 'list') {
606639
return
@@ -611,78 +644,6 @@ export function MapPanel() {
611644
[documentToHighlight],
612645
)
613646

614-
const moveToMapFeature = useEffectEvent((document: HighlightedDocument) => {
615-
if (!mapRef.current) {
616-
return
617-
}
618-
619-
const shouldZoomIn = mapRef.current.getZoom() < 10
620-
621-
if (document.type === 'observation') {
622-
const observationMatch = observationsFeatureCollection.features.find(
623-
({ properties }) => properties.docId === document.docId,
624-
)
625-
626-
if (observationMatch) {
627-
moveMapToObservation(
628-
{
629-
coordinates: [
630-
observationMatch.geometry.coordinates[0]!,
631-
observationMatch.geometry.coordinates[1]!,
632-
],
633-
shouldZoomIn,
634-
},
635-
mapRef.current,
636-
)
637-
}
638-
} else {
639-
const tracksMatch = tracksFeatureCollection.features.find(
640-
({ properties }) => properties.docId === document.docId,
641-
)
642-
643-
if (tracksMatch) {
644-
moveMapToTrack(
645-
{ trackFeature: tracksMatch, shouldZoomIn },
646-
mapRef.current,
647-
)
648-
}
649-
}
650-
})
651-
652-
useEffect(
653-
/**
654-
* Controls movement to map feature when navigating to document-specific
655-
* page via the list.
656-
*/
657-
function onUpdateFromListDoubleClick() {
658-
if (!mapLoaded) {
659-
return
660-
}
661-
662-
// NOTE: Only care about triggers from list interactions
663-
if (documentToHighlight?.from !== 'list') {
664-
return
665-
}
666-
667-
// NOTE: Double click in list means that a committed navigation to document-specific page occurred.
668-
if (
669-
!(
670-
currentRoute.fullPath.startsWith(
671-
'/app/projects/$projectId/observations/$observationDocId',
672-
) ||
673-
currentRoute.fullPath.startsWith(
674-
'/app/projects/$projectId/tracks/$trackDocId/',
675-
)
676-
)
677-
) {
678-
return
679-
}
680-
681-
moveToMapFeature(documentToHighlight)
682-
},
683-
[currentRoute, documentToHighlight, mapLoaded],
684-
)
685-
686647
return (
687648
<Box position="relative" display="flex" flex={1}>
688649
{mapLoaded ? null : (

0 commit comments

Comments
 (0)