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
// --------------------------------------------------
const sendingRef = useRef(false);
const sendCurrentLocation = useCallback(async () => {
if (sendingRef.current) return;
sendingRef.current = true;
try {
const loc = await Location.getCurrentPositionAsync({
accuracy: Location.Accuracy.High,
});
const { latitude, longitude } = loc.coords;
const loc = await Promise.race([
Location.getCurrentPositionAsync({
accuracy: Location.Accuracy.High,
}),
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 });
setLastUpdate(new Date());
await updateMyLocation(latitude, longitude);
} catch {
/* silent */
} catch (err) {
console.log("GPS error:", err);
} finally {
sendingRef.current = false;
}
}, []);