Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
[feat] Remove a custom map style from the base maps side panel (#2279)
Igor De056d01ade8a
diff --git a/src/actions/src/action-types.ts b/src/actions/src/action-types.tsindex f38d4e4b1..27ff63740 100644--- a/src/actions/src/action-types.ts+++ b/src/actions/src/action-types.ts@@ -134,6 +134,7 @@ export const ActionTypes = {INPUT_MAP_STYLE: `${ACTION_PREFIX}INPUT_MAP_STYLE`,LOAD_CUSTOM_MAP_STYLE: `${ACTION_PREFIX}LOAD_CUSTOM_MAP_STYLE`,ADD_CUSTOM_MAP_STYLE: `${ACTION_PREFIX}ADD_CUSTOM_MAP_STYLE`,+ REMOVE_CUSTOM_MAP_STYLE: `${ACTION_PREFIX}REMOVE_CUSTOM_MAP_STYLE`,REQUEST_MAP_STYLES: `${ACTION_PREFIX}REQUEST_MAP_STYLES`,SET_3D_BUILDING_COLOR: `${ACTION_PREFIX}SET_3D_BUILDING_COLOR`,SET_BACKGROUND_COLOR: `${ACTION_PREFIX}SET_BACKGROUND_COLOR`,diff --git a/src/actions/src/map-style-actions.ts b/src/actions/src/map-style-actions.tsindex faec45329..44078b802 100644--- a/src/actions/src/map-style-actions.ts+++ b/src/actions/src/map-style-actions.ts@@ -42,6 +42,31 @@ export const addCustomMapStyle: () => {type: typeof ActionTypes.ADD_CUSTOM_MAP_STYLE;} = createAction(ActionTypes.ADD_CUSTOM_MAP_STYLE);+export type RemoveCustomMapStyleUpdaterAction = {+ payload: {+ id: string;+ };+};++/**+ * Remove a custom map style from `state.mapStyle.mapStyles`.+ * @param id+ * @memberof mapStyleActions+ * @public+ */+export const removeCustomMapStyle: ({+ id+}: RemoveCustomMapStyleUpdaterAction['payload']) => Merge<+ RemoveCustomMapStyleUpdaterAction,+ {type: typeof ActionTypes.REMOVE_CUSTOM_MAP_STYLE}+> = createAction(ActionTypes.REMOVE_CUSTOM_MAP_STYLE, ({id}) => {+ return {+ payload: {+ id+ }+ };+});+/** INPUT_MAP_STYLE */export type InputMapStyleUpdaterAction = {payload: {diff --git a/src/components/src/side-panel/map-manager.tsx b/src/components/src/side-panel/map-manager.tsxindex 221480040..53a0da961 100644--- a/src/components/src/side-panel/map-manager.tsx+++ b/src/components/src/side-panel/map-manager.tsx@@ -19,22 +19,30 @@// THE SOFTWARE.import React, {Component} from 'react';+import {createSelector} from 'reselect';import {Button} from '../common/styled-components';import MapStyleSelectorFactory from './map-style-panel/map-style-selector';import LayerGroupSelectorFactory from './map-style-panel/map-layer-selector';import PanelTitleFactory from '../side-panel/panel-title';-import {Add} from '../common/icons';+import {Add, Trash} from '../common/icons';import {PanelMeta} from './common/types';import {injectIntl, WrappedComponentProps} from 'react-intl';import {FormattedMessage} from '@kepler.gl/localization';import {MapStyle} from '@kepler.gl/reducers';import {MapStyleActions} from '@kepler.gl/actions';+import {MapStyles} from '@kepler.gl/types';export type MapManagerProps = {mapStyle: MapStyle;- mapStyleActions: typeof MapStyleActions;+ mapStyleActions: {+ mapStyleChange: typeof MapStyleActions.mapStyleChange;+ mapConfigChange: typeof MapStyleActions.mapConfigChange;+ set3dBuildingColor: typeof MapStyleActions.set3dBuildingColor;+ setBackgroundColor: typeof MapStyleActions.setBackgroundColor;+ removeCustomMapStyle: typeof MapStyleActions.removeCustomMapStyle;+ };showAddMapStyleModal: () => void;panelMetadata: PanelMeta;} & WrappedComponentProps;@@ -62,15 +70,39 @@ function MapManagerFactory(this._toggleSelecting();};+ getCustomMapStylesActions = createSelector(+ (props: MapManagerProps) => props.mapStyle.mapStyles,+ (props: MapManagerProps) => props.mapStyleActions,+ (mapStyles: MapStyles, mapStyleActions) => {+ const actionsPerCustomStyle = {};+ Object.values(mapStyles)+ .filter(mapStyle => {+ return Boolean(mapStyle.custom);+ })+ .forEach(({id}) => {+ actionsPerCustomStyle[id] = [+ {+ id: `remove-map-style-${id}`,+ IconComponent: Trash,+ tooltip: 'tooltip.removeBaseMapStyle',+ onClick: () => mapStyleActions.removeCustomMapStyle({id})+ }+ ];+ });+ return actionsPerCustomStyle;+ }+ );+render() {const {mapStyle, intl, mapStyleActions, showAddMapStyleModal, panelMetadata} = this.props;const currentStyle = mapStyle.mapStyles[mapStyle.styleType] || {};const editableLayers = currentStyle.layerGroups || [];+ const customMapStylesActions = this.getCustomMapStylesActions(this.props);return (<div className="map-style-panel"><PanelTitle- className="filter-manager-title"+ className="map-manager-title"title={intl.formatMessage({id: panelMetadata.label})}><Button className="add-map-style-button" onClick={showAddMapStyleModal}>@@ -84,6 +116,7 @@ function MapManagerFactory(isSelecting={this.state.isSelecting}onChange={this._selectStyle}toggleActive={this._toggleSelecting}+ customMapStylesActions={customMapStylesActions}/>{editableLayers.length ? (<LayerGroupSelectordiff --git a/src/components/src/side-panel/map-style-panel/map-style-selector.tsx b/src/components/src/side-panel/map-style-panel/map-style-selector.tsxindex 2f4aadbb3..4eca2f672 100644--- a/src/components/src/side-panel/map-style-panel/map-style-selector.tsx+++ b/src/components/src/side-panel/map-style-panel/map-style-selector.tsx@@ -25,6 +25,7 @@ import classnames from 'classnames';import {FormattedMessage} from '@kepler.gl/localization';import {MapStyle} from '@kepler.gl/reducers';import {NO_BASEMAP_ICON} from '@kepler.gl/constants';+import {MapStyles} from '@kepler.gl/types';import {ArrowDown} from '../../common/icons';import PanelHeaderActionFactory from '../panel-header-action';@@ -36,6 +37,7 @@ import {StyledPanelHeaderProps} from '../../common/styled-components';import {BaseProps} from '../../common/icons';+import {PanelHeaderActionIcon} from '../panel-header-action';type StyledMapDropdownProps = StyledPanelHeaderProps & {hasCallout: boolean};@@ -67,6 +69,10 @@ const StyledMapDropdown = styled(StyledPanelHeader)<StyledMapDropdownProps>`width: 40px;}+ &.selected {+ outline: 1px solid #caf2f4;+ }+/* show callout dot if props.hasCallout and theme provides calloutDot base styles */:after {${({theme}) => theme.calloutDot}@@ -75,6 +81,12 @@ const StyledMapDropdown = styled(StyledPanelHeader)<StyledMapDropdownProps>`left: 15px;display: ${({theme, hasCallout}) => (theme.calloutDot && hasCallout ? 'block' : 'none')};}++ .custom-style-actions {+ display: flex;+ justify-content: space-between;+ align-items: center;+ }`;export type MapStyleSelectorProps = {@@ -82,6 +94,15 @@ export type MapStyleSelectorProps = {onChange: (payload: string) => void;toggleActive: () => void;isSelecting: boolean;+ customMapStylesActions?: Record<+ string,+ {+ id: string;+ IconComponent: PanelHeaderActionIcon;+ tooltip: string;+ onClick: () => void;+ }[]+ >;actionIcons?: Record<string, ComponentType<Partial<BaseProps>>>;};@@ -97,9 +118,10 @@ function MapStyleSelectorFactory(PanelHeaderAction: ReturnType<typeof PanelHeadeonChange,toggleActive,isSelecting,+ customMapStylesActions,actionIcons = defaultActionIcons}: MapStyleSelectorProps) => {- const {mapStyles, styleType} = mapStyle;+ const {mapStyles, styleType}: {mapStyles: MapStyles; styleType: string} = mapStyle;return (<div>@@ -111,7 +133,8 @@ function MapStyleSelectorFactory(PanelHeaderAction: ReturnType<typeof PanelHeade({id, custom, icon = NO_BASEMAP_ICON, label = 'Untitled'}) => (<StyledMapDropdownclassName={classnames('map-dropdown-option', {- collapsed: !isSelecting && styleType !== id+ collapsed: !isSelecting && id !== styleType,+ selected: isSelecting && id === styleType})}key={id}onClick={isSelecting ? () => onChange(id) : toggleActive}@@ -130,6 +153,23 @@ function MapStyleSelectorFactory(PanelHeaderAction: ReturnType<typeof PanelHeadeonClick={toggleActive}/>) : null}+ {isSelecting && custom ? (+ <div className="custom-style-actions">+ {(customMapStylesActions?.[id] || []).map(action => (+ <PanelHeaderAction+ key={action.id}+ className="map-dropdown-option__enable-config"+ id={action.id}+ IconComponent={action.IconComponent}+ tooltip={action.tooltip}+ onClick={e => {+ e.stopPropagation();+ action.onClick();+ }}+ />+ ))}+ </div>+ ) : null}</StyledMapDropdown>))}diff --git a/src/localization/src/translations/en.ts b/src/localization/src/translations/en.tsindex ce7a3a1fa..48fdfb15b 100644--- a/src/localization/src/translations/en.ts+++ b/src/localization/src/translations/en.ts@@ -200,7 +200,7 @@ export default {tooltip: {hideLayer: 'Hide layer',showLayer: 'Show layer',- hideFeature: 'Hide Feature',+ hideFeature: 'Hide feature',showFeature: 'Show feature',hide: 'hide',show: 'show',@@ -217,7 +217,8 @@ export default {hideLayerPanel: 'Hide layer panel',showLayerPanel: 'Show layer panel',moveToTop: 'Move to top of data layers',- selectBaseMapStyle: 'Select Base Map Style',+ selectBaseMapStyle: 'Select base map style',+ removeBaseMapStyle: 'Remove base map style',delete: 'Delete',timePlayback: 'Time Playback',cloudStorage: 'Cloud Storage',diff --git a/src/reducers/src/map-style-updaters.ts b/src/reducers/src/map-style-updaters.tsindex 3bc25cf94..539bdf061 100644--- a/src/reducers/src/map-style-updaters.ts+++ b/src/reducers/src/map-style-updaters.ts@@ -763,6 +763,33 @@ export const addCustomMapStyleUpdater = (state: MapStyle): MapStyle => {return mapStyleChangeUpdater(newState, {payload: {styleType: styleId}});};+/**+ * Remove a custom map style from `state.mapStyle.mapStyles`.+ * @memberof mapStyleUpdaters+ */+export const removeCustomMapStyleUpdater = (+ state: MapStyle,+ action: MapStyleActions.RemoveCustomMapStyleUpdaterAction+): MapStyle => {+ const {id} = action.payload;++ // eslint-disable-next-line no-unused-vars+ const {[id]: _, ...restOfMapStyles} = state.mapStyles;++ const newState = {+ ...state,+ mapStyles: restOfMapStyles+ };++ if (state.styleType === id) {+ // if removing a custom style that is also the current active base map,+ // then reset to the default active base map (`mapStyle.styleType`)+ return mapStyleChangeUpdater(newState, {payload: {styleType: getDefaultState().styleType}});+ }++ return newState;+};+/*** Updates 3d building color* @memberof mapStyleUpdatersdiff --git a/src/reducers/src/map-style.ts b/src/reducers/src/map-style.tsindex 7ce703f1f..438cefd7c 100644--- a/src/reducers/src/map-style.ts+++ b/src/reducers/src/map-style.ts@@ -37,6 +37,7 @@ const actionHandler = {[ActionTypes.RECEIVE_MAP_CONFIG]: mapStyleUpdaters.receiveMapConfigUpdater,[ActionTypes.LOAD_CUSTOM_MAP_STYLE]: mapStyleUpdaters.loadCustomMapStyleUpdater,[ActionTypes.ADD_CUSTOM_MAP_STYLE]: mapStyleUpdaters.addCustomMapStyleUpdater,+ [ActionTypes.REMOVE_CUSTOM_MAP_STYLE]: mapStyleUpdaters.removeCustomMapStyleUpdater,[ActionTypes.RESET_MAP_CONFIG]: mapStyleUpdaters.resetMapConfigMapStyleUpdater,[ActionTypes.SET_3D_BUILDING_COLOR]: mapStyleUpdaters.set3dBuildingColorUpdater,[ActionTypes.SET_BACKGROUND_COLOR]: mapStyleUpdaters.setBackgroundColorUpdater,diff --git a/test/node/reducers/map-style-test.js b/test/node/reducers/map-style-test.jsindex 79ace0e0f..07869eb8a 100644--- a/test/node/reducers/map-style-test.js+++ b/test/node/reducers/map-style-test.js@@ -32,7 +32,8 @@ import {receiveMapConfig,loadMapStyles,mapConfigChange,- mapStyleChange+ mapStyleChange,+ removeCustomMapStyle} from '@kepler.gl/actions';import SchemaManager from '@kepler.gl/schemas';import {DEFAULT_MAP_STYLES, DEFAULT_MAPBOX_API_URL, NO_MAP_ID} from '@kepler.gl/constants';@@ -829,3 +830,84 @@ test('#mapStyleReducer -> MAP_STYLE_CHANGE -> dark basemap to no basemap', t =>t.end();});++test('#mapStyleReducer -> REMOVE_CUSTOM_MAP_STYLE -> removal of currently selected custom style', t => {+ drainTasksForTesting();+ const stateWithToken = reducer(+ InitialMapStyle,+ keplerGlInit({+ mapboxApiAccessToken: 'smoothies_secret_token'+ })+ );++ // state with custom style which is also the active styleType+ let nextState = reducer(stateWithToken, receiveMapConfig(StateWCustomMapStyleLocal));++ t.equal(+ nextState.styleType,+ 'smoothie_the_cat',+ 'active styleType is currently the custom style'+ );++ // remove custom map style+ nextState = reducer(+ nextState,+ removeCustomMapStyle({+ id: 'smoothie_the_cat'+ })+ );++ t.equal(+ nextState.mapStyles.smoothie_the_cat,+ undefined,+ 'should remove custom style with id "smoothie_the_cat"'+ );++ t.equal(+ nextState.styleType,+ 'dark',+ 'should change active styleType to default "dark" when removing a custom style that was the active styleType'+ );++ t.end();+});++test('#mapStyleReducer -> REMOVE_CUSTOM_MAP_STYLE -> removal of not currently selected custom style', t => {+ drainTasksForTesting();+ const stateWithToken = reducer(+ InitialMapStyle,+ keplerGlInit({+ mapboxApiAccessToken: 'smoothies_secret_token'+ })+ );++ // state with custom style+ let nextState = reducer(stateWithToken, receiveMapConfig(StateWCustomMapStyleLocal));++ // set style type to no basemap+ nextState = reducer(nextState, mapStyleChange(NO_MAP_ID));++ t.equal(nextState.styleType, NO_MAP_ID, 'active styleType is currently the no basemap style');++ // remove custom map style+ nextState = reducer(+ nextState,+ removeCustomMapStyle({+ id: 'smoothie_the_cat'+ })+ );++ t.equal(+ nextState.mapStyles.smoothie_the_cat,+ undefined,+ 'should remove custom style with id "smoothie_the_cat"'+ );++ t.equal(+ nextState.styleType,+ NO_MAP_ID,+ 'should not change the active styleType when removing a custom style that was not the active styleType'+ );++ t.end();+});