-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontend.ts
More file actions
85 lines (74 loc) · 3.06 KB
/
Copy pathfrontend.ts
File metadata and controls
85 lines (74 loc) · 3.06 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
const form = document.getElementById("form") as HTMLFormElement;
const fromInput = document.getElementById("from") as HTMLInputElement;
const toInput = document.getElementById("to") as HTMLInputElement;
const submitBtn = document.getElementById("submit") as HTMLButtonElement;
const statusEl = document.getElementById("status") as HTMLDivElement;
const resultEl = document.getElementById("result") as HTMLDivElement;
const outputEl = document.getElementById("output") as HTMLDivElement;
const titleEl = document.getElementById("title") as HTMLHeadingElement;
const statPrs = document.getElementById("stat-prs") as HTMLDivElement;
const statRepos = document.getElementById("stat-repos") as HTMLDivElement;
const statContributors = document.getElementById("stat-contributors") as HTMLDivElement;
const copyBtn = document.getElementById("copy") as HTMLButtonElement;
const versionEl = document.getElementById("version") as HTMLElement;
const today = new Date();
const firstOfThisMonth = new Date(today.getFullYear(), today.getMonth(), 1);
const lastMonthStart = new Date(firstOfThisMonth);
lastMonthStart.setMonth(lastMonthStart.getMonth() - 1);
const lastMonthEnd = new Date(firstOfThisMonth);
lastMonthEnd.setDate(lastMonthEnd.getDate() - 1);
const toISO = (d: Date) => d.toISOString().slice(0, 10);
fromInput.value = toISO(lastMonthStart);
toInput.value = toISO(lastMonthEnd);
form.addEventListener("submit", async (e) => {
e.preventDefault();
const from = fromInput.value;
const to = toInput.value;
if (!from || !to) return;
submitBtn.disabled = true;
statusEl.className = "status";
statusEl.innerHTML = `<span class="spinner"></span>Fetching merged PRs…`;
resultEl.classList.add("hidden");
try {
const res = await fetch("/api/changelog", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ from, to }),
});
const data = await res.json();
if (!res.ok) {
statusEl.className = "status error";
statusEl.textContent = data.error;
return;
}
statusEl.classList.add("hidden");
statPrs.textContent = data.prCount;
statRepos.textContent = data.repoCount;
statContributors.textContent = data.contributorCount;
outputEl.textContent = data.changelog;
resultEl.classList.remove("hidden");
} catch {
statusEl.className = "status error";
statusEl.textContent = "Request failed. Is the server running?";
} finally {
submitBtn.disabled = false;
}
});
fetch("/api/version")
.then((r) => r.json())
.then((data: { sha: string; repoUrl: string; org: string }) => {
titleEl.textContent = `${data.org} changelog generator`;
const link = document.createElement("a");
link.href = `${data.repoUrl}/commit/${data.sha}`;
link.textContent = data.sha.slice(0, 7);
link.target = "_blank";
versionEl.appendChild(link);
});
copyBtn.addEventListener("click", async () => {
const text = outputEl.textContent ?? "";
await navigator.clipboard.writeText(text);
copyBtn.textContent = "Copied!";
setTimeout(() => {
copyBtn.textContent = "Copy";
}, 1500);
});