/** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: MIT-0 */ /* eslint-disable @typescript-eslint/no-var-requires */ import React, { useEffect, useState } from 'react' import ReactMapGL, { NavigationControl, Source, Layer } from 'react-map-gl' import * as polyline from '@mapbox/polyline' import Container from 'aws-northstar/layouts/Container' import Inline from 'aws-northstar/layouts/Inline' import Icon from 'aws-northstar/components/Icon' import Alert from 'aws-northstar/components/Alert' // to fix: https://github.com/visgl/react-map-gl/issues/1266 import mapboxgl from 'mapbox-gl' import MapPin from '../MapPin' import utils from '../../utils' import { appvars } from '../../config' // @ts-ignore // eslint-disable-next-line import/no-webpack-loader-syntax mapboxgl.workerClass = require('worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker').default const navControlStyle = { left: 10, top: 10, } export type MapInputProps = { orders?: any[] geofences?: any[] warehouses?: any[] customers?: any[] } const getColor = (idx: number) => { if (idx % 2) { return '#03AA46' } return '#AA0303' } const MAX_DRIVERS = 100 const { MAPBOX_VARS } = appvars const MapComponent: React.FC = ({ orders, geofences, warehouses, customers }) => { const [geoJSON, setGeoJSON] = useState([]) const [drivers, setDrivers] = useState([]) const [polygons, setPolygons] = useState([]) const baseLocation = { latitude: MAPBOX_VARS.DEFAULT_LATITUDE, longitude: MAPBOX_VARS.DEFAULT_LONGITUDE, zoom: 12, } if (warehouses && warehouses.length > 0) { baseLocation.latitude = warehouses[0].latitude baseLocation.longitude = warehouses[0].longitude } else if (customers && customers.length > 0) { baseLocation.latitude = customers[0].latitude baseLocation.longitude = customers[0].longitude } const [viewport, setViewport] = useState({ width: '100%', height: 800, ...baseLocation, }) const resetDefault = () => { setViewport((old) => ({ ...old, ...baseLocation, })) } useEffect(() => { if (orders) { // TODO: change once routing available q.detail.route const paths = orders.map((q: any) => q.detail.route && polyline.toGeoJSON(q.detail.route.pointsEncoded)).flat() if (paths.length) { setGeoJSON( paths.map((p: any) => ({ type: 'Feature', geometry: p, })), ) } } }, [orders]) return ( {viewport.latitude} {viewport.longitude} {parseInt(viewport.zoom.toString(), 10)} } > {drivers.length === MAX_DRIVERS && The Map only shows {MAX_DRIVERS} drivers} setViewport(v)} > {warehouses && warehouses.map((r: any, idx: number) => ( ))} {customers && customers.map((r: any, idx: number) => ( ))} {drivers && drivers.map((d: any, idx: number) => ( ))} {orders && orders .flatMap((q: any) => q.detail.segments) .filter((s) => s.segmentType === 'TO_DESTINATION') .map((r: any, idx: number) => ( ))} {orders && orders .flatMap((q: any) => q.detail.segments) .filter((s) => s.segmentType === 'TO_ORIGIN') .map((r: any, idx: number) => ( ))} {geoJSON && geoJSON.map((g: any, idx: number) => ( ))} {geofences && geofences.map((g: any, idx: number) => ( ))} {polygons && polygons.map((p: any, idx: number) => ( ))} ) } export default MapComponent