Skip to content

Commit 94e6db1

Browse files
committed
feat(weather): add other measurement units, closes #821 #790
1 parent 26e1d5f commit 94e6db1

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

src/app/api/weather/route.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export const POST = async (req: Request) => {
22
try {
3-
const body: { lat: number; lng: number } = await req.json();
3+
const body: { lat: number; lng: number; temperatureUnit: 'C' | 'F' } =
4+
await req.json();
45

56
if (!body.lat || !body.lng) {
67
return Response.json(
@@ -12,7 +13,7 @@ export const POST = async (req: Request) => {
1213
}
1314

1415
const res = await fetch(
15-
`https://api.open-meteo.com/v1/forecast?latitude=${body.lat}&longitude=${body.lng}&current=weather_code,temperature_2m,is_day,relative_humidity_2m,wind_speed_10m&timezone=auto`,
16+
`https://api.open-meteo.com/v1/forecast?latitude=${body.lat}&longitude=${body.lng}&current=weather_code,temperature_2m,is_day,relative_humidity_2m,wind_speed_10m&timezone=auto${body.temperatureUnit === 'C' ? '' : '&temperature_unit=fahrenheit'}`,
1617
);
1718

1819
const data = await res.json();
@@ -33,12 +34,14 @@ export const POST = async (req: Request) => {
3334
humidity: number;
3435
windSpeed: number;
3536
icon: string;
37+
temperatureUnit: 'C' | 'F';
3638
} = {
3739
temperature: data.current.temperature_2m,
3840
condition: '',
3941
humidity: data.current.relative_humidity_2m,
4042
windSpeed: data.current.wind_speed_10m,
4143
icon: '',
44+
temperatureUnit: body.temperatureUnit,
4245
};
4346

4447
const code = data.current.weather_code;

src/app/settings/page.tsx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ const Page = () => {
148148
const [automaticImageSearch, setAutomaticImageSearch] = useState(false);
149149
const [automaticVideoSearch, setAutomaticVideoSearch] = useState(false);
150150
const [systemInstructions, setSystemInstructions] = useState<string>('');
151+
const [temperatureUnit, setTemperatureUnit] = useState<'C' | 'F'>('C');
151152
const [savingStates, setSavingStates] = useState<Record<string, boolean>>({});
152153

153154
useEffect(() => {
@@ -210,6 +211,8 @@ const Page = () => {
210211

211212
setSystemInstructions(localStorage.getItem('systemInstructions')!);
212213

214+
setTemperatureUnit(localStorage.getItem('temperatureUnit')! as 'C' | 'F');
215+
213216
setIsLoading(false);
214217
};
215218

@@ -368,6 +371,8 @@ const Page = () => {
368371
localStorage.setItem('embeddingModel', value);
369372
} else if (key === 'systemInstructions') {
370373
localStorage.setItem('systemInstructions', value);
374+
} else if (key === 'temperatureUnit') {
375+
localStorage.setItem('temperatureUnit', value.toString());
371376
}
372377
} catch (err) {
373378
console.error('Failed to save:', err);
@@ -416,13 +421,35 @@ const Page = () => {
416421
) : (
417422
config && (
418423
<div className="flex flex-col space-y-6 pb-28 lg:pb-8">
419-
<SettingsSection title="Appearance">
424+
<SettingsSection title="Preferences">
420425
<div className="flex flex-col space-y-1">
421426
<p className="text-black/70 dark:text-white/70 text-sm">
422427
Theme
423428
</p>
424429
<ThemeSwitcher />
425430
</div>
431+
<div className="flex flex-col space-y-1">
432+
<p className="text-black/70 dark:text-white/70 text-sm">
433+
Temperature Unit
434+
</p>
435+
<Select
436+
value={temperatureUnit ?? undefined}
437+
onChange={(e) => {
438+
setTemperatureUnit(e.target.value as 'C' | 'F');
439+
saveConfig('temperatureUnit', e.target.value);
440+
}}
441+
options={[
442+
{
443+
label: 'Celsius',
444+
value: 'C',
445+
},
446+
{
447+
label: 'Fahrenheit',
448+
value: 'F',
449+
},
450+
]}
451+
/>
452+
</div>
426453
</SettingsSection>
427454

428455
<SettingsSection title="Automatic Search">
@@ -516,7 +543,7 @@ const Page = () => {
516543
<SettingsSection title="System Instructions">
517544
<div className="flex flex-col space-y-4">
518545
<Textarea
519-
value={systemInstructions}
546+
value={systemInstructions ?? undefined}
520547
isSaving={savingStates['systemInstructions']}
521548
onChange={(e) => {
522549
setSystemInstructions(e.target.value);

src/components/WeatherWidget.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ const WeatherWidget = () => {
99
humidity: 0,
1010
windSpeed: 0,
1111
icon: '',
12+
temperatureUnit: 'C',
1213
});
14+
1315
const [loading, setLoading] = useState(true);
1416

1517
useEffect(() => {
@@ -73,6 +75,7 @@ const WeatherWidget = () => {
7375
body: JSON.stringify({
7476
lat: location.latitude,
7577
lng: location.longitude,
78+
temperatureUnit: localStorage.getItem('temperatureUnit') ?? 'C',
7679
}),
7780
});
7881

@@ -91,6 +94,7 @@ const WeatherWidget = () => {
9194
humidity: data.humidity,
9295
windSpeed: data.windSpeed,
9396
icon: data.icon,
97+
temperatureUnit: data.temperatureUnit,
9498
});
9599
setLoading(false);
96100
});
@@ -125,7 +129,7 @@ const WeatherWidget = () => {
125129
className="h-10 w-auto"
126130
/>
127131
<span className="text-base font-semibold text-black dark:text-white">
128-
{data.temperature}°C
132+
{data.temperature}°{data.temperatureUnit}
129133
</span>
130134
</div>
131135
<div className="flex flex-col justify-between flex-1 h-full py-1">

0 commit comments

Comments
 (0)