import AppQueries from "../domain/AppQueries"; import { UpdateDefinition } from "../domain/projections/ApplicationUpdates"; import Caprover, { Application } from "../services/Caprover"; import Layout, { html } from "../ui/Layout"; const OLD_PERIOD_IN_DAYS = 60; const ApplicationOverview = (application: Application) => { const deployedAt = application.lastDeployedAt.toLocaleDateString("fr-FR"); return html` ${application.name}
${application.imageName}
${application.toString()}
${application.isOlderThan(OLD_PERIOD_IN_DAYS) ? `${deployedAt}` : deployedAt} ${application.dockerImage ? ` Docker hub ` : ""} `; }; const Pending = (pendingUpdates: UpdateDefinition[]) => { if (pendingUpdates.length === 0) { return ""; } return html`

Pending updates (${pendingUpdates.length})

`; }; type Sort = { field: string; order: "asc" | "desc" }; const Page = ( applications: Application[], currentSort: Sort, pendingUpdates: UpdateDefinition[] ) => { const sortLink = (field: string, title: string) => { let url = `?sort=${field}`; let className = ""; if (currentSort.field === field) { className = "current"; title += currentSort.order === "asc" ? " ▲" : " ▼"; url += `&order=${currentSort.order === "asc" ? "desc" : "asc"}`; } return `${title}`; }; return html`

Applications

Update applications now! ${Pending(pendingUpdates)}

All applications (${applications.length})

${applications.map((app) => ApplicationOverview(app)).join("")}
${sortLink("name", "Name")} Deployment ${sortLink("deployed", "Last deployed")} Actions
`; }; export default async ( req: Request, caprover: Caprover, queries: AppQueries ): Promise => { const applications = await caprover.getApps(); const sort: Sort = { field: new URL(req.url).searchParams.get("sort") ?? "name", order: new URL(req.url).searchParams.get("order") === "desc" ? "desc" : "asc", }; if (sort.field === "name") { applications.sort((a, b) => a.name.localeCompare(b.name)); } else if (sort.field === "deployed") { applications.sort( (a, b) => a.lastDeployedAt.getTime() - b.lastDeployedAt.getTime() ); } if (sort.order === "desc") { applications.reverse(); } const pendingUpdates = queries.pendingApplicationUpdates(); return new Response(Layout(Page(applications, sort, pendingUpdates)), { headers: { "Content-Type": "text/html" }, }); };