Repository navigation
Expand file tree
/
Copy pathmain.ts
More file actions
298 lines (258 loc) · 8.4 KB
/
Copy pathmain.ts
File metadata and controls
298 lines (258 loc) · 8.4 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import { invoke } from "@tauri-apps/api/core";
import { sendNotification, isPermissionGranted, requestPermission } from "@tauri-apps/plugin-notification";
import {
parseRepoInput,
getRepoInfo,
getBranches,
getBranchCommits,
} from "./lib/api";
import {
loadRepos,
addRepo,
updateRepo,
removeRepo,
getToken,
} from "./lib/store";
import type { Commit, WatchedRepo, PollResult, Status } from "./lib/types";
const POLL_MS = 60_000;
const feed = document.getElementById("feed") as HTMLElement;
const emptyState = document.getElementById("emptyState") as HTMLElement;
const addForm = document.getElementById("addForm") as HTMLFormElement;
const repoInput = document.getElementById("repoInput") as HTMLInputElement;
const addBtn = document.getElementById("addBtn") as HTMLButtonElement;
const statusDot = document.getElementById("liveDot") as HTMLElement;
const toasts = document.getElementById("toasts") as HTMLElement;
let repos: WatchedRepo[] = [];
let polling = false;
function log(line: string): void {
const ts = new Date().toISOString();
const entry = `${ts} ${line}`;
console.log(entry);
invoke("append_log", { line: entry }).catch(() => {
// logging is best-effort
});
}
function esc(s: string): string {
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
}
function shortSha(sha: string): string {
return sha.slice(0, 7);
}
function initials(name: string): string {
return name
.split(/\s+/)
.filter(Boolean)
.slice(0, 2)
.map((w) => w[0].toUpperCase())
.join("");
}
function setStatus(status: Status): void {
statusDot.className = `live-dot ${status === "idle" ? "" : status}`;
}
function render(): void {
if (repos.length === 0) {
emptyState.style.display = "flex";
feed.querySelectorAll(".section").forEach((el) => el.remove());
return;
}
emptyState.style.display = "none";
feed.querySelectorAll(".section").forEach((el) => el.remove());
repos.forEach((repo) => {
const section = document.createElement("section");
section.className = "section";
const label = document.createElement("div");
label.className = "section-label";
label.innerHTML = `
<span>${esc(repo.full_name)}</span>
<button class="remove-btn" data-id="${esc(repo.id)}" title="Stop watching">✕</button>
`;
const rows = document.createElement("div");
rows.className = "repo-rows";
rows.dataset.repo = repo.id;
section.appendChild(label);
section.appendChild(rows);
feed.appendChild(section);
});
feed.querySelectorAll(".remove-btn").forEach((btn) => {
btn.addEventListener("click", (e) => {
const id = (e.currentTarget as HTMLElement).dataset.id!;
removeRepo(id);
repos = loadRepos();
render();
});
});
}
function addCommitCard(repo: WatchedRepo, commit: Commit): void {
const container = feed.querySelector<HTMLElement>(`.repo-rows[data-repo="${repo.id}"]`);
if (!container) return;
const avatar = commit.author.avatar_url
? `background-image:url('${commit.author.avatar_url}')`
: "";
const hasImg = !!commit.author.avatar_url;
const row = document.createElement("article");
row.className = "commit is-new";
row.innerHTML = `
<div class="avatar ${hasImg ? "" : "fallback"}" style="${avatar}">${hasImg ? "" : esc(initials(commit.author.name))}</div>
<div class="commit-body">
<span class="commit-msg">${esc(commit.message)}</span>
</div>
<div class="commit-meta">
<span class="branch-badge">${esc(commit.branch)}</span>
<span class="author">${esc(commit.author.name)}</span>
<span class="sha">${shortSha(commit.sha)}</span>
</div>
`;
container.prepend(row);
}
function addToast(title: string, body: string): void {
const toast = document.createElement("div");
toast.className = "toast";
toast.innerHTML = `
<div class="toast-title"><span class="dot"></span>${esc(title)}</div>
<div class="toast-body">${esc(body)}</div>
`;
toasts.appendChild(toast);
setTimeout(() => {
toast.classList.add("out");
setTimeout(() => toast.remove(), 300);
}, 4500);
}
async function ensureNotifPermission(): Promise<void> {
try {
if (!(await isPermissionGranted())) {
await requestPermission();
}
} catch {
// Notifications unavailable (e.g. plain browser) — in-app toasts still work.
}
}
async function notifyCommit(repo: WatchedRepo, commit: Commit): Promise<void> {
const title = `${repo.full_name} · ${commit.branch}`;
const body = `${commit.author.name}: ${commit.message}`;
addToast(title, body);
try {
sendNotification({ title, body });
} catch {
// ignore
}
}
function stripAvatarQuery(url?: string): string | undefined {
if (!url) return undefined;
const base = url.split("?v=")[0];
return base + "?s=96";
}
async function watchCommits(repo: WatchedRepo, token?: string): Promise<Commit[]> {
const [owner, name] = repo.full_name.split("/");
const newCommits: Commit[] = [];
const nextBranches: Record<string, string> = { ...repo.branches };
const branches = await getBranches(owner, name, token);
for (const branch of branches) {
const raws = await getBranchCommits(owner, name, branch.name, token);
if (raws.length === 0) continue;
const known = repo.branches[branch.name];
const isFirst = !known;
if (isFirst) {
nextBranches[branch.name] = raws[0].sha;
log(`baseline ${repo.full_name}#${branch.name} = ${raws[0].sha.slice(0, 7)} (${raws.length} commits, no notify)`);
continue;
}
const knownIdx = raws.findIndex((c) => c.sha === known);
const cutoff = knownIdx === -1 ? raws.length : knownIdx;
nextBranches[branch.name] = raws[0].sha;
log(`poll ${repo.full_name}#${branch.name}: head=${raws[0].sha.slice(0, 7)} known=${known.slice(0, 7)} atIdx=${knownIdx} -> new=${cutoff}`);
for (let i = 0; i < cutoff; i++) {
const c = raws[i];
newCommits.push({
sha: c.sha,
message: c.message,
author: {
name: c.authorName,
email: "",
login: c.authorLogin,
avatar_url: stripAvatarQuery(c.avatarUrl),
},
date: c.date,
branch: branch.name,
html_url: c.htmlUrl,
});
}
}
const updated: WatchedRepo = { ...repo, branches: nextBranches };
updateRepo(updated);
return newCommits;
}
async function poll(): Promise<void> {
if (polling || repos.length === 0) return;
polling = true;
setStatus("polling");
const token = getToken();
try {
const results: PollResult[] = [];
for (const repo of repos) {
try {
const newCommits = await watchCommits(repo, token);
results.push({ repo, newCommits });
} catch (err) {
console.error(err);
log(`ERROR ${repo.full_name}: ${(err as Error).message}`);
setStatus("error");
}
}
for (const { repo, newCommits } of results) {
for (const commit of newCommits) {
addCommitCard(repo, commit);
log(`NEW ${repo.full_name}#${commit.branch} ${commit.sha.slice(0, 7)} ${commit.author.name}: ${commit.message}`);
await notifyCommit(repo, commit);
}
}
setStatus("online");
} finally {
polling = false;
}
}
async function onAddRepo(e: Event): Promise<void> {
e.preventDefault();
const parsed = parseRepoInput(repoInput.value);
if (!parsed) {
setStatus("error");
return;
}
addBtn.disabled = true;
addBtn.classList.add("loading");
setStatus("polling");
try {
const token = getToken();
const info = await getRepoInfo(parsed.owner, parsed.repo, token);
const repo: WatchedRepo = {
id: crypto.randomUUID(),
full_name: info.full_name,
html_url: info.html_url,
owner_avatar: stripAvatarQuery(info.owner_avatar),
branches: {},
};
addRepo(repo);
repos = loadRepos();
repoInput.value = "";
render();
const newCommits = await watchCommits(repo, token);
for (const c of newCommits) addCommitCard(repo, c);
addBtn.classList.remove("loading");
addBtn.classList.add("done");
setStatus("online");
} catch (err) {
addBtn.classList.remove("loading");
setStatus("error");
} finally {
addBtn.disabled = false;
setTimeout(() => addBtn.classList.remove("done"), 1600);
}
}
function start(): void {
ensureNotifPermission();
repos = loadRepos();
render();
void poll();
setInterval(() => void poll(), POLL_MS);
}
addForm.addEventListener("submit", onAddRepo);
start();