61 lines
2.0 KiB
React
Raw Normal View History

2022-08-24 10:44:35 +03:00
import useSWR from "swr";
2022-08-25 11:13:31 +03:00
import { BiError } from "react-icons/bi";
2022-09-08 11:48:16 +03:00
import { useTranslation } from "react-i18next";
2022-08-25 11:13:31 +03:00
2022-08-24 10:44:35 +03:00
import Icon from "./icon";
export default function WeatherApi({ 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
if (error) {
2022-08-25 11:13:31 +03:00
return (
<div className="flex flex-col">
<div className="flex flex-row items-center justify-end">
<div className="flex flex-col items-center">
<BiError className="w-8 h-8 text-theme-800 dark:text-theme-200" />
<div className="flex flex-col ml-3 text-left">
<span className="text-theme-800 dark:text-theme-200 text-sm">API</span>
<span className="text-theme-800 dark:text-theme-200 text-xs">Error</span>
</div>
</div>
2022-08-25 11:13:31 +03:00
</div>
</div>
);
2022-08-24 10:44:35 +03:00
}
if (!data) {
2022-09-07 16:53:24 +03:00
return <div className="flex flex-row items-center justify-end" />;
2022-08-24 10:44:35 +03:00
}
if (data.error) {
2022-09-07 16:53:24 +03:00
return <div className="flex flex-row items-center justify-end" />;
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 (
2022-09-03 12:40:04 +03:00
<div className="flex flex-col justify-center">
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>
);
}