From d1a5138c6e5bc9d4c935b0797a5bb581a04e6bc3 Mon Sep 17 00:00:00 2001 From: William Lima Date: Wed, 22 May 2024 11:48:20 -0100 Subject: [PATCH] include configs on .env vars or pass through props --- packages/components/.storybook/preview.ts | 9 ++++-- packages/components/src/components/Map.tsx | 34 ++++++++++++++++++---- packages/components/stories/Map.stories.ts | 9 +++--- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/packages/components/.storybook/preview.ts b/packages/components/.storybook/preview.ts index 1ec353c5..a1d3914d 100644 --- a/packages/components/.storybook/preview.ts +++ b/packages/components/.storybook/preview.ts @@ -1,9 +1,14 @@ import 'tailwindcss/tailwind.css' import '../src/index.css' - - import type { Preview } from '@storybook/react'; +window.process = { + ...window.process, + env:{ + ...window.process?.env + } +}; + const preview: Preview = { parameters: { actions: { argTypesRegex: '^on[A-Z].*' }, diff --git a/packages/components/src/components/Map.tsx b/packages/components/src/components/Map.tsx index 212efd79..70af81f0 100644 --- a/packages/components/src/components/Map.tsx +++ b/packages/components/src/components/Map.tsx @@ -8,17 +8,14 @@ import { TileLayer, GeoJSON as GeoJSONLayer, LayersControl, + TileLayerProps, } from 'react-leaflet'; import 'leaflet/dist/leaflet.css'; import * as L from 'leaflet'; export type MapProps = { - tile?: { - attribution?: string; - url: string; - data?: any; - }; + tile?: TileLayerProps; layers: { data: GeospatialData; name: string; @@ -64,6 +61,30 @@ export function Map({ const [isLoading, setIsLoading] = useState(false); const [layersData, setLayersData] = useState([]); + /* + tileEnvVars + extract all environment variables thats starts with NEXT_PUBLIC_MAP_TILE_. + the variables names are the same as the TileLayer object properties: + - url: + - attribution + - accessToken + - id + - ext + - bounds + - maxZoom + - minZoom + see TileLayerOptions inteface + */ + const tileEnvVars = Object.keys(process?.env) + .filter((key) => key.startsWith('NEXT_PUBLIC_MAP_TILE_')) + .reduce((obj, key) => { + obj[key.split('NEXT_PUBLIC_MAP_TILE_')[1]] = process.env[key]; + return obj; + }, {}); + + //tileData prioritizes properties passed through component over those passed through .env variables + const tileData = Object.assign(tileEnvVars, tile); + useEffect(() => { const loadDataPromises = layers.map(async (layer) => { const url = layer.data.url; @@ -154,7 +175,8 @@ export function Map({ map.target.fitBounds(layerToZoomBounds); }} > - + {tileData.url && } + {layers.map((layer) => { const data = layersData.find( diff --git a/packages/components/stories/Map.stories.ts b/packages/components/stories/Map.stories.ts index 343d1587..0da60f03 100644 --- a/packages/components/stories/Map.stories.ts +++ b/packages/components/stories/Map.stories.ts @@ -44,11 +44,10 @@ export const GeoJSONPolygons: Story = { name: 'GeoJSON polygons map', args: { tile : { - url : 'https://tiles.stadiamaps.com/tiles/alidade_satellite/{z}/{x}/{y}{r}.{ext}', - attribution:'© CNES, Distribution Airbus DS, © Airbus DS, © PlanetObserver (Contains Copernicus Data) | © Stadia Maps © OpenMapTiles © OpenStreetMap contributors', - data: { - ext: 'jpg' - } + url : 'https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', + attribution:'© OpenStreetMap contributors © Mapbox', + accessToken : 'pk.eyJ1Ijoid2lsbHktcGFsbWFyZWpvIiwiYSI6ImNqNzk5NmRpNDFzb2cyeG9sc2luMHNjajUifQ.lkoVRFSI8hOLH4uJeOzwXw', + id: 'mapbox/streets-v10' }, layers: [ {