"use client"; import { getWeatherIcon } from "@/utils/weather.js"; import Image from "next/image"; import { useEffect, useState } from "react"; import List from "./daily.json"; import styles from "./todo.module.css"; const version = require("../../../package.json").version; export default function WeatherPage() { const [weather, setWeather] = useState(null); const [time, setTime] = useState(new Date().toLocaleTimeString()); const [date, setDate] = useState(new Date().toLocaleDateString()); const [weatherIcon, setWeatherIcon] = useState("/weather/sunnyday.png"); useEffect(() => { async function fetchWeather() { try { const res = await fetch( `https://api.pirateweather.net/forecast/${process.env.NEXT_PUBLIC_WEATHER_API}/42.3967,-88.1837?units=us&exclude=minutely,hourly,flags&icon=pirate`, { cache: "no-store" }, ); const data = await res.json(); setWeather(data); } catch (err) { console.error("Failed to fetch weather:", err); } } // only fetch weather every 5 minutes on the 0th second const fetchWeatherInterval = () => { const now = new Date(); if (now.getSeconds() === 0 && now.getMinutes() % 5 === 0) { fetchWeather(); } }; fetchWeather(); const interval = setInterval(fetchWeatherInterval, 1000); return () => clearInterval(interval); }, []); useEffect(() => { const interval = setInterval(() => { setTime(new Date().toLocaleTimeString()); }, 1000); return () => clearInterval(interval); }, []); useEffect(() => { const interval = setInterval(() => { setDate(new Date().toLocaleDateString()); }, 1000); return () => clearInterval(interval); }, []); const weatherTemp = Math.floor(weather?.currently?.temperature) || "--"; const weatherFeelsLike = Math.floor(weather?.currently?.apparentTemperature) || "--"; const weatherHigh = Math.floor(weather?.daily?.data[0]?.temperatureHigh) || "--"; const weatherLow = Math.floor(weather?.daily?.data[0]?.temperatureLow) || "--"; const percentChance = Math.floor(weather?.currently?.precipProbability * 100) || "0"; const lastUpdatedTime = weather ? new Date(weather.currently.time * 1000).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit", }) : "--"; useEffect(() => { const interval = setInterval(() => { setWeatherIcon( `/weather/${getWeatherIcon(weather?.currently?.icon)}.png`, ); }, 1000); return () => clearInterval(interval); }, [weather]); const [dailyItems, setDailyItems] = useState([]); useEffect(() => { const updateDailyItems = () => { const now = new Date(); const currentHour = now.getHours(); const filteredItems = List?.List?.filter((item) => { const timeStart = parseInt(item.timeStart); const timeEnd = parseInt(item.timeEnd); return currentHour >= timeStart && currentHour < timeEnd; }) || []; setDailyItems( filteredItems.length > 0 ? filteredItems : [ { title: "N/A", desc: "No reminders", timeStart: "--", timeEnd: "--", }, ], ); }; // Initial check updateDailyItems(); // Check every second const interval = setInterval(updateDailyItems, 1000); return () => clearInterval(interval); }, [List]); const weatherAlerts = weather?.alerts || []; const futureForecasts = weather?.daily?.data.slice(1, 4) || []; return (
Raceway Car Wash Fox Lake{" "} v{version} {time || "00:00:00 AM"} {date}
Daily Reminders {dailyItems?.map((item, index) => ( {item.title} {item.desc} {item.textStart && item.textEnd ? ( <> {item.textStart} - {item.textEnd} ) : ( <> {item.timeStart}:00 - {item.timeEnd} :00 )} )) || "Test"} Important Reminders N/A No Reminders ⛅ Hyperlocal Weather {weatherAlerts.length > 0 && weatherAlerts.map((alert, index) => ( ⚠️ {alert.title} Expires:{" "} {new Date( alert.expires * 1000, ).toLocaleString()} ))} {weather?.currently?.summary} {weatherTemp}° Feels Like {weatherFeelsLike}° High: {weatherHigh}° · Low:{" "} {weatherLow}° Weather Icon {weather?.currently.windSpeed || "--"} mph Wind Speed {percentChance}% Precipitation {Math.floor( weather?.currently?.humidity * 100, ) || "--"} % Humidity {futureForecasts.map((forecast, index) => ( {new Date( forecast.time * 1000, ).toLocaleDateString([], { weekday: "short", })} Weather Icon {Math.floor( forecast.precipProbability * 100, )} % {Math.floor(forecast.temperatureHigh)}° / {Math.floor(forecast.temperatureLow)}° ))} Last Updated {lastUpdatedTime}
); }