-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
142 lines (125 loc) · 3.83 KB
/
script.js
File metadata and controls
142 lines (125 loc) · 3.83 KB
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
import { send, get_projects, socket } from "./api.js";
import { add_row, sort, reset, reload, project_link } from "./table.js";
window.user = localStorage.user;
function compare(a, b) {
return a > b ? 1 : a < b ? -1 : 0
}
async function load_stats() {
if (!user) {
return;
}
reset();
const table = document.getElementById("projects");
while (table.length > 1) {
table.removeChild(table.children[1]);
}
const user_input = document.getElementById("user");
document.getElementById("submit").innerText = "Change user"
user_input.placeholder = user;
user_input.value = "";
table.style.display = "";
const project_stats = {};
const progress = document.getElementById("progress");
let total = 0;
let loaded = 0;
const promises = [];
const promise_done = new Promise(res => {
get_projects(user, (projects, done) => {
total += projects.length;
if (done) {
res();
}
for (const project of projects) {
promises.push(new Promise(async res => {
let result = await send({
name: "get_project_comments",
project: project.id,
});
project_stats[project.slug] = {
title: project.title,
likes: project.likes,
comments: result.comments.length,
}
add_row(project.title, project.slug, project.likes, result.comments.length);
progress.innerText = `${++loaded}/${total}`;
res();
}));
}
});
});
await promise_done;
for (const promise of promises) {
await promise;
}
sort((a, b) => {
return compare(b[2], a[2]);
})
reload();
/*
let loaded = 0;
for (const project of projects) {
let result = await send({
name: "get_project_comments",
project: project.id,
})
project_stats[project.slug] = {
title: project.title,
likes: project.likes,
comments: result.comments.length
}
const row = document.createElement("tr");
add_cell(row, project_link(project.slug, project.title));
add_cell(row, project.slug);
add_cell(row, project.likes);
add_cell(row, result.comments.length);
table.appendChild(row);
progress.innerText = `${++loaded}/${projects.length}`;
}
*/
let project_stats_old = localStorage.project_stats;
if (!project_stats_old) {
project_stats_old = {};
} else {
project_stats_old = JSON.parse(project_stats_old);
}
const alerts = document.createElement("div");
alerts.id = "alerts";
for (const slug in project_stats_old) {
const stats_old = project_stats_old[slug];
const stats = project_stats[slug];
if (!stats) {
continue;
}
const title = stats.title;
let new_likes;
if (new_likes = stats.likes - stats_old.likes) {
let plural = new_likes == 1 ? "" : "s";
alerts.appendChild(project_link(slug, title));
alerts.appendChild(new Text(` has ${new_likes} new like${plural}!`));
alerts.appendChild(document.createElement("br"));
}
let new_comments;
if (new_comments = stats.comments - stats_old.comments) {
let plural = new_comments == 1 ? "" : "s";
alerts.appendChild(project_link(slug, title));
alerts.appendChild(new Text(` has ${new_comments} new comment${plural}!`));
alerts.appendChild(document.createElement("br"));
}
}
document.getElementById("alerts").replaceWith(alerts);
localStorage.project_stats = JSON.stringify(project_stats);
}
socket.onopen = load_stats;
if (user) {
document.getElementById("submit").innerText = "Change user"
document.getElementById("user").placeholder = user;
}
const form = document.getElementById("username");
form.addEventListener("submit", event => {
event.preventDefault();
const data = new FormData(form);
let username = data.get("user");
localStorage.user = username;
user = username;
load_stats();
});