Skip to content

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

Convert sandboxes ParameterTargetWidget and container to TS (#75330)

Andrei Chernykh295d0509349d
diff --git a/enterprise/frontend/src/metabase-enterprise/sandboxes/components/AttributeMappingEditor/DataAttributeMappingEditor.tsx b/enterprise/frontend/src/metabase-enterprise/sandboxes/components/AttributeMappingEditor/DataAttributeMappingEditor.tsx
index 168b4c4a4e5..019ceb291b6 100644
--- a/enterprise/frontend/src/metabase-enterprise/sandboxes/components/AttributeMappingEditor/DataAttributeMappingEditor.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/sandboxes/components/AttributeMappingEditor/DataAttributeMappingEditor.tsx
@@ -5,7 +5,7 @@ import _ from "underscore";
import CS from "metabase/css/core/index.css";
import { Box, Button, Icon, Select, Text, Tooltip } from "metabase/ui";
-import QuestionParameterTargetWidget from "metabase-enterprise/sandboxes/containers/QuestionParameterTargetWidget";
+import { QuestionParameterTargetWidget } from "metabase-enterprise/sandboxes/containers/QuestionParameterTargetWidget";
import type {
DataAttributeMap,
GroupTableAccessPolicyDraft,
@@ -22,13 +22,13 @@ import {
replaceEntryValue,
} from "metabase-enterprise/sandboxes/utils";
import type {
- DimensionRef,
GroupTableAccessPolicy,
+ ParameterTarget,
Table,
UserAttributeKey,
} from "metabase-types/api";
-type ValueType = string | DimensionRef | null;
+type ValueType = string | ParameterTarget | null;
export interface MappingEditorProps {
value: DataAttributeMap<ValueType>;
@@ -193,7 +193,7 @@ const ColumnPicker = ({
shouldUseSavedQuestion,
}: {
value: ValueType;
- onChange?: (value: string) => void;
+ onChange: (value: ParameterTarget) => void;
policyTable?: Table;
policy: GroupTableAccessPolicy | GroupTableAccessPolicyDraft;
shouldUseSavedQuestion: boolean;
@@ -204,10 +204,12 @@ const ColumnPicker = ({
shouldUseSavedQuestion && policy.card_id != null;
if (filterByTableColumn || filterBySavedQuestion) {
+ // ValueType's string member is the "no value set yet" sentinel.
+ const target = typeof value === "string" ? null : value;
return (
<Box miw={200}>
<QuestionParameterTargetWidget
- target={value}
+ target={target}
onChange={onChange}
questionObject={
filterByTableColumn && policyTable
diff --git a/enterprise/frontend/src/metabase-enterprise/sandboxes/components/AttributeMappingEditor/DataAttributeMappingEditor.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/sandboxes/components/AttributeMappingEditor/DataAttributeMappingEditor.unit.spec.tsx
index a08dd376b99..16ce1c93bee 100644
--- a/enterprise/frontend/src/metabase-enterprise/sandboxes/components/AttributeMappingEditor/DataAttributeMappingEditor.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/sandboxes/components/AttributeMappingEditor/DataAttributeMappingEditor.unit.spec.tsx
@@ -9,7 +9,10 @@ import {
} from "__support__/server-mocks";
import { renderWithProviders, screen } from "__support__/ui";
import type { DataAttributeMap } from "metabase-enterprise/sandboxes/types";
-import type { DimensionRef, GroupTableAccessPolicy } from "metabase-types/api";
+import type {
+ GroupTableAccessPolicy,
+ ParameterTarget,
+} from "metabase-types/api";
import {
createMockCard,
createMockCardQueryMetadata,
@@ -25,7 +28,7 @@ const tablePolicy: GroupTableAccessPolicy = {
group_id: 3,
card_id: null,
attribute_remappings: {
- color: ["dimension", ["field", 11]],
+ color: ["dimension", ["field", 11, null]],
},
};
@@ -36,14 +39,14 @@ const cardPolicy: GroupTableAccessPolicy = {
group_id: 3,
card_id: 2,
attribute_remappings: {
- color: ["dimension", ["field", 22]],
+ color: ["dimension", ["field", 22, null]],
},
};
const policyTable = createMockTable();
const options = ["type", "color", "personal", "other"];
-type MappingType = DataAttributeMap<DimensionRef | string | null>;
+type MappingType = DataAttributeMap<ParameterTarget | string | null>;
const Wrapper = ({
spy,
@@ -118,7 +121,7 @@ describe("DataAttributeMappingEditor", () => {
await userEvent.click(await screen.findByText("other"));
expect(onChange).toHaveBeenCalledWith({
- other: ["dimension", ["field", 11]],
+ other: ["dimension", ["field", 11, null]],
});
});
});
@@ -141,7 +144,7 @@ describe("DataAttributeMappingEditor", () => {
await userEvent.click(await screen.findByText("other"));
expect(onChange).toHaveBeenCalledWith({
- other: ["dimension", ["field", 22]],
+ other: ["dimension", ["field", 22, null]],
});
});
});
diff --git a/enterprise/frontend/src/metabase-enterprise/sandboxes/components/ParameterTargetWidget.jsx b/enterprise/frontend/src/metabase-enterprise/sandboxes/components/ParameterTargetWidget.tsx
similarity index 67%
rename from enterprise/frontend/src/metabase-enterprise/sandboxes/components/ParameterTargetWidget.jsx
rename to enterprise/frontend/src/metabase-enterprise/sandboxes/components/ParameterTargetWidget.tsx
index 93888f16703..9eb65461091 100644
--- a/enterprise/frontend/src/metabase-enterprise/sandboxes/components/ParameterTargetWidget.jsx
+++ b/enterprise/frontend/src/metabase-enterprise/sandboxes/components/ParameterTargetWidget.tsx
@@ -1,28 +1,46 @@
-/* eslint-disable react/prop-types */
import cx from "classnames";
-import { useState } from "react";
+import { type ReactNode, useState } from "react";
import { SelectButton } from "metabase/common/components/SelectButton";
import { useCaptureEvent } from "metabase/common/hooks";
import { ParameterTargetList } from "metabase/parameters/components/ParameterTargetList";
-import { getMappingOptionByTarget } from "metabase/parameters/utils/mapping-options";
+import {
+ type ParameterMappingOption,
+ getMappingOptionByTarget,
+} from "metabase/parameters/utils/mapping-options";
import { Box, Popover } from "metabase/ui";
+import type Question from "metabase-lib/v1/Question";
+import type { ParameterTarget } from "metabase-types/api";
-const defaultChildren = ({ selected, placeholder }) => (
+type TriggerRenderProps = {
+ selected?: ParameterMappingOption;
+ disabled: boolean;
+ placeholder?: string;
+};
+
+type ParameterTargetWidgetProps = {
+ question?: Question;
+ target?: ParameterTarget | null;
+ onChange: (target: ParameterTarget) => void;
+ mappingOptions: ParameterMappingOption[];
+ placeholder?: string;
+ children?: ReactNode | ((props: TriggerRenderProps) => ReactNode);
+};
+
+const defaultChildren = ({ selected, placeholder }: TriggerRenderProps) => (
<SelectButton hasValue={!!selected} className="border-med">
{selected ? selected.name : placeholder || "Select a target"}
</SelectButton>
);
-// eslint-disable-next-line import/no-default-export -- deprecated usage
-export default function ParameterTargetWidget({
+export function ParameterTargetWidget({
question,
target,
onChange,
mappingOptions,
placeholder,
children = defaultChildren,
-}) {
+}: ParameterTargetWidgetProps) {
const [isOpen, setIsOpen] = useState(false);
useCaptureEvent(
@@ -55,7 +73,7 @@ export default function ParameterTargetWidget({
onChange={setIsOpen}
>
<Popover.Target>
- <Box className={cx({ disabled })} onClick={() => setIsOpen(!isOpen)}>
+ <Box className={cx({ disabled })} onClick={() => setIsOpen((v) => !v)}>
{triggerElement}
</Box>
</Popover.Target>
diff --git a/enterprise/frontend/src/metabase-enterprise/sandboxes/components/ParameterTargetWidget.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/sandboxes/components/ParameterTargetWidget.unit.spec.tsx
new file mode 100644
index 00000000000..11f197bdd9f
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/sandboxes/components/ParameterTargetWidget.unit.spec.tsx
@@ -0,0 +1,53 @@
+import { renderWithProviders, screen } from "__support__/ui";
+import type { ParameterMappingOption } from "metabase/parameters/utils/mapping-options";
+import type { ParameterTarget } from "metabase-types/api";
+
+import { ParameterTargetWidget } from "./ParameterTargetWidget";
+
+const TARGET = ["variable", ["template-tag", "foo"]] as ParameterTarget;
+
+const OPTION = {
+ name: "Foo column",
+ target: TARGET,
+ icon: "string",
+} as unknown as ParameterMappingOption;
+
+const setup = (
+ props: Partial<React.ComponentProps<typeof ParameterTargetWidget>> = {},
+) =>
+ renderWithProviders(
+ <ParameterTargetWidget
+ target={undefined}
+ onChange={jest.fn()}
+ mappingOptions={[OPTION]}
+ {...props}
+ />,
+ );
+
+describe("ParameterTargetWidget", () => {
+ it("shows the placeholder when nothing is selected", () => {
+ setup({ placeholder: "Pick a column" });
+
+ expect(screen.getByText("Pick a column")).toBeInTheDocument();
+ });
+
+ it("shows the selected option's name when the target matches", () => {
+ setup({ target: TARGET });
+
+ expect(screen.getByText("Foo column")).toBeInTheDocument();
+ });
+
+ it("falls back to a default label when no placeholder is given", () => {
+ setup();
+
+ expect(screen.getByText("Select a target")).toBeInTheDocument();
+ });
+
+ it("disables the trigger when there are no mapping options", () => {
+ setup({ mappingOptions: [] });
+
+ expect(
+ screen.getByText("Select a target").closest(".disabled"),
+ ).toBeTruthy();
+ });
+});
diff --git a/enterprise/frontend/src/metabase-enterprise/sandboxes/containers/QuestionParameterTargetWidget.jsx b/enterprise/frontend/src/metabase-enterprise/sandboxes/containers/QuestionParameterTargetWidget.jsx
deleted file mode 100644
index a569eb664d1..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/sandboxes/containers/QuestionParameterTargetWidget.jsx
+++ /dev/null
@@ -1,35 +0,0 @@
-/* eslint-disable react/prop-types */
-import { Component } from "react";
-
-import { QuestionLoaderHOC } from "metabase/common/components/QuestionLoader";
-import { getParameterMappingOptions } from "metabase/parameters/utils/mapping-options";
-
-import ParameterTargetWidget from "../components/ParameterTargetWidget";
-
-class QuestionParameterTargetWidget extends Component {
- render() {
- const { question, ...props } = this.props;
- const mappingOptions = question
- ? getParameterMappingOptions(
- question,
- null,
- question.card(),
- null,
- null,
- {
- includeSensitiveFields: true,
- },
- )
- : [];
- return (
- <ParameterTargetWidget
- {...props}
- question={question}
- mappingOptions={mappingOptions}
- />
- );
- }
-}
-
-// eslint-disable-next-line import/no-default-export -- deprecated usage
-export default QuestionLoaderHOC(QuestionParameterTargetWidget);
diff --git a/enterprise/frontend/src/metabase-enterprise/sandboxes/containers/QuestionParameterTargetWidget.tsx b/enterprise/frontend/src/metabase-enterprise/sandboxes/containers/QuestionParameterTargetWidget.tsx
new file mode 100644
index 00000000000..7e423ab1b00
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/sandboxes/containers/QuestionParameterTargetWidget.tsx
@@ -0,0 +1,47 @@
+import {
+ type QuestionLoaderChildrenProps,
+ QuestionLoaderHOC,
+} from "metabase/common/components/QuestionLoader";
+import { getParameterMappingOptions } from "metabase/parameters/utils/mapping-options";
+import type { CardId, ParameterTarget, UnsavedCard } from "metabase-types/api";
+
+import { ParameterTargetWidget } from "../components/ParameterTargetWidget";
+
+type OwnProps = {
+ target?: ParameterTarget | null;
+ onChange: (target: ParameterTarget) => void;
+ placeholder?: string;
+};
+
+// Props the QuestionLoaderHOC forwards to its underlying QuestionLoader.
+type LoaderProps = {
+ questionObject?: UnsavedCard | null;
+ questionId?: CardId | null;
+ questionHash?: string | null;
+ includeSensitiveFields?: boolean;
+};
+
+function QuestionParameterTargetWidgetBase({
+ question,
+ ...props
+}: LoaderProps & OwnProps & QuestionLoaderChildrenProps) {
+ const mappingOptions = question
+ ? getParameterMappingOptions(question, null, question.card(), null, null, {
+ includeSensitiveFields: true,
+ })
+ : [];
+
+ return (
+ <ParameterTargetWidget
+ target={props.target}
+ onChange={props.onChange}
+ placeholder={props.placeholder}
+ question={question ?? undefined}
+ mappingOptions={mappingOptions}
+ />
+ );
+}
+
+export const QuestionParameterTargetWidget = QuestionLoaderHOC(
+ QuestionParameterTargetWidgetBase,
+);
diff --git a/enterprise/frontend/src/metabase-enterprise/sandboxes/types.ts b/enterprise/frontend/src/metabase-enterprise/sandboxes/types.ts
index 4638ac1a002..5c2ec5f442d 100644
--- a/enterprise/frontend/src/metabase-enterprise/sandboxes/types.ts
+++ b/enterprise/frontend/src/metabase-enterprise/sandboxes/types.ts
@@ -1,8 +1,8 @@
import type { EnterpriseSharedState } from "metabase-enterprise/shared/reducer";
import type { EnterpriseState } from "metabase-enterprise/shared/types";
import type {
- DimensionRef,
GroupTableAccessPolicy,
+ ParameterTarget,
UserAttributeKey,
} from "metabase-types/api";
@@ -22,7 +22,7 @@ export type GroupTableAccessPolicyDraft = Pick<
"card_id" | "table_id" | "group_id"
> & {
attribute_remappings: {
- [key: string]: string | DimensionRef | null;
+ [key: string]: string | ParameterTarget | null;
};
};
diff --git a/frontend/src/metabase-types/api/permissions.ts b/frontend/src/metabase-types/api/permissions.ts
index 0323a3bbe0b..4f9b6ee397a 100644
--- a/frontend/src/metabase-types/api/permissions.ts
+++ b/frontend/src/metabase-types/api/permissions.ts
@@ -6,6 +6,7 @@ import type {
} from "metabase-types/api";
import type { GroupId } from "./group";
+import type { ParameterTarget } from "./parameters";
import type { UserAttributeKey } from "./user";
export enum DataPermission {
@@ -154,16 +155,13 @@ export type CollectionPermissions = {
export type CollectionPermission = "write" | "read" | "none";
-// FIXME: is there a more suitable type for this?
-export type DimensionRef = ["dimension", any[]];
-
export type GroupTableAccessPolicy = {
id: number;
group_id: number;
table_id: number;
card_id: number | null;
attribute_remappings: {
- [key: UserAttributeKey]: DimensionRef;
+ [key: UserAttributeKey]: ParameterTarget;
};
permission_id: number | null;
};
diff --git a/frontend/src/metabase/data-studio/data-model/components/TablePicker/components/TablePicker.unit.spec.tsx b/frontend/src/metabase/data-studio/data-model/components/TablePicker/components/TablePicker.unit.spec.tsx
index a85597bb4c4..c70b86da969 100644
--- a/frontend/src/metabase/data-studio/data-model/components/TablePicker/components/TablePicker.unit.spec.tsx
+++ b/frontend/src/metabase/data-studio/data-model/components/TablePicker/components/TablePicker.unit.spec.tsx
@@ -413,7 +413,11 @@ describe("TablePicker", () => {
await clickItem(layerDatabase);
await waitLoading();
- expect(item(hiddenTable)).toBeInTheDocument();
+ // The auto-expansion re-renders the tree with the tables after
+ // waitLoading resolves, so wait for the first table to appear.
+ await waitFor(() => {
+ expect(item(hiddenTable)).toBeInTheDocument();
+ });
expect(item(finalTable)).toBeInTheDocument();
const labels = screen