Skip to content

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

Gdgt 1801 visualization typescript conversions leafletmap.tsx (#70248)

Jakub Chodorowiczd0ffa5115d08
diff --git a/frontend/src/metabase-types/api/dataset.ts b/frontend/src/metabase-types/api/dataset.ts
index 2ce50e2cb0b..97723c12341 100644
--- a/frontend/src/metabase-types/api/dataset.ts
+++ b/frontend/src/metabase-types/api/dataset.ts
@@ -272,3 +272,5 @@ export type GetRemappedParameterValueRequest = {
field_ids: FieldId[];
value: ParameterValueOrArray;
};
+
+export type Point = [number, number];
diff --git a/frontend/src/metabase/visualizations/components/LeafletMap.jsx b/frontend/src/metabase/visualizations/components/LeafletMap.tsx
similarity index 69%
rename from frontend/src/metabase/visualizations/components/LeafletMap.jsx
rename to frontend/src/metabase/visualizations/components/LeafletMap.tsx
index 86f73266649..38812f8f319 100644
--- a/frontend/src/metabase/visualizations/components/LeafletMap.jsx
+++ b/frontend/src/metabase/visualizations/components/LeafletMap.tsx
@@ -1,4 +1,3 @@
-/* eslint-disable react/prop-types */
import "leaflet-draw";
import "leaflet/dist/leaflet.css";
import "./LeafletMap.module.css";
@@ -8,26 +7,64 @@ import { Component, createRef } from "react";
import _ from "underscore";
import MetabaseSettings from "metabase/lib/settings";
+import { isNullOrUndefined } from "metabase/lib/types";
+import type { OnChangeCardAndRun } from "metabase/visualizations/types";
import * as Lib from "metabase-lib";
import Question from "metabase-lib/v1/Question";
+import type Metadata from "metabase-lib/v1/metadata/Metadata";
+import type { Series } from "metabase-types/api";
+import type { Point } from "metabase-types/api/dataset";
+import { isObject } from "metabase-types/guards/common";
+
+type MapSettings = {
+ "map.latitude_column"?: string;
+ "map.longitude_column"?: string;
+ "map.metric_column"?: string;
+ "map.center_latitude"?: number;
+ "map.center_longitude"?: number;
+ "map.zoom"?: number;
+};
+
+export interface LeafletMapProps {
+ className?: string;
+ width?: number;
+ height?: number;
+ bounds: L.LatLngBounds;
+ settings: MapSettings;
+ points?: Point[] | null;
+ series: Series;
+ metadata?: Metadata;
+ token?: string | null;
+ zoomControl?: boolean;
+ zoom?: number | null;
+ lat?: number | null;
+ lng?: number | null;
+ onMapCenterChange: (lat: number, lng: number) => void;
+ onMapZoomChange: (zoom: number) => void;
+ onRenderError: (error?: unknown) => void;
+ onFiltering: (filtering: boolean) => void;
+ onChangeCardAndRun: OnChangeCardAndRun;
+}
-export class LeafletMap extends Component {
- constructor(props) {
- super(props);
-
- this.mapRef = createRef();
- }
+export class LeafletMap extends Component<LeafletMapProps> {
+ mapRef = createRef<HTMLDivElement>();
+ map: L.Map | null = null;
+ drawControl: L.Control.Draw | null = null;
+ _filter?: L.Draw.Rectangle;
componentDidMount() {
try {
const element = this.mapRef.current;
+ if (!element) {
+ return;
+ }
- const map = (this.map = L.map(element, {
+ const mapOptions: L.MapOptions = {
scrollWheelZoom: true,
wheelPxPerZoomLevel: 30,
minZoom: 2,
- drawControlTooltips: false,
- zoomSnap: false,
+
+ zoomSnap: 0,
// Set max bounds for latitude only, allowing longitude to wrap
maxBounds: [
[-90, -Infinity], // Southwest corner (limit south, no limit west)
@@ -35,7 +72,8 @@ export class LeafletMap extends Component {
],
maxBoundsViscosity: 1.0, // Completely prevent panning outside latitude bounds
worldCopyJump: true, // Enable smooth horizontal wrapping
- }));
+ };
+ const map = (this.map = L.map(element, mapOptions));
const drawnItems = new L.FeatureGroup();
map.addLayer(drawnItems);
@@ -58,14 +96,14 @@ export class LeafletMap extends Component {
map.setView([0, 0], 8);
- const mapTileUrl = MetabaseSettings.get("map-tile-server-url");
+ const mapTileUrl = MetabaseSettings.get("map-tile-server-url") ?? "";
let mapTileHostname = "";
try {
mapTileHostname = new URL(mapTileUrl).host;
- } catch (e) {}
+ } catch {}
const mapTileAttribution = mapTileHostname.includes("openstreetmap.org")
? 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors'
- : null;
+ : undefined;
L.tileLayer(mapTileUrl, { attribution: mapTileAttribution }).addTo(map);
@@ -77,13 +115,19 @@ export class LeafletMap extends Component {
const zoom = map.getZoom();
this.props.onMapZoomChange(zoom);
});
- } catch (err) {
- console.error(err);
- this.props.onRenderError(err.message || err);
+ } catch (error) {
+ console.error(error);
+ this.props.onRenderError(
+ error instanceof Error ? error.message : (error ?? undefined),
+ );
}
}
- componentDidUpdate(prevProps) {
+ componentDidUpdate(prevProps: LeafletMapProps) {
+ if (!this.map) {
+ return;
+ }
+
const { bounds, settings, zoomControl, zoom, lat, lng } = this.props;
if (prevProps.zoomControl !== zoomControl) {
@@ -107,10 +151,10 @@ export class LeafletMap extends Component {
this.map.invalidateSize();
- const hasSavedView =
- settings["map.center_latitude"] != null ||
- settings["map.center_longitude"] != null ||
- settings["map.zoom"] != null;
+ const [centerLatitude, centerLongitude] = [
+ settings["map.center_latitude"],
+ settings["map.center_longitude"],
+ ];
// Pure resize (no data change): preserve user's current view
if (!isInitialUpdate && !pointsChanged && dimensionsChanged) {
@@ -126,11 +170,11 @@ export class LeafletMap extends Component {
}
// Initial update or data changed: apply saved settings if available
- if (hasSavedView) {
- this.map.setView(
- [settings["map.center_latitude"], settings["map.center_longitude"]],
- settings["map.zoom"],
- );
+ if (
+ !isNullOrUndefined(centerLatitude) &&
+ !isNullOrUndefined(centerLongitude)
+ ) {
+ this.map.setView([centerLatitude, centerLongitude], settings["map.zoom"]);
return;
}
@@ -149,15 +193,18 @@ export class LeafletMap extends Component {
[0, bounds.getEast()],
]),
);
- const zoom = Math.min(latZoom, lonZoom);
+ const mapZoom = Math.min(latZoom, lonZoom);
// NOTE: unclear why calling `fitBounds` twice is sometimes required to get it to work
this.map.fitBounds(bounds);
- this.map.setZoom(zoom);
+ this.map.setZoom(mapZoom);
this.map.fitBounds(bounds);
}
}
componentWillUnmount() {
+ if (!this.map) {
+ return;
+ }
this.map.remove();
}
@@ -180,19 +227,29 @@ export class LeafletMap extends Component {
}
startFilter() {
- this._filter = new L.Draw.Rectangle(
- this.map,
- this.drawControl.options.rectangle,
- );
+ if (!this.map || !this.drawControl) {
+ return;
+ }
+
+ const { options } = this.drawControl;
+
+ const rectangleOptions = getRectangleDrawOptions(options);
+
+ this._filter = new L.Draw.Rectangle(this.map, rectangleOptions);
this._filter.enable();
this.props.onFiltering(true);
}
+
stopFilter() {
- this._filter && this._filter.disable();
+ this._filter?.disable();
this.props.onFiltering(false);
}
- onFilter = (e) => {
- const bounds = e.layer.getBounds();
+
+ onFilter = (event: L.DrawEvents.Created) => {
+ if (!(event.layer instanceof L.Rectangle)) {
+ return;
+ }
+ const bounds = event.layer.getBounds();
const {
series: [
@@ -214,7 +271,7 @@ export class LeafletMap extends Component {
});
const question = new Question(card, metadata);
- if (this.supportsFilter()) {
+ if (this.supportsFilter() && latitudeColumn && longitudeColumn) {
const query = question.query();
const stageIndex = -1;
@@ -297,7 +354,9 @@ export class LeafletMap extends Component {
},
],
} = this.props;
- return _.findWhere(cols, { name: settings["map.metric_column"] });
+ return _.findWhere(cols, {
+ name: settings["map.metric_column"],
+ });
}
}
@@ -305,10 +364,27 @@ export class LeafletMap extends Component {
* Lightweight function to check if points have changed (e.g. due to filters)
* so that we should recalculate the zoom.
*/
-function shouldRecalculateZoom(prevPoints, nextPoints) {
+function shouldRecalculateZoom(
+ prevPoints?: Point[] | null,
+ nextPoints?: Point[] | null,
+) {
if (!prevPoints && !nextPoints) {
return false;
}
return !prevPoints || nextPoints !== prevPoints;
}
+
+function getRectangleDrawOptions(
+ options: L.ControlOptions,
+): L.DrawOptions.RectangleOptions | undefined {
+ if (!("draw" in options)) {
+ return undefined;
+ }
+
+ if (!isObject(options.draw) || !isObject(options.draw.rectangle)) {
+ return undefined;
+ }
+
+ return options.draw.rectangle;
+}
diff --git a/frontend/src/metabase/visualizations/components/LeafletMap.unit.spec.js b/frontend/src/metabase/visualizations/components/LeafletMap.unit.spec.tsx
similarity index 59%
rename from frontend/src/metabase/visualizations/components/LeafletMap.unit.spec.js
rename to frontend/src/metabase/visualizations/components/LeafletMap.unit.spec.tsx
index d0267e67463..6634324db88 100644
--- a/frontend/src/metabase/visualizations/components/LeafletMap.unit.spec.js
+++ b/frontend/src/metabase/visualizations/components/LeafletMap.unit.spec.tsx
@@ -3,17 +3,28 @@ import L from "leaflet";
import { createRef } from "react";
import MetabaseSettings from "metabase/lib/settings";
+import type { Point } from "metabase-types/api/dataset";
+import {
+ createMockCard,
+ createMockColumn,
+ createMockDatasetData,
+} from "metabase-types/api/mocks";
-import { LeafletMap } from "./LeafletMap";
+import { LeafletMap, type LeafletMapProps } from "./LeafletMap";
describe("LeafletMap", () => {
- const createProps = (overrides = {}) => ({
+ const createProps = (
+ overrides: Partial<LeafletMapProps> = {},
+ ): LeafletMapProps => ({
series: [
{
- card: {},
- data: {
- cols: [{ name: "lat" }, { name: "lng" }],
- },
+ card: createMockCard(),
+ data: createMockDatasetData({
+ cols: [
+ createMockColumn({ name: "lat" }),
+ createMockColumn({ name: "lng" }),
+ ],
+ }),
},
],
settings: {
@@ -33,12 +44,13 @@ describe("LeafletMap", () => {
onMapZoomChange: jest.fn(),
onRenderError: jest.fn(),
onFiltering: jest.fn(),
- metadata: {},
+ onChangeCardAndRun: jest.fn(),
+ metadata: undefined,
...overrides,
});
beforeEach(() => {
- jest.spyOn(MetabaseSettings, "get").mockImplementation((key) => {
+ jest.spyOn(MetabaseSettings, "get").mockImplementation((key: string) => {
if (key === "map-tile-server-url") {
return "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png";
}
@@ -53,12 +65,13 @@ describe("LeafletMap", () => {
describe("zoom preservation on component update", () => {
it("should not recalculate zoom if points did not change", () => {
const initialProps = createProps();
- const ref = createRef();
+ const ref = createRef<LeafletMap>();
const { rerender } = render(<LeafletMap ref={ref} {...initialProps} />);
- const mapInstance = ref.current.map;
+ const mapInstance = ref.current?.map;
- const setZoomSpy = jest.spyOn(mapInstance, "setZoom");
- const setViewSpy = jest.spyOn(mapInstance, "setView");
+ expect(mapInstance).toBeDefined();
+ const setZoomSpy = jest.spyOn(mapInstance!, "setZoom");
+ const setViewSpy = jest.spyOn(mapInstance!, "setView");
const updatedProps = createProps({ points: initialProps.points });
rerender(<LeafletMap ref={ref} {...updatedProps} />);
@@ -69,14 +82,15 @@ describe("LeafletMap", () => {
it("should recalculate zoom when points do change", () => {
const initialProps = createProps();
- const ref = createRef();
+ const ref = createRef<LeafletMap>();
const { rerender } = render(<LeafletMap ref={ref} {...initialProps} />);
- const mapInstance = ref.current.map;
+ const mapInstance = ref.current?.map;
- const setZoomSpy = jest.spyOn(mapInstance, "setZoom");
- const setViewSpy = jest.spyOn(mapInstance, "setView");
+ expect(mapInstance).toBeDefined();
+ const setZoomSpy = jest.spyOn(mapInstance!, "setZoom");
+ const setViewSpy = jest.spyOn(mapInstance!, "setView");
- const differentPoints = [
+ const differentPoints: Point[] = [
[1, 1],
[2, 2],
];
diff --git a/frontend/src/metabase/visualizations/echarts/pie/util/label.ts b/frontend/src/metabase/visualizations/echarts/pie/util/label.ts
index 4955817c020..4302cce84cc 100644
--- a/frontend/src/metabase/visualizations/echarts/pie/util/label.ts
+++ b/frontend/src/metabase/visualizations/echarts/pie/util/label.ts
@@ -1,4 +1,4 @@
-export type Point = [number, number];
+import type { Point } from "metabase-types/api/dataset";
/**
* Calculates the length of a chord given the radius of a circle and the central angle.
diff --git a/frontend/src/types/leaflet-draw-events.d.ts b/frontend/src/types/leaflet-draw-events.d.ts
new file mode 100644
index 00000000000..834176c139d
--- /dev/null
+++ b/frontend/src/types/leaflet-draw-events.d.ts
@@ -0,0 +1,12 @@
+import "leaflet";
+import "leaflet-draw";
+
+declare module "leaflet" {
+ interface Evented {
+ on(
+ type: "draw:created",
+ fn: (event: DrawEvents.Created) => void,
+ context?: unknown,
+ ): this;
+ }
+}