-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfront.html.tmpl
164 lines (146 loc) · 3.54 KB
/
front.html.tmpl
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<html>
<head>
<title>garagemon</title>
<link rel="icon" href="/static/favicon.png" type="image/png">
<link rel="apple-touch-icon" type="image/png" sizes="180x180" href="/static/favicon.png">
<style type="text/css">
* {
font-family: Helvetica, sans-serif;
}
#peer {
float: right;
text-align: center;
width: 30%;
}
#activate {
background: #999;
border: 1px solid #555;
border-radius: 2em;
clear: right;
color: white;
font-size: 72px;
margin: auto;
padding: 1em;
text-align: center;
width: 50%;
}
#activate.running {
background: orange;
}
#activate.okay {
background: green;
}
#activate.failed {
background: red;
}
#error {
color: red;
text-align: center;
}
table#events {
font-size: x-large;
margin-top: 1em;
}
table#events td {
padding: 0 1em;
}
table#events td.ago {
text-align: right;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
{{with .Peer}}
<div id="peer">
<img src="{{.ProfilePicURL}}" /><br />
{{.DisplayName}} <{{.LoginName}}><br />
(via Tailscale)
</div>
{{end}}
<h1>garagemon</h1>
<p>
Running for {{.Uptime}} in <em>{{.Config.Mode}}</em> mode.
</p>
<div id="activate">
Activate!
</div>
<h3 id="error" style="display: none;"></h3>
<table id="events">
<tr>
<th>Ago</th><th>Device</th><th>Action</th>
</tr>
<tr class="event">
<td colspan=3 style="text-align: center;">Data loading...</td>
</tr>
</table>
<script type="text/javascript">
function showError(message) {
$("#error").text(message);
$("#error").show();
}
var nextRefresh = Date.now(); // Initially due for a refresh.
var button = $("#activate");
button.click(function() {
if (button.hasClass("running")) {
return;
}
button.addClass("running");
$("#error").show(); // TODO: should this be hiding it?
nextRefresh = Date.now() + 4*1000; // get new events in 4s
$.post("/activate", function(data) {
if ("error" in data) {
button.addClass("failed");
showError(data.error);
} else {
button.addClass("okay");
}
}, "json").fail(function() {
// Errors with useful messages should be handled above.
button.addClass("failed");
}).always(function() {
button.removeClass("running");
setTimeout(function() {
button.removeClass(["okay", "failed"]);
}, 2000);
});
});
function refreshEvents() {
var now = Date.now();
if (now < nextRefresh) {
//console.log("waiting for " + (nextRefresh-now)/1000 + "s for next refresh")
return;
}
$.getJSON("/events", function(data) {
if ("error" in data) {
// TODO: Failures here and above will be fighting over the error text.
showError(data.error);
nextRefresh = now + 5*1000; // Try again in 5s.
return;
}
// Refresh again, either in 5s if there was a recent event (since events cluster),
// or in 30s.
nextRefresh = now + (data.recent ? 5*1000 : 30*1000);
var tbl = $("table#events");
$("table#events tr.event").remove();
for (const ev of data.events) {
var tr = $("<tr />").addClass("event");
tbl.append(tr);
tr.append($("<td />").text(ev.ago).addClass("ago"));
tr.append($("<td />").text(ev.label));
var op = ev.name;
if (ev["value"]) op += "=" + ev.value;
tr.append($("<td />").text(op));
}
})
.fail(function() {
// TODO: Failures here and above will be fighting over the error text.
showError("Network error");
nextRefresh = now + 5*1000; // Try again in 5s.
return;
});
}
setInterval(refreshEvents, 1005); // Check every 1.005s
</script>
</body>
</html>