Skip to content

Commit

Permalink
refactor(frontend): concurrent rendering in React 18
Browse files Browse the repository at this point in the history
- use the new `createRoot` API in React 18.
- refactor the base map layer to avoid an error in React Map GL when the map first mounts.
  • Loading branch information
eatyourgreens committed Jun 25, 2024
1 parent 2850a4f commit 1268811
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getAssetDataFormats } from 'config/assets/data-formats';
import { FeatureSidebarContent } from 'details/features/FeatureSidebarContent';
import { BoundingBox, extendBbox } from 'lib/bounding-box';
import { colorMap } from 'lib/color-map';
import { mapFitBoundsState } from 'map/MapView';
import { mapFitBoundsState } from 'lib/map/MapBoundsFitter';
import { ColorBox } from 'map/tooltip/content/ColorBox';
import { useCallback, useMemo } from 'react';
import { atom, useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
Expand Down
23 changes: 17 additions & 6 deletions frontend/src/lib/data-map/BaseMap.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
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);

function handleViewStateChange({ viewState }: { viewState: MapViewState }) {
setViewState(viewState);
}

return (
<Map
reuseMaps={true}
styleDiffing={true}
{...viewState}
onMove={({ viewState }) => onViewState(viewState)}
onMove={handleViewStateChange}
mapStyle={mapStyle}
dragRotate={false}
keyboard={false}
Expand Down
17 changes: 13 additions & 4 deletions frontend/src/lib/map/MapBoundsFitter.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import { WebMercatorViewport } from 'deck.gl/typed';
import { FC, useEffect } from 'react';
import { useMap } from 'react-map-gl/maplibre';
import { atom, useRecoilValue, useResetRecoilState } from 'recoil';

import { BoundingBox, appToDeckBoundingBox } from '../bounding-box';

interface MapBoundsFitterProps {
boundingBox: BoundingBox;
}
export const mapFitBoundsState = atom<BoundingBox>({
key: 'mapFitBoundsState',
default: null,
});

export const MapBoundsFitter: FC<MapBoundsFitterProps> = ({ boundingBox }) => {
export const MapBoundsFitter: FC = () => {
const { current: map } = useMap();
const boundingBox = useRecoilValue(mapFitBoundsState);

const resetFitBounds = useResetRecoilState(mapFitBoundsState);
useEffect(() => {
// reset map fit bounds whenever map is mounted
resetFitBounds();
}, [resetFitBounds]);

useEffect(() => {
if (boundingBox != null && map != null) {
Expand Down
40 changes: 10 additions & 30 deletions frontend/src/map/MapView.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import { Suspense, useCallback, useEffect } from 'react';
import {
atom,
useRecoilState,
useRecoilValue,
useResetRecoilState,
useSetRecoilState,
} from 'recoil';

import { mapViewStateState } from '../state/map-view/map-view-state';
import { BoundingBox } from 'lib/bounding-box';
import { useRecoilValue, useSetRecoilState } from 'recoil';

import { BaseMap } from 'lib/data-map/BaseMap';
import { DataMap } from 'lib/data-map/DataMap';
import { DataMapTooltip } from 'lib/data-map/DataMapTooltip';
import { MapBoundsFitter } from 'lib/map/MapBoundsFitter';
import { MapBoundsFitter, mapFitBoundsState } from 'lib/map/MapBoundsFitter';
import { MapHud } from 'lib/map/hud/MapHud';
import { MapHudRegion } from 'lib/map/hud/MapHudRegion';
import {
Expand All @@ -37,11 +29,6 @@ import { MapLegend } from './legend/MapLegend';
import { TooltipContent } from './tooltip/TooltipContent';
import { useBasemapStyle } from './use-basemap-style';

export const mapFitBoundsState = atom<BoundingBox>({
key: 'mapFitBoundsState',
default: null,
});

const AppPlaceSearch = () => {
const setFitBounds = useSetRecoilState(mapFitBoundsState);

Expand Down Expand Up @@ -114,12 +101,11 @@ const MapHudMobileLayout = () => {
};

const MapViewContent = () => {
const [viewState, setViewState] = useRecoilState(mapViewStateState);
const background = useRecoilValue(backgroundState);
const showLabels = useRecoilValue(showLabelsState);
const viewLayers = useRecoilValue(viewLayersFlatState);
const saveViewLayers = useSaveViewLayers();
const { mapStyle, firstLabelId } = useBasemapStyle(background, showLabels);
const { firstLabelId } = useBasemapStyle(background, showLabels);

useEffect(() => {
saveViewLayers(viewLayers);
Expand All @@ -129,37 +115,31 @@ const MapViewContent = () => {

const interactionGroups = useRecoilValue(interactionGroupsState);

const fitBounds = useRecoilValue(mapFitBoundsState);

const resetFitBounds = useResetRecoilState(mapFitBoundsState);
useEffect(() => {
// reset map fit bounds whenever MapView is mounted
resetFitBounds();
}, [resetFitBounds]);

const isMobile = useIsMobile();

return (
<BaseMap mapStyle={mapStyle} viewState={viewState} onViewState={setViewState}>
<>
<DataMap
beforeId={firstLabelId}
viewLayers={viewLayers}
viewLayersParams={viewLayersParams}
interactionGroups={interactionGroups}
/>
<MapBoundsFitter boundingBox={fitBounds} />
<MapBoundsFitter />
<DataMapTooltip>
<TooltipContent />
</DataMapTooltip>
{isMobile ? <MapHudMobileLayout /> : <MapHudDesktopLayout />}
</BaseMap>
</>
);
};

export const MapView = () => (
<ErrorBoundary message="There was a problem displaying the map." justifyErrorContent="center">
<Suspense fallback={null}>
<MapViewContent />
<BaseMap>
<MapViewContent />
</BaseMap>
</Suspense>
</ErrorBoundary>
);

0 comments on commit 1268811

Please sign in to comment.