The homepage now renders itself from the repository list: a live stat strip (repository count, language count, newest commit), a card grid for every public non-empty repository with its own description, language dot, last-commit age, latest release tag and links, plus client-side filter and sort, a language breakdown, and a single case-study link row. Nothing on the page is pinned to a fixed project set, so a repository added to publish-bot appears on next load.
230 lines
9.9 KiB
JavaScript
230 lines
9.9 KiB
JavaScript
(() => {
|
|
"use strict";
|
|
// The homepage is generated from the repository list itself: every public, non-empty repository
|
|
// is rendered as a card, with its own description, language, last commit and latest release.
|
|
// Adding a repository needs no rebuild — it appears on the next page load.
|
|
const grid = document.querySelector("[data-repo-grid]");
|
|
if (!grid) return;
|
|
const ORG = "publish-bot";
|
|
const PAGE = 50, MAX_PAGES = 4, ENRICH_LIMIT = 40;
|
|
const LANG_COLORS = {
|
|
"Python": "#3572A5", "C#": "#178600", "C++": "#f34b7d", "JavaScript": "#f1e05a",
|
|
"TypeScript": "#3178c6", "Go": "#00ADD8", "Rust": "#dea584", "Java": "#b07219",
|
|
"Ruby": "#701516", "Shell": "#89e051", "HTML": "#e34c26", "CSS": "#563d7c",
|
|
"C": "#555555", "Kotlin": "#A97BFF", "Swift": "#F05138", "PHP": "#4F5D95",
|
|
"Dockerfile": "#384d54", "PowerShell": "#012456", "Makefile": "#427819", "Lua": "#000080"
|
|
};
|
|
const statusEl = document.querySelector("[data-repo-status]");
|
|
const searchEl = document.querySelector("[data-repo-search]");
|
|
const sortEl = document.querySelector("[data-repo-sort]");
|
|
const langBox = document.querySelector("[data-lang-bars]");
|
|
const stats = {
|
|
repos: document.querySelector('[data-stat="repos"]'),
|
|
languages: document.querySelector('[data-stat="languages"]'),
|
|
updated: document.querySelector('[data-stat="updated"]')
|
|
};
|
|
|
|
const node = (tag, cls, text) => {
|
|
const n = document.createElement(tag);
|
|
if (cls) n.className = cls;
|
|
if (text != null) n.textContent = text;
|
|
return n;
|
|
};
|
|
const day = 864e5;
|
|
const shortDate = value => {
|
|
const d = new Date(value);
|
|
return Number.isNaN(d.valueOf()) ? "" : d.toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric" });
|
|
};
|
|
const relative = value => {
|
|
const t = Date.parse(value);
|
|
if (Number.isNaN(t)) return "";
|
|
const delta = t - Date.now(), abs = Math.abs(delta);
|
|
const rtf = new Intl.RelativeTimeFormat("en-US", { numeric: "auto" });
|
|
if (abs < 6e4) return "just now";
|
|
if (abs < 36e5) return rtf.format(Math.round(delta / 6e4), "minute");
|
|
if (abs < day) return rtf.format(Math.round(delta / 36e5), "hour");
|
|
if (abs < 30 * day) return rtf.format(Math.round(delta / day), "day");
|
|
if (abs < 365 * day) return rtf.format(Math.round(delta / (30 * day)), "month");
|
|
return rtf.format(Math.round(delta / (365 * day)), "year");
|
|
};
|
|
|
|
const controller = new AbortController();
|
|
const timeout = setTimeout(() => controller.abort(), 15000);
|
|
const api = path => fetch(`/api/v1${path}`, { credentials: "omit", headers: { Accept: "application/json" }, signal: controller.signal })
|
|
.then(response => { if (!response.ok) throw new Error("API " + response.status); return response.json(); });
|
|
|
|
const repos = [];
|
|
const extra = new Map(); // name -> { release, commit }
|
|
let view = [];
|
|
|
|
const searchable = repo => [repo.name, repo.description, repo.language, ...(repo.topics || [])]
|
|
.filter(Boolean).join(" ").toLowerCase();
|
|
const activity = repo => {
|
|
const info = extra.get(repo.name);
|
|
return (info && info.commit) || repo.updated_at || "";
|
|
};
|
|
|
|
async function load() {
|
|
for (let page = 1; page <= MAX_PAGES; page++) {
|
|
const batch = await api(`/users/${ORG}/repos?limit=${PAGE}&page=${page}&sort=updated`);
|
|
if (!Array.isArray(batch) || batch.length === 0) break;
|
|
repos.push(...batch);
|
|
if (batch.length < PAGE) break;
|
|
}
|
|
const active = repos.filter(repo => repo.private === false && !repo.empty);
|
|
await Promise.all(active.slice(0, ENRICH_LIMIT).map(async repo => {
|
|
const info = { release: null, commit: null };
|
|
const name = encodeURIComponent(repo.name);
|
|
try {
|
|
const releases = await api(`/repos/${ORG}/${name}/releases?limit=1`);
|
|
const first = Array.isArray(releases) && releases[0];
|
|
if (first) info.release = { tag: first.tag_name, url: first.html_url, at: first.published_at || first.created_at };
|
|
} catch { /* repositories without releases are normal */ }
|
|
try {
|
|
const commits = await api(`/repos/${ORG}/${name}/commits?limit=1&stat=false`);
|
|
const commit = Array.isArray(commits) && commits[0];
|
|
if (commit) info.commit = (commit.commit && (commit.commit.committer || commit.commit.author) || {}).date || null;
|
|
} catch { /* empty repositories have no commits */ }
|
|
extra.set(repo.name, info);
|
|
}));
|
|
}
|
|
|
|
function renderStats() {
|
|
const active = repos.filter(repo => repo.private === false && !repo.empty && !repo.archived);
|
|
const languages = new Set(active.map(repo => repo.language).filter(Boolean));
|
|
const stamps = active.map(activity).filter(Boolean).sort();
|
|
if (stats.repos) stats.repos.textContent = String(active.length);
|
|
if (stats.languages) stats.languages.textContent = String(languages.size);
|
|
if (stats.updated) stats.updated.textContent = stamps.length ? shortDate(stamps[stamps.length - 1]) : "—";
|
|
}
|
|
|
|
function card(repo) {
|
|
const info = extra.get(repo.name) || {};
|
|
const article = node("article", "repo-card");
|
|
article.dataset.name = repo.name;
|
|
|
|
const head = node("div", "repo-head");
|
|
const heading = node("h3");
|
|
const link = node("a", null, repo.name);
|
|
link.href = repo.html_url;
|
|
heading.append(link);
|
|
head.append(heading);
|
|
if (repo.archived) head.append(node("span", "repo-flag", "Archived"));
|
|
article.append(head);
|
|
|
|
article.append(node("p", "repo-desc", repo.description || "Public repository on code.knickqueue.win."));
|
|
|
|
if (repo.topics && repo.topics.length) {
|
|
const topics = node("div", "repo-topics");
|
|
repo.topics.slice(0, 4).forEach(topic => topics.append(node("span", null, topic)));
|
|
article.append(topics);
|
|
}
|
|
|
|
const meta = node("div", "repo-meta");
|
|
if (repo.language) {
|
|
const lang = node("span", "lang");
|
|
const dot = node("i");
|
|
dot.style.background = LANG_COLORS[repo.language] || "#738590";
|
|
lang.append(dot, document.createTextNode(repo.language));
|
|
meta.append(lang);
|
|
}
|
|
const last = activity(repo);
|
|
meta.append(node("span", null, last ? `last commit ${relative(last)}` : "no commits yet"));
|
|
if (repo.stars_count) meta.append(node("span", null, `★ ${repo.stars_count}`));
|
|
if (repo.forks_count) meta.append(node("span", null, `⑂ ${repo.forks_count}`));
|
|
if (info.release) meta.append(node("span", "tag", info.release.tag));
|
|
article.append(meta);
|
|
|
|
const links = node("div", "repo-links");
|
|
const source = node("a", null, "Source ↗");
|
|
source.href = repo.html_url;
|
|
links.append(source);
|
|
if (info.release) {
|
|
const release = node("a", null, "Releases ↗");
|
|
release.href = `${repo.html_url}/releases`;
|
|
links.append(release);
|
|
}
|
|
const issues = node("a", null, "Issues ↗");
|
|
issues.href = `${repo.html_url}/issues`;
|
|
links.append(issues);
|
|
article.append(links);
|
|
|
|
return article;
|
|
}
|
|
|
|
function ordered(list) {
|
|
const mode = sortEl ? sortEl.value : "updated";
|
|
const copy = [...list];
|
|
if (mode === "name") return copy.sort((a, b) => a.name.localeCompare(b.name, "en", { sensitivity: "base" }));
|
|
if (mode === "language") return copy.sort((a, b) => String(a.language || "~").localeCompare(String(b.language || "~")) || a.name.localeCompare(b.name));
|
|
return copy.sort((a, b) => String(activity(b)).localeCompare(String(activity(a))));
|
|
}
|
|
|
|
function renderGrid() {
|
|
const query = (searchEl ? searchEl.value : "").trim().toLowerCase();
|
|
const pool = repos.filter(repo => repo.private === false && !repo.empty && !repo.archived);
|
|
view = ordered(pool.filter(repo => !query || searchable(repo).includes(query)));
|
|
const rows = document.createDocumentFragment();
|
|
view.forEach(repo => rows.append(card(repo)));
|
|
grid.replaceChildren(rows);
|
|
if (statusEl) {
|
|
statusEl.textContent = !pool.length
|
|
? "No public repositories reported by the API."
|
|
: query
|
|
? `${view.length} of ${pool.length} repositories match “${searchEl.value.trim()}”.`
|
|
: `${pool.length} public ${pool.length === 1 ? "repository" : "repositories"}, most recent activity first.`;
|
|
}
|
|
}
|
|
|
|
function renderLanguages() {
|
|
if (!langBox) return;
|
|
const counts = new Map();
|
|
repos.filter(repo => repo.private === false && !repo.empty && !repo.archived).forEach(repo => {
|
|
if (!repo.language) return;
|
|
counts.set(repo.language, (counts.get(repo.language) || 0) + 1);
|
|
});
|
|
const rows = [...counts.entries()].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]));
|
|
if (!rows.length) { langBox.hidden = true; return; }
|
|
const max = rows[0][1];
|
|
const frag = document.createDocumentFragment();
|
|
rows.forEach(([language, count]) => {
|
|
const row = node("button", "lang-row");
|
|
row.type = "button";
|
|
row.title = `Show ${language} repositories`;
|
|
row.append(node("span", null, language));
|
|
const bar = node("span", "bar");
|
|
const fill = node("i");
|
|
fill.style.width = `${Math.round((count / max) * 100)}%`;
|
|
fill.style.background = LANG_COLORS[language] || "#9ae4f0";
|
|
bar.append(fill);
|
|
row.append(bar, node("b", null, String(count)));
|
|
row.addEventListener("click", () => {
|
|
if (!searchEl) return;
|
|
searchEl.value = language;
|
|
renderGrid();
|
|
document.getElementById("repos")?.scrollIntoView({ behavior: "smooth", block: "start" });
|
|
});
|
|
frag.append(row);
|
|
});
|
|
langBox.replaceChildren(frag);
|
|
}
|
|
|
|
load()
|
|
.then(() => {
|
|
renderStats();
|
|
renderLanguages();
|
|
renderGrid();
|
|
if (searchEl) searchEl.addEventListener("input", renderGrid);
|
|
if (sortEl) sortEl.addEventListener("change", renderGrid);
|
|
})
|
|
.catch(() => {
|
|
if (statusEl) {
|
|
statusEl.textContent = "The repository list could not be loaded. ";
|
|
const fallback = node("a", null, "Open the repository browser instead ↗");
|
|
fallback.href = "https://code.knickqueue.win/explore/repos";
|
|
statusEl.append(fallback);
|
|
}
|
|
})
|
|
.finally(() => clearTimeout(timeout));
|
|
})();
|