Skip to content

Commit

Permalink
refactor BaseMap props and Recoil state
Browse files Browse the repository at this point in the history
  • Loading branch information
eatyourgreens committed Jun 24, 2024
1 parent 01d8524 commit f164053
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
19 changes: 12 additions & 7 deletions frontend/src/lib/data-map/BaseMap.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import { MapViewState } from 'deck.gl/typed';
import { ComponentProps, FC, ReactNode } from 'react';
import { FC, ReactNode } from 'react';
import { Map } from 'react-map-gl/maplibre';
import { useRecoilState, useRecoilValue } from 'recoil';

import { backgroundState, showLabelsState } from 'map/layers/layers-state';
import { useBasemapStyle } from 'map/use-basemap-style';
import { mapViewStateState } from 'state/map-view/map-view-state';

export interface BaseMapProps {
mapStyle: ComponentProps<typeof Map>['mapStyle'];
viewState: MapViewState;
onViewState: (vs: MapViewState) => void;
children?: ReactNode;
}

/**
* Displays a react-map-gl basemap component.
* Accepts children such as a DeckGLOverlay, HUD controls etc
*/
export const BaseMap: FC<BaseMapProps> = ({ mapStyle, viewState, onViewState, children }) => {
export const BaseMap: FC<BaseMapProps> = ({ children }) => {
const background = useRecoilValue(backgroundState);
const showLabels = useRecoilValue(showLabelsState);
const [viewState, setViewState] = useRecoilState(mapViewStateState);
const { mapStyle } = useBasemapStyle(background, showLabels);
return (
<Map
reuseMaps={true}
styleDiffing={true}
{...viewState}
onMove={({ viewState }) => onViewState(viewState)}
onMove={({ viewState }) => setViewState(viewState)}
mapStyle={mapStyle}
dragRotate={false}
keyboard={false}
Expand Down
31 changes: 8 additions & 23 deletions frontend/src/map/MapView.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { Suspense, useCallback, useEffect } from 'react';
import {
atom,
useRecoilState,
useRecoilValue,
useResetRecoilState,
useSetRecoilState,
} from 'recoil';

import { mapViewStateState } from '../state/map-view/map-view-state';
import { atom, useRecoilValue, useResetRecoilState, useSetRecoilState } from 'recoil';

import { BoundingBox } from 'lib/bounding-box';
import { BaseMap } from 'lib/data-map/BaseMap';
import { DataMap } from 'lib/data-map/DataMap';
Expand Down Expand Up @@ -139,7 +132,7 @@ const MapViewContent = () => {
const isMobile = useIsMobile();

return (
<>
<Suspense fallback={null}>
<DataMap
beforeId={firstLabelId}
viewLayers={viewLayers}
Expand All @@ -151,22 +144,14 @@ const MapViewContent = () => {
<TooltipContent />
</DataMapTooltip>
{isMobile ? <MapHudMobileLayout /> : <MapHudDesktopLayout />}
</>
</Suspense>
);
};

export const MapView = () => {
const background = useRecoilValue(backgroundState);
const showLabels = useRecoilValue(showLabelsState);
const [viewState, setViewState] = useRecoilState(mapViewStateState);
const { mapStyle } = useBasemapStyle(background, showLabels);
return (
export const MapView = () => (
<ErrorBoundary message="There was a problem displaying the map." justifyErrorContent="center">
<BaseMap mapStyle={mapStyle} viewState={viewState} onViewState={setViewState}>
<Suspense fallback={null}>
<MapViewContent />
</Suspense>
<BaseMap>
<MapViewContent />
</BaseMap>
</ErrorBoundary>
)
};
);

0 comments on commit f164053

Please sign in to comment.