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
import { Hono } from 'jsr:@hono/hono'
import {
appendTrailingSlash,
} from 'npm:hono/trailing-slash'
import { html } from "https://deno.land/x/hono@v3.11.7/helper.ts";
import dayjs from "npm:dayjs";
// import { getVals, getValsWithVersions } from "./api.js";
import { getVals, getValsWithVersions } from "https://esm.town/v/yawnxyz/valVersions";
const app = new Hono();
app.use(appendTrailingSlash())
app.get("/graph/:username?", async (c) => {
console.log("graph:", c.req.param("username") || c.req.query("username"));
const username = c.req.param("username") || c.req.query("username");
if (!username) {
return c.html(`<p class="text-red-500">Error: Username is required</p>`);
}
try {
let graph = await generateGraph(username);
return c.html(`<div class="graph">${graph}</div>`);
} catch (error) {
console.error(`Error generating graph for ${username}:`, error);
return c.html(`<p class="text-red-500">Error: ${error.message}</p>`);
}
});
// hono bug doesn't handle optional params? so have to do this
app.get("/graph/", async (c) => {
const username = c.req.param("username") || c.req.query("username");
if (!username) {
return c.html(`<p class="text-red-500">Error: Username is required</p>`);
}
try {
let graph = await generateGraph(username);
return c.html(`<div class="graph">${graph}</div>`);
} catch (error) {
console.error(`Error generating graph for ${username}:`, error);
return c.html(`<p class="text-red-500">Error: ${error.message}</p>`);
}
});
app.get("/:username", (c) => {
if (c.req.url.pathname === '/favicon.ico') {
return c.json(null, 404);
}
const username = c.req.param("username") || c.req.query("username");
console.log(`Rendering page for username: ${username}`);
return renderPage(c, username);
});
app.get("/", (c) => {
console.log("Rendering home page");
const username = c.req.query("username");
if (username === 'favicon.ico') {
return c.json(null, 404);
}
if (username) {
console.log(`Username provided in query: ${username}`);
return renderPage(c, username);
}
return renderPage(c);
});
async function generateGraph(username, useVersions = true) {
if (!username) {
return html`<p class="text-red-500">Please enter a username</p>`;
}
console.log(`Fetching contributions for ${username}`);
try {
let vals;
if (useVersions) {
vals = await getValsWithVersions(username, 200);
} else {
vals = await getVals(username);
}
// if use all versions, we get all the versions of each val
if (useVersions) {
vals = vals.map(val => val.versions ? val.versions : val).flat();
}
console.log(`Fetched ${vals.length} contributions`);
const contributionData = processVals(vals, useVersions);
console.log(`Processed ${contributionData.length} contributions`);
return html`
<h2 class="text-xl font-bold mb-4">Contribution Graph for ${username}</h2>
<table class="contribution-table">
<thead>
<tr>
<th></th>
${generateMonthLabels(contributionData)}
</tr>