feat: refine widget interactions
This commit is contained in:
1 parent
a8a7545f40
commit
842e86012d
107 files changed
+3144
-1233
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { getPreferredUnit, getVisibleForecastDays } from "./component";
|
||||
import { getPreferredTime, getPreferredUnit, getPreferredWindSpeed, getVisibleForecastDays } from "./component";
|
||||
|
||||
describe("weather formatting", () => {
|
||||
it("preserves valid zero temperatures", () => {
|
||||
@@ -8,9 +8,15 @@ describe("weather formatting", () => {
|
||||
expect(getPreferredUnit(0, true, true)).toBe("32°F");
|
||||
});
|
||||
|
||||
it("keeps zero wind speeds and missing forecast values truthful", () => {
|
||||
expect(getPreferredWindSpeed(0)).toBe("0.0");
|
||||
expect(getPreferredWindSpeed(undefined)).toBe("?");
|
||||
expect(getPreferredTime(undefined)).toBe("?");
|
||||
expect(getPreferredTime("invalid")).toBe("?");
|
||||
});
|
||||
|
||||
it("limits compact forecasts by both axes", () => {
|
||||
expect(getVisibleForecastDays(7, 120, 100, false)).toBe(2);
|
||||
expect(getVisibleForecastDays(7, 600, 100, false)).toBe(3);
|
||||
expect(getVisibleForecastDays(7, 120, 100, true)).toBe(7);
|
||||
expect(getVisibleForecastDays(7, 120, 100)).toBe(2);
|
||||
expect(getVisibleForecastDays(7, 600, 100)).toBe(3);
|
||||
});
|
||||
});
|
||||
@@ -1,7 +1,16 @@
|
||||
"use client";
|
||||
|
||||
import { Box, Group, HoverCard, Stack, Text } from "@mantine/core";
|
||||
import { IconArrowDownRight, IconArrowUpRight, IconDroplets, IconMapPin, IconWind } from "@tabler/icons-react";
|
||||
import { useId } from "react";
|
||||
import { Box, Group, HoverCard, Stack, Table, Text } from "@mantine/core";
|
||||
import {
|
||||
IconArrowDownRight,
|
||||
IconArrowUpRight,
|
||||
IconDroplets,
|
||||
IconMapPin,
|
||||
IconSunrise,
|
||||
IconSunset,
|
||||
IconWind,
|
||||
} from "@tabler/icons-react";
|
||||
import combineClasses from "clsx";
|
||||
import dayjs from "dayjs";
|
||||
|
||||
@@ -30,6 +39,10 @@ export default function WeatherWidget({
|
||||
|
||||
if (!weather) return <WidgetEmptyState />;
|
||||
|
||||
if (displayMode === "advanced") {
|
||||
return <AdvancedWeather weather={weather} options={options} />;
|
||||
}
|
||||
|
||||
return (
|
||||
<Stack
|
||||
align="center"
|
||||
@@ -43,11 +56,10 @@ export default function WeatherWidget({
|
||||
<WeeklyForecast
|
||||
weather={weather}
|
||||
options={options}
|
||||
maxDays={getVisibleForecastDays(options.forecastDayCount, width, height, displayMode === "advanced")}
|
||||
showDetails={displayMode === "advanced"}
|
||||
maxDays={getVisibleForecastDays(options.forecastDayCount, width, height)}
|
||||
/>
|
||||
) : (
|
||||
<DailyWeather weather={weather} options={options} showDetails={displayMode === "advanced"} />
|
||||
<DailyWeather weather={weather} options={options} />
|
||||
)}
|
||||
</Stack>
|
||||
);
|
||||
@@ -57,7 +69,172 @@ interface WeatherProps extends Pick<WidgetComponentProps<"weather">, "options">
|
||||
weather: NonNullable<RouterOutputs["widget"]["weather"]["atLocation"]>;
|
||||
}
|
||||
|
||||
const DailyWeather = ({ options, weather, showDetails = false }: WeatherProps & { showDetails?: boolean }) => {
|
||||
const AdvancedWeather = ({ options, weather }: WeatherProps) => {
|
||||
const forecastHeadingId = useId();
|
||||
const t = useScopedI18n("widget.weather");
|
||||
const tCommon = useScopedI18n("common");
|
||||
|
||||
return (
|
||||
<Stack h="100%" w="100%" gap="sm" p="md" style={{ overflow: "hidden" }}>
|
||||
<Group justify="space-between" align="center" gap="sm" wrap="wrap">
|
||||
<Group gap="sm" wrap="nowrap">
|
||||
<AnimatedWeatherIcon size={42} code={weather.current.weathercode} />
|
||||
<Stack gap={0}>
|
||||
<Text fz="xl" fw={700} lh={1.1}>
|
||||
{getPreferredUnit(
|
||||
weather.current.temperature,
|
||||
options.isFormatFahrenheit,
|
||||
options.disableTemperatureDecimals,
|
||||
)}
|
||||
</Text>
|
||||
<WeatherDescription weatherOnly weatherCode={weather.current.weathercode} />
|
||||
</Stack>
|
||||
</Group>
|
||||
|
||||
<Group gap="lg" wrap="wrap">
|
||||
<Group gap={5} wrap="nowrap">
|
||||
<IconMapPin size={16} aria-hidden />
|
||||
<Text size="sm">{options.location.name}</Text>
|
||||
</Group>
|
||||
<Group gap={5} wrap="nowrap">
|
||||
<IconWind size={16} aria-hidden />
|
||||
<Text size="sm">
|
||||
{t("currentWindSpeed", {
|
||||
currentWindSpeed: getPreferredWindSpeed(weather.current.windspeed, options.useImperialSpeed),
|
||||
unit: options.useImperialSpeed
|
||||
? tCommon("unit.speed.milesPerHour")
|
||||
: tCommon("unit.speed.kilometersPerHour"),
|
||||
})}
|
||||
</Text>
|
||||
</Group>
|
||||
</Group>
|
||||
</Group>
|
||||
|
||||
<Text id={forecastHeadingId} component="h3" size="sm" fw={600}>
|
||||
{t("advanced.forecast")}
|
||||
</Text>
|
||||
<Table.ScrollContainer
|
||||
minWidth={680}
|
||||
maxHeight="100%"
|
||||
style={{ flex: 1, minHeight: 0 }}
|
||||
scrollAreaProps={{ type: "auto", offsetScrollbars: "present" }}
|
||||
>
|
||||
<Table
|
||||
aria-labelledby={forecastHeadingId}
|
||||
striped
|
||||
highlightOnHover
|
||||
verticalSpacing="xs"
|
||||
horizontalSpacing="sm"
|
||||
stickyHeader
|
||||
>
|
||||
<Table.Thead>
|
||||
<Table.Tr>
|
||||
<Table.Th>{t("advanced.day")}</Table.Th>
|
||||
<Table.Th>{t("advanced.conditions")}</Table.Th>
|
||||
<Table.Th>{t("advanced.temperature")}</Table.Th>
|
||||
<Table.Th>{t("advanced.humidity")}</Table.Th>
|
||||
<Table.Th>
|
||||
{t("advanced.wind", {
|
||||
unit: options.useImperialSpeed
|
||||
? tCommon("unit.speed.milesPerHour")
|
||||
: tCommon("unit.speed.kilometersPerHour"),
|
||||
})}
|
||||
</Table.Th>
|
||||
<Table.Th>{t("advanced.sun")}</Table.Th>
|
||||
</Table.Tr>
|
||||
</Table.Thead>
|
||||
<Table.Tbody>
|
||||
{weather.daily.length === 0 ? (
|
||||
<Table.Tr>
|
||||
<Table.Td colSpan={6}>
|
||||
<Text size="sm" c="dimmed" ta="center">
|
||||
{t("advanced.noForecast")}
|
||||
</Text>
|
||||
</Table.Td>
|
||||
</Table.Tr>
|
||||
) : (
|
||||
weather.daily.map((dayWeather) => (
|
||||
<Table.Tr key={dayWeather.time}>
|
||||
<Table.Td>
|
||||
<Stack gap={0}>
|
||||
<Text size="sm" fw={600} tt="capitalize">
|
||||
{dayjs(dayWeather.time).format("ddd")}
|
||||
</Text>
|
||||
<Text size="xs" c="dimmed">
|
||||
{dayjs(dayWeather.time).format("MMM D")}
|
||||
</Text>
|
||||
</Stack>
|
||||
</Table.Td>
|
||||
<Table.Td>
|
||||
<Group gap="xs" wrap="nowrap">
|
||||
<AnimatedWeatherIcon size={20} code={dayWeather.weatherCode} />
|
||||
<WeatherDescription weatherOnly weatherCode={dayWeather.weatherCode} />
|
||||
</Group>
|
||||
</Table.Td>
|
||||
<Table.Td>
|
||||
<Group gap={4} wrap="nowrap">
|
||||
<IconArrowUpRight size={15} aria-hidden />
|
||||
<Text size="sm">
|
||||
{getPreferredUnit(
|
||||
dayWeather.maxTemp,
|
||||
options.isFormatFahrenheit,
|
||||
options.disableTemperatureDecimals,
|
||||
)}
|
||||
</Text>
|
||||
<Text size="sm" c="dimmed">
|
||||
/
|
||||
</Text>
|
||||
<IconArrowDownRight size={15} aria-hidden />
|
||||
<Text size="sm">
|
||||
{getPreferredUnit(
|
||||
dayWeather.minTemp,
|
||||
options.isFormatFahrenheit,
|
||||
options.disableTemperatureDecimals,
|
||||
)}
|
||||
</Text>
|
||||
</Group>
|
||||
</Table.Td>
|
||||
<Table.Td>
|
||||
<Group gap={5} wrap="nowrap">
|
||||
<IconDroplets size={15} aria-hidden />
|
||||
<Text size="sm">{dayWeather.humidity === undefined ? "?" : `${dayWeather.humidity}%`}</Text>
|
||||
</Group>
|
||||
</Table.Td>
|
||||
<Table.Td>
|
||||
<Group gap={5} wrap="nowrap">
|
||||
<IconWind size={15} aria-hidden />
|
||||
<Text size="sm">
|
||||
{getPreferredWindSpeed(dayWeather.maxWindSpeed, options.useImperialSpeed)} /{" "}
|
||||
{getPreferredWindSpeed(dayWeather.maxWindGusts, options.useImperialSpeed)}
|
||||
</Text>
|
||||
</Group>
|
||||
</Table.Td>
|
||||
<Table.Td>
|
||||
<Group gap="xs" wrap="nowrap">
|
||||
<Group gap={3} wrap="nowrap">
|
||||
<IconSunrise size={15} aria-hidden />
|
||||
<Text size="sm">{getPreferredTime(dayWeather.sunrise)}</Text>
|
||||
</Group>
|
||||
<Text size="sm" c="dimmed">
|
||||
/
|
||||
</Text>
|
||||
<Group gap={3} wrap="nowrap">
|
||||
<IconSunset size={15} aria-hidden />
|
||||
<Text size="sm">{getPreferredTime(dayWeather.sunset)}</Text>
|
||||
</Group>
|
||||
</Group>
|
||||
</Table.Td>
|
||||
</Table.Tr>
|
||||
))
|
||||
)}
|
||||
</Table.Tbody>
|
||||
</Table>
|
||||
</Table.ScrollContainer>
|
||||
</Stack>
|
||||
);
|
||||
};
|
||||
|
||||
const DailyWeather = ({ options, weather }: WeatherProps) => {
|
||||
const t = useScopedI18n("widget.weather");
|
||||
const tCommon = useScopedI18n("common");
|
||||
|
||||
@@ -138,39 +315,11 @@ const DailyWeather = ({ options, weather, showDetails = false }: WeatherProps &
|
||||
</Group>
|
||||
</>
|
||||
)}
|
||||
{showDetails && weather.daily[0] && (
|
||||
<WeatherDescription
|
||||
useImperialSpeed={options.useImperialSpeed}
|
||||
dateFormat={options.dateFormat}
|
||||
time={weather.daily[0].time}
|
||||
weatherCode={weather.daily[0].weatherCode}
|
||||
maxTemp={getPreferredUnit(
|
||||
weather.daily[0].maxTemp,
|
||||
options.isFormatFahrenheit,
|
||||
options.disableTemperatureDecimals,
|
||||
)}
|
||||
minTemp={getPreferredUnit(
|
||||
weather.daily[0].minTemp,
|
||||
options.isFormatFahrenheit,
|
||||
options.disableTemperatureDecimals,
|
||||
)}
|
||||
sunrise={dayjs(weather.daily[0].sunrise).format("HH:mm")}
|
||||
sunset={dayjs(weather.daily[0].sunset).format("HH:mm")}
|
||||
maxWindSpeed={weather.daily[0].maxWindSpeed}
|
||||
maxWindGusts={weather.daily[0].maxWindGusts}
|
||||
humidity={weather.daily[0].humidity}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
const WeeklyForecast = ({
|
||||
options,
|
||||
weather,
|
||||
maxDays,
|
||||
showDetails,
|
||||
}: WeatherProps & { maxDays: number; showDetails: boolean }) => {
|
||||
const WeeklyForecast = ({ options, weather, maxDays }: WeatherProps & { maxDays: number }) => {
|
||||
return (
|
||||
<>
|
||||
<Group className="weather-forecast-city-temp-group" wrap="nowrap" gap="md">
|
||||
@@ -202,17 +351,12 @@ const WeeklyForecast = ({
|
||||
</Text>
|
||||
</Group>
|
||||
</Group>
|
||||
<Forecast weather={weather} options={options} maxDays={maxDays} showDetails={showDetails} />
|
||||
<Forecast weather={weather} options={options} maxDays={maxDays} />
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
function Forecast({
|
||||
weather,
|
||||
options,
|
||||
maxDays,
|
||||
showDetails,
|
||||
}: WeatherProps & { maxDays: number; showDetails: boolean }) {
|
||||
function Forecast({ weather, options, maxDays }: WeatherProps & { maxDays: number }) {
|
||||
const dateFormat = options.dateFormat;
|
||||
return (
|
||||
<Group className="weather-forecast-days-group" w="100%" justify="space-evenly" wrap="nowrap" pb="sm">
|
||||
@@ -233,11 +377,6 @@ function Forecast({
|
||||
<Text fz={16}>
|
||||
{getPreferredUnit(dayWeather.maxTemp, options.isFormatFahrenheit, options.disableTemperatureDecimals)}
|
||||
</Text>
|
||||
{showDetails && (
|
||||
<Text fz="xs" c="dimmed">
|
||||
{getPreferredUnit(dayWeather.minTemp, options.isFormatFahrenheit, options.disableTemperatureDecimals)}
|
||||
</Text>
|
||||
)}
|
||||
</Stack>
|
||||
</HoverCard.Target>
|
||||
<HoverCard.Dropdown>
|
||||
@@ -276,13 +415,16 @@ export const getPreferredUnit = (value?: number, isFahrenheit = false, disableTe
|
||||
: `${value.toFixed(disableTemperatureDecimals ? 0 : 1)}°C`
|
||||
: "?";
|
||||
|
||||
export const getVisibleForecastDays = (
|
||||
configuredDays: number,
|
||||
width: number,
|
||||
height: number,
|
||||
isAdvanced: boolean,
|
||||
): number => {
|
||||
if (isAdvanced) return configuredDays;
|
||||
export const getPreferredWindSpeed = (value: number | undefined, useImperialSpeed = false): string =>
|
||||
value === undefined ? "?" : (useImperialSpeed ? metricToImperial(value) : value).toFixed(1);
|
||||
|
||||
export const getPreferredTime = (value: string | undefined): string => {
|
||||
if (!value) return "?";
|
||||
const time = dayjs(value);
|
||||
return time.isValid() ? time.format("HH:mm") : "?";
|
||||
};
|
||||
|
||||
export const getVisibleForecastDays = (configuredDays: number, width: number, height: number): number => {
|
||||
const widthBudget = Math.max(1, Math.floor(width / 58));
|
||||
const heightBudget = height < 120 ? 3 : configuredDays;
|
||||
return Math.min(configuredDays, widthBudget, heightBudget);
|
||||
|
||||
Reference in new issue
Block a user