1
+ import Link from "next/link" ;
2
+ import { useState , useEffect } from "react" ;
3
+
4
+ function NavLink ( { children, href = "/" } ) {
5
+ return (
6
+ < Link href = { href } >
7
+ < a className = "inline-block p-3 font-bold text-decoration-none hover:underline" >
8
+ { children }
9
+ </ a >
10
+ </ Link >
11
+ )
12
+ }
13
+ function toBeautyString ( then ) {
14
+
15
+ var nowdate = new Date ( ) ;
16
+ var thendate = then ;
17
+
18
+ //finding the human-readable components of the date.
19
+
20
+ var y = nowdate . getFullYear ( ) - thendate . getFullYear ( ) ;
21
+ var m = nowdate . getMonth ( ) - thendate . getMonth ( ) ;
22
+ var d = nowdate . getDate ( ) - thendate . getDate ( ) ;
23
+ var h = nowdate . getHours ( ) - thendate . getHours ( ) ;
24
+ var mm = nowdate . getMinutes ( ) - thendate . getMinutes ( ) ;
25
+ var s = nowdate . getSeconds ( ) - thendate . getSeconds ( ) ;
26
+
27
+ //back to second grade math, now we must now 'borrow'.
28
+
29
+ if ( s < 0 ) {
30
+ s += 60 ;
31
+ mm -- ;
32
+ }
33
+ if ( mm < 0 ) {
34
+ mm += 60 ;
35
+ h -- ;
36
+ }
37
+ if ( h < 0 ) {
38
+ h += 24 ;
39
+ d -- ;
40
+ }
41
+ if ( d < 0 ) {
42
+
43
+ //here's where we take into account variable month lengths.
44
+
45
+ var a = thendate . getMonth ( ) ;
46
+ var b ;
47
+ if ( a <= 6 ) {
48
+ if ( a == 1 ) b = 28 ;
49
+ else if ( a % 2 == 0 ) b = 31 ;
50
+ else b = 30 ;
51
+ }
52
+ else if ( b % 2 == 0 ) b = 30 ;
53
+ else b = 31 ;
54
+
55
+ d += b ;
56
+ m -- ;
57
+ }
58
+ if ( m < 0 ) {
59
+ m += 12 ;
60
+ y -- ;
61
+ }
62
+
63
+ return `
64
+ ${ y > 0 ? `${ y } y` : "" }
65
+ ${ m > 0 ? `${ m } M` : "" }
66
+ ${ d > 0 ? `${ d } d` : "" }
67
+ ${ h > 0 ? `${ h } h` : "" }
68
+ ${ mm > 0 ? `${ mm } m` : "" }
69
+ ${ s > 0 ? `${ s } s` : "" }
70
+ ago
71
+ `
72
+ }
73
+ function Updated ( ) {
74
+ const [ date , setDate ] = useState ( new Date ( ) ) ;
75
+ const [ relatime , setRelatime ] = useState ( null ) ;
76
+
77
+ function update_time ( ) {
78
+ fetch ( "/api/updated" )
79
+ . then ( res => res . text ( ) )
80
+ . then (
81
+ ( text ) => {
82
+ setRelatime (
83
+ toBeautyString ( new Date ( text ) )
84
+ )
85
+ }
86
+ ) ;
87
+ }
88
+
89
+ useEffect ( ( ) => {
90
+ update_time ( )
91
+ setInterval ( ( ) => {
92
+ update_time ( )
93
+ } , 10000 ) ;
94
+ } , [ ] ) ;
95
+
96
+ return (
97
+ < div >
98
+ {
99
+ relatime ? (
100
+ < >
101
+
102
+ Updated { relatime }
103
+
104
+ </ >
105
+ ) : < >
106
+ < div >
107
+ loading...
108
+ </ div >
109
+ </ >
110
+ }
111
+ </ div >
112
+ )
113
+ }
114
+
115
+ export default function Nav ( ) {
116
+ return (
117
+ < nav className = "flex items-center w-full justify-between px-4" >
118
+ < div >
119
+ < ul >
120
+ < NavLink href = "/" > Servers</ NavLink >
121
+ < NavLink href = "/log" > Last Log</ NavLink >
122
+ </ ul >
123
+ </ div >
124
+ < div >
125
+ < Updated />
126
+ </ div >
127
+ </ nav >
128
+ ) ;
129
+ }
0 commit comments