The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Don't reset map zoom when filter is active and a marker is clicked to view details (#67063)
Anderson52bfdad99d70
diff --git a/frontend/src/metabase/visualizations/components/LeafletMap.jsx b/frontend/src/metabase/visualizations/components/LeafletMap.jsxindex a8aed5336cd..5a293b0ccd5 100644--- a/frontend/src/metabase/visualizations/components/LeafletMap.jsx+++ b/frontend/src/metabase/visualizations/components/LeafletMap.jsx@@ -109,7 +109,10 @@ export default class LeafletMap extends Component {[settings["map.center_latitude"], settings["map.center_longitude"]],settings["map.zoom"],);- } else {+ return;+ }++ if (shouldRecalculateZoom(prevProps?.points, this.props.points)) {// compute ideal lat and lon zoom separately and use the lesser zoom to ensure the bounds are visibleconst latZoom = this.map.getBoundsZoom(L.latLngBounds([@@ -275,3 +278,15 @@ export default class LeafletMap extends Component {return _.findWhere(cols, { name: settings["map.metric_column"] });}}++/**+ * Lightweight function to check if points have changed (e.g. due to filters)+ * so that we should recalculate the zoom.+ */+function shouldRecalculateZoom(prevPoints, nextPoints) {+ if (!prevPoints && !nextPoints) {+ return false;+ }++ return !prevPoints || nextPoints !== prevPoints;+}diff --git a/frontend/src/metabase/visualizations/components/LeafletMap.unit.spec.js b/frontend/src/metabase/visualizations/components/LeafletMap.unit.spec.jsnew file mode 100644index 00000000000..246ee0709bb--- /dev/null+++ b/frontend/src/metabase/visualizations/components/LeafletMap.unit.spec.js@@ -0,0 +1,90 @@+import { render } from "@testing-library/react";+import L from "leaflet";+import { createRef } from "react";++import MetabaseSettings from "metabase/lib/settings";++import LeafletMap from "./LeafletMap";++describe("LeafletMap", () => {+ const createProps = (overrides = {}) => ({+ series: [+ {+ card: {},+ data: {+ cols: [{ name: "lat" }, { name: "lng" }],+ },+ },+ ],+ settings: {+ "map.latitude_column": "lat",+ "map.longitude_column": "lng",+ },+ bounds: L.latLngBounds([+ [0, 0],+ [10, 10],+ ]),+ points: [+ [1, 1],+ [2, 2],+ [3, 3],+ ],+ onMapCenterChange: jest.fn(),+ onMapZoomChange: jest.fn(),+ onRenderError: jest.fn(),+ onFiltering: jest.fn(),+ metadata: {},+ ...overrides,+ });++ beforeEach(() => {+ jest.spyOn(MetabaseSettings, "get").mockImplementation((key) => {+ if (key === "map-tile-server-url") {+ return "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png";+ }+ return null;+ });+ });++ afterEach(() => {+ jest.restoreAllMocks();+ });++ describe("zoom preservation on component update", () => {+ it("should not recalculate zoom if points did not change", () => {+ const initialProps = createProps();+ const ref = createRef();+ const { rerender } = render(<LeafletMap ref={ref} {...initialProps} />);+ const mapInstance = ref.current.map;++ const setZoomSpy = jest.spyOn(mapInstance, "setZoom");+ const setViewSpy = jest.spyOn(mapInstance, "setView");++ const updatedProps = createProps({ points: initialProps.points });+ rerender(<LeafletMap ref={ref} {...updatedProps} />);++ expect(setZoomSpy).not.toHaveBeenCalled();+ expect(setViewSpy).not.toHaveBeenCalled();+ });++ it("should recalculate zoom when points do change", () => {+ const initialProps = createProps();+ const ref = createRef();+ const { rerender } = render(<LeafletMap ref={ref} {...initialProps} />);+ const mapInstance = ref.current.map;++ const setZoomSpy = jest.spyOn(mapInstance, "setZoom");+ const setViewSpy = jest.spyOn(mapInstance, "setView");++ const differentPoints = [+ [1, 1],+ [2, 2],+ ];+ const updatedProps = createProps({ points: differentPoints });+ rerender(<LeafletMap ref={ref} {...updatedProps} />);++ expect(setZoomSpy).toHaveBeenCalled();+ expect(setViewSpy).toHaveBeenCalled();+ });+ });+});diff --git a/frontend/src/metabase/visualizations/lib/utils.js b/frontend/src/metabase/visualizations/lib/utils.jsindex 5a9caca6a9a..5286517eb11 100644--- a/frontend/src/metabase/visualizations/lib/utils.js+++ b/frontend/src/metabase/visualizations/lib/utils.js@@ -196,7 +196,7 @@ const cardinalityCache = new Map();export function getColumnCardinality(cols, rows, index) {const col = cols[index];const key = getColumnKey(col);- if (!cardinalityCache.has(key)) {+ if (!cardinalityCache.has(key) && rows) {const dataset = crossfilter(rows);cardinalityCache.set(key,