Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Convert metabase/hoc files to TypeScript (#75070)

Andrei Chernykh89969fff33dc
diff --git a/frontend/src/metabase/hoc/ModalRoute.unit.spec.js b/frontend/src/metabase/hoc/ModalRoute.unit.spec.ts
similarity index 89%
rename from frontend/src/metabase/hoc/ModalRoute.unit.spec.js
rename to frontend/src/metabase/hoc/ModalRoute.unit.spec.ts
index 3a15a49ccde..b8d5be741fa 100644
--- a/frontend/src/metabase/hoc/ModalRoute.unit.spec.js
+++ b/frontend/src/metabase/hoc/ModalRoute.unit.spec.ts
@@ -1,13 +1,17 @@
import { mockSettings } from "__support__/settings";
+import { createMockLocation } from "metabase/redux/store/mocks";
import { getParentPath } from "./ModalRoute";
-const setup = (routePath, locationPath, siteURL = undefined) => {
+const setup = (routePath: string, locationPath: string, siteURL?: string) => {
if (siteURL) {
mockSettings({ "site-url": siteURL });
}
- return getParentPath({ path: routePath }, { pathname: locationPath });
+ return getParentPath(
+ { path: routePath },
+ createMockLocation({ pathname: locationPath }),
+ );
};
describe("getParentPath", () => {
diff --git a/frontend/src/metabase/hoc/Remapped.jsx b/frontend/src/metabase/hoc/Remapped.jsx
deleted file mode 100644
index e3b6bf2c77f..00000000000
--- a/frontend/src/metabase/hoc/Remapped.jsx
+++ /dev/null
@@ -1,104 +0,0 @@
-/* eslint-disable react/prop-types */
-import { Component } from "react";
-
-import { connect } from "metabase/redux";
-import { fetchRemapping } from "metabase/redux/metadata";
-import { getMetadata } from "metabase/selectors/metadata";
-
-const mapStateToProps = (state, props) => ({
- metadata: getMetadata(state, props),
-});
-
-const mapDispatchToProps = {
- fetchRemapping,
-};
-
-/**
- * @deprecated HOCs are deprecated
- */
-// eslint-disable-next-line import/no-default-export -- deprecated usage
-export default (ComposedComponent) =>
- connect(
- mapStateToProps,
- mapDispatchToProps,
- )(
- class extends Component {
- static displayName =
- "Remapped[" +
- (ComposedComponent.displayName || ComposedComponent.name) +
- "]";
-
- state = { remapping: null };
-
- UNSAFE_componentWillMount() {
- this.fetchRemapping(this.props);
- }
-
- UNSAFE_componentWillReceiveProps(nextProps) {
- if (
- this.props.value !== nextProps.value ||
- this.props.column?.id !== nextProps.column?.id ||
- this.props.parameter?.id !== nextProps.parameter?.id ||
- this.props.cardId !== nextProps.cardId ||
- this.props.dashboardId !== nextProps.dashboardId ||
- this.props.uuid !== nextProps.uuid ||
- this.props.token !== nextProps.token
- ) {
- this.setState({ remapping: null });
- this.fetchRemapping(nextProps);
- }
- }
-
- async fetchRemapping(props) {
- const result = await props.fetchRemapping({
- parameter: props.parameter,
- value: props.value,
- field: props.column,
- cardId: props.cardId,
- dashboardId: props.dashboardId,
- uuid: props.uuid,
- token: props.token,
- });
-
- const remapping = result?.payload;
- if (remapping != null && props.value === this.props.value) {
- this.setState({ remapping });
- }
- }
-
- getDisplayValue(field, value) {
- const fieldDisplayValue = field && field.remappedValue(value);
- if (fieldDisplayValue != null) {
- return fieldDisplayValue;
- }
-
- const [, remappedLabel] = this.state.remapping ?? [];
- if (remappedLabel != null) {
- return remappedLabel;
- }
-
- return null;
- }
-
- render() {
- const { metadata, fetchRemapping, ...props } = this.props;
- // Read the field from metadata so we pick up remappings stored
- // asynchronously by fetchRemapping (e.g. card/question label columns);
- // the column passed in props can be a stale instance.
- const field =
- (props.column?.id != null && metadata?.field(props.column.id)) ||
- props.column;
- const displayValue = this.getDisplayValue(field, props.value);
- const displayColumn =
- (displayValue != null && field && field.remappedField()) || null;
-
- return (
- <ComposedComponent
- {...props}
- displayValue={displayValue}
- displayColumn={displayColumn}
- />
- );
- }
- },
- );
diff --git a/frontend/src/metabase/hoc/Remapped.tsx b/frontend/src/metabase/hoc/Remapped.tsx
new file mode 100644
index 00000000000..e1785c3e7dc
--- /dev/null
+++ b/frontend/src/metabase/hoc/Remapped.tsx
@@ -0,0 +1,145 @@
+import { Component, type ComponentType } from "react";
+
+import { connect } from "metabase/redux";
+import { fetchRemapping } from "metabase/redux/metadata";
+import type { State } from "metabase/redux/store";
+import { getMetadata } from "metabase/selectors/metadata";
+import type Field from "metabase-lib/v1/metadata/Field";
+import type Metadata from "metabase-lib/v1/metadata/Metadata";
+
+type Remapping = [value: unknown, label: unknown];
+
+interface RemappedOwnProps {
+ value?: unknown;
+ column?: Field;
+ parameter?: { id?: unknown };
+ cardId?: unknown;
+ dashboardId?: unknown;
+ uuid?: unknown;
+ token?: unknown;
+}
+
+interface RemappedStateProps {
+ metadata: Metadata;
+}
+
+interface RemappedDispatchProps {
+ // The connect-bound form of `fetchRemapping`; the thunk resolves to its payload.
+ fetchRemapping: (args: {
+ parameter?: { id?: unknown };
+ value?: unknown;
+ field?: Field;
+ cardId?: unknown;
+ dashboardId?: unknown;
+ uuid?: unknown;
+ token?: unknown;
+ }) => Promise<{ payload?: Remapping } | undefined>;
+}
+
+type RemappedClassProps = RemappedOwnProps &
+ RemappedStateProps &
+ RemappedDispatchProps;
+
+const mapStateToProps = (state: State): RemappedStateProps => ({
+ metadata: getMetadata(state),
+});
+
+const mapDispatchToProps = {
+ fetchRemapping,
+};
+
+/**
+ * @deprecated HOCs are deprecated
+ */
+// eslint-disable-next-line import/no-default-export -- deprecated usage
+export default (ComposedComponent: ComponentType<any>) => {
+ class RemappedComponent extends Component<
+ RemappedClassProps,
+ { remapping: Remapping | null }
+ > {
+ static displayName =
+ "Remapped[" +
+ (ComposedComponent.displayName || ComposedComponent.name) +
+ "]";
+
+ state: { remapping: Remapping | null } = { remapping: null };
+
+ UNSAFE_componentWillMount() {
+ this.fetchRemapping(this.props);
+ }
+
+ UNSAFE_componentWillReceiveProps(nextProps: RemappedClassProps) {
+ if (
+ this.props.value !== nextProps.value ||
+ this.props.column?.id !== nextProps.column?.id ||
+ this.props.parameter?.id !== nextProps.parameter?.id ||
+ this.props.cardId !== nextProps.cardId ||
+ this.props.dashboardId !== nextProps.dashboardId ||
+ this.props.uuid !== nextProps.uuid ||
+ this.props.token !== nextProps.token
+ ) {
+ this.setState({ remapping: null });
+ this.fetchRemapping(nextProps);
+ }
+ }
+
+ async fetchRemapping(props: RemappedClassProps) {
+ const result = await props.fetchRemapping({
+ parameter: props.parameter,
+ value: props.value,
+ field: props.column,
+ cardId: props.cardId,
+ dashboardId: props.dashboardId,
+ uuid: props.uuid,
+ token: props.token,
+ });
+
+ const remapping = result?.payload;
+ if (remapping != null && props.value === this.props.value) {
+ this.setState({ remapping });
+ }
+ }
+
+ getDisplayValue(field: Field | null | undefined, value: unknown) {
+ const fieldDisplayValue = field && field.remappedValue(value);
+ if (fieldDisplayValue != null) {
+ return fieldDisplayValue;
+ }
+
+ const [, remappedLabel] = this.state.remapping ?? [];
+ if (remappedLabel != null) {
+ return remappedLabel;
+ }
+
+ return null;
+ }
+
+ render() {
+ const { metadata, fetchRemapping, ...props } = this.props;
+ // Read the field from metadata so we pick up remappings stored
+ // asynchronously by fetchRemapping (e.g. card/question label columns);
+ // the column passed in props can be a stale instance.
+ const field =
+ (props.column?.id != null && metadata?.field(props.column.id)) ||
+ props.column;
+ const displayValue = this.getDisplayValue(field, props.value);
+ const displayColumn =
+ (displayValue != null && field && field.remappedField()) || null;
+
+ return (
+ <ComposedComponent
+ {...props}
+ displayValue={displayValue}
+ displayColumn={displayColumn}
+ />
+ );
+ }
+ }
+
+ // connect's class-component prop matching can't see through the dynamic
+ // passthrough props, so cast around it and keep the public surface permissive.
+ return connect(
+ mapStateToProps,
+ mapDispatchToProps,
+ )(RemappedComponent as ComponentType<any>) as unknown as ComponentType<any>;
+};
diff --git a/frontend/src/metabase/hoc/RenderPropToHOC.jsx b/frontend/src/metabase/hoc/RenderPropToHOC.jsx
deleted file mode 100644
index e6f46604654..00000000000
--- a/frontend/src/metabase/hoc/RenderPropToHOC.jsx
+++ /dev/null
@@ -1,9 +0,0 @@
-// eslint-disable-next-line import/no-default-export -- deprecated usage
-export default function renderPropToHoc(RenderPropComponent) {
- // eslint-disable-next-line react/display-name
- return (ComposedComponent) => (props) => (
- <RenderPropComponent {...props}>
- {(childrenProps) => <ComposedComponent {...props} {...childrenProps} />}
- </RenderPropComponent>
- );
-}
diff --git a/frontend/src/metabase/hoc/RenderPropToHOC.tsx b/frontend/src/metabase/hoc/RenderPropToHOC.tsx
new file mode 100644
index 00000000000..d6947a8e2a9
--- /dev/null
+++ b/frontend/src/metabase/hoc/RenderPropToHOC.tsx
@@ -0,0 +1,29 @@
+import type { ComponentType, ReactNode } from "react";
+
+type RenderPropComponentType<TChildrenProps> = ComponentType<{
+ children: (childrenProps: TChildrenProps) => ReactNode;
+}>;
+
+/**
+ * @deprecated HOCs are deprecated
+ */
+// eslint-disable-next-line import/no-default-export -- deprecated usage
+export default function renderPropToHoc<TChildrenProps>(
+ RenderPropComponent: RenderPropComponentType<TChildrenProps>,
+) {
+ return <TProps extends object>(
+ ComposedComponent: ComponentType<TProps & TChildrenProps>,
+ ) => {
+ function RenderPropHoc(props: TProps) {
+ return (
+ <RenderPropComponent {...props}>
+ {(childrenProps: TChildrenProps) => (
+ <ComposedComponent {...props} {...childrenProps} />
+ )}
+ </RenderPropComponent>
+ );
+ }
+
+ return RenderPropHoc;
+ };
+}
diff --git a/frontend/src/metabase/hoc/ScrollToTop.js b/frontend/src/metabase/hoc/ScrollToTop.tsx
similarity index 61%
rename from frontend/src/metabase/hoc/ScrollToTop.js
rename to frontend/src/metabase/hoc/ScrollToTop.tsx
index 7e070aa9595..b9c997a7c78 100644
--- a/frontend/src/metabase/hoc/ScrollToTop.js
+++ b/frontend/src/metabase/hoc/ScrollToTop.tsx
@@ -1,9 +1,13 @@
-/* eslint-disable react/prop-types */
+import type { ReactNode } from "react";
import { Component } from "react";
-import { withRouter } from "react-router";
+import { type WithRouterProps, withRouter } from "react-router";
-class ScrollToTopInner extends Component {
- componentDidUpdate(prevProps) {
+interface ScrollToTopProps {
+ children?: ReactNode;
+}
+
+class ScrollToTopInner extends Component<ScrollToTopProps & WithRouterProps> {
+ componentDidUpdate(prevProps: ScrollToTopProps & WithRouterProps) {
// Compare location.pathname to see if we're on a different URL. Do this to ensure
// that query strings don't cause a scroll to the top
if (this.props.location.pathname !== prevProps.location.pathname) {
diff --git a/frontend/src/metabase/hoc/Uncontrollable.jsx b/frontend/src/metabase/hoc/Uncontrollable.jsx
deleted file mode 100644
index 5c076cc13cb..00000000000
--- a/frontend/src/metabase/hoc/Uncontrollable.jsx
+++ /dev/null
@@ -1,52 +0,0 @@
-import PropTypes from "prop-types";
-import { Component } from "react";
-
-import { getDisplayName } from "./utils";
-
-// wraps a component that takes `value` and `onChange` and allows it to be "uncontrolled"
-// i.e. https://reactjs.org/docs/uncontrolled-components.html
-
-const Uncontrollable = () => (WrappedComponent) =>
- class extends Component {
- static displayName = `Uncontrollable(${getDisplayName(WrappedComponent)})`;
-
- constructor(props) {
- super(props);
- this.state = {
- value: props.defaultValue,
- };
- }
-
- static propTypes = {
- ...WrappedComponent.propTypes,
- // controlled
- value: PropTypes.any,
- onChange: PropTypes.func,
- // uncontrolled
- defaultValue: PropTypes.any,
- };
-
- handleChange = (e) => {
- this.setState({ value: e.target.value });
- this.props.onChange?.(e);
- };
-
- render() {
- if (this.props.value !== undefined) {
- // controlled
- return <WrappedComponent {...this.props} />;
- } else {
- // uncontrolled
- return (
- <WrappedComponent
- {...this.props}
- value={this.state.value}
- onChange={this.handleChange}
- />
- );
- }
- }
- };
-
-// eslint-disable-next-line import/no-default-export -- deprecated usage
-export default Uncontrollable;
diff --git a/frontend/src/metabase/hoc/Uncontrollable.tsx b/frontend/src/metabase/hoc/Uncontrollable.tsx
new file mode 100644
index 00000000000..ae63e55966a
--- /dev/null
+++ b/frontend/src/metabase/hoc/Uncontrollable.tsx
@@ -0,0 +1,73 @@
+import PropTypes from "prop-types";
+import { Component, type ComponentType } from "react";
+
+import { getDisplayName } from "./utils";
+
+// wraps a component that takes `value` and `onChange` and allows it to be "uncontrolled"
+// i.e. https://reactjs.org/docs/uncontrolled-components.html
+
+type UncontrollableChangeEvent = { target: { value: unknown } };
+
+interface UncontrollableProps {
+ value?: unknown;
+ defaultValue?: unknown;
+ onChange?: (event: UncontrollableChangeEvent) => void;
+}
+
+/**
+ * @deprecated HOCs are deprecated
+ *
+ * Returns a transparently-typed component so wrapping doesn't tighten (or break)
+ * the wide-open prop types the many existing consumers rely on.
+ */
+const Uncontrollable =
+ () =>
+ (WrappedComponent: ComponentType<any>): ComponentType<any> => {
+ class UncontrollableComponent extends Component<
+ UncontrollableProps,
+ { value: unknown }
+ > {
+ static displayName = `Uncontrollable(${getDisplayName(
+ WrappedComponent,
+ )})`;
+
+ static propTypes = {
+ ...WrappedComponent.propTypes,
+ // controlled
+ value: PropTypes.any,
+ onChange: PropTypes.func,
+ // uncontrolled
+ defaultValue: PropTypes.any,
+ };
+
+ state = {
+ value: this.props.defaultValue,
+ };
+
+ handleChange = (e: UncontrollableChangeEvent) => {
+ this.setState({ value: e.target.value });
+ this.props.onChange?.(e);
+ };
+
+ render() {
+ if (this.props.value !== undefined) {
+ // controlled
+ return <WrappedComponent {...this.props} />;
+ } else {
+ // uncontrolled
+ return (
+ <WrappedComponent
+ {...this.props}
+ value={this.state.value}
+ onChange={this.handleChange}
+ />
+ );
+ }
+ }
+ }
+
+ return UncontrollableComponent;
+ };
+
+// eslint-disable-next-line import/no-default-export -- deprecated usage
+export default Uncontrollable;