Skip to content

Commit

Permalink
Some improvements to map state management
Browse files Browse the repository at this point in the history
  • Loading branch information
davenquinn committed Nov 27, 2024
1 parent 1a6c4bd commit 818fac8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
3 changes: 0 additions & 3 deletions packages/data-components/src/field-locations/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ export function LocationLink({
"a",
{
href,
icon: "link",
small: true,
minimal: true,
className: "location-link",
target: "_blank",
},
Expand Down
25 changes: 19 additions & 6 deletions packages/map-interface/src/dev/vector-tile-features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { useMapRef, useMapStatus } from "@macrostrat/mapbox-react";
import mapboxgl from "mapbox-gl";
import hyper from "@macrostrat/hyper";
import styles from "./main.module.sass";
import { useCallback, useEffect, useState } from "react";
import { JSONView, usePrevious } from "@macrostrat/ui-components";
import { useCallback, useEffect, useRef, useState } from "react";
import { JSONView } from "@macrostrat/ui-components";
import { group } from "d3-array";
import { ExpansionPanel } from "../expansion-panel";

Expand Down Expand Up @@ -59,8 +59,10 @@ export function FeatureSelectionHandler({
radius?: number;
}) {
const mapRef = useMapRef();
const { isLoading } = useMapStatus();
const prevLocation = usePrevious(selectedLocation);
const isLoading = useMapStatus((s) => s.isLoading);
const isInitialized = useMapStatus((s) => s.isInitialized);
const prevLocation = useRef(null);
const prevFeatures = useRef([]);

useEffect(() => {
const map = mapRef?.current;
Expand All @@ -70,8 +72,18 @@ export function FeatureSelectionHandler({
return;
}

if (!isInitialized) return;

const hasPreviouslyLoadedFeatures = prevFeatures.current.length > 0;

const locationMemo = JSON.stringify(selectedLocation);
if (locationMemo == prevLocation.current && hasPreviouslyLoadedFeatures)
return;

prevLocation.current = locationMemo;

// Don't update if the location hasn't changed
if (selectedLocation == prevLocation) return;
//if (selectedLocation == prevLocation) return;

const r = radius;
const pt = map.project(selectedLocation);
Expand All @@ -81,8 +93,9 @@ export function FeatureSelectionHandler({
[pt.x + r, pt.y + r],
];
const features = map.queryRenderedFeatures(bbox);
prevFeatures.current = features ?? [];
setFeatures(features);
}, [mapRef.current, prevLocation?.current, selectedLocation, isLoading]);
}, [isInitialized, selectedLocation, isLoading]);

return null;
}
Expand Down
1 change: 0 additions & 1 deletion packages/map-interface/src/map-view/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
useMapRef,
useMapDispatch,
useMapPosition,
useMapStatus,
} from "@macrostrat/mapbox-react";
import {
mapViewInfo,
Expand Down

0 comments on commit 818fac8

Please sign in to comment.