From f6431f85447b88c686e5be5ddc3465f19d4bba4b Mon Sep 17 00:00:00 2001 From: Victor Lin <13424970+victorlin@users.noreply.github.com> Date: Tue, 10 Oct 2023 17:26:54 -0700 Subject: [PATCH] controls: Add types used in panel-toggles Previously, the controls state could not be implicitly typed. This starts things off for further adoption. --- src/reducers/{controls.js => controls.ts} | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) rename src/reducers/{controls.js => controls.ts} (95%) diff --git a/src/reducers/controls.js b/src/reducers/controls.ts similarity index 95% rename from src/reducers/controls.js rename to src/reducers/controls.ts index 0691f3cba..58b20293f 100644 --- a/src/reducers/controls.js +++ b/src/reducers/controls.ts @@ -12,11 +12,22 @@ import { calcBrowserDimensionsInitialState } from "./browserDimensions"; import { doesColorByHaveConfidence } from "../actions/recomputeReduxState"; import { hasMultipleGridPanels } from "../actions/panelDisplay"; +export interface ControlsState { + panelsAvailable: string[] + panelsToDisplay: string[] + showTreeToo: boolean + canTogglePanelLayout: boolean + + // This allows arbitrary prop names while TypeScript adoption is incomplete. + // TODO: add all other props explicitly and remove this. + [propName: string]: any; +} + /* defaultState is a fn so that we can re-create it at any time, e.g. if we want to revert things (e.g. on dataset change) */ export const getDefaultControlsState = () => { - const defaults = { + const defaults: Partial = { distanceMeasure: defaultDistanceMeasure, layout: defaultLayout, geoResolution: defaultGeoResolution, @@ -80,7 +91,7 @@ export const getDefaultControlsState = () => { panelsToDisplay: [], panelLayout: calcBrowserDimensionsInitialState().width > twoColumnBreakpoint ? "grid" : "full", tipLabelKey: defaults.tipLabelKey, - showTreeToo: undefined, + showTreeToo: false, showTangle: false, zoomMin: undefined, zoomMax: undefined, @@ -102,7 +113,7 @@ export const getDefaultControlsState = () => { /* while this may change, div currently doesn't have CIs, so they shouldn't be displayed. */ export const shouldDisplayTemporalConfidence = (exists, distMeasure, layout) => exists && distMeasure === "num_date" && layout === "rect"; -const Controls = (state = getDefaultControlsState(), action) => { +const Controls = (state: ControlsState = getDefaultControlsState(), action): ControlsState => { switch (action.type) { case types.URL_QUERY_CHANGE_WITH_COMPUTED_STATE: /* fallthrough */ case types.CLEAN_START: @@ -142,7 +153,7 @@ const Controls = (state = getDefaultControlsState(), action) => { }) }); case types.CHANGE_DISTANCE_MEASURE: { - const updatesToState = { + const updatesToState: Partial = { distanceMeasure: action.data, branchLengthsToDisplay: state.branchLengthsToDisplay }; @@ -161,7 +172,7 @@ const Controls = (state = getDefaultControlsState(), action) => { return Object.assign({}, state, updatesToState); } case types.CHANGE_DATES_VISIBILITY_THICKNESS: { - const newDates = { quickdraw: action.quickdraw }; + const newDates: Partial = { quickdraw: action.quickdraw }; if (action.dateMin) { newDates.dateMin = action.dateMin; newDates.dateMinNumeric = action.dateMinNumeric; @@ -257,7 +268,7 @@ const Controls = (state = getDefaultControlsState(), action) => { }); case types.REMOVE_TREE_TOO: return Object.assign({}, state, { - showTreeToo: undefined, + showTreeToo: false, showTangle: false, canTogglePanelLayout: hasMultipleGridPanels(state.panelsAvailable), panelsToDisplay: state.panelsAvailable.slice()