From f5e29189f70c5c8532916504a1a22f8c586f6e73 Mon Sep 17 00:00:00 2001 From: navewindre Date: Tue, 11 Nov 2025 08:11:24 +0100 Subject: new web --- web/src/sidebar.tsx | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 web/src/sidebar.tsx (limited to 'web/src/sidebar.tsx') diff --git a/web/src/sidebar.tsx b/web/src/sidebar.tsx new file mode 100644 index 0000000..a1b3ad7 --- /dev/null +++ b/web/src/sidebar.tsx @@ -0,0 +1,126 @@ +import $ from 'jquery'; +import * as JSX from './jsx'; + +var interval = null; + +function cacheWeather( result: any ) { + const timestamp = new Date().getTime(); + + localStorage.setItem( "weather", JSON.stringify( result ) ); + localStorage.setItem( "weather_timestamp", timestamp.toString() ); +} + +function getWeatherCache() { + const timestamp = localStorage.getItem( "weather_timestamp" ); + if( !timestamp ) + return null; + + if( parseInt( timestamp ) + 60 * 60 * 1000 < new Date().getTime() ) { + localStorage.removeItem( "weather" ); + localStorage.removeItem( "weather_timestamp" ); + return null; + } + + try { + const res = JSON.parse( localStorage.getItem( "weather" ) ); + return res; + } catch( e ) { + console.log( e ); + return null; + } +} + +async function updateWeather() { + let weather = getWeatherCache(); + if( !weather ) { + weather = await fetch( + "https://api.open-meteo.com/v1/forecast?latitude=35.0647937&longitude=137.1784597¤t=temperature_2m,wind_speed_10m,relative_humidity_2m" + ) + + weather = await weather.json(); + cacheWeather( weather ); + } + + $( "#temp" ).text( weather.current.temperature_2m ); + $( "#wind" ).text( weather.current.wind_speed_10m ); + $( "#humi" ).text( weather.current.relative_humidity_2m ); + + setTimeout( updateWeather, 60 * 60 * 1000 ); +} + +function Weather() { + setTimeout( updateWeather ); + const weather = getWeatherCache(); + if( weather && weather.current ) { + const temp = weather.current.temperature_2m; + const wind = weather.current.wind_speed_10m; + const humi = weather.current.relative_humidity_2m; + return
+ + + + +
+ } + + return
+ + + + +
+} + +export function Sidebar() { + if( interval == null ) { + interval = setInterval( () => { + $( "#time" ).text( new Date().toLocaleTimeString() ); + $( "#date" ).text( new Date().toLocaleDateString() ); + }, 1000 ); + } + + return +} -- cgit v1.2.3