46 lines
1.6 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-08-24 10:44:35 +03:00
import Icon from "./icon";
export default function Weather({ options }) {
const { data, error } = useSWR(
2022-08-25 11:13:31 +03:00
`/api/widgets/weather?lat=${options.latitude}&lon=${options.longitude}&apiKey=${options.apiKey}&duration=${options.cache}`
2022-08-24 10:44:35 +03:00
);
if (error) {
2022-08-25 11:13:31 +03:00
return (
2022-08-27 00:55:13 +03:00
<div className="flex flex-row items-center">
2022-08-25 11:13:31 +03:00
<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-24 10:44:35 +03:00
}
if (!data) {
2022-08-27 00:55:13 +03:00
return <div className="flex flex-row items-center"></div>;
2022-08-24 10:44:35 +03:00
}
if (data.error) {
2022-08-27 00:55:13 +03:00
return <div className="flex flex-row items-center"></div>;
2022-08-24 10:44:35 +03:00
}
return (
2022-08-27 00:55:13 +03:00
<div className=" flex flex-col">
<div className="flex flex-row items-center justify-end">
<Icon condition={data.current.condition.code} timeOfDay={data.current.is_day ? "day" : "night"} />
<div className="flex flex-col ml-3 text-left">
<span className="text-theme-800 dark:text-theme-200 text-sm">
{options.units === "metric" ? data.current.temp_c : data.current.temp_f}&deg;
</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>
2022-08-27 00:55:13 +03:00
{options.label && <div className="text-right text-theme-800 dark:text-theme-200 text-xs">{options.label}</div>}
2022-08-24 10:44:35 +03:00
</div>
);
}