Skip to content

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

Convert LegacyChoropleth to TypeScript (#79497)

Andrei Chernykhb346394afd5a
diff --git a/frontend/src/metabase/visualizations/components/ChoroplethMap.tsx b/frontend/src/metabase/visualizations/components/ChoroplethMap.tsx
index ca9df7873c2..7f85ea4bccf 100644
--- a/frontend/src/metabase/visualizations/components/ChoroplethMap.tsx
+++ b/frontend/src/metabase/visualizations/components/ChoroplethMap.tsx
@@ -42,8 +42,11 @@ import {
type FeatureClickContext,
buildFeatureClickObject,
} from "./ChoroplethMap.utils";
-import { LeafletChoropleth } from "./LeafletChoropleth";
-import { LegacyChoropleth } from "./LegacyChoropleth";
+import {
+ type FeatureInteraction,
+ LeafletChoropleth,
+} from "./LeafletChoropleth";
+import { LegacyChoropleth, type ProjectionFrame } from "./LegacyChoropleth";
import { computeMinimalBounds } from "./leaflet-bounds";
const geoJsonCache = new Map<string, GeoJSONData>();
@@ -144,7 +147,6 @@ type ChoroplethMapState = {
};
type Projection = d3.GeoProjection | null;
-type ProjectionFrame = [[number, number], [number, number]];
function isFeatureCollection(value: GeoJSONData): value is FeatureCollection {
return value.type === "FeatureCollection";
@@ -201,11 +203,11 @@ function useGeoJson(geoJsonPath: string | null): ChoroplethMapState {
return state;
}
-// projectionFrame is the lng/lat of the top left and bottom right corners
-function getMapProjection(region: string | undefined): {
- projection: Projection;
- projectionFrame: ProjectionFrame | null;
-} {
+type MapProjection =
+ | { projection: d3.GeoProjection; projectionFrame: ProjectionFrame }
+ | { projection: null; projectionFrame: null };
+
+function getMapProjection(region: string | undefined): MapProjection {
// special case builtin maps to use legacy choropleth map
if (region === "us_states") {
return {
@@ -354,7 +356,7 @@ function ChoroplethMapInner(props: ChoroplethMapProps) {
const onClickFeature =
onVisualizationClick != null
- ? (click: { feature: Feature; event: MouseEvent }) => {
+ ? (click: FeatureInteraction) => {
const row = rowByFeatureKey.get(getFeatureKey(click.feature));
const clickData = {
...buildFeatureClickObject(row, click.feature, clickContext),
@@ -367,7 +369,7 @@ function ChoroplethMapInner(props: ChoroplethMapProps) {
: undefined;
const onHoverFeature = onHoverChange
- ? (hover: { feature: Feature; event: MouseEvent } | null) => {
+ ? (hover: FeatureInteraction | null) => {
const row = hover && rowByFeatureKey.get(getFeatureKey(hover.feature));
if (row && hover) {
onHoverChange({
@@ -447,7 +449,7 @@ function ChoroplethMapInner(props: ChoroplethMapProps) {
isDocument={isDocument}
isMetricsViewer={isMetricsViewer}
>
- {projection ? (
+ {projection && isFeatureCollection(geoJson) ? (
<LegacyChoropleth
series={series}
geoJson={geoJson}
diff --git a/frontend/src/metabase/visualizations/components/LeafletChoropleth.tsx b/frontend/src/metabase/visualizations/components/LeafletChoropleth.tsx
index 7dcb8bdb0fa..5f7acac8f8a 100644
--- a/frontend/src/metabase/visualizations/components/LeafletChoropleth.tsx
+++ b/frontend/src/metabase/visualizations/components/LeafletChoropleth.tsx
@@ -9,7 +9,7 @@ import type { GeoJSONData, Series } from "metabase-types/api";
import { CardRenderer } from "./CardRenderer";
import { computeMinimalBounds } from "./leaflet-bounds";
-type FeatureInteraction = {
+export type FeatureInteraction = {
feature: Feature;
event: MouseEvent;
};
diff --git a/frontend/src/metabase/visualizations/components/LegacyChoropleth.jsx b/frontend/src/metabase/visualizations/components/LegacyChoropleth.jsx
deleted file mode 100644
index 239c34e6821..00000000000
--- a/frontend/src/metabase/visualizations/components/LegacyChoropleth.jsx
+++ /dev/null
@@ -1,107 +0,0 @@
-/* eslint-disable react/prop-types */
-import cx from "classnames";
-import * as d3 from "d3";
-import { Component } from "react";
-
-import CS from "metabase/css/core/index.css";
-import { isSameSeries } from "metabase/visualizations/lib/utils";
-
-export const LegacyChoropleth = ({
- series,
- geoJson,
- projection,
- projectionFrame,
- getColor,
- isFeatureHighlighted,
- highlightedKey,
- onHoverFeature,
- onClickFeature,
-}) => {
- const geo = d3.geoPath().projection(projection);
-
- const [[minX, minY], [maxX, maxY]] = projectionFrame.map(projection);
- const width = maxX - minX;
- const height = maxY - minY;
-
- return (
- <div
- className={cx(
- CS.absolute,
- CS.top,
- CS.bottom,
- CS.left,
- CS.right,
- CS.flex,
- CS.layoutCentered,
- )}
- >
- <ShouldUpdate
- series={series}
- highlightedKey={highlightedKey}
- shouldUpdate={(props, nextProps) =>
- !isSameSeries(props.series, nextProps.series) ||
- props.highlightedKey !== nextProps.highlightedKey
- }
- >
- {() => (
- <svg
- className={cx(CS.flexFull, CS.m1)}
- viewBox={`${minX} ${minY} ${width} ${height}`}
- >
- {geoJson.features.map((feature, index) => {
- const opacity =
- isFeatureHighlighted?.(feature) === false ? 0.3 : 1;
-
- return (
- <path
- data-testid="choropleth-feature"
- key={index}
- d={geo(feature, index)}
- stroke="white"
- strokeWidth={1}
- fill={getColor(feature)}
- opacity={opacity}
- style={{ transition: "opacity 0.15s ease" }}
- onMouseMove={(e) =>
- onHoverFeature({
- feature: feature,
- event: e.nativeEvent,
- })
- }
- onMouseLeave={() => onHoverFeature(null)}
- className={cx({ [CS.cursorPointer]: !!onClickFeature })}
- onClick={
- onClickFeature
- ? (e) =>
- onClickFeature({
- feature: feature,
- event: e.nativeEvent,
- })
- : undefined
- }
- />
- );
- })}
- </svg>
- )}
- </ShouldUpdate>
- </div>
- );
-};
-
-class ShouldUpdate extends Component {
- shouldComponentUpdate(nextProps) {
- if (nextProps.shouldUpdate) {
- return nextProps.shouldUpdate(this.props, nextProps);
- }
- return true;
- }
- render() {
- const { children } = this.props;
- if (typeof children === "function") {
- return children();
- } else {
- return children;
- }
- }
-}
diff --git a/frontend/src/metabase/visualizations/components/LegacyChoropleth.tsx b/frontend/src/metabase/visualizations/components/LegacyChoropleth.tsx
new file mode 100644
index 00000000000..a6350c4b659
--- /dev/null
+++ b/frontend/src/metabase/visualizations/components/LegacyChoropleth.tsx
@@ -0,0 +1,104 @@
+import cx from "classnames";
+import * as d3 from "d3";
+import type { Feature, FeatureCollection } from "geojson";
+import { memo } from "react";
+
+import CS from "metabase/css/core/index.css";
+import { isSameSeries } from "metabase/visualizations/lib/utils";
+import type { Series } from "metabase-types/api";
+
+import type { FeatureInteraction } from "./LeafletChoropleth";
+
+type LngLat = [lng: number, lat: number];
+
+export type ProjectionFrame = [topLeft: LngLat, bottomRight: LngLat];
+
+interface LegacyChoroplethProps {
+ series: Series;
+ geoJson: FeatureCollection;
+ projection: d3.GeoProjection;
+ projectionFrame: ProjectionFrame;
+ getColor: (feature: Feature) => string;
+ isFeatureHighlighted: (feature: Feature) => boolean | null;
+ highlightedKey: string | null;
+ onHoverFeature?: (payload: FeatureInteraction | null) => void;
+ onClickFeature?: (payload: FeatureInteraction) => void;
+}
+
+export const LegacyChoropleth = memo(
+ function LegacyChoropleth({
+ geoJson,
+ projection,
+ projectionFrame,
+ getColor,
+ isFeatureHighlighted,
+ onHoverFeature,
+ onClickFeature,
+ }: LegacyChoroplethProps) {
+ const geo = d3.geoPath().projection(projection);
+
+ const [[minX, minY], [maxX, maxY]] = projectionFrame.map((coord) => {
+ const projected = projection(coord);
+ return projected ?? [0, 0];
+ });
+ const width = maxX - minX;
+ const height = maxY - minY;
+
+ return (
+ <div
+ className={cx(
+ CS.absolute,
+ CS.top,
+ CS.bottom,
+ CS.left,
+ CS.right,
+ CS.flex,
+ CS.layoutCentered,
+ )}
+ >
+ <svg
+ className={cx(CS.flexFull, CS.m1)}
+ viewBox={`${minX} ${minY} ${width} ${height}`}
+ >
+ {geoJson.features.map((feature, index) => {
+ const opacity = isFeatureHighlighted(feature) === false ? 0.3 : 1;
+
+ return (
+ <path
+ data-testid="choropleth-feature"
+ key={index}
+ d={geo(feature) ?? undefined}
+ stroke="white"
+ strokeWidth={1}
+ fill={getColor(feature)}
+ opacity={opacity}
+ style={{ transition: "opacity 0.15s ease" }}
+ onMouseMove={(e) =>
+ onHoverFeature?.({
+ feature,
+ event: e.nativeEvent,
+ })
+ }
+ onMouseLeave={() => onHoverFeature?.(null)}
+ className={cx({ [CS.cursorPointer]: Boolean(onClickFeature) })}
+ onClick={
+ onClickFeature
+ ? (e) =>
+ onClickFeature({
+ feature,
+ event: e.nativeEvent,
+ })
+ : undefined
+ }
+ />
+ );
+ })}
+ </svg>
+ </div>
+ );
+ },
+ // FIXME: verify if we really need memoization here
+ (prevProps, nextProps) =>
+ isSameSeries(prevProps.series, nextProps.series) &&
+ prevProps.highlightedKey === nextProps.highlightedKey,
+);