From 5870111d11073e778f15cf37bbb5ad937313fcc5 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Mon, 4 Aug 2025 23:16:22 -0400 Subject: [PATCH] Enhancement: Auto-request geolocation if permission already granted (#5638) --- src/components/widgets/openmeteo/openmeteo.jsx | 16 +++++++++++++--- .../widgets/openweathermap/weather.jsx | 16 +++++++++++++--- src/components/widgets/weather/weather.jsx | 16 +++++++++++++--- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/components/widgets/openmeteo/openmeteo.jsx b/src/components/widgets/openmeteo/openmeteo.jsx index a46f53abd..27b6a40f1 100644 --- a/src/components/widgets/openmeteo/openmeteo.jsx +++ b/src/components/widgets/openmeteo/openmeteo.jsx @@ -1,5 +1,5 @@ import { useTranslation } from "next-i18next"; -import { useState } from "react"; +import { useCallback, useEffect, useState } from "react"; import { MdLocationDisabled, MdLocationSearching } from "react-icons/md"; import { WiCloudDown } from "react-icons/wi"; import useSWR from "swr"; @@ -64,7 +64,7 @@ export default function OpenMeteo({ options }) { setLocation({ latitude: options.latitude, longitude: options.longitude }); } - const requestLocation = () => { + const requestLocation = useCallback(() => { setRequesting(true); if (typeof window !== "undefined") { navigator.geolocation.getCurrentPosition( @@ -82,7 +82,17 @@ export default function OpenMeteo({ options }) { }, ); } - }; + }, []); + + useEffect(() => { + if (!options.latitude && !options.longitude && typeof navigator !== "undefined") { + navigator.permissions?.query({ name: "geolocation" }).then((result) => { + if (result.state === "granted") { + requestLocation(); + } + }); + } + }, [options.latitude, options.longitude, requestLocation]); if (!location) { return ( diff --git a/src/components/widgets/openweathermap/weather.jsx b/src/components/widgets/openweathermap/weather.jsx index 3336ed4fc..4e7987844 100644 --- a/src/components/widgets/openweathermap/weather.jsx +++ b/src/components/widgets/openweathermap/weather.jsx @@ -1,5 +1,5 @@ import { useTranslation } from "next-i18next"; -import { useState } from "react"; +import { useCallback, useEffect, useState } from "react"; import { MdLocationDisabled, MdLocationSearching } from "react-icons/md"; import { WiCloudDown } from "react-icons/wi"; import useSWR from "swr"; @@ -59,7 +59,7 @@ export default function OpenWeatherMap({ options }) { setLocation({ latitude: options.latitude, longitude: options.longitude }); } - const requestLocation = () => { + const requestLocation = useCallback(() => { setRequesting(true); if (typeof window !== "undefined") { navigator.geolocation.getCurrentPosition( @@ -77,7 +77,17 @@ export default function OpenWeatherMap({ options }) { }, ); } - }; + }, []); + + useEffect(() => { + if (!options.latitude && !options.longitude && typeof navigator !== "undefined") { + navigator.permissions?.query({ name: "geolocation" }).then((result) => { + if (result.state === "granted") { + requestLocation(); + } + }); + } + }, [options.latitude, options.longitude, requestLocation]); if (!location) { return ( diff --git a/src/components/widgets/weather/weather.jsx b/src/components/widgets/weather/weather.jsx index 98768963d..45e6d8159 100644 --- a/src/components/widgets/weather/weather.jsx +++ b/src/components/widgets/weather/weather.jsx @@ -1,5 +1,5 @@ import { useTranslation } from "next-i18next"; -import { useState } from "react"; +import { useCallback, useEffect, useState } from "react"; import { MdLocationDisabled, MdLocationSearching } from "react-icons/md"; import { WiCloudDown } from "react-icons/wi"; import useSWR from "swr"; @@ -63,7 +63,7 @@ export default function WeatherApi({ options }) { setLocation({ latitude: options.latitude, longitude: options.longitude }); } - const requestLocation = () => { + const requestLocation = useCallback(() => { setRequesting(true); if (typeof window !== "undefined") { navigator.geolocation.getCurrentPosition( @@ -81,7 +81,17 @@ export default function WeatherApi({ options }) { }, ); } - }; + }, []); + + useEffect(() => { + if (!options.latitude && !options.longitude && typeof navigator !== "undefined") { + navigator.permissions?.query({ name: "geolocation" }).then((result) => { + if (result.state === "granted") { + requestLocation(); + } + }); + } + }, [options.latitude, options.longitude, requestLocation]); if (!location) { return (