diff --git a/src/store/weather.ts b/src/store/weather.ts index 71cfbfa..f3509b7 100644 --- a/src/store/weather.ts +++ b/src/store/weather.ts @@ -2,7 +2,15 @@ import { create } from "zustand"; import { devtools } from "zustand/middleware"; import fetchWeatherData from "@/api/weather"; -const iconNumberToPng = { +type IconMapping = Record< + string, + { + day: { description: string; image: string }; + night: { description: string; image: string }; + } +>; + +const iconNumberToPng: IconMapping = { "0": { day: { description: "Sunny", @@ -308,9 +316,15 @@ const useWeatherStore = create( return; } + const weatherCode = current.variables(7)?.value(); + const url = + weatherCode !== undefined + ? iconNumberToPng[weatherCode.toString()]?.day.image + : undefined; + const weatherData = { current: { - time: current.time(), + time: Number(current.time()), temperature_2m: current.variables(0)?.value(), precipitation: current.variables(1)?.value(), rain: current.variables(2)?.value(), @@ -318,9 +332,10 @@ const useWeatherStore = create( snowfall: current.variables(4)?.value(), relative_humidity_2m: current.variables(5)?.value(), apparent_temperature: current.variables(6)?.value(), - weather_code: current.variables(7)?.value(), + weather_code: weatherCode, cloud_cover: current.variables(8)?.value(), is_day: current.variables(9)?.value(), + icon: url, }, hourly: { time: [ @@ -367,10 +382,6 @@ const useWeatherStore = create( }; // const isDay = weatherData.current.is_day === 1; - const weatherCode = weatherData.current.weather_code; - const url = iconNumberToPng[weatherCode].day.image; - - weatherData.current = { ...weatherData.current, icon: url }; set({ weatherData }); },