Commit b00df8a9 authored by Steven's avatar Steven

fix(web): remove error notifications for location geocoding failures

Remove disruptive error toasts when reverse geocoding or geolocation fails.
Instead, silently fall back to using coordinates as the location placeholder.
This improves UX for users in regions where OpenStreetMap is restricted
(e.g., China) or when CSP blocks external API calls.

Fixes #5198, #5197

🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: 's avatarClaude <noreply@anthropic.com>
parent 5f7c758f
import { LatLng } from "leaflet"; import { LatLng } from "leaflet";
import { MapPinIcon, XIcon } from "lucide-react"; import { MapPinIcon, XIcon } from "lucide-react";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import toast from "react-hot-toast";
import LeafletMap from "@/components/LeafletMap"; import LeafletMap from "@/components/LeafletMap";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
...@@ -48,10 +47,9 @@ const LocationSelector = (props: Props) => { ...@@ -48,10 +47,9 @@ const LocationSelector = (props: Props) => {
useEffect(() => { useEffect(() => {
if (popoverOpen && !props.location) { if (popoverOpen && !props.location) {
const handleError = (error: any, errorMessage: string) => { const handleError = (error: any) => {
setState((prev) => ({ ...prev, initialized: true })); setState((prev) => ({ ...prev, initialized: true }));
toast.error(errorMessage); console.error("Geolocation error:", error);
console.error(error);
}; };
if (navigator.geolocation) { if (navigator.geolocation) {
...@@ -68,11 +66,11 @@ const LocationSelector = (props: Props) => { ...@@ -68,11 +66,11 @@ const LocationSelector = (props: Props) => {
})); }));
}, },
(error) => { (error) => {
handleError(error, "Failed to get current position"); handleError(error);
}, },
); );
} else { } else {
handleError("Geolocation is not supported by this browser.", "Geolocation is not supported by this browser."); handleError("Geolocation is not supported by this browser.");
} }
} }
}, [popoverOpen, props.location]); }, [popoverOpen, props.location]);
...@@ -91,16 +89,29 @@ const LocationSelector = (props: Props) => { ...@@ -91,16 +89,29 @@ const LocationSelector = (props: Props) => {
} }
// Fetch reverse geocoding data. // Fetch reverse geocoding data.
fetch(`https://nominatim.openstreetmap.org/reverse?lat=${state.position.lat}&lon=${state.position.lng}&format=json`) const lat = state.position.lat;
const lng = state.position.lng;
fetch(`https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lng}&format=json`)
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((data) => {
if (data && data.display_name) { if (data && data.display_name) {
setState((prev) => ({ ...prev, placeholder: data.display_name })); setState((prev) => ({ ...prev, placeholder: data.display_name }));
} else {
// Fallback to coordinates if no display name
setState((prev) => ({
...prev,
placeholder: `${lat.toFixed(6)}, ${lng.toFixed(6)}`,
}));
} }
}) })
.catch((error) => { .catch((error) => {
toast.error("Failed to fetch reverse geocoding data"); // Silent fallback: use coordinates as placeholder when geocoding fails
console.error("Failed to fetch reverse geocoding data:", error); console.error("Failed to fetch reverse geocoding data:", error);
setState((prev) => ({
...prev,
placeholder: `${lat.toFixed(6)}, ${lng.toFixed(6)}`,
}));
}); });
}, [state.position]); }, [state.position]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment