mirror of
https://github.com/DI0IK/homepage-plus.git
synced 2025-07-08 06:38:46 +00:00
Added OpenWeatherMap widget.
This commit is contained in:
parent
f0f470ba8b
commit
6a97d392c9
6 changed files with 482 additions and 0 deletions
44
src/components/widgets/openweathermap/weather.jsx
Normal file
44
src/components/widgets/openweathermap/weather.jsx
Normal file
|
@ -0,0 +1,44 @@
|
|||
import useSWR from "swr";
|
||||
import { BiError } from "react-icons/bi";
|
||||
|
||||
import Icon from "./icon";
|
||||
|
||||
export default function OpenWeatherMap({ options }) {
|
||||
const { data, error } = useSWR(
|
||||
`/api/widgets/openweathermap?lat=${options.latitude}&lon=${options.longitude}&apiKey=${options.apiKey}&duration=${options.cache}`
|
||||
);
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div className="order-last grow flex-none flex flex-row items-center justify-end">
|
||||
<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>
|
||||
);
|
||||
}
|
||||
|
||||
if (!data) {
|
||||
return <div className="order-last grow flex-none flex flex-row items-center justify-end"></div>;
|
||||
}
|
||||
|
||||
if (data.error) {
|
||||
return <div className="order-last grow flex-none flex flex-row items-center justify-end"></div>;
|
||||
}
|
||||
// OpenWeatherMap returns temperature in Kelvins
|
||||
var temp_c = data.main.temp - 273.15;
|
||||
var temp_f = temp_c * 9 / 5 + 32;
|
||||
return (
|
||||
<div className="order-last grow flex-none flex flex-row items-center justify-end">
|
||||
<Icon condition={data.weather[0].id} timeOfDay={(data.dt > data.sys.sunrise) && (data.dt < data.sys.sundown) ? "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" ? temp_c.toFixed(0) : temp_f.toFixed(0)}°
|
||||
</span>
|
||||
<span className="text-theme-800 dark:text-theme-200 text-xs">{data.weather[0].description}</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue