include configs on .env vars or pass through props
This commit is contained in:
parent
a6047a9341
commit
d1a5138c6e
@ -1,9 +1,14 @@
|
|||||||
import 'tailwindcss/tailwind.css'
|
import 'tailwindcss/tailwind.css'
|
||||||
import '../src/index.css'
|
import '../src/index.css'
|
||||||
|
|
||||||
|
|
||||||
import type { Preview } from '@storybook/react';
|
import type { Preview } from '@storybook/react';
|
||||||
|
|
||||||
|
window.process = {
|
||||||
|
...window.process,
|
||||||
|
env:{
|
||||||
|
...window.process?.env
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const preview: Preview = {
|
const preview: Preview = {
|
||||||
parameters: {
|
parameters: {
|
||||||
actions: { argTypesRegex: '^on[A-Z].*' },
|
actions: { argTypesRegex: '^on[A-Z].*' },
|
||||||
|
|||||||
@ -8,17 +8,14 @@ import {
|
|||||||
TileLayer,
|
TileLayer,
|
||||||
GeoJSON as GeoJSONLayer,
|
GeoJSON as GeoJSONLayer,
|
||||||
LayersControl,
|
LayersControl,
|
||||||
|
TileLayerProps,
|
||||||
} from 'react-leaflet';
|
} from 'react-leaflet';
|
||||||
|
|
||||||
import 'leaflet/dist/leaflet.css';
|
import 'leaflet/dist/leaflet.css';
|
||||||
import * as L from 'leaflet';
|
import * as L from 'leaflet';
|
||||||
|
|
||||||
export type MapProps = {
|
export type MapProps = {
|
||||||
tile?: {
|
tile?: TileLayerProps;
|
||||||
attribution?: string;
|
|
||||||
url: string;
|
|
||||||
data?: any;
|
|
||||||
};
|
|
||||||
layers: {
|
layers: {
|
||||||
data: GeospatialData;
|
data: GeospatialData;
|
||||||
name: string;
|
name: string;
|
||||||
@ -64,6 +61,30 @@ export function Map({
|
|||||||
const [isLoading, setIsLoading] = useState<boolean>(false);
|
const [isLoading, setIsLoading] = useState<boolean>(false);
|
||||||
const [layersData, setLayersData] = useState<any>([]);
|
const [layersData, setLayersData] = useState<any>([]);
|
||||||
|
|
||||||
|
/*
|
||||||
|
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(() => {
|
useEffect(() => {
|
||||||
const loadDataPromises = layers.map(async (layer) => {
|
const loadDataPromises = layers.map(async (layer) => {
|
||||||
const url = layer.data.url;
|
const url = layer.data.url;
|
||||||
@ -154,7 +175,8 @@ export function Map({
|
|||||||
map.target.fitBounds(layerToZoomBounds);
|
map.target.fitBounds(layerToZoomBounds);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<TileLayer url={tile.url} attribution={tile.attribution} {...tile.data} />
|
{tileData.url && <TileLayer {...tileData} />}
|
||||||
|
|
||||||
<LayersControl position="bottomright">
|
<LayersControl position="bottomright">
|
||||||
{layers.map((layer) => {
|
{layers.map((layer) => {
|
||||||
const data = layersData.find(
|
const data = layersData.find(
|
||||||
|
|||||||
@ -44,11 +44,10 @@ export const GeoJSONPolygons: Story = {
|
|||||||
name: 'GeoJSON polygons map',
|
name: 'GeoJSON polygons map',
|
||||||
args: {
|
args: {
|
||||||
tile : {
|
tile : {
|
||||||
url : 'https://tiles.stadiamaps.com/tiles/alidade_satellite/{z}/{x}/{y}{r}.{ext}',
|
url : 'https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}',
|
||||||
attribution:'© CNES, Distribution Airbus DS, © Airbus DS, © PlanetObserver (Contains Copernicus Data) | © <a href="https://www.stadiamaps.com/" target="_blank">Stadia Maps</a> © <a href="https://openmaptiles.org/" target="_blank">OpenMapTiles</a> © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
|
attribution:'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://www.mapbox.com/about/maps/">Mapbox</a>',
|
||||||
data: {
|
accessToken : 'pk.eyJ1Ijoid2lsbHktcGFsbWFyZWpvIiwiYSI6ImNqNzk5NmRpNDFzb2cyeG9sc2luMHNjajUifQ.lkoVRFSI8hOLH4uJeOzwXw',
|
||||||
ext: 'jpg'
|
id: 'mapbox/streets-v10'
|
||||||
}
|
|
||||||
},
|
},
|
||||||
layers: [
|
layers: [
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user