chore: fix crash gps

This commit is contained in:
2026-02-18 23:08:10 +01:00
parent 1f2e4e96dd
commit 5afa907609
@@ -281,17 +281,44 @@ export default function DashboardScreen() {
// -------------------------------------------------- // --------------------------------------------------
// Location tracking // Location tracking
// -------------------------------------------------- // --------------------------------------------------
const sendingRef = useRef(false);
const sendCurrentLocation = useCallback(async () => { const sendCurrentLocation = useCallback(async () => {
if (sendingRef.current) return;
sendingRef.current = true;
try { try {
const loc = await Location.getCurrentPositionAsync({ const loc = await Promise.race([
accuracy: Location.Accuracy.High, Location.getCurrentPositionAsync({
}); accuracy: Location.Accuracy.High,
const { latitude, longitude } = loc.coords; }),
new Promise<never>((_, reject) =>
setTimeout(() => reject(new Error("GPS timeout")), 8000),
),
]);
// validation runtime + typage TS
if (
!loc ||
typeof loc !== "object" ||
!("coords" in loc) ||
!loc.coords
) {
console.log("GPS invalide — update ignoré");
return;
}
const coords = (loc as Location.LocationObject).coords;
const { latitude, longitude } = coords;
setLastCoords({ lat: latitude, lng: longitude }); setLastCoords({ lat: latitude, lng: longitude });
setLastUpdate(new Date()); setLastUpdate(new Date());
await updateMyLocation(latitude, longitude); await updateMyLocation(latitude, longitude);
} catch { } catch (err) {
/* silent */ console.log("GPS error:", err);
} finally {
sendingRef.current = false;
} }
}, []); }, []);