UNPKG

@actinc/dls

Version:

Design Language System (DLS) for ACT & Encoura front-end projects.

95 lines 4.81 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime"; /** * Copyright (c) ACT, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ /* eslint-disable filenames/match-exported */ import { useTheme } from '@mui/material/styles'; import bbox from '@turf/bbox'; import isString from 'lodash/isString'; import numeral from 'numeral'; import React from 'react'; import Map from "../Map"; import MapPopup from "../MapPopup"; /** * SCFMap component which under the hood uses mapbox and react-map-gl. For this to work it's necessary * to add the link bellow in the head of your page: <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.css' rel='stylesheet' /> * For more information: https://visgl.github.io/react-map-gl/docs/get-started/get-started#styling */ export var SCFMap = function (_a) { var _b, _c; var data = _a.data, geoJSONPath = _a.geoJSONPath, mapboxAccessToken = _a.mapboxAccessToken, mapPopupProps = _a.mapPopupProps, mapProps = _a.mapProps, onHoverInfo = _a.onHoverInfo, processDataFn = _a.processDataFn, selectedSCF = _a.selectedSCF, setOnHoverInfo = _a.setOnHoverInfo, tooltipElement = _a.tooltipElement; var _d = useTheme(), breakpoints = _d.breakpoints, spacing = _d.spacing; var _e = React.useState(), scfsJSON = _e[0], setSCFsJSON = _e[1]; var _f = React.useState(), hoverInfo = _f[0], setHoverInfo = _f[1]; var finalHoverInfo = onHoverInfo || hoverInfo; React.useEffect(function () { fetch(geoJSONPath) .then(function (resp) { return resp.json(); }) .then(function (json) { return setSCFsJSON(json); }) .catch(function (err) { return console.error('Could not load data', err); }); // eslint-disable-line }, []); var processedData = React.useMemo(function () { return scfsJSON && processDataFn && processDataFn(scfsJSON, data); }, [data, scfsJSON, processDataFn]); var initialBoundsPosition = React.useMemo(function () { if (selectedSCF && processedData) { // eslint-disable-next-line @typescript-eslint/restrict-template-expressions var features = processedData.features.filter(function (f) { return selectedSCF.includes(f.id); }); if (features.length > 0) { var _a = bbox({ features: features, type: 'FeatureCollection', }), minLng = _a[0], minLat = _a[1], maxLng = _a[2], maxLat = _a[3]; return { id: features.length > 1 ? undefined : selectedSCF[0], position: [ [minLng, minLat], [maxLng, maxLat], ], }; } } return undefined; }, [processedData, selectedSCF]); // eslint-disable-next-line react/jsx-no-useless-fragment if (!processedData) return _jsx(_Fragment, {}); var parentHeight = (mapProps === null || mapProps === void 0 ? void 0 : mapProps.height) || 450; var parentWidth = (mapProps === null || mapProps === void 0 ? void 0 : mapProps.width) || '100%'; return (_jsx(Map, __assign({ data: processedData, height: parentHeight, initialBoundsPosition: initialBoundsPosition, mapboxAccessToken: mapboxAccessToken, setHoverInfo: setOnHoverInfo || setHoverInfo, width: parentWidth }, mapProps, { children: tooltipElement || (finalHoverInfo && (isString(parentWidth) || parentWidth > breakpoints.values.sm - parseInt(spacing(12), 10)) ? (_jsx(MapPopup, __assign({ popupProps: { latitude: finalHoverInfo.lat, longitude: finalHoverInfo.lng, }, rows: [ { title: 'SCF Code', value: (_b = finalHoverInfo.feature.properties) === null || _b === void 0 ? void 0 : _b.name, }, { title: 'Volume', value: numeral((_c = finalHoverInfo.feature.properties) === null || _c === void 0 ? void 0 : _c.value).format('0,0'), }, ] }, mapPopupProps))) : undefined) }))); }; export default SCFMap; //# sourceMappingURL=index.js.map