Skip to content

Commit

Permalink
refactor(frontend): remove dataLoadTrigger from DataMap
Browse files Browse the repository at this point in the history
dataLoadTrigger is always 0, so this removes it from the DataMap component.
  • Loading branch information
eatyourgreens committed Oct 10, 2024
1 parent 05b51d2 commit 7aff777
Showing 1 changed file with 29 additions and 39 deletions.
68 changes: 29 additions & 39 deletions frontend/src/lib/data-map/DataMap.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { MapboxOverlay } from '@deck.gl/mapbox/typed';
import { useMap } from 'react-map-gl/maplibre';
import { FC, useCallback, useMemo, useRef } from 'react';
import { FC, useMemo, useRef } from 'react';

import { useInteractions } from 'lib/state/interactions/use-interactions';
import { useTriggerMemo } from 'lib/hooks/use-trigger-memo';
import { useDataLoadTrigger } from 'lib/data-map/use-data-load-trigger';
import { InteractionGroupConfig } from 'lib/data-map/types';
import { DeckGLOverlay } from 'lib/map/DeckGLOverlay';
import { ViewLayer, ViewLayerParams } from 'lib/data-map/view-layers';
Expand All @@ -15,6 +13,30 @@ function lookupViewForDeck(deckLayerId: string) {
return deckLayerId.split('@')[0];
}

function makeDeckLayers(
viewLayer: ViewLayer,
viewLayerParams: ViewLayerParams,
zoom: number,
beforeId: string | undefined,
) {
return viewLayer.fn({
deckProps: { id: viewLayer.id, pickable: !!viewLayer.interactionGroup, beforeId },
zoom,
...viewLayerParams,
});
}

function buildLayers(
viewLayers: ViewLayer[],
viewLayersParams: ViewLayerParams,
zoom: number,
beforeId: string | undefined,
) {
return viewLayers.map((viewLayer) =>
makeDeckLayers(viewLayer, viewLayersParams[viewLayer.id], zoom, beforeId),
);
}

export const DataMap: FC<{
firstLabelId: string;
interactionGroups: Map<string, InteractionGroupConfig>;
Expand All @@ -25,35 +47,16 @@ export const DataMap: FC<{
const { current: map } = useMap();
const zoom = map.getMap().getZoom();

const dataLoaders = useMemo(
() =>
viewLayers
.map((vl) => vl.dataAccessFn?.(vl.styleParams?.colorMap?.fieldSpec)?.dataLoader)
.filter(Boolean),
[viewLayers],
);

const dataLoadTrigger = useDataLoadTrigger(dataLoaders);

const layersFunction = useCallback(
({ zoom }: { zoom: number }) =>
viewLayers.map((viewLayer) =>
makeDeckLayers(viewLayer, viewLayersParams[viewLayer.id], zoom, firstLabelId),
) as LayersList,
[firstLabelId, viewLayers, viewLayersParams],
);

const { onHover, onClick, layerFilter, pickingRadius } = useInteractions(
viewLayers,
lookupViewForDeck,
interactionGroups,
);

const layers = useTriggerMemo(
() => layersFunction({ zoom }),
[layersFunction, zoom],
dataLoadTrigger,
);
const layers = useMemo(
() => buildLayers(viewLayers, viewLayersParams, zoom, firstLabelId),
[viewLayers, viewLayersParams, zoom, firstLabelId],
) as LayersList;

return (
<DeckGLOverlay
Expand All @@ -71,16 +74,3 @@ export const DataMap: FC<{
/>
);
};

function makeDeckLayers(
viewLayer: ViewLayer,
viewLayerParams: ViewLayerParams,
zoom: number,
beforeId: string | undefined,
) {
return viewLayer.fn({
deckProps: { id: viewLayer.id, pickable: !!viewLayer.interactionGroup, beforeId },
zoom,
...viewLayerParams,
});
}

0 comments on commit 7aff777

Please sign in to comment.