Skip to content

Commit

Permalink
[chore] Avoid confusion in viewstate context (#2361)
Browse files Browse the repository at this point in the history
Signed-off-by: Ihor Dykhta <dikhta.igor@gmail.com>
Co-authored-by: Ilya Boyandin <ilya@boyandin.me>
  • Loading branch information
igorDykhta and ilyabo committed Oct 10, 2023
1 parent 1fcdfde commit d0a8658
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/components/src/map-container.tsx
Expand Up @@ -695,6 +695,7 @@ export default function MapContainerFactory(
const {
mapStyle,
visState,
mapState,
visStateActions,
mapboxApiAccessToken,
mapboxApiUrl,
Expand All @@ -716,7 +717,8 @@ export default function MapContainerFactory(
const isEditorDrawingMode = EditorLayerUtils.isDrawingActive(editorMenuActive, editor.mode);

const internalViewState = this.context?.getInternalViewState(index);
const viewport = getViewportFromMapState(internalViewState);
const internalMapState = {...mapState, ...internalViewState};
const viewport = getViewportFromMapState(internalMapState);

const editorFeatureSelectedIndex = this.selectedPolygonIndexSelector(this.props);

Expand All @@ -726,7 +728,7 @@ export default function MapContainerFactory(
const deckGlLayers = generateDeckGLLayersMethod(
{
visState,
mapState: internalViewState,
mapState: internalMapState,
mapStyle
},
{
Expand Down
3 changes: 2 additions & 1 deletion src/components/src/map-view-state-context.tsx
@@ -1,6 +1,7 @@
import React, {useState, useEffect, createContext} from 'react';
import isEqual from 'lodash.isequal';
import pick from 'lodash.pick';
import {pickViewportPropsFromMapState} from '@kepler.gl/reducers';

import {MapState} from '@kepler.gl/types';

Expand Down Expand Up @@ -38,7 +39,7 @@ export const MapViewStateContextProvider = ({
}
} else {
if (hasChanged(primaryState, mapState)) {
setViewStates([mapState]);
setViewStates([pickViewportPropsFromMapState(mapState)] as MapState[]);
}
}
// Only update internalViewState when viewState changes
Expand Down
2 changes: 1 addition & 1 deletion src/reducers/src/index.ts
Expand Up @@ -63,7 +63,7 @@ export * from './provider-state';
export * from './ui-state';
export * from './map-state';
export {getInitialInputStyle, loadMapStylesUpdater, INITIAL_MAP_STYLE} from './map-style-updaters';
export {fitBoundsUpdater, INITIAL_MAP_STATE} from './map-state-updaters';
export {fitBoundsUpdater, pickViewportPropsFromMapState, INITIAL_MAP_STATE} from './map-state-updaters';

// Helpers
export * from './composer-helpers';
Expand Down
8 changes: 4 additions & 4 deletions src/reducers/src/map-state-updaters.ts
Expand Up @@ -378,9 +378,9 @@ export const toggleSplitMapViewportUpdater = (
// if current viewport is synced, and we are unsyncing it
// or already in unsynced mode and NOT toggling locked zoom
// make a fresh copy of the current viewport object, assign it to splitMapViewports[]
// getViewportFromMapState is called twice to avoid memory allocation conflicts
const leftViewport = getViewportFromMapState(newMapState);
const rightViewport = getViewportFromMapState(newMapState);
// pickViewportPropsFromMapState is called twice to avoid memory allocation conflicts
const leftViewport = pickViewportPropsFromMapState(newMapState);
const rightViewport = pickViewportPropsFromMapState(newMapState);
newMapState.splitMapViewports = [leftViewport, rightViewport];
}

Expand Down Expand Up @@ -468,7 +468,7 @@ function updateViewportBasedOnBounds(state: MapState, newMapState: MapState) {
return newMapState;
}

function getViewportFromMapState(state) {
export function pickViewportPropsFromMapState(state: MapState): Viewport {
return pick(state, [
'width',
'height',
Expand Down

0 comments on commit d0a8658

Please sign in to comment.