Skip to content

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

Fix static embedding dashboard calling GET: /api/dashboard unnecessarily (#63753)

Mahatthana (Kelvin) Nomsawadi814dd5c80051
diff --git a/e2e/test/scenarios/embedding/embedding-dashboard.cy.spec.js b/e2e/test/scenarios/embedding/embedding-dashboard.cy.spec.js
index cc471e6cb53..89fa8a6a0df 100644
--- a/e2e/test/scenarios/embedding/embedding-dashboard.cy.spec.js
+++ b/e2e/test/scenarios/embedding/embedding-dashboard.cy.spec.js
@@ -14,6 +14,31 @@ import {
const { ORDERS, PEOPLE, PRODUCTS, ORDERS_ID } = SAMPLE_DATABASE;
+describe("scenarios > embedding > static embedding dashboard", () => {
+ beforeEach(() => {
+ H.restore();
+ cy.signInAsAdmin();
+ cy.request("PUT", `/api/dashboard/${ORDERS_DASHBOARD_ID}`, {
+ enable_embedding: true,
+ });
+ });
+
+ it("should not call `GET /api/database` (metabase#63310)", () => {
+ cy.intercept("GET", "/api/database").as("getDatabases");
+ const payload = {
+ resource: { dashboard: ORDERS_DASHBOARD_ID },
+ params: {},
+ };
+ H.visitEmbeddedPage(payload);
+
+ cy.findByRole("heading", { name: "Orders in a dashboard" }).should(
+ "be.visible",
+ );
+ cy.log("GET /api/database should not be called");
+ cy.get("@getDatabases.all").should("have.length", 0);
+ });
+});
+
describe("scenarios > embedding > dashboard parameters", () => {
beforeEach(() => {
H.restore();
diff --git a/frontend/src/metabase/dashboard/components/Dashboard/components/Grid.tsx b/frontend/src/metabase/dashboard/components/Dashboard/components/Grid.tsx
index 7b3fb28907c..96f541230e0 100644
--- a/frontend/src/metabase/dashboard/components/Dashboard/components/Grid.tsx
+++ b/frontend/src/metabase/dashboard/components/Dashboard/components/Grid.tsx
@@ -1,9 +1,11 @@
import { useCallback, useMemo } from "react";
import { t } from "ttag";
-import { useListDatabasesQuery } from "metabase/api";
+import { skipToken, useListDatabasesQuery } from "metabase/api";
import { useDashboardContext } from "metabase/dashboard/context";
+import { useSelector } from "metabase/lib/redux";
import { getHasDataAccess, getHasNativeWrite } from "metabase/selectors/data";
+import { getUser } from "metabase/selectors/user";
import { Loader, Stack, Text } from "metabase/ui";
import type { DashboardCard } from "metabase-types/api";
@@ -46,7 +48,16 @@ export const Grid = ({
const tabHasCards = currentTabDashcards.length > 0;
const dashboardHasCards = dashboard && dashboard.dashcards.length > 0;
- const { data: databasesResponse, isError } = useListDatabasesQuery();
+ /**
+ * In the original code before (metabase#59529), we render `DashboardEmptyStateWithoutAddPrompt` directly
+ * inside `PublicOrEmbeddedDashboardView` and this component doesn't need to check conditions that relies
+ * on `GET /api/database`. After the consolidation every dashboard uses the same component, so we probably
+ * missed this case.
+ */
+ const isLoggedIn = useSelector((state) => !!getUser(state));
+ const { data: databasesResponse, isError } = useListDatabasesQuery(
+ isLoggedIn ? undefined : skipToken,
+ );
const databases = useMemo(
() => databasesResponse?.data ?? [],
[databasesResponse],