import { Card, Flex, Group, Stack, Text, Title } from "@mantine/core";
import {
IconArrowDownRight,
IconArrowUpRight,
IconMapPin,
} from "@tabler/icons-react";
import type { RouterOutputs } from "@homarr/api";
import { clientApi } from "@homarr/api/client";
import type { WidgetComponentProps } from "../definition";
import { WeatherIcon } from "./icon";
export default function WeatherWidget({
options,
width,
}: WidgetComponentProps<"weather">) {
const [weather] = clientApi.widget.weather.atLocation.useSuspenseQuery(
{
latitude: options.location.latitude,
longitude: options.location.longitude,
},
{
refetchOnMount: false,
refetchOnWindowFocus: false,
refetchOnReconnect: false,
},
);
return (
);
}
interface DailyWeatherProps
extends Pick, "width" | "options"> {
shouldHide: boolean;
weather: RouterOutputs["widget"]["weather"]["atLocation"];
}
const DailyWeather = ({
shouldHide,
width,
options,
weather,
}: DailyWeatherProps) => {
if (shouldHide) {
return null;
}
return (
<>
{getPreferredUnit(
weather.current_weather.temperature,
options.isFormatFahrenheit,
)}
{width > 200 && (
{getPreferredUnit(
weather.daily.temperature_2m_max[0]!,
options.isFormatFahrenheit,
)}
{getPreferredUnit(
weather.daily.temperature_2m_min[0]!,
options.isFormatFahrenheit,
)}
)}
{options.showCity && (
{options.location.name}
)}
>
);
};
interface WeeklyForecastProps
extends Pick, "width" | "options"> {
shouldHide: boolean;
weather: RouterOutputs["widget"]["weather"]["atLocation"];
}
const WeeklyForecast = ({
shouldHide,
width,
options,
weather,
}: WeeklyForecastProps) => {
if (shouldHide) {
return null;
}
return (
<>
{options.showCity && (
{options.location.name}
)}
20 ? "red" : "blue"}
>
{getPreferredUnit(
weather.current_weather.temperature,
options.isFormatFahrenheit,
)}
>
);
};
interface ForecastProps
extends Pick, "options" | "width"> {
weather: RouterOutputs["widget"]["weather"]["atLocation"];
}
function Forecast({ weather, options, width }: ForecastProps) {
return (
{weather.daily.time
.slice(
0,
Math.min(options.forecastDayCount, width / (width < 300 ? 64 : 92)),
)
.map((time, index) => (
{new Date(time).getDate().toString().padStart(2, "0")}
{getPreferredUnit(
weather.daily.temperature_2m_max[index]!,
options.isFormatFahrenheit,
)}
{getPreferredUnit(
weather.daily.temperature_2m_min[index]!,
options.isFormatFahrenheit,
)}
))}
);
}
const getPreferredUnit = (value: number, isFahrenheit = false): string =>
isFahrenheit
? `${(value * (9 / 5) + 32).toFixed(1)}°F`
: `${value.toFixed(1)}°C`;