The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Refactor CustomURLPicker (#65574)
Fraser4fe0ecbeb992
diff --git a/e2e/test/scenarios/dashboard-cards/click-behavior.cy.spec.js b/e2e/test/scenarios/dashboard-cards/click-behavior.cy.spec.jsindex 85d673c0a46..096206593a9 100644--- a/e2e/test/scenarios/dashboard-cards/click-behavior.cy.spec.js+++ b/e2e/test/scenarios/dashboard-cards/click-behavior.cy.spec.js@@ -1060,8 +1060,8 @@ describe("scenarios > dashboard > dashboard cards > click behavior", () => {cy.findByText(COUNT_COLUMN_ID).should("exist");cy.findByText(CREATED_AT_COLUMN_ID).should("exist");cy.findByText(DASHBOARD_FILTER_TEXT.name).should("exist");- cy.realPress("Escape");});+ H.modal().findByText("Values you can reference").click();H.modal().within(() => {cy.findByRole("textbox").type(URL_WITH_PARAMS, {parseSpecialCharSequences: false,diff --git a/frontend/src/metabase/dashboard/components/ClickBehaviorSidebar/LinkOptions/CustomURLPicker.tsx b/frontend/src/metabase/dashboard/components/ClickBehaviorSidebar/LinkOptions/CustomURLPicker.tsxindex aff3bbfd22f..caaa6653ac4 100644--- a/frontend/src/metabase/dashboard/components/ClickBehaviorSidebar/LinkOptions/CustomURLPicker.tsx+++ b/frontend/src/metabase/dashboard/components/ClickBehaviorSidebar/LinkOptions/CustomURLPicker.tsx@@ -1,13 +1,12 @@+import { useDisclosure } from "@mantine/hooks";import cx from "classnames";import { useCallback, useState } from "react";import { t } from "ttag";import InputBlurChange from "metabase/common/components/InputBlurChange";-import ModalContent from "metabase/common/components/ModalContent";-import ModalWithTrigger from "metabase/common/components/ModalWithTrigger";import CS from "metabase/css/core/index.css";import { isTableDisplay } from "metabase/lib/click-behavior";-import { Box, Button, Icon } from "metabase/ui";+import { Button, Flex, Icon, Modal, Text } from "metabase/ui";import type { UiParameter } from "metabase-lib/v1/parameters/types";import { clickBehaviorIsValid } from "metabase-lib/v1/parameters/utils/click-behavior";import type {@@ -37,6 +36,8 @@ export function CustomURLPicker({}: Props) {const [url, setUrl] = useState(clickBehavior?.linkTemplate ?? "");const hasLinkTemplate = !!clickBehavior.linkTemplate;+ const [modalOpened, { open: openModal, close: closeModal }] =+ useDisclosure(!hasLinkTemplate);const canSelect = clickBehaviorIsValid({...clickBehavior,linkTemplate: url,@@ -65,41 +66,40 @@ export function CustomURLPicker({}, [clickBehavior, updateSettings]);return (- <ModalWithTrigger- isInitiallyOpen={!hasLinkTemplate}- triggerElement={- <Button.Group>- <Button- justify="flex-start"- leftSection={<Icon name="link" />}- size="lg"- variant="filled"- classNames={{- root: LinkOptionsS.ButtonRoot,- }}- >- <SidebarItem.Name>- {hasLinkTemplate ? clickBehavior.linkTemplate : t`URL`}- </SidebarItem.Name>- </Button>- <Button- onClick={handleReset}- miw="3rem"- size="lg"- variant="filled"- rightSection={<Icon name="close" />}- />- </Button.Group>- }- >- {({ onClose }: { onClose: () => void }) => (- <ModalContent- title={t`Enter a URL to link to`}- onClose={hasLinkTemplate ? onClose : undefined}+ <>+ <Button.Group>+ <Button+ justify="flex-start"+ leftSection={<Icon name="link" />}+ size="lg"+ variant="filled"+ classNames={{+ root: LinkOptionsS.ButtonRoot,+ }}+ onClick={openModal}>- <Box component="span" mb="md">+ <SidebarItem.Name>+ {hasLinkTemplate ? clickBehavior.linkTemplate : t`URL`}+ </SidebarItem.Name>+ </Button>+ <Button+ onClick={handleReset}+ miw="3rem"+ size="lg"+ variant="filled"+ rightSection={<Icon name="close" />}+ />+ </Button.Group>+ <Modal+ opened={modalOpened}+ onClose={closeModal}+ title={t`Enter a URL to link to`}+ size="lg"+ >+ <Flex direction="column" gap="md" mt="sm">+ <Text>{t`You can insert the value of a column or dashboard filter using its name, like this: {{some_column}}`}- </Box>+ </Text><InputBlurChangeautoFocusvalue={url}@@ -121,12 +121,12 @@ export function CustomURLPicker({type="button"onClick={() => {handleSubmit();- onClose();+ closeModal();}}disabled={!canSelect}>{t`Done`}</Button>- </ModalContent>- )}- </ModalWithTrigger>+ </Flex>+ </Modal>+ </>);}diff --git a/frontend/src/metabase/dashboard/components/ClickBehaviorSidebar/LinkOptions/ValuesYouCanReference.jsx b/frontend/src/metabase/dashboard/components/ClickBehaviorSidebar/LinkOptions/ValuesYouCanReference.jsxdeleted file mode 100644index a0957e946ff..00000000000--- a/frontend/src/metabase/dashboard/components/ClickBehaviorSidebar/LinkOptions/ValuesYouCanReference.jsx+++ /dev/null@@ -1,72 +0,0 @@-import { t } from "ttag";--import { AccordionList } from "metabase/common/components/AccordionList";-import PopoverWithTrigger from "metabase/common/components/PopoverWithTrigger";-import CS from "metabase/css/core/index.css";-import {- isMappableColumn,- withUserAttributes,-} from "metabase/dashboard/components/ClickMappings";-import { Flex, Icon } from "metabase/ui";--import S from "./ValuesYouCanReference.module.css";--function prefixIfNeeded(values, prefix, otherLists) {- const otherValues = otherLists.flat().map((s) => s.toLowerCase());- return values.map((value) =>- otherValues.includes(value.toLowerCase()) ? `${prefix}:${value}` : value,- );-}--export const ValuesYouCanReference = withUserAttributes(- ({ dashcard, parameters, userAttributes }) => {- const columnMetadata = dashcard.card.result_metadata || [];- const columns = columnMetadata?.filter(isMappableColumn).map((c) => c.name);- const parameterNames = parameters.map((p) => p.name);- const sections = [- {- items: prefixIfNeeded(columns, "column", [- parameterNames,- userAttributes,- ]),- name: t`Columns`,- },- {- items: prefixIfNeeded(parameterNames, "filter", [- columns,- userAttributes,- ]),- name: t`Dashboard filters`,- },- {- items: prefixIfNeeded(userAttributes, "user", [- parameterNames,- columns,- ]),- name: t`User attributes`,- },- ].filter((section) => section.items.length > 0);-- if (!sections.length) {- return null;- }-- return (- <PopoverWithTrigger- triggerElement={- <Flex align="center" my="md" className={S.PopoverTrigger}>- <h4>{t`Values you can reference`}</h4>- <Icon name="chevrondown" className={CS.ml1} size={12} />- </Flex>- }- >- <AccordionList- alwaysExpanded- sections={sections}- renderItemName={(name) => name}- itemIsClickable={() => false}- />- </PopoverWithTrigger>- );- },-);diff --git a/frontend/src/metabase/dashboard/components/ClickBehaviorSidebar/LinkOptions/ValuesYouCanReference.tsx b/frontend/src/metabase/dashboard/components/ClickBehaviorSidebar/LinkOptions/ValuesYouCanReference.tsxnew file mode 100644index 00000000000..d029ace07f5--- /dev/null+++ b/frontend/src/metabase/dashboard/components/ClickBehaviorSidebar/LinkOptions/ValuesYouCanReference.tsx@@ -0,0 +1,97 @@+import { useState } from "react";+import { t } from "ttag";++import { AccordionList } from "metabase/common/components/AccordionList";+import {+ isMappableColumn,+ withUserAttributes,+} from "metabase/dashboard/components/ClickMappings";+import { Flex, Icon, Popover, UnstyledButton } from "metabase/ui";+import type { UiParameter } from "metabase-lib/v1/parameters/types";+import type { DashboardCard } from "metabase-types/api";++import S from "./ValuesYouCanReference.module.css";++interface ValuesYouCanReferenceProps {+ dashcard: DashboardCard;+ parameters: UiParameter[];+ userAttributes: string[];+}++interface Section {+ items: string[];+ name: string;+}++function prefixIfNeeded(+ values: string[],+ prefix: string,+ otherLists: string[][],+): string[] {+ const otherValues = otherLists.flat().map((s) => s.toLowerCase());+ return values.map((value) =>+ otherValues.includes(value.toLowerCase()) ? `${prefix}:${value}` : value,+ );+}++const ValuesYouCanReferenceComponent = ({+ dashcard,+ parameters,+ userAttributes,+}: ValuesYouCanReferenceProps) => {+ const [opened, setOpened] = useState(false);++ const columnMetadata = dashcard.card.result_metadata || [];+ const columns = columnMetadata?.filter(isMappableColumn).map((c) => c.name);+ const parameterNames = parameters.map((p) => p.name);++ const sections: Section[] = [+ {+ items: prefixIfNeeded(columns, "column", [+ parameterNames,+ userAttributes,+ ]),+ name: t`Columns`,+ },+ {+ items: prefixIfNeeded(parameterNames, "filter", [+ columns,+ userAttributes,+ ]),+ name: t`Dashboard filters`,+ },+ {+ items: prefixIfNeeded(userAttributes, "user", [parameterNames, columns]),+ name: t`User attributes`,+ },+ ].filter((section) => section.items.length > 0);++ if (!sections.length) {+ return null;+ }++ return (+ <Popover opened={opened} onChange={setOpened} position="bottom-start">+ <Popover.Target>+ <UnstyledButton onClick={() => setOpened(!opened)}>+ <Flex align="center" className={S.PopoverTrigger}>+ <h4>{t`Values you can reference`}</h4>+ <Icon name="chevrondown" ml="sm" size={12} />+ </Flex>+ </UnstyledButton>+ </Popover.Target>+ <Popover.Dropdown>+ <AccordionList+ alwaysExpanded+ sections={sections}+ renderItemName={(name) => name.toString()}+ itemIsClickable={() => false}+ />+ </Popover.Dropdown>+ </Popover>+ );+};++export const ValuesYouCanReference = withUserAttributes(+ ValuesYouCanReferenceComponent,+);