Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions web/src/components/LeafletMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const LocationMarker = (props: MarkerProps) => {

useEffect(() => {
map.attributionControl.setPrefix("");
map.attributionControl.addAttribution("<a href='https://www.openstreetmap.org/copyright'>OpenStreetMap</a>");
map.locate();
}, []);

Expand Down
32 changes: 28 additions & 4 deletions web/src/components/MemoEditor/ActionButton/LocationSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LatLng } from "leaflet";
import { MapPinIcon, XIcon } from "lucide-react";
import { useEffect, useState } from "react";
import { useEffect, useState, useRef } from "react";
import toast from "react-hot-toast";
import LeafletMap from "@/components/LeafletMap";
import { Button } from "@/components/ui/button";
Expand All @@ -21,13 +21,25 @@
position?: LatLng;
}

interface NomatimRateLimit {
lastNominatimFetch: Date;
nominatimTimeoutId: number | undefined;
timeBetweenFetch: number;
}

const LocationSelector = (props: Props) => {
const t = useTranslate();
const [state, setState] = useState<State>({
initilized: false,
placeholder: props.location?.placeholder || "",
position: props.location ? new LatLng(props.location.latitude, props.location.longitude) : undefined,
});
const rateLimit = useRef<NomatimRateLimit>({
lastNominatimFetch: new Date(0),
nominatimTimeoutId: undefined,
timeBetweenFetch: 1300,
});

const [popoverOpen, setPopoverOpen] = useState<boolean>(false);

useEffect(() => {
Expand Down Expand Up @@ -63,14 +75,16 @@
}
}, [popoverOpen]);

useEffect(() => {
const updateReverseGeocoding = () => {
if (!state.position) {
setState({ ...state, placeholder: "" });
return;
}

// Fetch reverse geocoding data.
fetch(`https://nominatim.openstreetmap.org/reverse?lat=${state.position.lat}&lon=${state.position.lng}&format=json`)
fetch(`https://nominatim.openstreetmap.org/reverse?lat=${state.position.lat}&lon=${state.position.lng}&format=json`, {
cache: "default",
headers: new Headers({ "Cache-Control": "max-age=86400" }),
})
.then((response) => response.json())
.then((data) => {
if (data && data.display_name) {
Expand All @@ -81,6 +95,16 @@
toast.error("Failed to fetch reverse geocoding data");
console.error("Failed to fetch reverse geocoding data:", error);
});
}

Check failure on line 98 in web/src/components/MemoEditor/ActionButton/LocationSelector.tsx

View workflow job for this annotation

GitHub Actions / static-checks

Insert `;`

useEffect(() => {
// Fetch reverse geocoding with rate limits
clearTimeout(rateLimit.current.nominatimTimeoutId);
const timeLeft = rateLimit.current.timeBetweenFetch - (new Date().getTime() - rateLimit.current.lastNominatimFetch.getTime());
rateLimit.current.nominatimTimeoutId = setTimeout(() => {
updateReverseGeocoding();
rateLimit.current.lastNominatimFetch = new Date();
}, timeLeft);
}, [state.position]);

const onPositionChanged = (position: LatLng) => {
Expand Down
Loading