summaryrefslogtreecommitdiff
path: root/web/src/sidebar.tsx
blob: ee937062fd439ec781a10fcb55fff011bc098d15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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 ) {
    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&current=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 <div id="weather">
      <div class="sidebar-row"><h4>weather in </h4> <h4>愛知県、日本:</h4></div>
      <div class="sidebar-row"><h4>temperature:</h4><h4><span id="temp">{temp.toString()}</span>°C</h4></div>
      <div class="sidebar-row"><h4>wind speed:</h4><h4><span id="wind">{wind.toString()}</span>km/h</h4></div>
      <div class="sidebar-row"><h4>humidity:</h4><h4><span id="humi">{humi.toString()}</span>%</h4></div>
    </div>
  }

  return <div id="weather">
    <div class="sidebar-row"><h4>weather in </h4> <h4>愛知県、日本:</h4></div>
    <div class="sidebar-row"><h4>temperature:</h4><h4><span id="temp">--</span>°C</h4></div>
    <div class="sidebar-row"><h4>wind speed:</h4><h4><span id="wind">--</span>km/h</h4></div>
    <div class="sidebar-row"><h4>humidity:</h4><h4><span id="humi">--</span>%</h4></div>
  </div>
}

export function Sidebar() {
  if( interval == null ) {
    interval = setInterval( () => {
      $( "#time" ).text( new Date().toLocaleTimeString() );
      $( "#date" ).text( new Date().toLocaleDateString() );
    }, 1000 );
  }

  return <div id="sidebar">
    <div><h3>information</h3></div>
    <hr/>
    <div class="sidebar-row"><h4>current time: </h4><h4><span id="time">{new Date().toLocaleTimeString()}</span></h4></div>
    <div class="sidebar-row"><h4>today: </h4><h4><span id="date">{new Date().toLocaleDateString()}</span></h4></div>
    <Weather />
    <div style="margin-top: 15px"><h3>links</h3></div>
    <hr/>
    <div class="sidebar-row">
      <h4>
        <a class="nogradient" style="margin-bottom: 0" href="#" onclick={ () => JSX.navigate( "/" ) }>
          homepage
        </a>
      </h4>
    </div>
    <div class="sidebar-row" style="margin-top: -18px">
      <h4>
        <a class="nogradient" href="#" onclick={ () => JSX.navigate( "/pkg" ) }>
          slackware packages
        </a>
      </h4>
    </div>
    <div class="sidebar-row" style="margin-top: -18px">
      <h4>
        <a class="nogradient" href="#" onclick={ () => JSX.navigate( "/blog" ) }>
          blog entries
        </a>
      </h4>
    </div>
    <div class="sidebar-row" style="margin-top: -18px">
      <h4>
        <a class="nogradient" href="https://steamcommunity.com/groups/networkheaven">
          steam
        </a>
      </h4>
    </div>
    <div class="sidebar-row" style="margin-top: -18px">
      <h4>
        <a class="nogradient" href="https://git.networkheaven.net">
          git
        </a>
      </h4>
    </div>
  </div>
}