129 lines
4.5 KiB
React
Raw Normal View History

2022-08-24 10:44:35 +03:00
import useSWR from "swr";
2022-09-09 12:44:34 +03:00
import { useState } from "react";
import { WiCloudDown } from "react-icons/wi";
import { MdLocationDisabled, MdLocationSearching } from "react-icons/md";
2022-09-25 19:43:00 +03:00
import { useTranslation } from "next-i18next";
import classNames from "classnames";
import Error from "../error";
2022-08-25 11:13:31 +03:00
2022-08-24 10:44:35 +03:00
import Icon from "./icon";
2022-09-09 12:44:34 +03:00
function Widget({ options }) {
2022-09-08 11:48:16 +03:00
const { t, i18n } = useTranslation();
const { data, error } = useSWR(
`/api/widgets/weather?${new URLSearchParams({ lang: i18n.language, ...options }).toString()}`
);
2022-08-24 10:44:35 +03:00
2022-09-09 12:44:34 +03:00
if (error || data?.error) {
return <Error options={options} />
2022-08-24 10:44:35 +03:00
}
if (!data) {
2022-09-09 12:44:34 +03:00
return (
<div className={classNames(
"flex flex-col justify-center first:ml-0 ml-4 mr-2",
options?.styleBoxed === true && " ml-4 mt-2 m:mb-0 rounded-md shadow-md shadow-theme-900/10 dark:shadow-theme-900/20 bg-theme-100/20 dark:bg-white/5 p-3",
)}>
2022-09-09 12:44:34 +03:00
<div className="flex flex-row items-center justify-end">
<div className="flex flex-col items-center">
<WiCloudDown className="w-8 h-8 text-theme-800 dark:text-theme-200" />
</div>
<div className="flex flex-col ml-3 text-left">
<span className="text-theme-800 dark:text-theme-200 text-sm">{t("weather.updating")}</span>
<span className="text-theme-800 dark:text-theme-200 text-xs">{t("weather.wait")}</span>
</div>
</div>
</div>
);
2022-08-24 10:44:35 +03:00
}
2022-09-08 11:48:16 +03:00
const unit = options.units === "metric" ? "celsius" : "fahrenheit";
2022-08-24 10:44:35 +03:00
return (
<div className={classNames(
"flex flex-col justify-center first:ml-0 ml-4 mr-2",
options?.styleBoxed === true && " ml-4 mt-2 m:mb-0 rounded-md shadow-md shadow-theme-900/10 dark:shadow-theme-900/20 bg-theme-100/20 dark:bg-white/5 p-3",
)}>
2022-08-27 00:55:13 +03:00
<div className="flex flex-row items-center justify-end">
2022-08-27 11:04:06 +03:00
<div className="flex flex-col items-center">
<Icon condition={data.current.condition.code} timeOfDay={data.current.is_day ? "day" : "night"} />
</div>
2022-08-27 00:55:13 +03:00
<div className="flex flex-col ml-3 text-left">
<span className="text-theme-800 dark:text-theme-200 text-sm">
2022-08-27 11:04:06 +03:00
{options.label && `${options.label}, `}
2022-09-08 11:48:16 +03:00
{t("common.number", {
value: options.units === "metric" ? data.current.temp_c : data.current.temp_f,
style: "unit",
unit,
})}
2022-08-27 00:55:13 +03:00
</span>
<span className="text-theme-800 dark:text-theme-200 text-xs">{data.current.condition.text}</span>
</div>
2022-08-24 10:44:35 +03:00
</div>
</div>
);
}
2022-09-09 12:44:34 +03:00
export default function WeatherApi({ options }) {
const { t } = useTranslation();
const [location, setLocation] = useState(false);
const [requesting, setRequesting] = useState(false);
if (!location && options.latitude && options.longitude) {
2022-09-09 12:44:34 +03:00
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,
}
);
}
2022-09-09 12:44:34 +03:00
};
2022-09-26 15:03:02 +03:00
// if (!requesting && !location) requestLocation();
2022-09-09 12:44:34 +03:00
if (!location) {
return (
2022-10-08 16:40:36 +03:00
<button
type="button"
onClick={() => requestLocation()}
className={classNames(
"flex flex-col justify-center first:ml-0 ml-4 mr-2",
options?.styleBoxed === true && " ml-4 mt-2 m:mb-0 rounded-md shadow-md shadow-theme-900/10 dark:shadow-theme-900/20 bg-theme-100/20 dark:bg-white/5 p-3",
)}
2022-10-08 16:40:36 +03:00
>
2022-09-09 12:44:34 +03:00
<div className="flex flex-row items-center justify-end">
<div className="flex flex-col items-center">
{requesting ? (
<MdLocationSearching className="w-6 h-6 text-theme-800 dark:text-theme-200 animate-pulse" />
) : (
<MdLocationDisabled className="w-6 h-6 text-theme-800 dark:text-theme-200" />
)}
</div>
<div className="flex flex-col ml-3 text-left">
<span className="text-theme-800 dark:text-theme-200 text-sm">{t("weather.current")}</span>
<span className="text-theme-800 dark:text-theme-200 text-xs">{t("weather.allow")}</span>
</div>
</div>
</button>
);
}
return <Widget options={{ ...location, ...options }} />;
}