-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
332 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
meta { | ||
name: coordinates | ||
type: http | ||
seq: 4 | ||
} | ||
|
||
get { | ||
url: {{base}}/v2/route/coordinates?chain_uid={{chainUID}} | ||
body: json | ||
auth: none | ||
} | ||
|
||
query { | ||
chain_uid: {{chainUID}} | ||
} | ||
|
||
body:json { | ||
{ | ||
"chain_uid": "{{chainUID}}", | ||
"route_order": [ | ||
"a151dcbc-d97b-46c5-bac0-90565da8e345", | ||
"7711dc5f-396f-4c98-b86e-171f568f7ddb", | ||
"6d71acc5-a2d5-4734-b2ac-ed5c11242dfd", | ||
"ba6cf778-9d87-4090-bd88-52195b6aa876", | ||
"2d5e1a55-0b56-48b0-a55e-c32a09614f80", | ||
"11e52eea-7542-42ab-aa4e-c3a08ce51d7d" | ||
] | ||
} | ||
} | ||
|
||
body:text { | ||
{ | ||
"chain_uid": "{{chainUID}}", | ||
"route_order": [ | ||
"a151dcbc-d97b-46c5-bac0-90565da8e345", | ||
"7711dc5f-396f-4c98-b86e-171f568f7ddb", | ||
"6d71acc5-a2d5-4734-b2ac-ed5c11242dfd", | ||
"ba6cf778-9d87-4090-bd88-52195b6aa876", | ||
"2d5e1a55-0b56-48b0-a55e-c32a09614f80", | ||
"11e52eea-7542-42ab-aa4e-c3a08ce51d7d" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import mapboxjs, { GeoJSONSource } from "mapbox-gl"; | ||
import { useEffect, useState } from "react"; | ||
import { RouteCoordinate, routeCoordinates } from "../../api/route"; | ||
import { Chain, UID } from "../../api/types"; | ||
import type { FeatureCollection } from "geojson"; | ||
import { useDebouncedCallback } from "use-debounce"; | ||
const MAPBOX_TOKEN = import.meta.env.VITE_MAPBOX_KEY; | ||
|
||
export default function RouteMap(props: { chain: Chain; route: UID[] }) { | ||
const [map, setMap] = useState<mapboxjs.Map | null>(null); | ||
const [id] = useState(() => window.crypto.randomUUID()); | ||
useEffect(() => { | ||
const _map = new mapboxjs.Map({ | ||
container: id, | ||
accessToken: MAPBOX_TOKEN, | ||
style: "mapbox://styles/mapbox/light-v11", | ||
center: [props.chain.longitude, props.chain.latitude], | ||
zoom: 11, | ||
}); | ||
setMap(_map); | ||
|
||
_map.on("load", () => { | ||
(async () => { | ||
const coords = (await routeCoordinates(props.chain.uid)).data; | ||
_map.addSource("route", { | ||
type: "geojson", | ||
data: mapToGeoJSONCoords(coords, props.route), | ||
}); | ||
|
||
_map.addLayer({ | ||
id: "point-bg", | ||
type: "circle", | ||
source: "route", | ||
paint: { | ||
"circle-color": ["rgba", 239, 149, 61, 0.6], // #ef953d | ||
"circle-radius": 15, | ||
"circle-stroke-width": 0, | ||
}, | ||
}); | ||
|
||
_map.addLayer({ | ||
id: "point-text", | ||
type: "symbol", | ||
source: "route", | ||
layout: { | ||
"text-field": ["get", "route_order"], | ||
"text-font": ["DIN Offc Pro Medium", "Arial Unicode MS Bold"], | ||
"text-size": 12, | ||
}, | ||
}); | ||
})(); | ||
}); | ||
|
||
return () => { | ||
_map?.remove(); | ||
}; | ||
}, []); | ||
|
||
const debouncedUpdateSource = useDebouncedCallback( | ||
async () => { | ||
const routeSource = map!.getSource("route") as GeoJSONSource; | ||
if (!routeSource) return; | ||
const coords = (await routeCoordinates(props.chain.uid)).data; | ||
routeSource.setData(mapToGeoJSONCoords(coords, props.route)); | ||
}, | ||
2e3, | ||
{} | ||
); | ||
useEffect(() => { | ||
console.log("Loading", map === null ? "map is null" : "map exists"); | ||
|
||
if (!map) return; | ||
debouncedUpdateSource(); | ||
}, [props.chain, props.route]); | ||
|
||
return <div id={id} className="w-full h-full"></div>; | ||
} | ||
|
||
function mapToGeoJSONCoords( | ||
coords: RouteCoordinate[], | ||
route: UID[] | ||
): FeatureCollection { | ||
return { | ||
type: "FeatureCollection", | ||
features: coords.map((coord) => { | ||
const route_order = route.indexOf(coord.user_uid) + 1; | ||
return { | ||
type: "Feature", | ||
geometry: { | ||
type: "Point", | ||
coordinates: [coord.longitude, coord.latitude], | ||
}, | ||
properties: { | ||
uid: coord.user_uid, | ||
route_order, | ||
}, | ||
}; | ||
}), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { useTranslation } from "react-i18next"; | ||
import { Chain, UID } from "../../api/types"; | ||
import { useRef, MouseEvent, useState } from "react"; | ||
import RouteMap from "./RouteMap"; | ||
|
||
export default function RouteMapPopup(props: { | ||
chain: Chain; | ||
closeFunc: () => void; | ||
route: UID[]; | ||
optimizeRoute: () => Promise<void>; | ||
returnToPreviousRoute: () => void; | ||
}) { | ||
const { t } = useTranslation(); | ||
const ref = useRef<HTMLDialogElement>(null); | ||
const refButtonClose = useRef<HTMLButtonElement>(null); | ||
const [routeWasOptimized, setRouteWasOptimized] = useState(false); | ||
|
||
function returnToPreviousRoute() { | ||
props.returnToPreviousRoute(); | ||
setRouteWasOptimized(false); | ||
} | ||
|
||
function optimizeRoute() { | ||
props.optimizeRoute().then( | ||
() => { | ||
setRouteWasOptimized(true); | ||
}, | ||
() => { | ||
setRouteWasOptimized(false); | ||
} | ||
); | ||
} | ||
|
||
function handleBackgroundClick(e: MouseEvent) { | ||
e.preventDefault(); | ||
if (window.innerWidth > 900) { | ||
if (e.target === e.currentTarget) { | ||
props.closeFunc(); | ||
} | ||
} | ||
} | ||
|
||
return ( | ||
<div className="absolute inset-0 p-4 bg-white/30 hidden lg:block"> | ||
<dialog | ||
open | ||
className="relative w-full h-full flex p-0 shadow-lg" | ||
ref={ref} | ||
tabIndex={-1} | ||
onClick={handleBackgroundClick} | ||
> | ||
<form | ||
className="flex flex-col bg-white w-full h-full p-6" | ||
style={{ "--tw-shadow": "#333" } as any} | ||
> | ||
<div className="flex-grow"> | ||
<RouteMap chain={props.chain} route={props.route} /> | ||
</div> | ||
<div className="mt-4 flex justify-between"> | ||
{routeWasOptimized ? ( | ||
<button | ||
type="button" | ||
className="btn btn-sm btn-ghost bg-teal-light text-teal" | ||
onClick={returnToPreviousRoute} | ||
> | ||
{t("routeUndoOptimize")} | ||
<span className="feather feather-corner-left-up ms-2" /> | ||
</button> | ||
) : ( | ||
<button | ||
type="button" | ||
className="btn btn-sm btn-ghost bg-teal-light text-teal" | ||
onClick={optimizeRoute} | ||
> | ||
{t("routeOptimize")} | ||
<span className="feather feather-zap ms-2" /> | ||
</button> | ||
)} | ||
<button | ||
key="close" | ||
type="reset" | ||
ref={refButtonClose} | ||
className="btn btn-sm btn-ghost" | ||
onClick={() => props.closeFunc()} | ||
> | ||
{t("close")} | ||
</button> | ||
</div> | ||
</form> | ||
</dialog> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.