Skip to content

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

Unify Custom homepage and Landing page into a single Homepage radio (#74724)

Andrei Chernykh565cccc7b4f7
diff --git a/e2e/test/scenarios/admin-2/whitelabel.cy.spec.js b/e2e/test/scenarios/admin-2/whitelabel.cy.spec.js
index 23c847395e6..7d8efccf67d 100644
--- a/e2e/test/scenarios/admin-2/whitelabel.cy.spec.js
+++ b/e2e/test/scenarios/admin-2/whitelabel.cy.spec.js
@@ -729,46 +729,44 @@ describe("formatting > whitelabel", { tags: "@EE" }, () => {
cy.intercept("GET", "/api/setting").as("getSettings");
cy.signInAsAdmin();
cy.visit("/admin/settings/general");
+ cy.wait("@getSettings");
});
- it("should not render the widget when users does not have a valid license", () => {
+ const urlInput = () =>
+ cy
+ .findByTestId("homepage-setting")
+ .findByLabelText("Landing page custom destination");
+
+ it("should not offer the Custom URL option when the user does not have a valid license", () => {
H.activateToken("starter");
cy.reload();
- cy.findByLabelText("Landing page custom destination").should("not.exist");
+ cy.wait("@getSettings");
+ // Anchor on a positive signal so the negative assertion can't pass while
+ // the page is still loading.
+ cy.findByTestId("homepage-setting")
+ .findByRole("radio", { name: "Default Metabase home" })
+ .should("be.visible");
+ cy.findByTestId("homepage-setting")
+ .findByRole("radio", { name: "Custom URL" })
+ .should("not.exist");
});
- it("should allow users to provide internal urls", () => {
- cy.findByLabelText("Landing page custom destination")
- .click()
- .clear()
- .type("/test-1")
- .blur();
- H.undoToast().findByText("Changes saved").should("be.visible");
-
- H.goToMainApp();
- cy.url().should("include", "/test-1");
- });
+ it("should validate the URL and persist only same-origin relative paths", () => {
+ cy.findByTestId("homepage-setting")
+ .findByRole("radio", { name: "Custom URL" })
+ .click();
- it("should not allow users to provide external urls", () => {
- cy.findByLabelText("Landing page custom destination")
- .click()
- .clear()
- .type("/test-2")
- .blur();
+ urlInput().click().clear().type("/test-1").blur();
H.undoToast().findByText("Changes saved").should("be.visible");
- // set to valid value then test invalid value is not persisted
- cy.findByLabelText("Landing page custom destination")
- .click()
- .clear()
- .type("https://google.com")
- .blur();
+ // External URLs are rejected and the previous value is preserved.
+ urlInput().click().clear().type("https://google.com").blur();
cy.findByTestId("admin-layout-content")
.findByText("This field must be a relative URL.")
.should("be.visible");
H.goToMainApp();
- cy.url().should("include", "/test-2");
+ cy.url().should("include", "/test-1");
});
});
});
diff --git a/e2e/test/scenarios/onboarding/home/homepage.cy.spec.js b/e2e/test/scenarios/onboarding/home/homepage.cy.spec.js
index 65b74c14487..65c6ff52199 100644
--- a/e2e/test/scenarios/onboarding/home/homepage.cy.spec.js
+++ b/e2e/test/scenarios/onboarding/home/homepage.cy.spec.js
@@ -330,19 +330,23 @@ describe("scenarios > home > custom homepage", () => {
H.restore();
cy.signInAsAdmin();
cy.intercept("GET", "/api/search*").as("search");
+ cy.intercept("PUT", "/api/setting").as("putSettings");
});
it("should give you the option to set a custom home page in settings", () => {
cy.visit("/admin/settings/general");
- cy.findByTestId("custom-homepage-setting").within(() => {
- cy.findByText("Disabled").should("be.visible");
- cy.findByLabelText("Disabled").click();
- cy.findByText("Enabled").should("be.visible");
+ cy.findByTestId("homepage-setting").within(() => {
+ cy.findByRole("radio", { name: "Default Metabase home" }).should(
+ "be.checked",
+ );
+ cy.findByRole("radio", { name: "Dashboard" }).click();
});
+ cy.wait("@putSettings");
cy.findByTestId("custom-homepage-dashboard-setting")
.findByRole("button")
+ .should("be.visible")
.click();
H.entityPickerModal().findByText("Orders in a dashboard").click();
@@ -355,34 +359,23 @@ describe("scenarios > home > custom homepage", () => {
);
cy.log(
- "disabling custom-homepage-setting should also remove custom-homepage-dashboard-setting",
+ "switching to Default Metabase home should hide the dashboard picker but keep the persisted id",
);
cy.visit("/admin/settings/general");
- cy.findByTestId("custom-homepage-setting").within(() => {
- cy.findByText("Enabled").should("exist");
- cy.findByLabelText("Enabled").click();
- cy.findByText("Disabled").should("exist");
+ cy.findByTestId("homepage-setting").within(() => {
+ cy.findByRole("radio", { name: "Dashboard" }).should("be.checked");
+ cy.findByRole("radio", { name: "Default Metabase home" }).click();
});
+ cy.wait("@putSettings");
H.undoToast().findByText("Changes saved").should("be.visible");
+ cy.findByTestId("custom-homepage-dashboard-setting").should("not.exist");
- cy.findByTestId("custom-homepage-setting").within(() => {
- cy.findByText("Disabled").should("exist");
- cy.findByLabelText("Disabled").click();
- cy.findByText("Enabled").should("exist");
- });
-
- cy.findByTestId("custom-homepage-dashboard-setting").should(
- "contain",
- "Pick a dashboard",
- );
-
- cy.findByTestId("custom-homepage-dashboard-setting")
- .findByRole("button")
+ cy.findByTestId("homepage-setting")
+ .findByRole("radio", { name: "Dashboard" })
.click();
-
- H.entityPickerModal().findByText("Orders in a dashboard").click();
+ cy.wait("@putSettings");
cy.findByTestId("custom-homepage-dashboard-setting").should(
"contain",
@@ -642,13 +635,16 @@ describe("scenarios > setup", () => {
});
it("should send snowplow events through admin settings", () => {
+ cy.intercept("PUT", "/api/setting").as("putSettings");
cy.visit("/admin/settings/general");
- cy.findByTestId("custom-homepage-setting")
- .findByLabelText("Disabled")
+ cy.findByTestId("homepage-setting")
+ .findByRole("radio", { name: "Dashboard" })
.click();
+ cy.wait("@putSettings");
cy.findByTestId("custom-homepage-dashboard-setting")
.findByRole("button")
+ .should("be.visible")
.click();
H.entityPickerModal().findByText("Orders in a dashboard").click();
diff --git a/enterprise/frontend/src/metabase-enterprise/whitelabel/components/LandingPageWidget/LandingPageWidget.tsx b/enterprise/frontend/src/metabase-enterprise/whitelabel/components/LandingPageUrlField/LandingPageUrlField.tsx
similarity index 67%
rename from enterprise/frontend/src/metabase-enterprise/whitelabel/components/LandingPageWidget/LandingPageWidget.tsx
rename to enterprise/frontend/src/metabase-enterprise/whitelabel/components/LandingPageUrlField/LandingPageUrlField.tsx
index 36cbaf70bf2..ea363ff2d84 100644
--- a/enterprise/frontend/src/metabase-enterprise/whitelabel/components/LandingPageWidget/LandingPageWidget.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/whitelabel/components/LandingPageUrlField/LandingPageUrlField.tsx
@@ -1,16 +1,15 @@
import { useEffect, useState } from "react";
import { t } from "ttag";
-import { SettingHeader } from "metabase/admin/settings/components/SettingHeader";
import { useAdminSetting } from "metabase/api/utils";
-import { TextInput } from "metabase/ui";
+import { Stack, Text, TextInput } from "metabase/ui";
import type { GenericErrorResponse } from "metabase/utils/errors";
import { getRelativeLandingPageUrl } from "./utils";
-export function LandingPageWidget() {
+export function LandingPageUrlField() {
const [error, setError] = useState<string | null>(null);
- const { value, updateSetting, description } = useAdminSetting("landing-page");
+ const { value, updateSetting } = useAdminSetting("landing-page");
const [inputValue, setInputValue] = useState(value ?? "");
useEffect(() => {
@@ -19,10 +18,8 @@ export function LandingPageWidget() {
}
}, [value]);
- const handleChange = async (value: string) => {
- const { isSameOrigin, relativeUrl } = getRelativeLandingPageUrl(
- value.trim(),
- );
+ const handleBlur = async (raw: string) => {
+ const { isSameOrigin, relativeUrl } = getRelativeLandingPageUrl(raw.trim());
if (!isSameOrigin) {
setError(t`This field must be a relative URL.`);
@@ -44,25 +41,24 @@ export function LandingPageWidget() {
};
return (
- <div>
- <SettingHeader
- id="landing-page"
- title={t`Landing page`}
- description={description}
- />
+ <Stack gap="xs">
<TextInput
id="landing-page"
data-testid="landing-page"
aria-label={t`Landing page custom destination`}
- placeholder="/"
+ aria-describedby="landing-page-hint"
+ placeholder="/dashboard/1"
error={error}
- value={String(inputValue ?? "")}
+ value={inputValue}
onChange={(e) => {
setError(null);
setInputValue(e.target.value);
}}
- onBlur={(e) => handleChange(e.target.value)}
+ onBlur={(e) => handleBlur(e.target.value)}
/>
- </div>
+ <Text id="landing-page-hint" size="xs" c="text-secondary">
+ {t`Enter a relative URL like /dashboard/1 or /collection/2.`}
+ </Text>
+ </Stack>
);
}
diff --git a/enterprise/frontend/src/metabase-enterprise/whitelabel/components/LandingPageUrlField/index.ts b/enterprise/frontend/src/metabase-enterprise/whitelabel/components/LandingPageUrlField/index.ts
new file mode 100644
index 00000000000..5cfeef87219
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/whitelabel/components/LandingPageUrlField/index.ts
@@ -0,0 +1 @@
+export { LandingPageUrlField } from "./LandingPageUrlField";
diff --git a/enterprise/frontend/src/metabase-enterprise/whitelabel/components/LandingPageWidget/utils.ts b/enterprise/frontend/src/metabase-enterprise/whitelabel/components/LandingPageUrlField/utils.ts
similarity index 100%
rename from enterprise/frontend/src/metabase-enterprise/whitelabel/components/LandingPageWidget/utils.ts
rename to enterprise/frontend/src/metabase-enterprise/whitelabel/components/LandingPageUrlField/utils.ts
diff --git a/enterprise/frontend/src/metabase-enterprise/whitelabel/components/LandingPageWidget/utils.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/whitelabel/components/LandingPageUrlField/utils.unit.spec.ts
similarity index 100%
rename from enterprise/frontend/src/metabase-enterprise/whitelabel/components/LandingPageWidget/utils.unit.spec.ts
rename to enterprise/frontend/src/metabase-enterprise/whitelabel/components/LandingPageUrlField/utils.unit.spec.ts
diff --git a/enterprise/frontend/src/metabase-enterprise/whitelabel/components/LandingPageWidget/index.tsx b/enterprise/frontend/src/metabase-enterprise/whitelabel/components/LandingPageWidget/index.tsx
deleted file mode 100644
index 5b6d743d347..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/whitelabel/components/LandingPageWidget/index.tsx
+++ /dev/null
@@ -1 +0,0 @@
-export * from "./LandingPageWidget";
diff --git a/enterprise/frontend/src/metabase-enterprise/whitelabel/index.ts b/enterprise/frontend/src/metabase-enterprise/whitelabel/index.ts
index 386a8926866..346ac78463c 100644
--- a/enterprise/frontend/src/metabase-enterprise/whitelabel/index.ts
+++ b/enterprise/frontend/src/metabase-enterprise/whitelabel/index.ts
@@ -1,5 +1,8 @@
+import { t } from "ttag";
+
import {
PLUGIN_APP_INIT_FUNCTIONS,
+ PLUGIN_HOMEPAGE_SETTING,
PLUGIN_LANDING_PAGE,
PLUGIN_LOGO_ICON_COMPONENTS,
PLUGIN_SELECTORS,
@@ -19,7 +22,7 @@ import {
} from "metabase-enterprise/settings/selectors";
import type { SettingKey } from "metabase-types/api";
-import { LandingPageWidget } from "./components/LandingPageWidget";
+import { LandingPageUrlField } from "./components/LandingPageUrlField";
import LogoIcon from "./components/LogoIcon";
import { WhiteLabelBrandingSettingsPage } from "./components/WhiteLabelBrandingSettingsPage";
import { WhiteLabelConcealSettingsPage } from "./components/WhiteLabelConcealSettingsPage";
@@ -33,7 +36,10 @@ export function initializePlugin() {
PLUGIN_LANDING_PAGE.getLandingPage = () =>
// TODO: MetabaseSettings doesn't have EnterpriseSetting keys, so the type and mapping don't work
MetabaseSettings.get("landing-page" as SettingKey) as string;
- PLUGIN_LANDING_PAGE.LandingPageWidget = LandingPageWidget;
+ PLUGIN_HOMEPAGE_SETTING.CustomUrlOption = {
+ label: t`Custom URL`,
+ Control: LandingPageUrlField,
+ };
PLUGIN_WHITELABEL.WhiteLabelBrandingSettingsPage =
WhiteLabelBrandingSettingsPage;
diff --git a/frontend/src/metabase/admin/settings/components/SettingHeader/SettingHeader.tsx b/frontend/src/metabase/admin/settings/components/SettingHeader/SettingHeader.tsx
index 5ab3b3e4f41..7862f0add3b 100644
--- a/frontend/src/metabase/admin/settings/components/SettingHeader/SettingHeader.tsx
+++ b/frontend/src/metabase/admin/settings/components/SettingHeader/SettingHeader.tsx
@@ -10,7 +10,7 @@ export const SettingHeader = ({
titleProps,
...boxProps
}: {
- id: string;
+ id?: string;
description?: string | React.ReactNode;
title?: string;
titleProps?: TextProps;
diff --git a/frontend/src/metabase/admin/settings/components/SettingsPages/GeneralSettingsPage.tsx b/frontend/src/metabase/admin/settings/components/SettingsPages/GeneralSettingsPage.tsx
index fbc6c63eaca..120286e5b06 100644
--- a/frontend/src/metabase/admin/settings/components/SettingsPages/GeneralSettingsPage.tsx
+++ b/frontend/src/metabase/admin/settings/components/SettingsPages/GeneralSettingsPage.tsx
@@ -8,11 +8,11 @@ import { CollectUserDataInput } from "metabase/admin/settings/components/widgets
import { UpsellDevInstances } from "metabase/admin/upsells";
import { ExternalLink } from "metabase/common/components/ExternalLink";
import { useDocsUrl, useHasTokenFeature } from "metabase/common/hooks";
-import { PLUGIN_LANDING_PAGE, PLUGIN_SEMANTIC_SEARCH } from "metabase/plugins";
+import { PLUGIN_SEMANTIC_SEARCH } from "metabase/plugins";
import { DevInstanceBanner } from "../GeneralSettings/DevInstanceBanner";
import { AdminSettingInput } from "../widgets/AdminSettingInput";
-import { CustomHomepageDashboardSetting } from "../widgets/CustomHomepageDashboardSetting";
+import { HomepageSetting } from "../widgets/HomepageSetting";
import { HttpsOnlyWidget } from "../widgets/HttpsOnlyWidget";
import { SiteUrlWidget } from "../widgets/SiteUrlWidget";
import { AnonymousTrackingInput } from "../widgets/UsageTracking/AnonymousTrackingInput";
@@ -42,9 +42,7 @@ export function GeneralSettingsPage() {
<PLUGIN_SEMANTIC_SEARCH.SearchSettingsWidget />
- <CustomHomepageDashboardSetting />
-
- <PLUGIN_LANDING_PAGE.LandingPageWidget />
+ <HomepageSetting />
</SettingsSection>
<SettingsSection title={t`Email`}>
diff --git a/frontend/src/metabase/admin/settings/components/SettingsPages/GeneralSettingsPage.unit.spec.tsx b/frontend/src/metabase/admin/settings/components/SettingsPages/GeneralSettingsPage.unit.spec.tsx
index 5c0b77007a4..b4850fd0837 100644
--- a/frontend/src/metabase/admin/settings/components/SettingsPages/GeneralSettingsPage.unit.spec.tsx
+++ b/frontend/src/metabase/admin/settings/components/SettingsPages/GeneralSettingsPage.unit.spec.tsx
@@ -91,7 +91,7 @@ describe("GeneralSettingsPage", () => {
"Site name",
"Site url",
"Redirect to HTTPS",
- "Custom homepage",
+ "Homepage",
"Email address for help requests",
"Send anonymous tracking data to Metabase",
"Collect user data to display in usage analytics",
diff --git a/frontend/src/metabase/admin/settings/components/widgets/CustomHomepageDashboardSetting.tsx b/frontend/src/metabase/admin/settings/components/widgets/CustomHomepageDashboardSetting.tsx
deleted file mode 100644
index e65f080fbac..00000000000
--- a/frontend/src/metabase/admin/settings/components/widgets/CustomHomepageDashboardSetting.tsx
+++ /dev/null
@@ -1,88 +0,0 @@
-import { t } from "ttag";
-
-import { useAdminSetting } from "metabase/api/utils";
-import { trackCustomHomepageDashboardEnabled } from "metabase/common/analytics";
-import { DashboardSelector } from "metabase/common/components/DashboardSelector";
-import { useDispatch } from "metabase/redux";
-import { refreshCurrentUser } from "metabase/redux/user";
-import { Stack } from "metabase/ui";
-import type { DashboardId } from "metabase-types/api";
-
-import { SettingHeader } from "../SettingHeader";
-
-import { BasicAdminSettingInput } from "./AdminSettingInput";
-
-export function CustomHomepageDashboardSetting() {
- const {
- value: customHomepage,
- updateSetting,
- description,
- } = useAdminSetting("custom-homepage");
- const { value: customHomepageDashboardId } = useAdminSetting(
- "custom-homepage-dashboard",
- );
- const dispatch = useDispatch();
-
- const handleDashboardChange = async (newDashboardId?: DashboardId) => {
- const result = await updateSetting({
- key: "custom-homepage-dashboard",
- value: newDashboardId ?? null,
- });
-
- if (!result) {
- return;
- }
-
- if (newDashboardId && !customHomepageDashboardId) {
- trackCustomHomepageDashboardEnabled("admin");
- }
-
- await updateSetting({
- key: "dismissed-custom-dashboard-toast",
- value: true,
- toast: false,
- });
-
- await dispatch(refreshCurrentUser());
- };
-
- const handleToggleChange = async (newValue: boolean) => {
- await updateSetting({
- key: "custom-homepage",
- value: newValue,
- toast: false,
- });
- if (newValue === false) {
- await updateSetting({
- key: "custom-homepage-dashboard",
- value: null,
- });
- }
-
- await dispatch(refreshCurrentUser());
- };
-
- return (
- <Stack data-testid="custom-homepage-setting">
- <SettingHeader
- id="custom-homepage"
- title={t`Custom homepage`}
- description={description}
- />
- <BasicAdminSettingInput
- name="custom-homepage"
- inputType="boolean"
- value={customHomepage}
- onChange={(newValue) => handleToggleChange(Boolean(newValue))}
- />
- {customHomepage && (
- <div data-testid="custom-homepage-dashboard-setting">
- <DashboardSelector
- value={customHomepageDashboardId ?? undefined}
- onChange={handleDashboardChange}
- />
- </div>
- )}
- </Stack>
- );
-}
diff --git a/frontend/src/metabase/admin/settings/components/widgets/CustomHomepageDashboardSetting.unit.spec.tsx b/frontend/src/metabase/admin/settings/components/widgets/CustomHomepageDashboardSetting.unit.spec.tsx
deleted file mode 100644
index 67875b5b797..00000000000
--- a/frontend/src/metabase/admin/settings/components/widgets/CustomHomepageDashboardSetting.unit.spec.tsx
+++ /dev/null
@@ -1,224 +0,0 @@
-import userEvent from "@testing-library/user-event";
-import fetchMock from "fetch-mock";
-
-import {
- findRequests,
- setupCollectionByIdEndpoint,
- setupCollectionItemsEndpoint,
- setupCollectionsEndpoints,
- setupCurrentUserEndpoint,
- setupDashboardEndpoints,
- setupPropertiesEndpoints,
- setupRecentViewsAndSelectionsEndpoints,
- setupSettingsEndpoints,
- setupUpdateSettingEndpoint,
-} from "__support__/server-mocks";
-import {
- mockGetBoundingClientRect,
- renderWithProviders,
- screen,
- waitFor,
-} from "__support__/ui";
-import { UndoListing } from "metabase/common/components/UndoListing";
-import {
- createMockCollection,
- createMockCollectionItem,
- createMockDashboard,
- createMockRecentCollectionItem,
- createMockSettingDefinition,
- createMockSettings,
- createMockUser,
-} from "metabase-types/api/mocks";
-
-import { CustomHomepageDashboardSetting } from "./CustomHomepageDashboardSetting";
-
-const setup = (
- props: {
- "custom-homepage"?: boolean;
- "custom-homepage-dashboard"?: number | null;
- "dismissed-custom-dashboard-toast"?: boolean;
- } = {
- "custom-homepage": false,
- "custom-homepage-dashboard": null,
- "dismissed-custom-dashboard-toast": false,
- },
-) => {
- mockGetBoundingClientRect();
- const settings = createMockSettings(props);
-
- const collection = createMockCollection({
- id: 1,
- name: "Good Collection",
- });
-
- setupCollectionsEndpoints({
- collections: [collection],
- });
-
- setupCollectionByIdEndpoint({ collections: [collection] });
- setupCollectionItemsEndpoint({
- collection: { id: "root" },
- collectionItems: [
- createMockCollectionItem({
- model: "dashboard",
- id: 4242,
- can_write: true,
- name: "My dashboard",
- }),
- ],
- });
-
- setupCollectionItemsEndpoint({
- collection: { id: 1 },
- collectionItems: [
- createMockCollectionItem({
- model: "dashboard",
- id: 4243,
- can_write: true,
- name: "My other dashboard",
- }),
- ],
- });
-
- setupRecentViewsAndSelectionsEndpoints([
- createMockRecentCollectionItem({
- model: "dashboard",
- id: 4242,
- can_write: true,
- name: "My dashboard",
- }),
- ]);
-
- setupPropertiesEndpoints(settings);
- setupCurrentUserEndpoint(
- createMockUser({
- id: 1,
- email: "[email protected]",
- }),
- );
-
- setupDashboardEndpoints(
- createMockDashboard({
- id: 4242,
- name: "My dashboard",
- }),
- );
-
- setupUpdateSettingEndpoint();
- setupSettingsEndpoints([
- createMockSettingDefinition({
- key: "custom-homepage",
- description: "Pick one of your dashboards",
- value: false,
- }),
- createMockSettingDefinition({
- key: "custom-homepage-dashboard",
- value: null,
- }),
- createMockSettingDefinition({
- key: "dismissed-custom-dashboard-toast",
- value: false,
- }),
- ]);
-
- return renderWithProviders(
- <div>
- <CustomHomepageDashboardSetting />
- <UndoListing />
- </div>,
- );
-};
-
-describe("CustomHomepageDashboardSetting", () => {
- it("should render a Custom homepage toggle", async () => {
- setup();
- expect(await screen.findByText("Custom homepage")).toBeInTheDocument();
- expect(
- await screen.findByText(/Pick one of your dashboards/),
- ).toBeInTheDocument();
- expect(screen.getByRole("switch")).toBeInTheDocument();
- expect(screen.getByRole("switch")).not.toBeChecked();
- expect(screen.queryByText("Pick a dashboard")).not.toBeInTheDocument();
- });
-
- it("should render dashboard picker if toggle is on", async () => {
- setup({ "custom-homepage": true });
- await waitFor(() => {
- expect(screen.getByRole("switch")).toBeChecked();
- });
- expect(screen.getByText("Pick a dashboard")).toBeInTheDocument();
- });
-
- it("should update the toggle value", async () => {
- setup({ "custom-homepage": false });
- const toggle = await screen.findByRole("switch");
- setupPropertiesEndpoints(createMockSettings({ "custom-homepage": true }));
- await userEvent.click(toggle);
- expect(await screen.findByRole("switch")).toBeChecked();
-
- const [{ url, body }] = await findRequests("PUT");
- expect(url).toContain("/api/setting/custom-homepage");
- expect(body).toEqual({ value: true });
- });
-
- it("should show selected dashboard name", async () => {
- setup({ "custom-homepage": true, "custom-homepage-dashboard": 4242 });
- expect(await screen.findByText("My dashboard")).toBeInTheDocument();
- });
-
- it("should update the dashboard value", async () => {
- setup({ "custom-homepage": true });
- const dashboardSelector = await screen.findByText("Pick a dashboard");
- await userEvent.click(dashboardSelector);
-
- await screen.findByText("Choose a dashboard"); // modal opens
- setupPropertiesEndpoints(
- createMockSettings({
- "custom-homepage": true,
- "custom-homepage-dashboard": 4242,
- }),
- );
-
- await userEvent.click(await screen.findByText(/My dashboard/));
- await waitFor(() => {
- // modal closes
- expect(screen.queryByText("Choose a dashboard")).not.toBeInTheDocument();
- });
-
- const [{ url: putUrl, body: putBody }, { url: putUrl2, body: putBody2 }] =
- await findRequests("PUT");
- expect(putUrl).toContain("/api/setting/custom-homepage-dashboard");
- expect(putBody).toEqual({ value: 4242 });
- expect(putUrl2).toContain("/api/setting/dismissed-custom-dashboard-toast");
- expect(putBody2).toEqual({ value: true });
-
- // dashboard name should be displayed
- expect(await screen.findByText("My dashboard")).toBeInTheDocument();
- });
-
- it("should hide dashboard selector when toggling off", async () => {
- setup({ "custom-homepage": true, "custom-homepage-dashboard": 4242 });
- const toggle = await screen.findByRole("switch");
- setupPropertiesEndpoints(createMockSettings({ "custom-homepage": false }));
- await userEvent.click(toggle);
- expect(await screen.findByRole("switch")).not.toBeChecked();
- expect(screen.queryByText(/my dashboard/i)).not.toBeInTheDocument();
- expect(screen.queryByText("Pick a dashboard")).not.toBeInTheDocument();
- });
-
- it("should refresh current user after updating settings", async () => {
- setup({ "custom-homepage": true });
- const dashboardSelector = await screen.findByText("Pick a dashboard");
- await userEvent.click(dashboardSelector);
-
- await userEvent.click(await screen.findByText(/My dashboard/));
-
- await waitFor(() => {
- const calls = fetchMock.callHistory.calls();
- const userCall = calls.find((call) =>
- call.url?.includes("/api/user/current"),
- );
- expect(userCall).toBeDefined();
- });
- });
-});
diff --git a/frontend/src/metabase/admin/settings/components/widgets/HomepageSetting.tsx b/frontend/src/metabase/admin/settings/components/widgets/HomepageSetting.tsx
new file mode 100644
index 00000000000..6675e9c3c69
--- /dev/null
+++ b/frontend/src/metabase/admin/settings/components/widgets/HomepageSetting.tsx
@@ -0,0 +1,131 @@
+import { useState } from "react";
+import { t } from "ttag";
+
+import { useAdminSetting } from "metabase/api/utils";
+import { trackCustomHomepageDashboardEnabled } from "metabase/common/analytics";
+import { DashboardSelector } from "metabase/common/components/DashboardSelector";
+import { PLUGIN_HOMEPAGE_SETTING } from "metabase/plugins";
+import { useDispatch } from "metabase/redux";
+import { refreshCurrentUser } from "metabase/redux/user";
+import { Box, Radio, Stack, Text } from "metabase/ui";
+import type { DashboardId } from "metabase-types/api";
+
+import { SettingHeader } from "../SettingHeader";
+
+type Mode = "default" | "dashboard" | "url";
+
+export function getHomepageMode(
+ landingPage: string | undefined | null,
+ customHomepage: boolean | undefined | null,
+): Mode {
+ if (landingPage && landingPage !== "/") {
+ return "url";
+ }
+ if (customHomepage) {
+ return "dashboard";
+ }
+ return "default";
+}
+
+export function HomepageSetting() {
+ const dispatch = useDispatch();
+
+ const { value: customHomepage, updateSettings } =
+ useAdminSetting("custom-homepage");
+ const { value: customHomepageDashboardId } = useAdminSetting(
+ "custom-homepage-dashboard",
+ );
+ const { value: landingPage } = useAdminSetting("landing-page");
+
+ const persistedMode = getHomepageMode(landingPage, customHomepage);
+
+ // Optimistic local mode so the UI flips on click instead of waiting for the
+ // bulk PUT to settle. Persisted settings naturally catch up after each save,
+ // so this stays in sync without ever being cleared.
+ const [localMode, setLocalMode] = useState<Mode | null>(null);
+
+ const mode: Mode = localMode ?? persistedMode;
+
+ // Keep the persisted dashboard id across mode switches so the user doesn't
+ // re-pick when toggling away and back. Backend ignores it unless
+ // custom-homepage is also true.
+ const handleModeChange = async (next: Mode) => {
+ setLocalMode(next);
+ if (next === "default") {
+ await updateSettings({
+ "custom-homepage": false,
+ "landing-page": "",
+ });
+ } else if (next === "dashboard") {
+ await updateSettings({
+ "custom-homepage": true,
+ "landing-page": "",
+ });
+ } else {
+ await updateSettings({
+ "custom-homepage": false,
+ });
+ }
+ await dispatch(refreshCurrentUser());
+ };
+
+ const handleDashboardChange = async (newDashboardId?: DashboardId) => {
+ const wasUnset = !customHomepageDashboardId;
+ await updateSettings({
+ "custom-homepage-dashboard": newDashboardId ?? null,
+ "dismissed-custom-dashboard-toast": true,
+ });
+ if (newDashboardId && wasUnset) {
+ trackCustomHomepageDashboardEnabled("admin");
+ }
+ await dispatch(refreshCurrentUser());
+ };
+
+ return (
+ <Stack data-testid="homepage-setting">
+ <SettingHeader
+ title={t`Homepage`}
+ description={t`Where users land after signing in.`}
+ />
+ <Radio.Group
+ value={mode}
+ onChange={(v) => handleModeChange(v as Mode)}
+ name="homepage-mode"
+ aria-label={t`Homepage`}
+ >
+ <Stack gap="md">
+ <Radio value="default" label={t`Default Metabase home`} />
+
+ <Stack gap="xs">
+ <Radio value="dashboard" label={t`Dashboard`} />
+ {mode === "dashboard" && (
+ <Box pl="xl" data-testid="custom-homepage-dashboard-setting">
+ <DashboardSelector
+ value={customHomepageDashboardId ?? undefined}
+ onChange={handleDashboardChange}
+ />
+ <Text size="xs" c="text-secondary" mt="xs">
+ {t`Users without dashboard access see the default home.`}
+ </Text>
+ </Box>
+ )}
+ </Stack>
+
+ {PLUGIN_HOMEPAGE_SETTING.CustomUrlOption && (
+ <Stack gap="xs">
+ <Radio
+ value="url"
+ label={PLUGIN_HOMEPAGE_SETTING.CustomUrlOption.label}
+ />
+ {mode === "url" && (
+ <Box pl="xl">
+ <PLUGIN_HOMEPAGE_SETTING.CustomUrlOption.Control />
+ </Box>
+ )}
+ </Stack>
+ )}
+ </Stack>
+ </Radio.Group>
+ </Stack>
+ );
+}
diff --git a/frontend/src/metabase/admin/settings/components/widgets/HomepageSetting.unit.spec.tsx b/frontend/src/metabase/admin/settings/components/widgets/HomepageSetting.unit.spec.tsx
new file mode 100644
index 00000000000..be5ad756eaa
--- /dev/null
+++ b/frontend/src/metabase/admin/settings/components/widgets/HomepageSetting.unit.spec.tsx
@@ -0,0 +1,322 @@
+import userEvent from "@testing-library/user-event";
+import fetchMock from "fetch-mock";
+
+import {
+ findRequests,
+ setupCollectionByIdEndpoint,
+ setupCollectionItemsEndpoint,
+ setupCollectionsEndpoints,
+ setupCurrentUserEndpoint,
+ setupDashboardEndpoints,
+ setupPropertiesEndpoints,
+ setupRecentViewsAndSelectionsEndpoints,
+ setupSettingsEndpoints,
+ setupUpdateSettingsEndpoint,
+} from "__support__/server-mocks";
+import {
+ mockGetBoundingClientRect,
+ renderWithProviders,
+ screen,
+ waitFor,
+} from "__support__/ui";
+import { UndoListing } from "metabase/common/components/UndoListing";
+import { PLUGIN_HOMEPAGE_SETTING } from "metabase/plugins";
+import {
+ createMockCollection,
+ createMockCollectionItem,
+ createMockDashboard,
+ createMockRecentCollectionItem,
+ createMockSettingDefinition,
+ createMockSettings,
+ createMockUser,
+} from "metabase-types/api/mocks";
+
+import { HomepageSetting, getHomepageMode } from "./HomepageSetting";
+
+function FakeUrlControl() {
+ return <div data-testid="fake-url-control">URL CONTROL</div>;
+}
+
+type SetupProps = {
+ "custom-homepage"?: boolean;
+ "custom-homepage-dashboard"?: number | null;
+ "landing-page"?: string;
+ "dismissed-custom-dashboard-toast"?: boolean;
+ withCustomUrlPlugin?: boolean;
+};
+
+const setup = ({
+ withCustomUrlPlugin = false,
+ ...settingsProps
+}: SetupProps = {}) => {
+ mockGetBoundingClientRect();
+ const settings = createMockSettings({
+ "custom-homepage": false,
+ "custom-homepage-dashboard": null,
+ "landing-page": "",
+ "dismissed-custom-dashboard-toast": false,
+ ...settingsProps,
+ });
+
+ const collection = createMockCollection({ id: 1, name: "Good Collection" });
+ setupCollectionsEndpoints({ collections: [collection] });
+ setupCollectionByIdEndpoint({ collections: [collection] });
+ setupCollectionItemsEndpoint({
+ collection: { id: "root" },
+ collectionItems: [
+ createMockCollectionItem({
+ model: "dashboard",
+ id: 4242,
+ can_write: true,
+ name: "My dashboard",
+ }),
+ ],
+ });
+ setupCollectionItemsEndpoint({
+ collection: { id: 1 },
+ collectionItems: [
+ createMockCollectionItem({
+ model: "dashboard",
+ id: 4243,
+ can_write: true,
+ name: "My other dashboard",
+ }),
+ ],
+ });
+ setupRecentViewsAndSelectionsEndpoints([
+ createMockRecentCollectionItem({
+ model: "dashboard",
+ id: 4242,
+ can_write: true,
+ name: "My dashboard",
+ }),
+ ]);
+ setupDashboardEndpoints(
+ createMockDashboard({ id: 4242, name: "My dashboard" }),
+ );
+
+ setupPropertiesEndpoints(settings);
+ setupCurrentUserEndpoint(createMockUser({ id: 1, email: "[email protected]" }));
+ setupUpdateSettingsEndpoint();
+ setupSettingsEndpoints([
+ createMockSettingDefinition({ key: "custom-homepage", value: false }),
+ createMockSettingDefinition({
+ key: "custom-homepage-dashboard",
+ value: null,
+ }),
+ createMockSettingDefinition({ key: "landing-page", value: "" }),
+ createMockSettingDefinition({
+ key: "dismissed-custom-dashboard-toast",
+ value: false,
+ }),
+ ]);
+
+ PLUGIN_HOMEPAGE_SETTING.CustomUrlOption = withCustomUrlPlugin
+ ? { label: "Custom URL", Control: FakeUrlControl }
+ : null;
+
+ return renderWithProviders(
+ <div>
+ <HomepageSetting />
+ <UndoListing />
+ </div>,
+ );
+};
+
+const findBulkPutBody = async () => {
+ const requests = await findRequests("PUT");
+ const bulk = requests.find(({ url }) => url.endsWith("/api/setting"));
+ return bulk?.body as Record<string, unknown> | undefined;
+};
+
+describe("getHomepageMode", () => {
+ it("returns 'default' when nothing is set", () => {
+ expect(getHomepageMode(null, null)).toBe("default");
+ expect(getHomepageMode("", false)).toBe("default");
+ expect(getHomepageMode(undefined, undefined)).toBe("default");
+ });
+
+ it("treats a bare '/' landing page as 'default'", () => {
+ expect(getHomepageMode("/", false)).toBe("default");
+ expect(getHomepageMode("/", true)).toBe("dashboard");
+ });
+
+ it("returns 'dashboard' when custom-homepage is on and landing-page is empty", () => {
+ expect(getHomepageMode("", true)).toBe("dashboard");
+ expect(getHomepageMode(null, true)).toBe("dashboard");
+ });
+
+ it("returns 'url' when landing-page is set, taking precedence over custom-homepage", () => {
+ expect(getHomepageMode("/dashboard/7", false)).toBe("url");
+ expect(getHomepageMode("/dashboard/7", true)).toBe("url");
+ });
+});
+
+describe("HomepageSetting", () => {
+ afterEach(() => {
+ PLUGIN_HOMEPAGE_SETTING.CustomUrlOption = null;
+ });
+
+ it("renders the radio with default option selected when nothing is set", async () => {
+ setup();
+ expect(await screen.findByText("Homepage")).toBeInTheDocument();
+ expect(
+ screen.getByRole("radio", { name: "Default Metabase home" }),
+ ).toBeChecked();
+ expect(screen.getByRole("radio", { name: "Dashboard" })).not.toBeChecked();
+ expect(screen.queryByText("Pick a dashboard")).not.toBeInTheDocument();
+ });
+
+ it("hides Custom URL when the plugin is not registered", async () => {
+ setup();
+ await screen.findByText("Homepage");
+ expect(
+ screen.queryByRole("radio", { name: "Custom URL" }),
+ ).not.toBeInTheDocument();
+ });
+
+ it("shows Custom URL when the plugin is registered", async () => {
+ setup({ withCustomUrlPlugin: true });
+ expect(
+ await screen.findByRole("radio", { name: "Custom URL" }),
+ ).toBeInTheDocument();
+ });
+
+ it("selects Dashboard mode when custom-homepage is on", async () => {
+ setup({ "custom-homepage": true, "custom-homepage-dashboard": 4242 });
+ expect(
+ await screen.findByRole("radio", { name: "Dashboard" }),
+ ).toBeChecked();
+ expect(await screen.findByText("My dashboard")).toBeInTheDocument();
+ });
+
+ it("selects Custom URL mode when landing-page is set, taking precedence over custom-homepage", async () => {
+ setup({
+ withCustomUrlPlugin: true,
+ "custom-homepage": true,
+ "custom-homepage-dashboard": 4242,
+ "landing-page": "/dashboard/7",
+ });
+ const urlRadio = await screen.findByRole("radio", { name: "Custom URL" });
+ await waitFor(() => expect(urlRadio).toBeChecked());
+ expect(screen.getByTestId("fake-url-control")).toBeInTheDocument();
+ });
+
+ it("treats a bare-slash landing page as Default", async () => {
+ setup({ withCustomUrlPlugin: true, "landing-page": "/" });
+ expect(
+ await screen.findByRole("radio", { name: "Default Metabase home" }),
+ ).toBeChecked();
+ });
+
+ it("clears landing-page and turns custom-homepage on when switching to Dashboard", async () => {
+ setup({ withCustomUrlPlugin: true, "landing-page": "/some/url" });
+
+ await userEvent.click(
+ await screen.findByRole("radio", { name: "Dashboard" }),
+ );
+
+ await waitFor(async () => {
+ const body = await findBulkPutBody();
+ expect(body).toEqual({ "custom-homepage": true, "landing-page": "" });
+ });
+ });
+
+ it("clears landing-page and turns custom-homepage off when switching to Default", async () => {
+ setup({ withCustomUrlPlugin: true, "landing-page": "/some/url" });
+
+ await userEvent.click(
+ await screen.findByRole("radio", { name: "Default Metabase home" }),
+ );
+
+ await waitFor(async () => {
+ const body = await findBulkPutBody();
+ expect(body).toEqual({ "custom-homepage": false, "landing-page": "" });
+ });
+ });
+
+ it("turns custom-homepage off when switching to Custom URL", async () => {
+ setup({
+ withCustomUrlPlugin: true,
+ "custom-homepage": true,
+ "custom-homepage-dashboard": 4242,
+ });
+
+ await userEvent.click(
+ await screen.findByRole("radio", { name: "Custom URL" }),
+ );
+
+ await waitFor(async () => {
+ const body = await findBulkPutBody();
+ expect(body).toEqual({ "custom-homepage": false });
+ });
+ });
+
+ it("hides the dashboard picker immediately on mode switch and restores it on switch back", async () => {
+ // Optimistic UI: clicking another radio should flip the visible mode
+ // without waiting for the bulk PUT round-trip.
+ setup({ "custom-homepage": true, "custom-homepage-dashboard": 4242 });
+
+ expect(await screen.findByText("My dashboard")).toBeInTheDocument();
+
+ await userEvent.click(
+ screen.getByRole("radio", { name: "Default Metabase home" }),
+ );
+ expect(screen.queryByText("My dashboard")).not.toBeInTheDocument();
+
+ await userEvent.click(screen.getByRole("radio", { name: "Dashboard" }));
+ expect(await screen.findByText("My dashboard")).toBeInTheDocument();
+ });
+
+ it("does not clear the dashboard id when leaving Dashboard mode", async () => {
+ setup({ "custom-homepage": true, "custom-homepage-dashboard": 4242 });
+
+ await screen.findByText("My dashboard");
+ await userEvent.click(
+ screen.getByRole("radio", { name: "Default Metabase home" }),
+ );
+
+ await waitFor(async () => {
+ const body = await findBulkPutBody();
+ expect(body).toBeDefined();
+ expect(body).not.toHaveProperty("custom-homepage-dashboard");
+ });
+ });
+
+ it("writes the dashboard id and dismisses the toast when picking a dashboard", async () => {
+ setup({ "custom-homepage": true });
+ const picker = await screen.findByText("Pick a dashboard");
+ await userEvent.click(picker);
+
+ await screen.findByText("Choose a dashboard");
+ setupPropertiesEndpoints(
+ createMockSettings({
+ "custom-homepage": true,
+ "custom-homepage-dashboard": 4242,
+ }),
+ );
+ await userEvent.click(await screen.findByText(/My dashboard/));
+
+ await waitFor(async () => {
+ const body = await findBulkPutBody();
+ expect(body).toEqual({
+ "custom-homepage-dashboard": 4242,
+ "dismissed-custom-dashboard-toast": true,
+ });
+ });
+ });
+
+ it("refreshes the current user after switching modes", async () => {
+ setup();
+ await userEvent.click(
+ await screen.findByRole("radio", { name: "Dashboard" }),
+ );
+
+ await waitFor(() => {
+ const calls = fetchMock.callHistory.calls();
+ expect(
+ calls.find((call) => call.url?.includes("/api/user/current")),
+ ).toBeDefined();
+ });
+ });
+});
diff --git a/frontend/src/metabase/plugins/index.ts b/frontend/src/metabase/plugins/index.ts
index 52f5023845f..0e4c2d0fd33 100644
--- a/frontend/src/metabase/plugins/index.ts
+++ b/frontend/src/metabase/plugins/index.ts
@@ -39,6 +39,7 @@ export {
export {
PLUGIN_APP_INIT_FUNCTIONS,
PLUGIN_LANDING_PAGE,
+ PLUGIN_HOMEPAGE_SETTING,
PLUGIN_REDUX_MIDDLEWARES,
PLUGIN_LOGO_ICON_COMPONENTS,
PLUGIN_ADMIN_ALLOWED_PATH_GETTERS,
diff --git a/frontend/src/metabase/plugins/oss/core.ts b/frontend/src/metabase/plugins/oss/core.ts
index 9f366b53f16..b65bea4ab5c 100644
--- a/frontend/src/metabase/plugins/oss/core.ts
+++ b/frontend/src/metabase/plugins/oss/core.ts
@@ -3,7 +3,6 @@ import type { ComponentType } from "react";
import { t } from "ttag";
import noResultsSource from "assets/img/no_results.svg";
-import { PluginPlaceholder } from "metabase/plugins/components/PluginPlaceholder";
import type {
AdminPathKey,
DraftDashboardSubscription,
@@ -48,14 +47,20 @@ export const PLUGIN_APP_INIT_FUNCTIONS = getDefaultAppInitFunctions();
const getDefaultLandingPage = () => ({
getLandingPage: () => "/",
- LandingPageWidget: PluginPlaceholder,
});
export const PLUGIN_LANDING_PAGE: {
getLandingPage: () => string | null | undefined;
- LandingPageWidget: ComponentType;
} = getDefaultLandingPage();
+const getDefaultHomepageSetting = () => ({
+ CustomUrlOption: null,
+});
+
+export const PLUGIN_HOMEPAGE_SETTING: {
+ CustomUrlOption: { label: string; Control: ComponentType } | null;
+} = getDefaultHomepageSetting();
+
const getDefaultReduxMiddlewares = (): Middleware[] => [];
export const PLUGIN_REDUX_MIDDLEWARES = getDefaultReduxMiddlewares();
@@ -155,6 +160,7 @@ export function reinitialize() {
PLUGIN_APP_INIT_FUNCTIONS.push(...getDefaultAppInitFunctions());
Object.assign(PLUGIN_LANDING_PAGE, getDefaultLandingPage());
+ Object.assign(PLUGIN_HOMEPAGE_SETTING, getDefaultHomepageSetting());
PLUGIN_REDUX_MIDDLEWARES.length = 0;
PLUGIN_REDUX_MIDDLEWARES.push(...getDefaultReduxMiddlewares());
diff --git a/src/metabase/appearance/settings.clj b/src/metabase/appearance/settings.clj
index 4a02c48a810..f1c807f5cc6 100644
--- a/src/metabase/appearance/settings.clj
+++ b/src/metabase/appearance/settings.clj
@@ -57,7 +57,7 @@
:else landing-page))
(defsetting landing-page
- (deferred-tru "Enter a URL of the landing page to show the user. This overrides the custom homepage setting above.")
+ (deferred-tru "Enter a relative URL like /dashboard/1 or /collection/2.")
:encryption :no
:visibility :public
:export? true