import type { Context } from "hono"; import { newestSaves, type RepoStats, repoStats, type Save } from "../mirror/queries.ts"; import { repoNames } from "../server/db.ts"; import { signedIn } from "../server/login.ts"; import { type Html, html } from "./html.ts"; import { openRepo, type PageContext, type PublicRepo, plural } from "./layout.ts"; import { repoIntro } from "./repo.ts"; export type LibraryRepo = PublicRepo & { stats: RepoStats; description: string }; export type RecentSave = { repo: string; save: Save }; export function libraryRepos(ctx: PageContext, visibility: 0 | 1 = 1): LibraryRepo[] { return repoNames(ctx.kepr, visibility).flatMap((name) => { const repo = openRepo(ctx, name, visibility); if (!repo) return []; const description = repoIntro(repo)?.text ?? ""; return [{ ...repo, stats: repoStats(repo.mirror), description }]; }); } export function privateRepoCards(c: Context, ctx: PageContext): Html | null { c.header("Vary", "Cookie", { append: true }); if (!signedIn(ctx, c)) return null; c.header("Cache-Control", "no-store"); const repos = libraryRepos(ctx, 0); return repos.length ? html`` : null; } /** The newest saves across the library, checkpoints never among them. */ export function recentSaves(library: LibraryRepo[], limit: number): RecentSave[] { return library .flatMap((repo) => newestSaves(repo.mirror, limit).map((save) => ({ repo: repo.settings.name, save })), ) .sort((a, b) => b.save.created_at - a.save.created_at) .slice(0, limit); } export function repoCards(library: LibraryRepo[]): Html { return html`
${library.map(repoCard)}
`; } // Kepr 2's card: name, save count, the description, and the category. function repoCard(repo: LibraryRepo): Html { const { name, category } = repo.settings; return html`
${ name }${plural(repo.stats.saves, "save")}
${repo.description && html`

${repo.description}

`} ${category && html`

${category}

`}
`; }