Skip to content

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

DSN-67 We could improve the "Scanning for Filter Values" setting (#63887)

Kamil Mielnikd44a2cab6669
diff --git a/e2e/test/scenarios/admin/databases.cy.spec.js b/e2e/test/scenarios/admin/databases.cy.spec.js
index f5a2f5a0e44..026856c6e05 100644
--- a/e2e/test/scenarios/admin/databases.cy.spec.js
+++ b/e2e/test/scenarios/admin/databases.cy.spec.js
@@ -122,9 +122,9 @@ describe("admin > database > add", () => {
.click({ force: true })
.should("have.attr", "data-checked", "true");
- cy.findByLabelText(
+ cy.findByDisplayValue(
"Never, I'll do this manually if I need to",
- ).should("have.attr", "aria-selected", "true");
+ ).should("exist");
// make sure tooltips behave as expected
cy.findByLabelText("Host")
@@ -243,11 +243,9 @@ describe("admin > database > add", () => {
"true",
);
- cy.findByLabelText("Never, I'll do this manually if I need to").should(
- "have.attr",
- "aria-selected",
- "true",
- );
+ cy.findByDisplayValue(
+ "Never, I'll do this manually if I need to",
+ ).should("exist");
});
});
@@ -711,17 +709,15 @@ describe("scenarios > admin > databases > sample database", () => {
});
// "lets you change the cache_field_values period"
- cy.findByLabelText("Never, I'll do this manually if I need to").should(
- "have.attr",
- "aria-selected",
- "true",
- );
-
- cy.findByLabelText("Regularly, on a schedule")
- .click()
- .within(() => {
- cy.findByText("Daily").click();
- });
+ cy.findByDisplayValue("Never, I'll do this manually if I need to")
+ .should("be.visible")
+ .click();
+
+ H.popover().findByText("Regularly, on a schedule").click();
+ cy.findAllByRole("button", { name: /Daily/ })
+ .should("have.length", 2)
+ .eq(1)
+ .click();
H.popover().findByText("Weekly").click();
cy.button("Save changes").click();
@@ -735,7 +731,8 @@ describe("scenarios > admin > databases > sample database", () => {
});
// "lets you change the cache_field_values to 'Only when adding a new filter widget'"
- cy.findByLabelText("Only when adding a new filter widget").click();
+ cy.findByDisplayValue("Regularly, on a schedule").click();
+ H.popover().findByText("Only when adding a new filter widget").click();
cy.button("Save changes", { timeout: 10000 }).click();
cy.wait("@databaseUpdate").then(({ response: { body } }) => {
editDatabase();
@@ -744,7 +741,8 @@ describe("scenarios > admin > databases > sample database", () => {
});
// and back to never
- cy.findByLabelText("Never, I'll do this manually if I need to").click();
+ cy.findByDisplayValue("Only when adding a new filter widget").click();
+ H.popover().findByText("Never, I'll do this manually if I need to").click();
cy.button("Save changes", { timeout: 10000 }).click();
cy.wait("@databaseUpdate").then(({ response: { body } }) => {
editDatabase();
@@ -769,7 +767,8 @@ describe("scenarios > admin > databases > sample database", () => {
});
editDatabase();
- cy.findByLabelText("Regularly, on a schedule").click();
+ cy.findByDisplayValue("Never, I'll do this manually if I need to").click();
+ H.popover().findByText("Regularly, on a schedule").click();
cy.button("Save changes").click();
cy.wait("@databaseUpdate").then(({ request: { body } }) => {
expect(body.is_full_sync).to.equal(true);
@@ -783,7 +782,8 @@ describe("scenarios > admin > databases > sample database", () => {
});
editDatabase();
- cy.findByLabelText("Only when adding a new filter widget").click();
+ cy.findByDisplayValue("Regularly, on a schedule").click();
+ H.popover().findByText("Only when adding a new filter widget").click();
cy.button("Save changes").click();
cy.wait("@databaseUpdate").then(({ request: { body } }) => {
expect(body.is_full_sync).to.equal(false);
diff --git a/frontend/src/metabase/databases/components/DatabaseCacheScheduleField/DatabaseCacheScheduleField.styled.tsx b/frontend/src/metabase/databases/components/DatabaseCacheScheduleField/DatabaseCacheScheduleField.styled.tsx
deleted file mode 100644
index e0017fc74ff..00000000000
--- a/frontend/src/metabase/databases/components/DatabaseCacheScheduleField/DatabaseCacheScheduleField.styled.tsx
+++ /dev/null
@@ -1,78 +0,0 @@
-// eslint-disable-next-line no-restricted-imports
-import styled from "@emotion/styled";
-
-import { color } from "metabase/lib/colors";
-
-export const ScheduleOptionList = styled.div`
- border: 1px solid var(--mb-color-border);
- box-shadow: 0 2px 2px var(--mb-color-shadow);
-`;
-
-interface ScheduleOptionRootProps {
- isSelected: boolean;
-}
-
-export const ScheduleOptionRoot = styled.div<ScheduleOptionRootProps>`
- display: flex;
- cursor: ${(props) => !props.isSelected && "pointer"};
- padding: 1.5rem 1rem;
- border-bottom: 1px solid var(--mb-color-border);
-
- &:last-child {
- border-bottom: none;
- }
-`;
-
-interface ScheduleOptionIndicatorProps {
- isSelected: boolean;
-}
-
-export const ScheduleOptionIndicator = styled.div<ScheduleOptionIndicatorProps>`
- display: flex;
- justify-content: center;
- align-items: center;
- width: 1.125rem;
- height: 1.125rem;
- border: 0.125rem solid
- ${(props) => (props.isSelected ? color("brand") : color("text-light"))};
- border-radius: 50%;
- flex: 0 0 1.125rem;
-`;
-
-interface ScheduleOptionIndicatorBackgroundProps {
- isSelected: boolean;
-}
-
-export const ScheduleOptionIndicatorBackground = styled.div<ScheduleOptionIndicatorBackgroundProps>`
- width: 0.5rem;
- height: 0.5rem;
- border-radius: 50%;
- background-color: ${(props) => props.isSelected && color("brand")};
-`;
-
-export const ScheduleOptionBody = styled.div`
- margin-left: 1rem;
-`;
-
-interface ScheduleOptionTitleProps {
- isSelected: boolean;
-}
-
-export const ScheduleOptionTitle = styled.div<ScheduleOptionTitleProps>`
- color: ${(props) =>
- props.isSelected ? color("brand") : color("text-medium")};
- font-size: 1rem;
- font-weight: bold;
- line-height: 1.25rem;
-`;
-
-export const ScheduleOptionContent = styled.div`
- margin-top: 1rem;
-`;
-
-export const ScheduleOptionText = styled.div`
- color: var(--mb-color-text-medium);
- font-size: 1rem;
- line-height: 1.5rem;
- max-width: 38.75rem;
-`;
diff --git a/frontend/src/metabase/databases/components/DatabaseCacheScheduleField/DatabaseCacheScheduleField.tsx b/frontend/src/metabase/databases/components/DatabaseCacheScheduleField/DatabaseCacheScheduleField.tsx
index 09dc8788150..7dff29209f3 100644
--- a/frontend/src/metabase/databases/components/DatabaseCacheScheduleField/DatabaseCacheScheduleField.tsx
+++ b/frontend/src/metabase/databases/components/DatabaseCacheScheduleField/DatabaseCacheScheduleField.tsx
@@ -1,26 +1,20 @@
import { useField, useFormikContext } from "formik";
import type { ReactNode } from "react";
import { useCallback } from "react";
+import { match } from "ts-pattern";
import { t } from "ttag";
import SchedulePicker from "metabase/common/components/SchedulePicker/SchedulePickerView";
import { FormField } from "metabase/forms";
+import { Box, rem } from "metabase/ui";
import type {
DatabaseData,
ScheduleSettings,
ScheduleType,
} from "metabase-types/api";
-import {
- ScheduleOptionBody,
- ScheduleOptionContent,
- ScheduleOptionIndicator,
- ScheduleOptionIndicatorBackground,
- ScheduleOptionList,
- ScheduleOptionRoot,
- ScheduleOptionText,
- ScheduleOptionTitle,
-} from "./DatabaseCacheScheduleField.styled";
+import { ScheduleModePicker } from "./ScheduleModePicker";
+import type { ScheduleMode } from "./types";
const DEFAULT_SCHEDULE: ScheduleSettings = {
schedule_day: "mon",
@@ -37,7 +31,7 @@ export interface DatabaseCacheScheduleFieldProps {
description?: ReactNode;
}
-const DatabaseCacheScheduleField = ({
+export const DatabaseCacheScheduleField = ({
name,
title,
description,
@@ -78,75 +72,49 @@ const DatabaseCacheScheduleField = ({
setValue(null);
}, [setFieldValue, setValue]);
+ const scheduleMode = getScheduleMode(values);
+
+ const handleScheduleModeChange = (scheduleMode: ScheduleMode) => {
+ match(scheduleMode)
+ .with("full", handleFullSyncSelect)
+ .with("on-demand", handleOnDemandSyncSelect)
+ .with("none", handleNoneSyncSelect)
+ .exhaustive();
+ };
+
return (
<FormField title={title} description={description}>
- <ScheduleOptionList>
- <ScheduleOption
- title={t`Regularly, on a schedule`}
- isSelected={values.is_full_sync}
- onSelect={handleFullSyncSelect}
- >
- <SchedulePicker
- schedule={value ?? DEFAULT_SCHEDULE}
- scheduleOptions={SCHEDULE_OPTIONS}
- onScheduleChange={handleScheduleChange}
- />
- </ScheduleOption>
- <ScheduleOption
- title={t`Only when adding a new filter widget`}
- isSelected={!values.is_full_sync && values.is_on_demand}
- onSelect={handleOnDemandSyncSelect}
- >
- <ScheduleOptionText>
- {/* eslint-disable-next-line no-literal-metabase-strings -- Metabase settings */}
- {t`When a user adds a new filter to a dashboard or a SQL question, Metabase will scan the field(s) mapped to that filter in order to show the list of selectable values.`}
- </ScheduleOptionText>
- </ScheduleOption>
- <ScheduleOption
- title={t`Never, I'll do this manually if I need to`}
- isSelected={!values.is_full_sync && !values.is_on_demand}
- onSelect={handleNoneSyncSelect}
+ <ScheduleModePicker
+ value={scheduleMode}
+ onChange={handleScheduleModeChange}
+ />
+
+ {scheduleMode === "full" && (
+ <SchedulePicker
+ schedule={value ?? DEFAULT_SCHEDULE}
+ scheduleOptions={SCHEDULE_OPTIONS}
+ onScheduleChange={handleScheduleChange}
/>
- </ScheduleOptionList>
+ )}
+
+ {scheduleMode === "on-demand" && (
+ <Box c="text-medium" fz="sm" maw={rem(620)} mt="sm">
+ {/* eslint-disable-next-line no-literal-metabase-strings -- Metabase settings */}
+ {t`When a user adds a new filter to a dashboard or a SQL question, Metabase will scan the field(s) mapped to that filter in order to show the list of selectable values.`}
+ </Box>
+ )}
</FormField>
);
};
-interface ScheduleOptionProps {
- title: string;
- isSelected: boolean;
- children?: ReactNode;
- onSelect: () => void;
-}
+function getScheduleMode(values: DatabaseData): ScheduleMode {
+ if (values.is_full_sync) {
+ return "full";
+ }
-const ScheduleOption = ({
- title,
- isSelected,
- children,
- onSelect,
-}: ScheduleOptionProps): JSX.Element => {
- return (
- <ScheduleOptionRoot
- isSelected={isSelected}
- role="option"
- aria-label={title}
- aria-selected={isSelected}
- onClick={onSelect}
- >
- <ScheduleOptionIndicator isSelected={isSelected}>
- <ScheduleOptionIndicatorBackground isSelected={isSelected} />
- </ScheduleOptionIndicator>
- <ScheduleOptionBody>
- <ScheduleOptionTitle isSelected={isSelected}>
- {title}
- </ScheduleOptionTitle>
- {children && isSelected && (
- <ScheduleOptionContent>{children}</ScheduleOptionContent>
- )}
- </ScheduleOptionBody>
- </ScheduleOptionRoot>
- );
-};
+ if (values.is_on_demand) {
+ return "on-demand";
+ }
-// eslint-disable-next-line import/no-default-export -- deprecated usage
-export default DatabaseCacheScheduleField;
+ return "none";
+}
diff --git a/frontend/src/metabase/databases/components/DatabaseCacheScheduleField/ScheduleModePicker.tsx b/frontend/src/metabase/databases/components/DatabaseCacheScheduleField/ScheduleModePicker.tsx
new file mode 100644
index 00000000000..556ef5fc4d0
--- /dev/null
+++ b/frontend/src/metabase/databases/components/DatabaseCacheScheduleField/ScheduleModePicker.tsx
@@ -0,0 +1,58 @@
+import { useMemo } from "react";
+import { t } from "ttag";
+
+import { Select, type SelectProps } from "metabase/ui";
+import type { SelectData } from "metabase/ui/components/inputs/Select/Select";
+
+import type { ScheduleMode } from "./types";
+
+interface Props extends Omit<SelectProps, "data" | "value" | "onChange"> {
+ value: ScheduleMode;
+ onChange: (value: ScheduleMode) => void;
+}
+
+export const ScheduleModePicker = ({
+ comboboxProps,
+ value,
+ onChange,
+ onFocus,
+ ...props
+}: Props) => {
+ const data = useMemo(() => getData(), []);
+
+ return (
+ <Select<ScheduleMode>
+ comboboxProps={{
+ middlewares: {
+ flip: true,
+ size: {
+ padding: 6,
+ },
+ },
+ position: "bottom-start",
+ ...comboboxProps,
+ }}
+ data={data}
+ value={value}
+ onChange={(value) => onChange(value)}
+ {...props}
+ />
+ );
+};
+
+function getData(): SelectData<ScheduleMode> {
+ return [
+ {
+ label: t`Regularly, on a schedule`,
+ value: "full",
+ },
+ {
+ label: t`Only when adding a new filter widget`,
+ value: "on-demand",
+ },
+ {
+ label: t`Never, I'll do this manually if I need to`,
+ value: "none",
+ },
+ ];
+}
diff --git a/frontend/src/metabase/databases/components/DatabaseCacheScheduleField/index.ts b/frontend/src/metabase/databases/components/DatabaseCacheScheduleField/index.ts
index d1ec66136dd..3416660fff3 100644
--- a/frontend/src/metabase/databases/components/DatabaseCacheScheduleField/index.ts
+++ b/frontend/src/metabase/databases/components/DatabaseCacheScheduleField/index.ts
@@ -1,2 +1 @@
-// eslint-disable-next-line import/no-default-export -- deprecated usage
-export { default } from "./DatabaseCacheScheduleField";
+export * from "./DatabaseCacheScheduleField";
diff --git a/frontend/src/metabase/databases/components/DatabaseCacheScheduleField/types.ts b/frontend/src/metabase/databases/components/DatabaseCacheScheduleField/types.ts
new file mode 100644
index 00000000000..b761a8b61fe
--- /dev/null
+++ b/frontend/src/metabase/databases/components/DatabaseCacheScheduleField/types.ts
@@ -0,0 +1 @@
+export type ScheduleMode = "full" | "on-demand" | "none";
diff --git a/frontend/src/metabase/databases/constants.tsx b/frontend/src/metabase/databases/constants.tsx
index c1a7d006001..5c8649631ad 100644
--- a/frontend/src/metabase/databases/constants.tsx
+++ b/frontend/src/metabase/databases/constants.tsx
@@ -4,7 +4,7 @@ import { SAVED_QUESTIONS_VIRTUAL_DB_ID } from "metabase-lib/v1/metadata/utils/sa
import DatabaseAuthCodeDescription from "./components/DatabaseAuthCodeDescription";
import DatabaseAuthProviderSectionField from "./components/DatabaseAuthProviderSectionField";
-import DatabaseCacheScheduleField from "./components/DatabaseCacheScheduleField";
+import { DatabaseCacheScheduleField } from "./components/DatabaseCacheScheduleField";
import DatabaseClientIdDescription from "./components/DatabaseClientIdDescription";
import { DatabaseConnectionSectionField } from "./components/DatabaseConnectionSectionField";
import DatabaseHostnameSectionField from "./components/DatabaseHostnameSectionField";