Skip to content

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

add copy value click action (#79837)

Nick Fitzpatrick866b1b7bb001
diff --git a/e2e/test/scenarios/filters-reproductions/filters-reproductions.cy.spec.js b/e2e/test/scenarios/filters-reproductions/filters-reproductions.cy.spec.js
index 53d3f964b35..8687e2a719b 100644
--- a/e2e/test/scenarios/filters-reproductions/filters-reproductions.cy.spec.js
+++ b/e2e/test/scenarios/filters-reproductions/filters-reproductions.cy.spec.js
@@ -112,9 +112,10 @@ describe("issue 18770", () => {
H.popover().within(() => {
cy.findByText("Filter by this value").should("be.visible");
cy.findAllByRole("button")
- .should("have.length", 6)
+ .should("have.length", 7)
.and("contain", "See these Orders")
.and("contain", "Break out by")
+ .and("contain", "Copy value")
.and("contain", "<")
.and("contain", ">")
.and("contain", "=")
diff --git a/frontend/src/metabase/visualizations/click-actions/actions/CopyValueAction/CopyValueAction.tsx b/frontend/src/metabase/visualizations/click-actions/actions/CopyValueAction/CopyValueAction.tsx
new file mode 100644
index 00000000000..13e7d64ea25
--- /dev/null
+++ b/frontend/src/metabase/visualizations/click-actions/actions/CopyValueAction/CopyValueAction.tsx
@@ -0,0 +1,57 @@
+import { t } from "ttag";
+
+import { formatValue } from "metabase/value-formatting";
+import type {
+ ComputedVisualizationSettings,
+ LegacyDrill,
+} from "metabase/visualizations/types";
+import { getColumnSettings } from "metabase-lib/v1/queries/utils/column-key";
+import { isPK } from "metabase-lib/v1/types/utils/isa";
+
+import { nativeDrillFallback } from "../utils";
+
+export const CopyValueAction: LegacyDrill = ({
+ clicked,
+ settings,
+ question,
+}) => {
+ const column = clicked?.column;
+
+ // "View details" is a PK cell's only action, and a lone default action skips the
+ // popover — a second action here would break that jump to the object detail view.
+ if (!clicked || column == null || clicked.value == null || isPK(column)) {
+ return [];
+ }
+ // This action should not override the native query fallback
+ if (nativeDrillFallback({ question })) {
+ return [];
+ }
+
+ const computedSettings: ComputedVisualizationSettings | undefined = settings;
+ const columnSettings =
+ computedSettings?.column?.(column) ?? getColumnSettings(settings, column);
+
+ const formattedValue = String(
+ formatValue(clicked.value, {
+ ...columnSettings,
+ column,
+ type: "cell",
+ clicked,
+ }),
+ );
+
+ return [
+ {
+ name: "copy-value",
+ section: "copy",
+ title: t`Copy value`,
+ buttonType: "horizontal",
+ icon: "copy",
+ type: "custom",
+ onClick: ({ closePopover }) => {
+ navigator.clipboard.writeText(formattedValue);
+ closePopover();
+ },
+ },
+ ];
+};
diff --git a/frontend/src/metabase/visualizations/click-actions/actions/CopyValueAction/CopyValueAction.unit.spec.tsx b/frontend/src/metabase/visualizations/click-actions/actions/CopyValueAction/CopyValueAction.unit.spec.tsx
new file mode 100644
index 00000000000..ac3e2501480
--- /dev/null
+++ b/frontend/src/metabase/visualizations/click-actions/actions/CopyValueAction/CopyValueAction.unit.spec.tsx
@@ -0,0 +1,141 @@
+import { createMockMetadata } from "__support__/metadata";
+import {
+ type ClickObject,
+ type ComputedVisualizationSettings,
+ isCustomClickAction,
+} from "metabase/visualizations/types";
+import Question from "metabase-lib/v1/Question";
+import { getColumnKey } from "metabase-lib/v1/queries/utils/column-key";
+import {
+ createMockColumn,
+ createMockNumericColumn,
+} from "metabase-types/api/mocks";
+import {
+ createSampleDatabase,
+ createSavedStructuredCard,
+} from "metabase-types/api/mocks/presets";
+
+import { CopyValueAction } from "./CopyValueAction";
+
+const setup = (
+ clicked: ClickObject | undefined,
+ settings: ComputedVisualizationSettings = {},
+) => {
+ const metadata = createMockMetadata({
+ databases: [createSampleDatabase()],
+ });
+ const question = new Question(createSavedStructuredCard(), metadata);
+ return CopyValueAction({ question, clicked, settings });
+};
+
+const copyValue = (
+ clicked: ClickObject,
+ settings?: ComputedVisualizationSettings,
+) => {
+ const writeText = jest.fn().mockResolvedValue(undefined);
+ Object.assign(navigator, { clipboard: { writeText } });
+ const closePopover = jest.fn();
+
+ const [action] = setup(clicked, settings);
+
+ if (!isCustomClickAction(action)) {
+ throw new Error("expected a custom click action");
+ }
+ action.onClick?.({ dispatch: jest.fn(), closePopover });
+
+ return { writeText, closePopover };
+};
+
+describe("CopyValueAction", () => {
+ const column = createMockColumn({ name: "NAME", display_name: "Name" });
+
+ it("returns no actions when the column is missing", () => {
+ expect(setup({ value: "hello" })).toHaveLength(0);
+ });
+
+ it("returns no actions on a header click (value is undefined)", () => {
+ expect(setup({ column })).toHaveLength(0);
+ });
+
+ it("returns no actions on an empty cell (value is null)", () => {
+ expect(setup({ column, value: null })).toHaveLength(0);
+ });
+
+ it("returns no actions on a primary key cell, so it still drills straight to the object detail view", () => {
+ const pkColumn = createMockColumn({
+ name: "ID",
+ display_name: "ID",
+ semantic_type: "type/PK",
+ });
+
+ expect(setup({ column: pkColumn, value: 42 })).toHaveLength(0);
+ });
+
+ it("returns an action on a foreign key cell", () => {
+ const fkColumn = createMockColumn({
+ name: "PRODUCT_ID",
+ display_name: "Product ID",
+ semantic_type: "type/FK",
+ });
+
+ expect(setup({ column: fkColumn, value: 42 })).toHaveLength(1);
+ });
+
+ it("returns a copy-value action for a cell with a value", () => {
+ const actions = setup({ column, value: "hello" });
+ const [action] = actions;
+
+ expect(actions).toHaveLength(1);
+
+ if (!isCustomClickAction(action)) {
+ throw new Error("expected a custom click action");
+ }
+
+ expect(action.name).toBe("copy-value");
+ expect(action.section).toBe("copy");
+ });
+
+ it("returns an action for falsy but present values (0, false, empty string)", () => {
+ expect(setup({ column, value: 0 })).toHaveLength(1);
+ expect(setup({ column, value: false })).toHaveLength(1);
+ expect(setup({ column, value: "" })).toHaveLength(1);
+ });
+
+ it("copies the formatted value to the clipboard and closes the popover", () => {
+ const { writeText, closePopover } = copyValue({ column, value: "hello" });
+
+ expect(writeText).toHaveBeenCalledWith("hello");
+ expect(closePopover).toHaveBeenCalledTimes(1);
+ });
+
+ describe("column settings", () => {
+ const numericColumn = createMockNumericColumn({
+ name: "DISCOUNT",
+ display_name: "Discount",
+ });
+
+ it("copies the value formatted by the computed column settings", () => {
+ const { writeText } = copyValue(
+ { column: numericColumn, value: 0.8567 },
+ {
+ column: () => ({ prefix: "~", number_style: "percent", decimals: 1 }),
+ },
+ );
+
+ expect(writeText).toHaveBeenCalledWith("~85.7%");
+ });
+
+ it("falls back to the saved column settings when there are no computed settings", () => {
+ const { writeText } = copyValue(
+ { column: numericColumn, value: 1234.5 },
+ {
+ column_settings: {
+ [getColumnKey(numericColumn)]: { prefix: "$", decimals: 2 },
+ },
+ },
+ );
+
+ expect(writeText).toHaveBeenCalledWith("$1,234.50");
+ });
+ });
+});
diff --git a/frontend/src/metabase/visualizations/click-actions/actions/CopyValueAction/index.ts b/frontend/src/metabase/visualizations/click-actions/actions/CopyValueAction/index.ts
new file mode 100644
index 00000000000..db8e977463b
--- /dev/null
+++ b/frontend/src/metabase/visualizations/click-actions/actions/CopyValueAction/index.ts
@@ -0,0 +1 @@
+export * from "./CopyValueAction";
diff --git a/frontend/src/metabase/visualizations/click-actions/modes/DefaultMode.ts b/frontend/src/metabase/visualizations/click-actions/modes/DefaultMode.ts
index 6e38b9eec29..6f4fe39dc21 100644
--- a/frontend/src/metabase/visualizations/click-actions/modes/DefaultMode.ts
+++ b/frontend/src/metabase/visualizations/click-actions/modes/DefaultMode.ts
@@ -1,6 +1,7 @@
import type { QueryClickActionsMode } from "../../types";
import { ColumnFormattingAction } from "../actions/ColumnFormattingAction";
import { CombineColumnsAction } from "../actions/CombineColumnsAction";
+import { CopyValueAction } from "../actions/CopyValueAction";
import { ExtractColumnAction } from "../actions/ExtractColumnAction";
import { HideColumnAction } from "../actions/HideColumnAction";
import { NativeQueryClickFallback } from "../actions/NativeQueryClickFallback";
@@ -9,6 +10,7 @@ export const DefaultMode: QueryClickActionsMode = {
name: "default",
hasDrills: true,
clickActions: [
+ CopyValueAction,
HideColumnAction,
ColumnFormattingAction,
ExtractColumnAction,
diff --git a/frontend/src/metabase/visualizations/components/ClickActions/ClickActionsView.tsx b/frontend/src/metabase/visualizations/components/ClickActions/ClickActionsView.tsx
index 7a964482948..4532b2f0711 100644
--- a/frontend/src/metabase/visualizations/components/ClickActions/ClickActionsView.tsx
+++ b/frontend/src/metabase/visualizations/components/ClickActions/ClickActionsView.tsx
@@ -45,7 +45,6 @@ export const ClickActionsView = ({
);
const withBottomDivider =
sectionKey === "records" && !hasOnlyOneSection;
- const withTopDivider = sectionKey === "details" && !hasOnlyOneSection;
return (
<ClickActionsViewSection
@@ -54,7 +53,6 @@ export const ClickActionsView = ({
title={sectionTitle}
contentDirection={contentDirection}
>
- {withTopDivider && <Divider mx="-lg" my="sm" />}
{actions.map((action) => (
<ClickActionControl
key={action.name}
diff --git a/frontend/src/metabase/visualizations/components/ClickActions/utils.ts b/frontend/src/metabase/visualizations/components/ClickActions/utils.ts
index 6ae77e8cdfb..a4c55fee9e0 100644
--- a/frontend/src/metabase/visualizations/components/ClickActions/utils.ts
+++ b/frontend/src/metabase/visualizations/components/ClickActions/utils.ts
@@ -26,8 +26,9 @@ export const SECTIONS: Record<ClickActionSection, Section> = {
auto: {},
"auto-popover": {},
info: {},
- filter: {},
details: {},
+ copy: {},
+ filter: {},
custom: {},
"new-column": {},
};
@@ -38,10 +39,10 @@ Object.values(SECTIONS).map((section, index) => {
export const getGroupedAndSortedActions = (
clickActions: RegularClickAction[],
) => {
- // Unjustified type cast. FIXME
- const groupedClickActions = _.groupBy(clickActions, "section") as {
- [key in ClickActionSection]?: RegularClickAction[];
- };
+ const groupedClickActions = Object.groupBy(
+ clickActions,
+ (action) => action.section,
+ );
if (groupedClickActions["sum"]?.length === 1) {
// if there's only one "sum" click action, merge it into "summarize" and change its button type and icon
diff --git a/frontend/src/metabase/visualizations/types/click-actions.ts b/frontend/src/metabase/visualizations/types/click-actions.ts
index b1888a3d9ba..ce7d12bef1f 100644
--- a/frontend/src/metabase/visualizations/types/click-actions.ts
+++ b/frontend/src/metabase/visualizations/types/click-actions.ts
@@ -44,6 +44,7 @@ export type ClickActionSection =
| "breakout-popover"
| "combine"
| "combine-popover"
+ | "copy"
| "details"
| "extract"
| "extract-popover"