ContextMenu.res.mjs

  1// Generated by ReScript, PLEASE EDIT WITH CARE
  2
  3import * as Log from "../utils/Log.res.mjs";
  4import * as State from "../State.res.mjs";
  5import * as Waldo from "../ffi/Waldo.res.mjs";
  6import * as NanoID from "../ffi/NanoID.res.mjs";
  7import * as Core__Option from "@rescript/core/lib/es6/src/Core__Option.res.mjs";
  8import * as LucidePreact from "lucide-preact";
  9import * as Signals from "@preact/signals";
 10import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
 11import * as JsxRuntime from "preact/jsx-runtime";
 12import * as ReactMaplibre from "@vis.gl/react-maplibre";
 13
 14let show = Signals.signal(undefined);
 15
 16async function handleWaypoint(lon, lat, editor) {
 17  let params = {
 18    lon: lon,
 19    lat: lat
 20  };
 21  let results = await Waldo.Reverse.geocode(params);
 22  switch (results.TAG) {
 23    case "Success" :
 24      let result = results._0[0];
 25      if (result !== undefined) {
 26        let waypointContent = {
 27          type: "waypoint",
 28          attrs: {
 29            id: NanoID.make(),
 30            point: [
 31              lon,
 32              lat
 33            ],
 34            label: result.name,
 35            country: result.country,
 36            gid: result.gid.toString(),
 37            from: undefined,
 38            cons: [],
 39            nonroutable: false,
 40            error: undefined
 41          }
 42        };
 43        let textContent = {
 44          type: "text",
 45          content: " "
 46        };
 47        if (editor !== undefined) {
 48          let ed = Primitive_option.valFromOption(editor);
 49          ed.commands.insertContent(waypointContent);
 50          ed.commands.insertContent(textContent);
 51        }
 52      } else {
 53        Log.warn("Reverse geocode returned no results", {
 54          lon: lon.toString(),
 55          lat: lat.toString()
 56        });
 57      }
 58      break;
 59    case "Error" :
 60      Log.error("Reverse geocode error", {
 61        error: results._0,
 62        lon: lon.toString(),
 63        lat: lat.toString()
 64      });
 65      break;
 66    case "NetworkError" :
 67      Log.error("Reverse geocode network error", {
 68        error: results._0,
 69        lon: lon.toString(),
 70        lat: lat.toString()
 71      });
 72      break;
 73  }
 74  show.value = undefined;
 75}
 76
 77function ContextMenu(props) {
 78  let editor = State.Editor.get(props.doc);
 79  let evt = show.value;
 80  let match = evt !== undefined ? [
 81      evt.lngLat.lng,
 82      evt.lngLat.lat
 83    ] : [
 84      0.0,
 85      0.0
 86    ];
 87  let lat = match[1];
 88  let lon = match[0];
 89  if (Core__Option.isSome(show.value)) {
 90    return JsxRuntime.jsxs(JsxRuntime.Fragment, {
 91      children: [
 92        JsxRuntime.jsx(ReactMaplibre.Marker, {
 93          longitude: lon,
 94          latitude: lat,
 95          children: JsxRuntime.jsx(LucidePreact.Dot, {
 96            strokeWidth: 6,
 97            className: "text-(--color-orange)"
 98          })
 99        }),
100        JsxRuntime.jsxs(ReactMaplibre.Popup, {
101          longitude: lon,
102          latitude: lat,
103          anchor: "top-left",
104          children: [
105            JsxRuntime.jsx("style", {
106              children: "\n        .maplibregl-popup-content {\n          margin: 0;\n          padding: 0;\n          padding-top: 2px;\n        }\n      "
107            }),
108            JsxRuntime.jsxs("div", {
109              children: [
110                JsxRuntime.jsxs("div", {
111                  children: [
112                    JsxRuntime.jsx(LucidePreact.MapPinned, {
113                      className: "my-auto"
114                    }),
115                    JsxRuntime.jsx("a", {
116                      children: "Open Google Maps",
117                      className: "w-full px-2 py-1 text-md",
118                      href: `https://www.google.com/maps/@` + lat.toString() + `,` + lon.toString() + `,15z`,
119                      target: "_blank",
120                      onClick: param => {
121                        show.value = undefined;
122                      }
123                    })
124                  ],
125                  className: "flex flex-row gap-1 hover:bg-blue-100 justify-start px-1 w-full"
126                }),
127                JsxRuntime.jsxs("div", {
128                  children: [
129                    JsxRuntime.jsx(LucidePreact.LocateFixed, {
130                      className: "my-auto"
131                    }),
132                    JsxRuntime.jsx("button", {
133                      children: "Add waypoint",
134                      className: "w-full py-1 px-2 text-md",
135                      onClick: param => {
136                        handleWaypoint(lon, lat, editor);
137                      }
138                    })
139                  ],
140                  className: "flex flex-row gap-1 hover:bg-blue-100 justify-start px-1 w-full"
141                })
142              ],
143              className: "flex flex-col m-0 p-0 rounded-b-sm rounded-r-sm gap-1 justify-start items-start"
144            })
145          ],
146          style: {
147            margin: 0,
148            padding: 0,
149            background: "#FFFFFF00"
150          },
151          className: "m-0 p-0 bg-[#FFFFFF00]",
152          onClose: param => {
153            show.value = undefined;
154          },
155          closeButton: false,
156          closeOnClick: true,
157          closeOnMove: true
158        })
159      ]
160    });
161  } else {
162    return null;
163  }
164}
165
166let make = ContextMenu;
167
168export {
169  show,
170  handleWaypoint,
171  make,
172}
173/* show Not a pure module */