mirror of
https://github.com/gethomepage/homepage.git
synced 2025-08-11 09:13:38 -04:00
Enhancement: Auto-request geolocation if permission already granted (#5638)
This commit is contained in:
parent
bb23c25690
commit
5870111d11
@ -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 (
|
||||
|
@ -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 (
|
||||
|
@ -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 (
|
||||
|
Loading…
x
Reference in New Issue
Block a user