Skip to content

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

Fix empty cards with no results making dashboard longer than expected (#67581)

Mahatthana (Kelvin) Nomsawadicf7026a6a241
diff --git a/e2e/test/scenarios/dashboard/visualizer/reproductions.cy.spec.ts b/e2e/test/scenarios/dashboard/visualizer/reproductions.cy.spec.ts
index 0fd19a55274..32ece424c86 100644
--- a/e2e/test/scenarios/dashboard/visualizer/reproductions.cy.spec.ts
+++ b/e2e/test/scenarios/dashboard/visualizer/reproductions.cy.spec.ts
@@ -1,5 +1,9 @@
import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
-import type { StructuredQuestionDetails } from "e2e/support/helpers";
+import type {
+ DashboardDetails,
+ StructuredQuestionDetails,
+} from "e2e/support/helpers";
+import type { DashboardCard, Parameter } from "metabase-types/api";
const { H } = cy;
const { ORDERS, ORDERS_ID } = SAMPLE_DATABASE;
@@ -165,3 +169,124 @@ describe("issue 61521", () => {
});
});
});
+
+describe("issue 65908 (UXW-2293)", () => {
+ const dateParameters: Parameter = {
+ default: "2030-01-01~",
+ id: "d3b78b27",
+ name: "Date Filter",
+ slug: "date_filter",
+ type: "date/all-options",
+ };
+
+ interface CreateDashcardProps {
+ index: number;
+ questionId: number;
+ hideEmptyResults: boolean;
+ withParameterMappings: boolean;
+ }
+ function createDashcard({
+ index,
+ questionId,
+ hideEmptyResults,
+ withParameterMappings,
+ }: CreateDashcardProps): Partial<DashboardCard> {
+ const cardHeightInRows = 10;
+
+ return {
+ col: 0,
+ row: cardHeightInRows * index,
+ size_x: 24,
+ size_y: cardHeightInRows,
+ card_id: questionId,
+ visualization_settings: {
+ "card.hide_empty": hideEmptyResults,
+ },
+ parameter_mappings: withParameterMappings
+ ? [
+ {
+ parameter_id: dateParameters.id,
+ card_id: questionId,
+ target: ["dimension", ["field", ORDERS.CREATED_AT, null]],
+ },
+ ]
+ : undefined,
+ };
+ }
+
+ beforeEach(() => {
+ H.restore();
+ cy.signInAsAdmin();
+
+ const dashboardDetails = {
+ name: "Dashboard with empty result cards",
+ parameters: [dateParameters],
+ } satisfies DashboardDetails;
+
+ const questionDetails: StructuredQuestionDetails = {
+ name: "Orders question",
+ query: {
+ "source-table": ORDERS_ID,
+ limit: 5,
+ },
+ };
+
+ H.createDashboardWithQuestions({
+ dashboardDetails,
+ questions: [questionDetails],
+ }).then(({ dashboard, questions }) => {
+ const [question] = questions;
+ H.updateDashboardCards({
+ dashboard_id: dashboard.id,
+ cards: [
+ createDashcard({
+ index: 0,
+ questionId: question.id,
+ hideEmptyResults: true,
+ withParameterMappings: true,
+ }),
+ createDashcard({
+ index: 1,
+ questionId: question.id,
+ hideEmptyResults: true,
+ withParameterMappings: true,
+ }),
+ createDashcard({
+ index: 2,
+ questionId: question.id,
+ hideEmptyResults: true,
+ withParameterMappings: true,
+ }),
+ createDashcard({
+ index: 3,
+ questionId: question.id,
+ hideEmptyResults: false,
+ withParameterMappings: false,
+ }),
+ ],
+ });
+ cy.wrap(dashboard.id).as("dashboardId");
+ });
+ });
+
+ it("should not take into account the height of cards with no results when calculating dashboard height", () => {
+ cy.get("@dashboardId").then((dashboardId: any) => {
+ H.visitDashboard(dashboardId);
+
+ cy.findByDisplayValue("Dashboard with empty result cards").should(
+ "be.visible",
+ );
+
+ H.getDashboardCard().within(() => {
+ cy.findByText("Orders question").should("be.visible");
+ cy.log("Checks for the subtotal value from the first row");
+ cy.findByText("37.65").should("be.visible");
+ });
+
+ cy.findByRole("main").should(($main) => {
+ // 4 cards take about 2000px in height, so only 1 card should definitely take less than 1000px
+ expect($main[0].scrollHeight).to.be.lessThan(1000);
+ });
+ });
+ });
+});
diff --git a/frontend/src/metabase/dashboard/components/grid/GridLayout.tsx b/frontend/src/metabase/dashboard/components/grid/GridLayout.tsx
index 2665700ed50..c25b3d88a48 100644
--- a/frontend/src/metabase/dashboard/components/grid/GridLayout.tsx
+++ b/frontend/src/metabase/dashboard/components/grid/GridLayout.tsx
@@ -92,8 +92,11 @@ export function GridLayout<T extends { id: number | null }>(
),
);
+ const [localLayout, setLocalLayout] = useState<ReactGridLayout.Layout[]>();
+
const onLayoutChangeWrapped = useCallback(
(currentLayout: ReactGridLayout.Layout[]) => {
+ setLocalLayout(currentLayout);
onLayoutChange({
layout: currentLayout,
// Calculating the breakpoint right here,
@@ -159,13 +162,18 @@ export function GridLayout<T extends { id: number | null }>(
);
const height = useMemo(() => {
- let lowestLayoutCellPoint = Math.max(...layout.map((l) => l.y + l.h));
+ // Once `localLayout` is set, use it instead of the prop layout.
+ // The prop layout includes all cards (visible and hidden), causing
+ // incorrect y/height calculations. `localLayout` only includes visible cards.
+ let lowestLayoutCellPoint = Math.max(
+ ...(localLayout ?? layout).map((l) => l.y + l.h),
+ );
if (isEditing) {
lowestLayoutCellPoint += Math.ceil(window.innerHeight / cellSize.height);
}
- const verticalMargin = margin[1];
+ const [_horizontalMargin, verticalMargin] = margin;
return (cellSize.height + verticalMargin) * lowestLayoutCellPoint;
- }, [cellSize.height, layout, margin, isEditing]);
+ }, [localLayout, layout, isEditing, margin, cellSize.height]);
const background = useMemo(
() =>