Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
[Feat]: Technical: Translate redusers (map-state and map-style) to typescript (#1717)
Maksim Suslov8121893cb0c7
diff --git a/src/reducers/map-state-updaters.d.ts b/src/reducers/map-state-updaters.d.tsdeleted file mode 100644index 5075585d2..000000000--- a/src/reducers/map-state-updaters.d.ts+++ /dev/null@@ -1,64 +0,0 @@-import * as MapStateActions from 'actions/map-state-actions';-import {ReceiveMapConfigPayload} from '../actions/actions';-import ActionTypes from 'constants/action-types';--export type MapState = {- pitch: number;- bearing: number;- latitude: number;- longitude: number;- zoom: number;- dragRotate: boolean;- width: number;- height: number;- isSplit: boolean;- initialState?: any;-};--export type Bounds = [number, number, number, number];-/** Width of viewport */-export type Viewport = {- /** Width of viewport */- width?: number;- /** Height of viewport */- height?: number;- /** Zoom of viewport */- zoom?: number;- /** Camera angle in degrees (0 is straight down) */- pitch?: number;- /** Map rotation in degrees (0 means north is up) */- bearing?: number;- /** Latitude center of viewport on map in mercator projection */- latitude?: number;- /** Longitude Center of viewport on map in mercator projection */- longitude?: number;- /** Whether to enable drag and rotate map into perspective viewport */- dragRotate?: boolean;-};--export const INITIAL_MAP_STATE: MapState;--export function updateMapUpdater(- state: MapState,- action: MapStateActions.UpdateMapUpdaterAction-): MapState;-export function fitBoundsUpdater(- state: MapState,- action: MapStateActions.FitBoundsUpdaterAction-): MapState;-export function togglePerspectiveUpdater(- state: MapState,- action: MapStateActions.TogglePerspectiveUpdaterAction-): MapState;-export function resetMapConfigUpdater(state: MapState): MapState;-export function receiveMapConfigUpdater(- state: MapState,- action: {- type?: ActionTypes.RECEIVE_MAP_CONFIG;- payload: ReceiveMapConfigPayload;- }-): MapState;-export function toggleSplitMapUpdater(- state: MapState,- action: MapStateActions.ToggleSplitMapUpdaterAction-): MapState;diff --git a/src/reducers/map-state-updaters.js b/src/reducers/map-state-updaters.tssimilarity index 73%rename from src/reducers/map-state-updaters.jsrename to src/reducers/map-state-updaters.tsindex e0fe91f9d..6930b4a55 100644--- a/src/reducers/map-state-updaters.js+++ b/src/reducers/map-state-updaters.ts@@ -19,8 +19,43 @@// THE SOFTWARE.import {getCenterAndZoomFromBounds} from 'utils/projection-utils';+import * as MapStateActions from 'actions/map-state-actions';+import {ReceiveMapConfigPayload} from '../actions/actions';+import ActionTypes from '../constants/action-types';-/** @typedef {import('./map-state-updaters').MapState} MapState */+export type MapState = {+ pitch: number;+ bearing: number;+ latitude: number;+ longitude: number;+ zoom: number;+ dragRotate: boolean;+ width: number;+ height: number;+ isSplit: boolean;+ initialState?: any;+};++export type Bounds = [number, number, number, number];+/** Width of viewport */+export type Viewport = {+ /** Width of viewport */+ width?: number;+ /** Height of viewport */+ height?: number;+ /** Zoom of viewport */+ zoom?: number;+ /** Camera angle in degrees (0 is straight down) */+ pitch?: number;+ /** Map rotation in degrees (0 means north is up) */+ bearing?: number;+ /** Latitude center of viewport on map in mercator projection */+ latitude?: number;+ /** Longitude Center of viewport on map in mercator projection */+ longitude?: number;+ /** Whether to enable drag and rotate map into perspective viewport */+ dragRotate?: boolean;+};/*** Updaters for `mapState` reducer. Can be used in your root reducer to directly modify kepler.gl's state.@@ -75,10 +110,9 @@ const mapStateUpdaters = null;* @property width Default: `800`* @property height Default: `800`* @property isSplit Default: `false`- * @type {MapState}* @public*/-export const INITIAL_MAP_STATE = {+export const INITIAL_MAP_STATE: MapState = {pitch: 0,bearing: 0,latitude: 37.75043,@@ -94,10 +128,12 @@ export const INITIAL_MAP_STATE = {/*** Update map viewport* @memberof mapStateUpdaters- * @type {typeof import('./map-state-updaters').updateMapUpdater}* @public*/-export const updateMapUpdater = (state, action) => ({+export const updateMapUpdater = (+ state: MapState,+ action: MapStateActions.UpdateMapUpdaterAction+): MapState => ({...state,...(action.payload || {})});@@ -105,10 +141,12 @@ export const updateMapUpdater = (state, action) => ({/*** Fit map viewport to bounds* @memberof mapStateUpdaters- * @type {typeof import('./map-state-updaters').fitBoundsUpdater}* @public*/-export const fitBoundsUpdater = (state, action) => {+export const fitBoundsUpdater = (+ state: MapState,+ action: MapStateActions.FitBoundsUpdaterAction+): MapState => {const centerAndZoom = getCenterAndZoomFromBounds(action.payload, {width: state.width,height: state.height@@ -131,10 +169,12 @@ export const fitBoundsUpdater = (state, action) => {/*** Toggle between 3d and 2d map.* @memberof mapStateUpdaters- * @type {typeof import('./map-state-updaters').togglePerspectiveUpdater}* @public*/-export const togglePerspectiveUpdater = state => ({+export const togglePerspectiveUpdater = (+ state: MapState,+ action: MapStateActions.TogglePerspectiveUpdaterAction+): MapState => ({...state,...{pitch: state.dragRotate ? 0 : 50,@@ -146,10 +186,9 @@ export const togglePerspectiveUpdater = state => ({/*** reset mapState to initial State* @memberof mapStateUpdaters- * @type {typeof import('./map-state-updaters').resetMapConfigUpdater}* @public*/-export const resetMapConfigUpdater = state => ({+export const resetMapConfigUpdater = (state: MapState): MapState => ({...INITIAL_MAP_STATE,...state.initialState,initialState: state.initialState@@ -159,13 +198,18 @@ export const resetMapConfigUpdater = state => ({/*** Update `mapState` to propagate a new config* @memberof mapStateUpdaters- * @type {typeof import('./map-state-updaters').receiveMapConfigUpdater}* @public*/export const receiveMapConfigUpdater = (- state,- {payload: {config = {}, options = {}, bounds = null}}-) => {+ state: MapState,+ {+ // @ts-expect-error+ payload: {config = {}, options = {}, bounds = null}+ }: {+ type?: typeof ActionTypes.RECEIVE_MAP_CONFIG;+ payload: ReceiveMapConfigPayload;+ }+): MapState => {const {mapState} = config || {};// merged received mapstate with previous state@@ -189,10 +233,12 @@ export const receiveMapConfigUpdater = (/*** Toggle between one or split maps* @memberof mapStateUpdaters- * @type {typeof import('./map-state-updaters').toggleSplitMapUpdater}* @public*/-export const toggleSplitMapUpdater = state => ({+export const toggleSplitMapUpdater = (+ state: MapState,+ action: MapStateActions.ToggleSplitMapUpdaterAction+): MapState => ({...state,isSplit: !state.isSplit,...getMapDimForSplitMap(!state.isSplit, state)diff --git a/src/reducers/map-state.js b/src/reducers/map-state.tssimilarity index 99%rename from src/reducers/map-state.jsrename to src/reducers/map-state.tsindex 2d3117c69..f8d6d503e 100644--- a/src/reducers/map-state.js+++ b/src/reducers/map-state.ts@@ -37,7 +37,6 @@ const actionHandler = {/* Reducer */export const mapStateReducerFactory = (initialState = {}) =>- // @ts-ignorehandleActions(actionHandler, {...mapStateUpdaters.INITIAL_MAP_STATE,...initialState,diff --git a/src/reducers/map-style-updaters.d.ts b/src/reducers/map-style-updaters.d.tsdeleted file mode 100644index 0bccada8f..000000000--- a/src/reducers/map-style-updaters.d.ts+++ /dev/null@@ -1,76 +0,0 @@-import {RGBColor} from './types';-import ActionTypes from 'constants/action-types';-import {ReceiveMapConfigPayload, KeplerGlInitPayload} from '../actions/actions';-import * as MapStyleActions from '../actions/map-style-actions';--export type LayerGroup = {- slug: string;- filter(layer: {id: string}): boolean;- defaultVisibility: boolean;-};--export type VisibleLayerGroups = {- [key: string]: boolean;-};--export type MapboxStyleUrl = string;--export type BaseMapStyle = {- id: string;- label: string;- url: string;- icon: string;- style?: object;- layerGroups: LayerGroup[];-};--export type MapStyles = {- [key: string]: BaseMapStyle;-};--export type InputStyle = {- accessToken: string | null;- error: boolean;- isValid: boolean;- label: string | null;- style: object | null;- url: string | null;- icon: string | null;- custom: boolean;-};--export type MapStyle = {- styleType: string;- visibleLayerGroups: VisibleLayerGroups;- topLayerGroups: VisibleLayerGroups;- mapStyles: MapStyles;- // save mapbox access token- mapboxApiAccessToken: string | null;- mapboxApiUrl: string;- mapStylesReplaceDefault: boolean;- inputStyle: InputStyle;- threeDBuildingColor: RGBColor;- custom3DBuildingColor: boolean;-};--export const INITIAL_MAP_STYLE: MapStyle;--export function addCustomMapStyleUpdater(state: MapStyle, action: MapStyleActions.AddCustomMapStyleUpdaterAction): MapStyle;-export function inputMapStyleUpdater(state: MapStyle, action: MapStyleActions.InputMapStyleUpdaterAction): MapStyle;-export function mapConfigChangeUpdater(state: MapStyle, action: MapStyleActions.MapConfigChangeUpdaterAction): MapStyle;-export function requestMapStylesUpdater(state: MapStyle, action: MapStyleActions.RequestMapStylesUpdaterAction): MapStyle;-export function loadMapStylesUpdater(state: MapStyle, action: MapStyleActions.LoadMapStylesUpdaterAction): MapStyle;-export function loadMapStyleErrUpdater(state: MapStyle, action: MapStyleActions.LoadMapStyleErrUpdaterAction): MapStyle;-export function mapStyleChangeUpdater(state: MapStyle, action: MapStyleActions.MapStyleChangeUpdaterAction): MapStyle;-export function loadCustomMapStyleUpdater(state: MapStyle, action: MapStyleActions.LoadCustomMapStyleUpdaterAction): MapStyle;-export function set3dBuildingColorUpdater(state: MapStyle, action: MapStyleActions.Set3dBuildingColorUpdaterAction): MapStyle;--export function receiveMapConfigUpdater(state: MapStyle, action: {- type?: ActionTypes.RECEIVE_MAP_CONFIG;- payload: ReceiveMapConfigPayload;-}): MapStyle;-export function initMapStyleUpdater(state: MapStyle, action: {- type?: ActionTypes.INIT;- payload: KeplerGlInitPayload;-}): MapStyle;-export function resetMapConfigMapStyleUpdater(state: MapStyle): MapStyle;\ No newline at end of filediff --git a/src/reducers/map-style-updaters.js b/src/reducers/map-style-updaters.tssimilarity index 79%rename from src/reducers/map-style-updaters.jsrename to src/reducers/map-style-updaters.tsindex 47a0ca7dd..344c778a7 100644--- a/src/reducers/map-style-updaters.js+++ b/src/reducers/map-style-updaters.ts@@ -18,7 +18,6 @@// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN// THE SOFTWARE.-// @ts-nocheckimport Task, {withTask} from 'react-palm/tasks';import cloneDeep from 'lodash.clonedeep';@@ -43,12 +42,66 @@ import {loadMapStyles, loadMapStyleErr} from 'actions/map-style-actions';import {rgb} from 'd3-color';import {hexToRgb} from 'utils/color-utils';+import {RGBColor} from './types';+import ActionTypes from 'constants/action-types';+import {ReceiveMapConfigPayload, KeplerGlInitPayload} from '../actions/actions';+import * as MapStyleActions from '../actions/map-style-actions';++export type LayerGroup = {+ slug: string;+ filter(layer: {id: string}): boolean;+ defaultVisibility: boolean;+};++export type VisibleLayerGroups = {+ [key: string]: boolean;+};++export type MapboxStyleUrl = string;++export type BaseMapStyle = {+ id: string;+ label: string;+ url: string;+ icon: string;+ style?: Object;+ layerGroups: LayerGroup[];+};++export type MapStyles = {+ [key: string]: BaseMapStyle;+};++export type InputStyle = {+ accessToken: string | null;+ error: boolean;+ isValid: boolean;+ label: string | null;+ style: Object | null;+ url: string | null;+ icon: string | null;+ custom: boolean;+};++export type MapStyle = {+ styleType: string;+ visibleLayerGroups: VisibleLayerGroups;+ topLayerGroups: VisibleLayerGroups;+ mapStyles: MapStyles;+ // save mapbox access token+ mapboxApiAccessToken: string | null;+ mapboxApiUrl: string;+ mapStylesReplaceDefault: boolean;+ inputStyle: InputStyle;+ threeDBuildingColor: RGBColor;+ custom3DBuildingColor: boolean;++ initialState?: MapStyle;+};+const DEFAULT_BLDG_COLOR = '#D1CEC7';-/**- * @return {import('./map-style-updaters').MapStyle}- */-const getDefaultState = () => {+const getDefaultState = (): MapStyle => {const visibleLayerGroups = {};const styleType = 'dark';const topLayerGroups = {};@@ -111,6 +164,7 @@ const getDefaultState = () => {* export default composedReducer;*//* eslint-disable no-unused-vars */+// @ts-expect-errorconst mapStyleUpdaters = null;/* eslint-enable no-unused-vars *//**@@ -126,10 +180,9 @@ const mapStyleUpdaters = null;* @Property mapStylesReplaceDefault - Default: `false`* @property inputStyle - Default: `{}`* @property threeDBuildingColor - Default: `[r, g, b]`- * @type {import('./map-style-updaters').MapStyle}* @public*/-export const INITIAL_MAP_STYLE = getDefaultState();+export const INITIAL_MAP_STYLE: MapStyle = getDefaultState();/*** Create two map styles from preset map style, one for top map one for bottom@@ -186,7 +239,7 @@ function findLayerFillColor(layer) {return layer && layer.paint && layer.paint['background-color'];}-function get3DBuildingColor(style) {+function get3DBuildingColor(style): RGBColor {// set building color to be the same as the background color.if (!style.style) {return hexToRgb(DEFAULT_BLDG_COLOR);@@ -220,10 +273,17 @@ function getLayerGroupsFromStyle(style) {* populate mapStyles.** @memberof mapStyleUpdaters- * @type {typeof import('./map-style-updaters').initMapStyleUpdater}* @public*/-export const initMapStyleUpdater = (state, {payload = {}}) => ({+export const initMapStyleUpdater = (+ state: MapStyle,+ {+ payload = {}+ }: {+ type?: typeof ActionTypes.INIT;+ payload: KeplerGlInitPayload;+ }+): MapStyle => ({...state,// save mapbox access token to map style statemapboxApiAccessToken: payload.mapboxApiAccessToken || state.mapboxApiAccessToken,@@ -236,10 +296,12 @@ export const initMapStyleUpdater = (state, {payload = {}}) => ({/*** Update `visibleLayerGroups`to change layer group visibility* @memberof mapStyleUpdaters- * @type {typeof import('./map-style-updaters').mapConfigChangeUpdater}* @public*/-export const mapConfigChangeUpdater = (state, action) => ({+export const mapConfigChangeUpdater = (+ state: MapStyle,+ action: MapStyleActions.MapConfigChangeUpdaterAction+): MapStyle => ({...state,...action.payload,...getMapStyles({@@ -251,10 +313,12 @@ export const mapConfigChangeUpdater = (state, action) => ({/*** Change to another map style. The selected style should already been loaded into `mapStyle.mapStyles`* @memberof mapStyleUpdaters- * @type {typeof import('./map-style-updaters').mapStyleChangeUpdater}* @public*/-export const mapStyleChangeUpdater = (state, {payload: styleType}) => {+export const mapStyleChangeUpdater = (+ state: MapStyle,+ {payload: styleType}: MapStyleActions.MapStyleChangeUpdaterAction+): MapStyle => {if (!state.mapStyles[styleType]) {// we might not have received the style yetreturn state;@@ -266,7 +330,7 @@ export const mapStyleChangeUpdater = (state, {payload: styleType}) => {state.visibleLayerGroups);- const threeDBuildingColor = state.custom3DBuildingColor+ const threeDBuildingColor: RGBColor = state.custom3DBuildingColor? state.threeDBuildingColor: get3DBuildingColor(state.mapStyles[styleType]);@@ -286,10 +350,12 @@ export const mapStyleChangeUpdater = (state, {payload: styleType}) => {/*** Callback when load map style success* @memberof mapStyleUpdaters- * @type {typeof import('./map-style-updaters').loadMapStylesUpdater}* @public*/-export const loadMapStylesUpdater = (state, action) => {+export const loadMapStylesUpdater = (+ state: MapStyle,+ action: MapStyleActions.LoadMapStylesUpdaterAction+): MapStyle => {const newStyles = action.payload || {};const addLayerGroups = Object.keys(newStyles).reduce((accu, id) => ({@@ -319,18 +385,22 @@ export const loadMapStylesUpdater = (state, action) => {/*** Callback when load map style error* @memberof mapStyleUpdaters- * @type {typeof import('./map-style-updaters').loadMapStyleErrUpdater}* @public*/// do nothing for now, if didn't load, skip it-export const loadMapStyleErrUpdater = state => state;+export const loadMapStyleErrUpdater = (+ state: MapStyle,+ action: MapStyleActions.LoadMapStyleErrUpdaterAction+): MapStyle => state;/*** @memberof mapStyleUpdaters- * @type {typeof import('./map-style-updaters').requestMapStylesUpdater}* @public*/-export const requestMapStylesUpdater = (state, {payload: mapStyles}) => {+export const requestMapStylesUpdater = (+ state: MapStyle,+ {payload: mapStyles}: MapStyleActions.RequestMapStylesUpdaterAction+): MapStyle => {const loadMapStyleTasks = getLoadMapStyleTasks(mapStyles,state.mapboxApiAccessToken,@@ -346,9 +416,16 @@ export const requestMapStylesUpdater = (state, {payload: mapStyles}) => {* @param action* @param action.payload saved map config `{mapStyle, visState, mapState}`* @returns nextState or `react-pam` tasks to load map style object- * @type {typeof import('./map-style-updaters').receiveMapConfigUpdater}*/-export const receiveMapConfigUpdater = (state, {payload: {config = {}}}) => {+export const receiveMapConfigUpdater = (+ state: MapStyle,+ {+ payload: {config}+ }: {+ type?: typeof ActionTypes.RECEIVE_MAP_CONFIG;+ payload: ReceiveMapConfigPayload;+ }+): MapStyle => {const {mapStyle} = config || {};if (!mapStyle) {@@ -372,7 +449,9 @@ export const receiveMapConfigUpdater = (state, {payload: {config = {}}}) => {: mapStyle;// set custom3DBuildingColor: true if mapStyle contains threeDBuildingColor+ // @ts-expect-errormerged.custom3DBuildingColor =+ // @ts-expect-errorBoolean(mapStyle.threeDBuildingColor) || merged.custom3DBuildingColor;const newState = mapConfigChangeUpdater(state, {payload: merged});@@ -383,6 +462,7 @@ function getLoadMapStyleTasks(mapStyles, mapboxApiAccessToken, mapboxApiUrl) {return [Task.all(Object.values(mapStyles)+ // @ts-expect-error.map(({id, url, accessToken}) => ({id,url: isValidStyleUrl(url)@@ -415,10 +495,9 @@ function getLoadMapStyleTasks(mapStyles, mapboxApiAccessToken, mapboxApiUrl) {* @memberof mapStyleUpdaters* @param state `mapStyle`* @returns nextState- * @type {typeof import('./map-style-updaters').resetMapConfigMapStyleUpdater}* @public*/-export const resetMapConfigMapStyleUpdater = state => {+export const resetMapConfigMapStyleUpdater = (state: MapStyle): MapStyle => {const emptyConfig = {...INITIAL_MAP_STYLE,mapboxApiAccessToken: state.mapboxApiAccessToken,@@ -435,19 +514,24 @@ export const resetMapConfigMapStyleUpdater = state => {/*** Callback when a custom map style object is received* @memberof mapStyleUpdaters- * @type {typeof import('./map-style-updaters').loadCustomMapStyleUpdater}* @public*/-export const loadCustomMapStyleUpdater = (state, {payload: {icon, style, error}}) => ({+export const loadCustomMapStyleUpdater = (+ state: MapStyle,+ {payload: {icon, style, error}}: MapStyleActions.LoadCustomMapStyleUpdaterAction+): MapStyle => ({...state,+ // @ts-expect-errorinputStyle: {...state.inputStyle,// style json and icon will load asynchronously...(style? {+ // @ts-expect-errorid: style.id || generateHashId(),// make a copy of the style objectstyle: cloneDeep(style),+ // @ts-expect-errorlabel: style.name,// gathering layer group info from style jsonlayerGroups: getLayerGroupsFromStyle(style)@@ -461,10 +545,12 @@ export const loadCustomMapStyleUpdater = (state, {payload: {icon, style, error}}/*** Input a custom map style object* @memberof mapStyleUpdaters- * @type {typeof import('./map-style-updaters').inputMapStyleUpdater}* @public*/-export const inputMapStyleUpdater = (state, {payload: {inputStyle, mapState}}) => {+export const inputMapStyleUpdater = (+ state: MapStyle,+ {payload: {inputStyle, mapState}}: MapStyleActions.InputMapStyleUpdaterAction+): MapStyle => {const updated = {...state.inputStyle,...inputStyle@@ -474,7 +560,9 @@ export const inputMapStyleUpdater = (state, {payload: {inputStyle, mapState}}) =const icon = isValid? getStyleImageIcon({mapState,+ // @ts-expect-errorstyleUrl: updated.url,+ // @ts-expect-errormapboxApiAccessToken: updated.accessToken || state.mapboxApiAccessToken,mapboxApiUrl: state.mapboxApiUrl || DEFAULT_MAPBOX_API_URL})@@ -495,9 +583,9 @@ export const inputMapStyleUpdater = (state, {payload: {inputStyle, mapState}}) =* This action is called when user click confirm after putting in a valid style url in the custom map style dialog.* It should not be called from outside kepler.gl without a valid `inputStyle` in the `mapStyle` reducer.* @memberof mapStyleUpdaters- * @type {typeof import('./map-style-updaters').addCustomMapStyleUpdater}*/-export const addCustomMapStyleUpdater = state => {+export const addCustomMapStyleUpdater = (state: MapStyle): MapStyle => {+ // @ts-expect-errorconst styleId = state.inputStyle.id;const newState = {...state,@@ -515,9 +603,11 @@ export const addCustomMapStyleUpdater = state => {/*** Updates 3d building color* @memberof mapStyleUpdaters- * @type {typeof import('./map-style-updaters').set3dBuildingColorUpdater}*/-export const set3dBuildingColorUpdater = (state, {payload: color}) => ({+export const set3dBuildingColorUpdater = (+ state: MapStyle,+ {payload: color}: MapStyleActions.Set3dBuildingColorUpdaterAction+): MapStyle => ({...state,threeDBuildingColor: color,custom3DBuildingColor: truediff --git a/src/reducers/map-style.js b/src/reducers/map-style.tssimilarity index 99%rename from src/reducers/map-style.jsrename to src/reducers/map-style.tsindex 30fdcbec3..51e7717e2 100644--- a/src/reducers/map-style.js+++ b/src/reducers/map-style.ts@@ -43,7 +43,6 @@ const actionHandler = {};export const mapStyleReducerFactory = (initialState = {}) =>- // @ts-ignorehandleActions(actionHandler, {...mapStyleUpdaters.INITIAL_MAP_STYLE,...initialState,