From 8de0faf8fad595c299e0288b47fa81d17c0a40ed Mon Sep 17 00:00:00 2001 From: wont-stream <143244075+wont-stream@users.noreply.github.com> Date: Thu, 10 Apr 2025 10:45:03 -0400 Subject: [PATCH] refactor: restructure geolocation handling for improved readability --- src/components/navbar/index.tsx | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/components/navbar/index.tsx b/src/components/navbar/index.tsx index c32c859..765a6c3 100644 --- a/src/components/navbar/index.tsx +++ b/src/components/navbar/index.tsx @@ -37,11 +37,24 @@ export default () => { setWttrDesc(`${weatherDesc.description} & ${dayOrNight}`); } - (async () => { + const getAproximateLocation = async () => { const req = await fetch("https://cf.ipv4-army.workers.dev/") const res = await req.json(); return await getWeather(res); - })() + } + + if ("geolocation" in navigator) { + navigator.geolocation.getCurrentPosition((position) => { + const coords = position.coords; + getWeather(coords); + }, getAproximateLocation, { + enableHighAccuracy: true, + maximumAge: 30 * 60 * 1000, // 30 minutes + timeout: 5000 + }); + } else { + getAproximateLocation(); + } return ( <>