Skip to content

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

UXW-4929 Resolve unknown string dashboard ids (#79586)

Denis Berezin570a0e18c857
diff --git a/frontend/src/metabase/dashboard/containers/DashboardApp/DashboardApp.tsx b/frontend/src/metabase/dashboard/containers/DashboardApp/DashboardApp.tsx
index 8a59743c4a3..802f39994a4 100644
--- a/frontend/src/metabase/dashboard/containers/DashboardApp/DashboardApp.tsx
+++ b/frontend/src/metabase/dashboard/containers/DashboardApp/DashboardApp.tsx
@@ -1,5 +1,5 @@
import cx from "classnames";
-import { useMemo, useState } from "react";
+import { useEffect, useMemo, useState } from "react";
import ErrorBoundary from "metabase/ErrorBoundary";
import { isRouteInSync } from "metabase/common/hooks/is-route-in-sync";
@@ -39,7 +39,7 @@ import {
parseSearchQuery,
stringifyHashOptions,
} from "metabase/utils/browser";
-import type { DashboardId, Dashboard as IDashboard } from "metabase-types/api";
+import type { Dashboard as IDashboard } from "metabase-types/api";
import { useRegisterDashboardMetabotContext } from "../../hooks/use-register-dashboard-metabot-context";
import { getDocumentTitle, getFavicon } from "../../selectors";
@@ -89,8 +89,7 @@ export const DashboardApp = () => {
() => parseSearchQuery(location.search),
[location.search],
);
- // Unjustified type cast. FIXME
- const dashboardId = Urls.extractEntityId(params.slug) as DashboardId;
+ const dashboardId = Urls.extractEntityId(params.slug);
useRegisterDashboardMetabotContext();
useDashboardUrlQuery(location);
@@ -116,7 +115,7 @@ export const DashboardApp = () => {
options = await extractHashOption("edit", options);
}
- if (addCardOnLoad != null) {
+ if (addCardOnLoad != null && dashboardId != null) {
options = await extractHashOption("add", options);
const searchParams = new URLSearchParams(window.location.search);
const tabParam = searchParams.get("tab");
@@ -156,12 +155,24 @@ export const DashboardApp = () => {
const { autoScrollToDashcardId, reportAutoScrolledToDashcard } =
useAutoScrollToDashcard(location);
+ // A slug that yields no id (e.g. /dashboard/not-a-number) would otherwise
+ // leave the provider waiting for a fetch that never starts (metabase#78725)
+ useEffect(() => {
+ if (dashboardId == null) {
+ dispatch(setErrorPage({ status: 404 }));
+ }
+ }, [dashboardId, dispatch]);
+
// Prevent rendering the dashboard app if the route is out of sync
// metabase#65500
if (!isRouteInSync(location.pathname)) {
return null;
}
+ if (dashboardId == null) {
+ return null;
+ }
+
return (
<ErrorBoundary message={error}>
<DashboardContextProvider
diff --git a/frontend/src/metabase/dashboard/containers/DashboardApp/DashboardApp.unit.spec.tsx b/frontend/src/metabase/dashboard/containers/DashboardApp/DashboardApp.unit.spec.tsx
index 82d0ad5e50e..4e9a018c5f2 100644
--- a/frontend/src/metabase/dashboard/containers/DashboardApp/DashboardApp.unit.spec.tsx
+++ b/frontend/src/metabase/dashboard/containers/DashboardApp/DashboardApp.unit.spec.tsx
@@ -58,9 +58,10 @@ const TestHome = () => <div />;
interface Options {
dashboard?: Partial<Dashboard>;
+ slug?: string;
}
-async function setup({ dashboard }: Options = {}) {
+async function setup({ dashboard, slug }: Options = {}) {
const mockDashboard = createMockDashboard(dashboard);
const dashboardId = mockDashboard.id;
@@ -97,13 +98,13 @@ async function setup({ dashboard }: Options = {}) {
);
};
- const { router } = renderWithProviders(
+ const { router, store } = renderWithProviders(
<>
<Route path="/" element={<TestHome />} />
<Route path="/dashboard/:slug" element={<DashboardAppContainer />} />
</>,
{
- initialRoute: `/dashboard/${dashboardId}`,
+ initialRoute: `/dashboard/${slug ?? dashboardId}`,
withRouter: true,
storeInitialState: {
dashboard: createMockDashboardState(),
@@ -120,6 +121,7 @@ async function setup({ dashboard }: Options = {}) {
return {
dashboardId,
router: checkNotNull(router),
+ store,
mockEventListener,
};
}
@@ -276,6 +278,13 @@ describe("DashboardApp", () => {
);
});
+ it("should show the error page instead of an endless loader for a non-numeric slug (metabase#78725)", async () => {
+ const { store } = await setup({ slug: "thisisinvalid" });
+
+ expect(screen.queryByText("Loading…")).not.toBeInTheDocument();
+ expect(store.getState().app.errorPage).toMatchObject({ status: 404 });
+ });
+
it("should not allow to enter a dashboard name longer than 254 characters", async () => {
await setup();