From 5afa907609d887129793387e8b3fb8ed4356bf3c Mon Sep 17 00:00:00 2001 From: Xor290 Date: Wed, 18 Feb 2026 23:08:10 +0100 Subject: [PATCH] chore: fix crash gps --- .../src/screens/delivery/DashboardScreen.tsx | 39 ++++++++++++++++--- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/frontend-admin/src/screens/delivery/DashboardScreen.tsx b/frontend-admin/src/screens/delivery/DashboardScreen.tsx index e29aa803..6b75fa16 100644 --- a/frontend-admin/src/screens/delivery/DashboardScreen.tsx +++ b/frontend-admin/src/screens/delivery/DashboardScreen.tsx @@ -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((_, 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; } }, []);