2025-03-30 21:40:03 -07:00
|
|
|
import { useTranslation } from "next-i18next";
|
2022-10-20 01:52:47 -07:00
|
|
|
import { useState } from "react";
|
|
|
|
import { MdLocationDisabled, MdLocationSearching } from "react-icons/md";
|
2025-03-30 21:40:03 -07:00
|
|
|
import { WiCloudDown } from "react-icons/wi";
|
|
|
|
import useSWR from "swr";
|
2023-04-16 00:05:50 +01:00
|
|
|
|
2025-03-30 21:40:03 -07:00
|
|
|
import mapIcon from "../../../utils/weather/openmeteo-condition-map";
|
2023-06-03 01:10:15 +01:00
|
|
|
import Container from "../widget/container";
|
|
|
|
import ContainerButton from "../widget/container_button";
|
2025-03-30 21:40:03 -07:00
|
|
|
import Error from "../widget/error";
|
2023-06-03 01:10:15 +01:00
|
|
|
import PrimaryText from "../widget/primary_text";
|
|
|
|
import SecondaryText from "../widget/secondary_text";
|
2025-03-30 21:40:03 -07:00
|
|
|
import WidgetIcon from "../widget/widget_icon";
|
2022-10-20 01:52:47 -07:00
|
|
|
|
|
|
|
function Widget({ options }) {
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
2023-10-18 11:44:26 -07:00
|
|
|
const { data, error } = useSWR(`/api/widgets/openmeteo?${new URLSearchParams({ ...options }).toString()}`);
|
2022-10-20 01:52:47 -07:00
|
|
|
|
|
|
|
if (error || data?.error) {
|
2023-10-17 23:26:55 -07:00
|
|
|
return <Error options={options} />;
|
2022-10-20 01:52:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!data) {
|
2023-10-17 23:26:55 -07:00
|
|
|
return (
|
|
|
|
<Container options={options} additionalClassNames="information-widget-openmeteo">
|
|
|
|
<PrimaryText>{t("weather.updating")}</PrimaryText>
|
|
|
|
<SecondaryText>{t("weather.wait")}</SecondaryText>
|
|
|
|
<WidgetIcon icon={WiCloudDown} size="l" />
|
|
|
|
</Container>
|
|
|
|
);
|
2022-10-20 01:52:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const unit = options.units === "metric" ? "celsius" : "fahrenheit";
|
2023-07-19 23:37:30 -07:00
|
|
|
const condition = data.current_weather.weathercode;
|
2023-10-17 23:26:55 -07:00
|
|
|
const timeOfDay =
|
|
|
|
data.current_weather.time > data.daily.sunrise[0] && data.current_weather.time < data.daily.sunset[0]
|
|
|
|
? "day"
|
|
|
|
: "night";
|
2022-10-20 01:52:47 -07:00
|
|
|
|
2023-10-17 23:26:55 -07:00
|
|
|
return (
|
|
|
|
<Container options={options} additionalClassNames="information-widget-openmeteo">
|
|
|
|
<PrimaryText>
|
|
|
|
{options.label && `${options.label}, `}
|
|
|
|
{t("common.number", {
|
|
|
|
value: data.current_weather.temperature,
|
|
|
|
style: "unit",
|
|
|
|
unit,
|
2024-03-10 08:52:57 +02:00
|
|
|
...options.format,
|
2023-10-17 23:26:55 -07:00
|
|
|
})}
|
|
|
|
</PrimaryText>
|
|
|
|
<SecondaryText>{t(`wmo.${data.current_weather.weathercode}-${timeOfDay}`)}</SecondaryText>
|
|
|
|
<WidgetIcon icon={mapIcon(condition, timeOfDay)} size="xl" />
|
|
|
|
</Container>
|
|
|
|
);
|
2022-10-20 01:52:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function OpenMeteo({ options }) {
|
|
|
|
const { t } = useTranslation();
|
|
|
|
const [location, setLocation] = useState(false);
|
|
|
|
const [requesting, setRequesting] = useState(false);
|
|
|
|
|
|
|
|
if (!location && options.latitude && options.longitude) {
|
|
|
|
setLocation({ latitude: options.latitude, longitude: options.longitude });
|
|
|
|
}
|
|
|
|
|
|
|
|
const requestLocation = () => {
|
|
|
|
setRequesting(true);
|
|
|
|
if (typeof window !== "undefined") {
|
|
|
|
navigator.geolocation.getCurrentPosition(
|
|
|
|
(position) => {
|
|
|
|
setLocation({ latitude: position.coords.latitude, longitude: position.coords.longitude });
|
|
|
|
setRequesting(false);
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
setRequesting(false);
|
|
|
|
},
|
|
|
|
{
|
|
|
|
enableHighAccuracy: true,
|
|
|
|
maximumAge: 1000 * 60 * 60 * 3,
|
|
|
|
timeout: 1000 * 30,
|
2023-10-17 23:26:55 -07:00
|
|
|
},
|
2022-10-20 01:52:47 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!location) {
|
2023-10-17 23:26:55 -07:00
|
|
|
return (
|
|
|
|
<ContainerButton
|
|
|
|
options={options}
|
|
|
|
callback={requestLocation}
|
|
|
|
additionalClassNames="information-widget-openmeteo-location-button"
|
|
|
|
>
|
|
|
|
<PrimaryText>{t("weather.current")}</PrimaryText>
|
|
|
|
<SecondaryText>{t("weather.allow")}</SecondaryText>
|
|
|
|
<WidgetIcon icon={requesting ? MdLocationSearching : MdLocationDisabled} size="m" pulse />
|
|
|
|
</ContainerButton>
|
|
|
|
);
|
2022-10-20 01:52:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return <Widget options={{ ...location, ...options }} />;
|
|
|
|
}
|