MapEvents.res.mjs
1// Generated by ReScript, PLEASE EDIT WITH CARE
2
3import * as Geo from "../lib/Geo.res.mjs";
4import * as Log from "../utils/Log.res.mjs";
5import * as Fetch from "../ffi/Fetch.res.mjs";
6import * as Stats from "./Stats.res.mjs";
7import * as Window from "../ffi/Window.res.mjs";
8import * as Core__Array from "@rescript/core/lib/es6/src/Core__Array.res.mjs";
9import * as JsxRuntime from "preact/jsx-runtime";
10import * as ReactMaplibre from "@vis.gl/react-maplibre";
11
12function fetchFeatureStats(f, event, waypoints) {
13 let hash = f.source.replace("route-", "");
14 Log.debug(`[MapInstance] Got mouseover hash ` + hash, undefined);
15 let url = `/route/` + hash + `.geojson`;
16 Log.debug(`[MapInstance] Fetching from URL: ` + url, undefined);
17 return Fetch.send(Fetch.onResponse(Fetch.get(url), json => {
18 Log.debug(`[MapInstance] Successfully fetched geojson for ` + hash, undefined);
19 let stats = Geo.getStats(Geo.lngLatToPoint(event.lngLat), hash, waypoints, json);
20 console.log("[MapInstance] Calculated stats:", stats);
21 return JsxRuntime.jsx(Stats.make, {
22 dist: stats.dist,
23 time: stats.time,
24 waypoints: waypoints
25 });
26 }, (status, msg) => {
27 Log.error(`[MapInstance] Failed to fetch geojson for ` + hash, {
28 status: status.toString(),
29 message: msg,
30 url: url
31 });
32 }));
33}
34
35function getStats(features, event, waypoints, popup) {
36 Log.debug(`[MapInstance] Fetching stats for ` + features.length.toString() + ` features`, undefined);
37 let promises = features.map(f => fetchFeatureStats(f, event, waypoints));
38 promises.reverse();
39 (async () => {
40 let results = await Promise.all(promises);
41 let statsElements = Core__Array.filterMap(results, x => x);
42 if (statsElements.length > 0) {
43 Log.debug(`[MapInstance] Successfully loaded ` + statsElements.length.toString() + ` of ` + features.length.toString() + ` stats`, undefined);
44 popup.value = JsxRuntime.jsx(ReactMaplibre.Popup, {
45 longitude: event.lngLat.lng,
46 latitude: event.lngLat.lat,
47 children: JsxRuntime.jsx("div", {
48 children: statsElements.length !== 1 ? statsElements.map((elem, i) => JsxRuntime.jsxs("div", {
49 children: [
50 JsxRuntime.jsx("h3", {
51 children: "Leg " + (i + 1 | 0).toString(),
52 className: "font-bold text-[var(--color-blue)] mb-0"
53 }),
54 elem
55 ],
56 className: "mb-2"
57 })) : statsElements[0],
58 className: "flex flex-col"
59 }),
60 maxWidth: Window.isMobile.value ? "300px" : "600px",
61 onClose: _evt => {
62 popup.value = undefined;
63 },
64 closeButton: false
65 });
66 return;
67 } else {
68 return Log.warn("[MapInstance] All feature stats failed to load", undefined);
69 }
70 })();
71}
72
73function handleDistance(event, popup, waypoints) {
74 Log.debug("[MapInstance] Mouse enter event", undefined);
75 let features = event.features;
76 if (features !== undefined) {
77 popup.value = undefined;
78 return getStats(features, event, waypoints, popup);
79 } else {
80 Log.debug("[MapInstance] No features on mouse enter", undefined);
81 popup.value = undefined;
82 return;
83 }
84}
85
86export {
87 fetchFeatureStats,
88 getStats,
89 handleDistance,
90}
91/* Geo Not a pure module */