You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ma...@apache.org on 2025/07/14 17:09:46 UTC
(superset) branch ts_migrate_claude updated (f984cb27a1 -> 04ba0a00b3)
This is an automated email from the ASF dual-hosted git repository.
maximebeauchemin pushed a change to branch ts_migrate_claude
in repository https://gitbox.apache.org/repos/asf/superset.git
discard f984cb27a1 vibing
discard 0da58c93f3 chore: migrating to ts using Claude code automation
new 04ba0a00b3 src/utils
This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version. This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:
* -- * -- B -- O -- O -- O (f984cb27a1)
\
N -- N -- N refs/heads/ts_migrate_claude (04ba0a00b3)
You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.
Any revisions marked "omit" are not gone; other references still
refer to them. Any revisions marked "discard" are gone forever.
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../generators/app/{index.ts => index.js} | 0
.../generators/plugin-chart/{index.ts => index.js} | 0
.../templates/test}/__mocks__/mockExportString.js | 0
.../templates/test/__mocks__/mockExportString.ts | 19 --
.../{jest.config.ts => jest.config.js} | 0
.../test/{app.test.ts => app.test.js} | 0
.../{plugin-chart.test.ts => plugin-chart.test.js} | 0
.../__mocks__/mockExportObject.js | 0
.../superset-ui-core/__mocks__/mockExportObject.ts | 19 --
.../__mocks__/mockExportString.js | 0
.../superset-ui-core/__mocks__/mockExportString.ts | 19 --
...hange.test.tsx => ConfirmStatusChange.test.jsx} | 0
...ut.test.tsx => LabeledErrorBoundInput.test.jsx} | 8 +-
.../{IconTooltip.test.tsx => IconTooltip.test.jsx} | 0
...ampleApp.stories.tsx => ExampleApp.stories.jsx} | 0
.../Arc/{payload.ts => payload.js} | 0
.../Grid/{payload.ts => payload.js} | 0
.../Hex/{payload.ts => payload.js} | 0
.../Path/{payload.ts => payload.js} | 0
.../{geojsonPayload.ts => geojsonPayload.js} | 0
.../Polygon/{payload.ts => payload.js} | 0
.../Scatter/{payload.ts => payload.js} | 0
.../Screengrid/{payload.ts => payload.js} | 0
.../src/{index.ts => index.js} | 10 +-
.../src/{transformProps.ts => transformProps.js} | 23 +-
.../src/utils/{geo.ts => geo.js} | 6 +-
.../{luminanceFromRGB.ts => luminanceFromRGB.js} | 6 +-
.../src/utils/roundDecimal.js} | 19 +-
.../src/utils/roundDecimal.ts | 27 --
.../{roundDecimal.test.ts => roundDecimal.test.js} | 0
.../Chart/{chartAction.ts => chartAction.js} | 107 +++-----
.../components/Datasource/{utils.ts => utils.js} | 36 +--
.../ErrorMessage/MarshmallowErrorMessage.tsx | 2 +-
.../ErrorMessage/OAuth2RedirectMessage.tsx | 2 +-
.../{ListView.test.tsx => ListView.test.jsx} | 6 +-
.../src/components/ListView/ListView.test.tsx | 226 ++--------------
.../{mockMessageToasts.ts => mockMessageToasts.js} | 0
.../MessageToasts/{reducers.ts => reducers.js} | 22 +-
.../components/gridComponents/Tab.test.jsx | 141 ++++++++++
.../components/gridComponents/Tabs.test.jsx | 195 ++++++++++++++
...shboardState.test.ts => dashboardState.test.js} | 0
.../src/dashboard/reducers/dashboardState.test.ts | 283 ++++++++-------------
.../DataTablesPane/components/SamplesPane.tsx | 9 +-
.../DatasourceControl/DatasourceControl.test.jsx | 169 ++++++++++++
.../VizTypeControl/VizTypeControl.test.jsx | 130 ++++++++++
...cedMessageQueue.js => DebouncedMessageQueue.ts} | 26 +-
.../src/utils/{common.test.jsx => common.test.tsx} | 0
.../src/utils/{common.js => common.ts} | 27 +-
.../{datasourceUtils.js => datasourceUtils.ts} | 16 +-
.../src/utils/getControlsForVizType.js | 52 ----
...zType.test.js => getControlsForVizType.test.ts} | 0
.../src/utils/getControlsForVizType.ts | 58 +++++
.../{hostNamesConfig.js => hostNamesConfig.ts} | 6 +-
.../src/utils/{reducerUtils.js => reducerUtils.ts} | 38 ++-
54 files changed, 984 insertions(+), 723 deletions(-)
rename superset-frontend/packages/generator-superset/generators/app/{index.ts => index.js} (100%)
rename superset-frontend/packages/generator-superset/generators/plugin-chart/{index.ts => index.js} (100%)
copy superset-frontend/{spec => packages/generator-superset/generators/plugin-chart/templates/test}/__mocks__/mockExportString.js (100%)
delete mode 100644 superset-frontend/packages/generator-superset/generators/plugin-chart/templates/test/__mocks__/mockExportString.ts
rename superset-frontend/packages/generator-superset/{jest.config.ts => jest.config.js} (100%)
rename superset-frontend/packages/generator-superset/test/{app.test.ts => app.test.js} (100%)
rename superset-frontend/packages/generator-superset/test/{plugin-chart.test.ts => plugin-chart.test.js} (100%)
copy superset-frontend/{spec => packages/superset-ui-core}/__mocks__/mockExportObject.js (100%)
delete mode 100644 superset-frontend/packages/superset-ui-core/__mocks__/mockExportObject.ts
copy superset-frontend/{spec => packages/superset-ui-core}/__mocks__/mockExportString.js (100%)
delete mode 100644 superset-frontend/packages/superset-ui-core/__mocks__/mockExportString.ts
rename superset-frontend/packages/superset-ui-core/src/components/ConfirmStatusChange/{ConfirmStatusChange.test.tsx => ConfirmStatusChange.test.jsx} (100%)
rename superset-frontend/packages/superset-ui-core/src/components/Form/{LabeledErrorBoundInput.test.tsx => LabeledErrorBoundInput.test.jsx} (94%)
rename superset-frontend/packages/superset-ui-core/src/components/IconTooltip/{IconTooltip.test.tsx => IconTooltip.test.jsx} (100%)
rename superset-frontend/packages/superset-ui-demo/storybook/stories/{ExampleApp.stories.tsx => ExampleApp.stories.jsx} (100%)
rename superset-frontend/packages/superset-ui-demo/storybook/stories/plugins/legacy-preset-chart-deckgl/Arc/{payload.ts => payload.js} (100%)
rename superset-frontend/packages/superset-ui-demo/storybook/stories/plugins/legacy-preset-chart-deckgl/Grid/{payload.ts => payload.js} (100%)
rename superset-frontend/packages/superset-ui-demo/storybook/stories/plugins/legacy-preset-chart-deckgl/Hex/{payload.ts => payload.js} (100%)
rename superset-frontend/packages/superset-ui-demo/storybook/stories/plugins/legacy-preset-chart-deckgl/Path/{payload.ts => payload.js} (100%)
rename superset-frontend/packages/superset-ui-demo/storybook/stories/plugins/legacy-preset-chart-deckgl/Polygon/{geojsonPayload.ts => geojsonPayload.js} (100%)
rename superset-frontend/packages/superset-ui-demo/storybook/stories/plugins/legacy-preset-chart-deckgl/Polygon/{payload.ts => payload.js} (100%)
rename superset-frontend/packages/superset-ui-demo/storybook/stories/plugins/legacy-preset-chart-deckgl/Scatter/{payload.ts => payload.js} (100%)
rename superset-frontend/packages/superset-ui-demo/storybook/stories/plugins/legacy-preset-chart-deckgl/Screengrid/{payload.ts => payload.js} (100%)
rename superset-frontend/plugins/legacy-plugin-chart-map-box/src/{index.ts => index.js} (80%)
rename superset-frontend/plugins/legacy-plugin-chart-map-box/src/{transformProps.ts => transformProps.js} (83%)
rename superset-frontend/plugins/legacy-plugin-chart-map-box/src/utils/{geo.ts => geo.js} (92%)
rename superset-frontend/plugins/legacy-plugin-chart-map-box/src/utils/{luminanceFromRGB.ts => luminanceFromRGB.js} (92%)
copy superset-frontend/{packages/superset-ui-core/src/components/FaveStar/types.ts => plugins/legacy-plugin-chart-map-box/src/utils/roundDecimal.js} (77%)
delete mode 100644 superset-frontend/plugins/legacy-plugin-chart-map-box/src/utils/roundDecimal.ts
rename superset-frontend/plugins/legacy-plugin-chart-map-box/test/utils/{roundDecimal.test.ts => roundDecimal.test.js} (100%)
rename superset-frontend/src/components/Chart/{chartAction.ts => chartAction.js} (88%)
rename superset-frontend/src/components/Datasource/{utils.ts => utils.js} (84%)
copy superset-frontend/src/components/ListView/{ListView.test.tsx => ListView.test.jsx} (97%)
rename superset-frontend/src/components/MessageToasts/{mockMessageToasts.ts => mockMessageToasts.js} (100%)
rename superset-frontend/src/components/MessageToasts/{reducers.ts => reducers.js} (75%)
create mode 100644 superset-frontend/src/dashboard/components/gridComponents/Tab.test.jsx
create mode 100644 superset-frontend/src/dashboard/components/gridComponents/Tabs.test.jsx
copy superset-frontend/src/dashboard/reducers/{dashboardState.test.ts => dashboardState.test.js} (100%)
create mode 100644 superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.jsx
create mode 100644 superset-frontend/src/explore/components/controls/VizTypeControl/VizTypeControl.test.jsx
rename superset-frontend/src/utils/{DebouncedMessageQueue.js => DebouncedMessageQueue.ts} (73%)
rename superset-frontend/src/utils/{common.test.jsx => common.test.tsx} (100%)
rename superset-frontend/src/utils/{common.js => common.ts} (85%)
rename superset-frontend/src/utils/{datasourceUtils.js => datasourceUtils.ts} (76%)
delete mode 100644 superset-frontend/src/utils/getControlsForVizType.js
rename superset-frontend/src/utils/{getControlsForVizType.test.js => getControlsForVizType.test.ts} (100%)
create mode 100644 superset-frontend/src/utils/getControlsForVizType.ts
rename superset-frontend/src/utils/{hostNamesConfig.js => hostNamesConfig.ts} (91%)
rename superset-frontend/src/utils/{reducerUtils.js => reducerUtils.ts} (72%)
(superset) 01/01: src/utils
Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
maximebeauchemin pushed a commit to branch ts_migrate_claude
in repository https://gitbox.apache.org/repos/asf/superset.git
commit 04ba0a00b37b837558e9a10a47339b32f95f5561
Author: Maxime Beauchemin <ma...@gmail.com>
AuthorDate: Sun Jul 13 11:22:22 2025 -0700
src/utils
---
...cedMessageQueue.js => DebouncedMessageQueue.ts} | 26 ++++++++--
.../src/utils/{common.test.jsx => common.test.tsx} | 0
.../src/utils/{common.js => common.ts} | 27 ++++++----
.../{datasourceUtils.js => datasourceUtils.ts} | 16 +++++-
.../src/utils/getControlsForVizType.js | 52 -------------------
...zType.test.js => getControlsForVizType.test.ts} | 0
.../src/utils/getControlsForVizType.ts | 58 ++++++++++++++++++++++
.../{hostNamesConfig.js => hostNamesConfig.ts} | 6 +--
.../src/utils/{reducerUtils.js => reducerUtils.ts} | 38 ++++++++++++--
9 files changed, 147 insertions(+), 76 deletions(-)
diff --git a/superset-frontend/src/utils/DebouncedMessageQueue.js b/superset-frontend/src/utils/DebouncedMessageQueue.ts
similarity index 73%
rename from superset-frontend/src/utils/DebouncedMessageQueue.js
rename to superset-frontend/src/utils/DebouncedMessageQueue.ts
index 6fd6c5b778..cf1550ad75 100644
--- a/superset-frontend/src/utils/DebouncedMessageQueue.js
+++ b/superset-frontend/src/utils/DebouncedMessageQueue.ts
@@ -18,33 +18,49 @@
*/
import { debounce } from 'lodash';
+interface DebouncedMessageQueueConfig {
+ callback?: (events: any[]) => void;
+ sizeThreshold?: number;
+ delayThreshold?: number;
+}
+
class DebouncedMessageQueue {
+ private queue: any[];
+
+ private sizeThreshold: number;
+
+ private delayThreshold: number;
+
+ private callback: (events: any[]) => void;
+
+ private trigger: () => void;
+
constructor({
callback = () => {},
sizeThreshold = 1000,
delayThreshold = 1000,
- }) {
+ }: DebouncedMessageQueueConfig = {}) {
this.queue = [];
this.sizeThreshold = sizeThreshold;
this.delayThreshold = delayThreshold;
- this.trigger = debounce(this.trigger.bind(this), this.delayThreshold);
+ this.trigger = debounce(this.triggerQueue.bind(this), this.delayThreshold);
this.callback = callback;
}
- append(eventData) {
+ append(eventData: any): void {
this.queue.push(eventData);
this.trigger();
}
- trigger() {
+ private triggerQueue(): void {
if (this.queue.length > 0) {
const events = this.queue.splice(0, this.sizeThreshold);
this.callback.call(null, events);
// If there are remaining items, call it again.
if (this.queue.length > 0) {
- this.trigger();
+ this.triggerQueue();
}
}
}
diff --git a/superset-frontend/src/utils/common.test.jsx b/superset-frontend/src/utils/common.test.tsx
similarity index 100%
rename from superset-frontend/src/utils/common.test.jsx
rename to superset-frontend/src/utils/common.test.tsx
diff --git a/superset-frontend/src/utils/common.js b/superset-frontend/src/utils/common.ts
similarity index 85%
rename from superset-frontend/src/utils/common.js
rename to superset-frontend/src/utils/common.ts
index 4ceceb0b41..ad51161565 100644
--- a/superset-frontend/src/utils/common.js
+++ b/superset-frontend/src/utils/common.ts
@@ -21,6 +21,7 @@ import {
getTimeFormatter,
TimeFormats,
ensureIsArray,
+ JsonObject,
} from '@superset-ui/core';
// ATTENTION: If you change any constants, make sure to also change constants.py
@@ -36,7 +37,7 @@ export const SHORT_TIME = 'h:m a';
const DATETIME_FORMATTER = getTimeFormatter(TimeFormats.DATABASE_DATETIME);
-export function storeQuery(query) {
+export function storeQuery(query: JsonObject): Promise<string> {
return SupersetClient.post({
endpoint: '/kv/store/',
postPayload: { data: query },
@@ -47,7 +48,7 @@ export function storeQuery(query) {
});
}
-export function optionLabel(opt) {
+export function optionLabel(opt: any): string {
if (opt === null) {
return NULL_STRING;
}
@@ -66,23 +67,26 @@ export function optionLabel(opt) {
return opt;
}
-export function optionValue(opt) {
+export function optionValue(opt: any): any {
if (opt === null) {
return NULL_STRING;
}
return opt;
}
-export function optionFromValue(opt) {
+export function optionFromValue(opt: any): { value: any; label: string } {
// From a list of options, handles special values & labels
return { value: optionValue(opt), label: optionLabel(opt) };
}
-function getColumnName(column) {
+function getColumnName(column: any): string {
return column.name || column;
}
-export function prepareCopyToClipboardTabularData(data, columns) {
+export function prepareCopyToClipboardTabularData(
+ data: JsonObject[],
+ columns: any[],
+): string {
let result = columns.length
? `${columns.map(getColumnName).join('\t')}\n`
: '';
@@ -103,7 +107,10 @@ export function prepareCopyToClipboardTabularData(data, columns) {
return result;
}
-export function applyFormattingToTabularData(data, timeFormattedColumns) {
+export function applyFormattingToTabularData(
+ data: JsonObject[],
+ timeFormattedColumns: string[],
+): JsonObject[] {
if (
!data ||
data.length === 0 ||
@@ -124,10 +131,10 @@ export function applyFormattingToTabularData(data, timeFormattedColumns) {
}));
}
-export const noOp = () => undefined;
+export const noOp = (): undefined => undefined;
// Detects the user's OS through the browser
-export const detectOS = () => {
+export const detectOS = (): string => {
const { appVersion } = navigator;
// Leveraging this condition because of stackOverflow
@@ -140,7 +147,7 @@ export const detectOS = () => {
return 'Unknown OS';
};
-export const isSafari = () => {
+export const isSafari = (): boolean => {
const { userAgent } = navigator;
return userAgent && /^((?!chrome|android).)*safari/i.test(userAgent);
diff --git a/superset-frontend/src/utils/datasourceUtils.js b/superset-frontend/src/utils/datasourceUtils.ts
similarity index 76%
rename from superset-frontend/src/utils/datasourceUtils.js
rename to superset-frontend/src/utils/datasourceUtils.ts
index 144a3ff88b..20ab4329bb 100644
--- a/superset-frontend/src/utils/datasourceUtils.js
+++ b/superset-frontend/src/utils/datasourceUtils.ts
@@ -16,7 +16,21 @@
* specific language governing permissions and limitations
* under the License.
*/
-export const getDatasourceAsSaveableDataset = source => ({
+import { Dataset } from '@superset-ui/chart-controls';
+
+interface SaveableDataset {
+ columns: any[];
+ name: string;
+ dbId?: number;
+ sql: string;
+ catalog?: string | null;
+ schema?: string;
+ templateParams?: string;
+}
+
+export const getDatasourceAsSaveableDataset = (
+ source: Partial<Dataset>,
+): SaveableDataset => ({
columns: source.columns,
name: source?.datasource_name || source?.name || 'Untitled',
dbId: source?.database?.id || source?.dbId,
diff --git a/superset-frontend/src/utils/getControlsForVizType.js b/superset-frontend/src/utils/getControlsForVizType.js
deleted file mode 100644
index 8771d91dc7..0000000000
--- a/superset-frontend/src/utils/getControlsForVizType.js
+++ /dev/null
@@ -1,52 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import memoizeOne from 'memoize-one';
-import { isControlPanelSectionConfig } from '@superset-ui/chart-controls';
-import { getChartControlPanelRegistry } from '@superset-ui/core';
-import { controls } from '../explore/controls';
-
-const memoizedControls = memoizeOne((vizType, controlPanel) => {
- const controlsMap = {};
- (controlPanel?.controlPanelSections || [])
- .filter(isControlPanelSectionConfig)
- .forEach(section => {
- section.controlSetRows.forEach(row => {
- row.forEach(control => {
- if (!control) return;
- if (typeof control === 'string') {
- // For now, we have to look in controls.jsx to get the config for some controls.
- // Once everything is migrated out, delete this if statement.
- controlsMap[control] = controls[control];
- } else if (control.name && control.config) {
- // condition needed because there are elements, e.g. <hr /> in some control configs (I'm looking at you, FilterBox!)
- controlsMap[control.name] = control.config;
- }
- });
- });
- });
- return controlsMap;
-});
-
-const getControlsForVizType = vizType => {
- const controlPanel = getChartControlPanelRegistry().get(vizType);
- return memoizedControls(vizType, controlPanel);
-};
-
-export default getControlsForVizType;
diff --git a/superset-frontend/src/utils/getControlsForVizType.test.js b/superset-frontend/src/utils/getControlsForVizType.test.ts
similarity index 100%
rename from superset-frontend/src/utils/getControlsForVizType.test.js
rename to superset-frontend/src/utils/getControlsForVizType.test.ts
diff --git a/superset-frontend/src/utils/getControlsForVizType.ts b/superset-frontend/src/utils/getControlsForVizType.ts
new file mode 100644
index 0000000000..c77ca77d2e
--- /dev/null
+++ b/superset-frontend/src/utils/getControlsForVizType.ts
@@ -0,0 +1,58 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import memoizeOne from 'memoize-one';
+import {
+ isControlPanelSectionConfig,
+ ControlStateMapping,
+ ControlPanelConfig,
+} from '@superset-ui/chart-controls';
+import { getChartControlPanelRegistry } from '@superset-ui/core';
+import { controls } from '../explore/controls';
+
+const memoizedControls = memoizeOne(
+ (vizType: string, controlPanel: ControlPanelConfig): ControlStateMapping => {
+ const controlsMap: ControlStateMapping = {};
+ (controlPanel?.controlPanelSections || [])
+ .filter(isControlPanelSectionConfig)
+ .forEach(section => {
+ section.controlSetRows.forEach(row => {
+ row.forEach(control => {
+ if (!control) return;
+ if (typeof control === 'string') {
+ // For now, we have to look in controls.jsx to get the config for some controls.
+ // Once everything is migrated out, delete this if statement.
+ controlsMap[control] = controls[control];
+ } else if (control.name && control.config) {
+ // condition needed because there are elements, e.g. <hr /> in some control configs (I'm looking at you, FilterBox!)
+ controlsMap[control.name] = control.config;
+ }
+ });
+ });
+ });
+ return controlsMap;
+ },
+);
+
+const getControlsForVizType = (vizType: string): ControlStateMapping => {
+ const controlPanel = getChartControlPanelRegistry().get(vizType);
+ return memoizedControls(vizType, controlPanel);
+};
+
+export default getControlsForVizType;
diff --git a/superset-frontend/src/utils/hostNamesConfig.js b/superset-frontend/src/utils/hostNamesConfig.ts
similarity index 91%
rename from superset-frontend/src/utils/hostNamesConfig.js
rename to superset-frontend/src/utils/hostNamesConfig.ts
index 2ba9b7555c..f2359f2e84 100644
--- a/superset-frontend/src/utils/hostNamesConfig.js
+++ b/superset-frontend/src/utils/hostNamesConfig.ts
@@ -19,7 +19,7 @@
import { initFeatureFlags } from '@superset-ui/core';
import getBootstrapData from './getBootstrapData';
-function getDomainsConfig() {
+function getDomainsConfig(): string[] {
const appContainer = document.getElementById('app');
if (!appContainer) {
return [];
@@ -49,6 +49,6 @@ function getDomainsConfig() {
return Array.from(availableDomains);
}
-export const availableDomains = getDomainsConfig();
+export const availableDomains: string[] = getDomainsConfig();
-export const allowCrossDomain = availableDomains.length > 1;
+export const allowCrossDomain: boolean = availableDomains.length > 1;
diff --git a/superset-frontend/src/utils/reducerUtils.js b/superset-frontend/src/utils/reducerUtils.ts
similarity index 72%
rename from superset-frontend/src/utils/reducerUtils.js
rename to superset-frontend/src/utils/reducerUtils.ts
index 199c8aa292..300f878d94 100644
--- a/superset-frontend/src/utils/reducerUtils.js
+++ b/superset-frontend/src/utils/reducerUtils.ts
@@ -18,7 +18,16 @@
*/
import { nanoid } from 'nanoid';
-export function addToObject(state, arrKey, obj) {
+interface StateWithId {
+ id?: string;
+ [key: string]: any;
+}
+
+export function addToObject<T extends Record<string, any>>(
+ state: T,
+ arrKey: keyof T,
+ obj: StateWithId,
+): T {
const newObject = { ...state[arrKey] };
const copiedObject = { ...obj };
@@ -29,13 +38,23 @@ export function addToObject(state, arrKey, obj) {
return { ...state, [arrKey]: newObject };
}
-export function alterInObject(state, arrKey, obj, alterations) {
+export function alterInObject<T extends Record<string, any>>(
+ state: T,
+ arrKey: keyof T,
+ obj: StateWithId,
+ alterations: Record<string, any>,
+): T {
const newObject = { ...state[arrKey] };
newObject[obj.id] = { ...newObject[obj.id], ...alterations };
return { ...state, [arrKey]: newObject };
}
-export function alterInArr(state, arrKey, obj, alterations) {
+export function alterInArr<T extends Record<string, any>>(
+ state: T,
+ arrKey: keyof T,
+ obj: StateWithId,
+ alterations: Record<string, any>,
+): T {
// Finds an item in an array in the state and replaces it with a
// new object with an altered property
const idKey = 'id';
@@ -50,7 +69,12 @@ export function alterInArr(state, arrKey, obj, alterations) {
return { ...state, [arrKey]: newArr };
}
-export function removeFromArr(state, arrKey, obj, idKey = 'id') {
+export function removeFromArr<T extends Record<string, any>>(
+ state: T,
+ arrKey: keyof T,
+ obj: StateWithId,
+ idKey: string = 'id',
+): T {
const newArr = [];
state[arrKey].forEach(arrItem => {
if (!(obj[idKey] === arrItem[idKey])) {
@@ -60,7 +84,11 @@ export function removeFromArr(state, arrKey, obj, idKey = 'id') {
return { ...state, [arrKey]: newArr };
}
-export function addToArr(state, arrKey, obj) {
+export function addToArr<T extends Record<string, any>>(
+ state: T,
+ arrKey: keyof T,
+ obj: StateWithId,
+): T {
const newObj = { ...obj };
if (!newObj.id) {
newObj.id = nanoid();