Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add weekly forecast to weather widget #1932

Merged
merged 9 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions public/locales/en/modules/weather.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
"displayCityName":{
"label":"Display City Name"
},
"displayWeekly":{
"label": "Display Weekly Forecast"
},
"forecastDays":{
"label": "Days To Display"
},
"location": {
"label": "Weather location"
}
Expand Down
2 changes: 2 additions & 0 deletions src/server/api/routers/weather.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const weatherSchema = z.object({
temperature: z.number(),
}),
daily: z.object({
time: z.array(z.string()),
weathercode: z.array(z.number()),
temperature_2m_max: z.array(z.number()),
temperature_2m_min: z.array(z.number()),
}),
Expand Down
174 changes: 114 additions & 60 deletions src/widgets/weather/WeatherTile.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Center, Flex, Group, Skeleton, Stack, Text, Title } from '@mantine/core';
import { Card, Center, Flex, Group, Stack, Text, Title } from '@mantine/core';
import { useElementSize } from '@mantine/hooks';
import {
IconArrowDownRight,
Expand All @@ -7,9 +7,11 @@ import {
IconMapPin,
} from '@tabler/icons-react';
import { useTranslation } from 'react-i18next';
import { Weather } from '~/server/api/routers/weather';
import { api } from '~/utils/api';

import { defineWidget } from '../helper';
import { WidgetLoading } from '../loading';
import { IWidget } from '../widgets';
import { WeatherIcon } from './WeatherIcon';

Expand All @@ -25,6 +27,17 @@ const definition = defineWidget({
type: 'switch',
defaultValue: false,
},
displayWeekly: {
type: 'switch',
defaultValue: false,
},
forecastDays: {
type: 'slider',
defaultValue: 5,
min: 1,
max: 7,
step: 1,
},
location: {
type: 'location',
defaultValue: {
Expand Down Expand Up @@ -55,24 +68,7 @@ function WeatherTile({ widget }: WeatherTileProps) {
const { t } = useTranslation('modules/weather');

if (isLoading) {
return (
<Stack
ref={ref}
spacing="xs"
justify="space-around"
align="center"
style={{ height: '100%', width: '100%' }}
>
<Skeleton height={40} width={100} mb="xl" />
<Group noWrap>
<Skeleton height={50} circle />
<Group>
<Skeleton height={25} width={70} mr="lg" />
<Skeleton height={25} width={70} />
</Group>
</Group>
</Stack>
);
return <WidgetLoading />;
}

if (isError) {
Expand All @@ -83,56 +79,114 @@ function WeatherTile({ widget }: WeatherTileProps) {
);
}

// TODO: add widgetWrapper that is generic and uses the definition
return (
<Stack
style={{ height: '100%', width: '100%' }}
justify="space-around"
ref={ref}
spacing={0}
align="center"
>
<Flex
align="center"
gap={width < 120 ? '0.25rem' : 'xs'}
justify={'center'}
direction={width < 200 ? 'column' : 'row'}
>
<WeatherIcon size={width < 300 ? 30 : 50} code={weather.current_weather.weathercode} />
<Title size={'h2'}>
{getPerferedUnit(
weather.current_weather.temperature,
widget.properties.displayInFahrenheit
)}
</Title>
</Flex>
<Stack w="100%" h="100%" justify="space-around" ref={ref} spacing={0} align="center">
{(widget?.properties.displayWeekly && (
<>
<Flex
align="center"
gap={width < 120 ? '0.25rem' : 'xs'}
justify={'center'}
direction={'row'}
>
{widget.properties.displayCityName && (
<Group noWrap spacing={5} align="center">
<IconMapPin color="blue" size={30} />
<Text size={25} style={{ whiteSpace: 'nowrap' }}>
{widget.properties.location.name}
</Text>
</Group>
)}
<WeatherIcon size={width < 300 ? 30 : 50} code={weather.current_weather.weathercode} />
<Title size={'h2'} color={weather.current_weather.temperature > 20 ? 'red' : 'blue'}>
{getPreferredUnit(
weather.current_weather.temperature,
widget.properties.displayInFahrenheit
)}
</Title>
</Flex>
<Forecast weather={weather} widget={widget} />
</>
)) || (
<>
<Flex
align="center"
gap={width < 120 ? '0.25rem' : 'xs'}
justify={'center'}
direction={width < 200 ? 'column' : 'row'}
>
<WeatherIcon size={width < 300 ? 30 : 50} code={weather.current_weather.weathercode} />
<Title size={'h2'}>
{getPreferredUnit(
weather.current_weather.temperature,
widget.properties.displayInFahrenheit
)}
</Title>
</Flex>

{width > 200 && (
<Group noWrap spacing="xs">
<IconArrowUpRight />
{getPerferedUnit(
weather.daily.temperature_2m_max[0],
widget.properties.displayInFahrenheit
{width > 200 && (
<Group noWrap spacing="xs">
<IconArrowUpRight />
{getPreferredUnit(
weather.daily.temperature_2m_max[0],
widget.properties.displayInFahrenheit
)}
<IconArrowDownRight />
{getPreferredUnit(
weather.daily.temperature_2m_min[0],
widget.properties.displayInFahrenheit
)}
</Group>
)}
<IconArrowDownRight />
{getPerferedUnit(
weather.daily.temperature_2m_min[0],
widget.properties.displayInFahrenheit
)}
</Group>
)}

{widget.properties.displayCityName && (
<Group noWrap spacing={5} align="center">
<IconMapPin height={15} width={15} />
<Text style={{ whiteSpace: 'nowrap' }}>{widget.properties.location.name}</Text>
</Group>
{widget.properties.displayCityName && (
<Group noWrap spacing={5} align="center">
<IconMapPin height={15} width={15} />
<Text style={{ whiteSpace: 'nowrap' }}>{widget.properties.location.name}</Text>
</Group>
)}
</>
)}
</Stack>
);
}

const getPerferedUnit = (value: number, isFahrenheit = false): string =>
const getPreferredUnit = (value: number, isFahrenheit = false): string =>
isFahrenheit ? `${(value * (9 / 5) + 32).toFixed(1)}°F` : `${value.toFixed(1)}°C`;

interface ForecastProps {
weather: Weather;
widget: IWeatherWidget;
}

function Forecast({ weather: { daily }, widget }: ForecastProps) {
const { width } = useElementSize();
return (
<Flex align="center" direction="row" justify="space-between" w="100%" px="sm">
{daily.time.slice(0, widget.properties.forecastDays).map((time: any, index: number) => (
<Card key={index} padding="0.25rem">
<Flex direction="column" align="center">
<Text fw={700} lh="1.25rem">
{time.split('-')[2]}
</Text>
<WeatherIcon size={width < 300 ? 30 : 50} code={daily.weathercode[index]} />
<Text fz="sm" lh="1rem">
{getPreferredUnit(
daily.temperature_2m_max[index],
widget.properties.displayInFahrenheit
)}
</Text>
<Text fz="sm" lh="1rem" color="grey">
{getPreferredUnit(
daily.temperature_2m_min[index],
widget.properties.displayInFahrenheit
)}
</Text>
</Flex>
</Card>
))}
</Flex>
);
}

export default definition;
Loading