import { Box, Group, HoverCard, Space, Stack, Text } from "@mantine/core"; import { IconArrowDownRight, IconArrowUpRight, IconMapPin } from "@tabler/icons-react"; import combineClasses from "clsx"; import dayjs from "dayjs"; import type { RouterOutputs } from "@homarr/api"; import { clientApi } from "@homarr/api/client"; import type { WidgetComponentProps } from "../definition"; import { WeatherDescription, WeatherIcon } from "./icon"; export default function WeatherWidget({ options }: WidgetComponentProps<"weather">) { const [weather] = clientApi.widget.weather.atLocation.useSuspenseQuery( { latitude: options.location.latitude, longitude: options.location.longitude, }, { refetchOnMount: false, refetchOnWindowFocus: false, refetchOnReconnect: false, }, ); return ( {options.hasForecast ? ( ) : ( )} ); } interface WeatherProps extends Pick, "options"> { weather: RouterOutputs["widget"]["weather"]["atLocation"]; } const DailyWeather = ({ options, weather }: WeatherProps) => { return ( <> {getPreferredUnit(weather.current.temperature, options.isFormatFahrenheit)} {getPreferredUnit(weather.daily[0]?.maxTemp, options.isFormatFahrenheit)} {getPreferredUnit(weather.daily[0]?.minTemp, options.isFormatFahrenheit)} {options.showCity && ( <> {options.location.name} )} ); }; const WeeklyForecast = ({ options, weather }: WeatherProps) => { return ( <> {options.showCity && ( <> {options.location.name} )} {getPreferredUnit(weather.current.temperature, options.isFormatFahrenheit)} ); }; function Forecast({ weather, options }: WeatherProps) { return ( {weather.daily.slice(0, options.forecastDayCount).map((dayWeather, index) => ( {dayjs(dayWeather.time).format("dd")} {getPreferredUnit(dayWeather.maxTemp, options.isFormatFahrenheit)} ))} ); } const getPreferredUnit = (value?: number, isFahrenheit = false): string => value ? (isFahrenheit ? `${(value * (9 / 5) + 32).toFixed(1)}°F` : `${value.toFixed(1)}°C`) : "?";