11import { useTheme } from "@emotion/react" ;
22import { format } from "date-fns" ;
33import { fi } from "date-fns/locale/fi" ;
4- import { memo } from "react" ;
4+ import { type LucideProps } from "lucide-react" ;
5+ import { createElement , memo , useState } from "react" ;
56import useSWR from "swr" ;
67
78import { api , fetcher } from "../api" ;
@@ -20,13 +21,25 @@ import LocationForm from "./LocationForm";
2021import RaindropIcon from "./RaindropIcon" ;
2122import WeatherChart from "./WeatherChart" ;
2223
24+ type WeatherIconProps = {
25+ weatherSymbol : number ;
26+ isNight : boolean ;
27+ } & LucideProps ;
28+
29+ const WeatherIcon : React . FC < WeatherIconProps > = ( {
30+ weatherSymbol,
31+ isNight,
32+ ...props
33+ } ) => createElement ( getFmiWeatherIcon ( weatherSymbol , isNight ) , props ) ;
34+
2335type WeatherBoxProps = {
2436 className ?: string ;
2537} ;
2638
2739const WeatherBox : React . FC < WeatherBoxProps > = ( { className } ) => {
2840 const theme = useTheme ( ) ;
2941 const { location, isLoading : locationLoading } = useLocationSettings ( ) ;
42+ const [ now ] = useState ( ( ) => new Date ( ) ) ;
3043
3144 const weatherUrl = location
3245 ? api ( `/api/weather/fmi?lat=${ location . lat } &lon=${ location . lon } ` )
@@ -95,9 +108,8 @@ const WeatherBox: React.FC<WeatherBoxProps> = ({ className }) => {
95108 } ) ;
96109 }
97110
98- const nowDate = new Date ( ) ;
99- const todayKey = format ( nowDate , "yyyy-MM-dd" ) ;
100- const hideToday = nowDate . getHours ( ) >= HIDE_TODAY_PAST_HOUR ;
111+ const todayKey = format ( now , "yyyy-MM-dd" ) ;
112+ const hideToday = now . getHours ( ) >= HIDE_TODAY_PAST_HOUR ;
101113
102114 const chartData = daily
103115 . filter ( ( d ) => ! ( d . date . slice ( 0 , 10 ) === todayKey && hideToday ) )
@@ -117,7 +129,6 @@ const WeatherBox: React.FC<WeatherBoxProps> = ({ className }) => {
117129
118130 const segments = getFmiTemperatureSegments ( hourly ) ;
119131
120- const now = new Date ( ) ;
121132 const isNight =
122133 now < new Date ( current . sunrise ) || now > new Date ( current . sunset ) ;
123134 const title = getFmiWeatherDescription ( current . weatherSymbol ) ;
@@ -194,21 +205,15 @@ const WeatherBox: React.FC<WeatherBoxProps> = ({ className }) => {
194205 } }
195206 > { `${ Math . round ( current . temperatureApparent ) } °` } </ div >
196207 </ div >
197- { ( ( ) => {
198- const IconComponent = getFmiWeatherIcon (
199- current . weatherSymbol ,
200- isNight ,
201- ) ;
202- return (
203- < IconComponent
204- css = { {
205- marginLeft : "25px" ,
206- alignSelf : "center" ,
207- } }
208- size = { 52 }
209- />
210- ) ;
211- } ) ( ) }
208+ < WeatherIcon
209+ weatherSymbol = { current . weatherSymbol }
210+ isNight = { isNight }
211+ css = { {
212+ marginLeft : "25px" ,
213+ alignSelf : "center" ,
214+ } }
215+ size = { 52 }
216+ />
212217 < div
213218 css = { {
214219 fontSize : "13px" ,
0 commit comments