progmaweb
The site and account service of Progmasoft: a Next.js frontend and an ASP.NET Core API.
Clonegit clone https://git.progmasoft.com/progmaweb
Split gitRoute and the git frame, and tidy the stylesheet
- Commit
b15823f391cc82fb77934f18bb22306f2ade7c28- Author
- Leitwolf11 ·
- Parents
f103618012d6
Changes
apps/web/app/git/GitFrame.tsx | 80 +++++++++++++++++++++++------------------ apps/web/app/git/git.css | 12 ++++--- apps/web/e2e/fake-git-api.mjs | 82 ++++++++++++++++++++++--------------------- apps/web/lib/git-messages.ts | 32 ++++++++++------- 4 files changed, 116 insertions(+), 90 deletions(-) diff --git a/apps/web/app/git/GitFrame.tsx b/apps/web/app/git/GitFrame.tsx index cb36120..e1da09a 100644 --- a/apps/web/app/git/GitFrame.tsx +++ b/apps/web/app/git/GitFrame.tsx @@ -51,6 +51,44 @@ export function gitSize(bytes: number | null): string { : `${(bytes / 1024 / 1024).toFixed(1)} MiB`; } +/** The navigation and clone address of a repository. */ +function RepositoryHeader({ + locale, + repository, + view, + reference, +}: Readonly<{ + locale: Locale; + repository: GitRepository; + view?: GitView; + reference?: string; +}>) { + const text = getGitMessages(locale); + const base = `/${repository.name}`; + const ref = reference === repository.defaultBranch ? undefined : reference; + const current = (name: GitView) => (view === name ? "page" : undefined); + return ( + <> + <nav className="pm-git-nav" aria-label={text.repository}> + <a href={gitHref(base, { ref })} aria-current={current("summary")}> + {text.files} + </a> + <a href={gitHref(`${base}/log`, { ref })} aria-current={current("log")}> + {text.log} + </a> + <a href={`${base}/refs`} aria-current={current("refs")}> + {text.refs} + </a> + <a href={repository.homepage}>{text.developedOn}</a> + </nav> + <p className="pm-git-clone"> + <span>{text.clone}</span> + <code>git clone {repository.cloneUrl}</code> + </p> + </> + ); +} + /** The frame of every page of git.progmasoft.com. */ export function GitFrame({ locale, @@ -68,11 +106,6 @@ export function GitFrame({ children: ReactNode; }>) { const text = getGitMessages(locale); - const base = repository ? `/${repository.name}` : ""; - const ref = - reference && reference !== repository?.defaultBranch - ? reference - : undefined; return ( <div className="pm-page"> <SiteHeader locale={locale} /> @@ -82,38 +115,17 @@ export function GitFrame({ <p className="eyebrow"> <Link href="/">{text.title}</Link> </p> - <h1>{repository ? repository.name : text.title}</h1> + <h1>{repository?.name ?? text.title}</h1> <p className="pm-lede"> - {repository ? repository.description : text.description} + {repository?.description ?? text.description} </p> {repository ? ( - <> - <nav className="pm-git-nav" aria-label={text.repository}> - <a - href={gitHref(base, { ref })} - aria-current={view === "summary" ? "page" : undefined} - > - {text.files} - </a> - <a - href={gitHref(`${base}/log`, { ref })} - aria-current={view === "log" ? "page" : undefined} - > - {text.log} - </a> - <a - href={`${base}/refs`} - aria-current={view === "refs" ? "page" : undefined} - > - {text.refs} - </a> - <a href={repository.homepage}>{text.developedOn}</a> - </nav> - <p className="pm-git-clone"> - <span>{text.clone}</span> - <code>git clone {repository.cloneUrl}</code> - </p> - </> + <RepositoryHeader + locale={locale} + repository={repository} + view={view} + reference={reference} + /> ) : null} </div> </section> diff --git a/apps/web/app/git/git.css b/apps/web/app/git/git.css index 77e8816..d4a7794 100644 --- a/apps/web/app/git/git.css +++ b/apps/web/app/git/git.css @@ -1,10 +1,14 @@ -/* SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> */ -/* SPDX-License-Identifier: AGPL-3.0-or-later */ +/* + * SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> + * SPDX-License-Identifier: AGPL-3.0-or-later + */ /* git.progmasoft.com: the mirrors, in the tokens of the rest of the site. */ .pm-git { - --mono: ui-monospace, "Cascadia Code", "SF Mono", Menlo, Consolas, monospace; + --mono: + ui-monospace, "Cascadia Code", "SF Mono", "Menlo", "Consolas", monospace; + min-width: 0; } @@ -31,7 +35,7 @@ min-height: 32px; } -.pm-git-nav a[aria-current="page"] { +.pm-git-nav [aria-current="page"] { font-weight: 700; text-decoration: underline; } diff --git a/apps/web/e2e/fake-git-api.mjs b/apps/web/e2e/fake-git-api.mjs index 81a88a9..9dae433 100644 --- a/apps/web/e2e/fake-git-api.mjs +++ b/apps/web/e2e/fake-git-api.mjs @@ -36,45 +36,47 @@ const commits = Array.from({ length: 12 }, (_, index) => ({ subject: `Change ${index}: a subject line long enough to wrap on a phone screen of 360 pixels`, })); -const routes = { - "/": [{ name: "Examples", repositories: [repository] }], - [`/${repository.name}`]: repository, - [`/${repository.name}/refs`]: { - branches: [ - { name: "main", commit, date, subject: commits[0].subject }, - { name: `feature/${long}`, commit: parent, date, subject: "Work" }, - ], - tags: [{ name: "example-1.0.0", commit, date, subject: "Release" }], - }, - [`/${repository.name}/log`]: { - reference: "main", - commit, - path: "", - page: 0, - commits, - hasMore: true, - }, - [`/${repository.name}/commits/${commit}`]: { - commit, - parents: [parent], - author: "Progmasoft", - authorDate: date, - committer: "Progmasoft", - commitDate: date, - message: `${commits[0].subject}\n\nThe body of the message, with a line that is long: ${long}${long}.`, - diff: [ - " src/main.c | 2 +-", - "", - "diff --git a/src/main.c b/src/main.c", - "--- a/src/main.c", - "+++ b/src/main.c", - "@@ -1 +1 @@", - `-${line}`, - `+${line}`, - ].join("\n"), - isDiffTruncated: false, - }, -}; +const routes = new Map( + Object.entries({ + "/": [{ name: "Examples", repositories: [repository] }], + [`/${repository.name}`]: repository, + [`/${repository.name}/refs`]: { + branches: [ + { name: "main", commit, date, subject: commits[0].subject }, + { name: `feature/${long}`, commit: parent, date, subject: "Work" }, + ], + tags: [{ name: "example-1.0.0", commit, date, subject: "Release" }], + }, + [`/${repository.name}/log`]: { + reference: "main", + commit, + path: "", + page: 0, + commits, + hasMore: true, + }, + [`/${repository.name}/commits/${commit}`]: { + commit, + parents: [parent], + author: "Progmasoft", + authorDate: date, + committer: "Progmasoft", + commitDate: date, + message: `${commits[0].subject}\n\nThe body of the message, with a line that is long: ${long}${long}.`, + diff: [ + " src/main.c | 2 +-", + "", + "diff --git a/src/main.c b/src/main.c", + "--- a/src/main.c", + "+++ b/src/main.c", + "@@ -1 +1 @@", + `-${line}`, + `+${line}`, + ].join("\n"), + isDiffTruncated: false, + }, + }), +); function tree(path) { if (path.endsWith(".c") || path.endsWith(".txt") || path.endsWith(".md")) { @@ -120,7 +122,7 @@ createServer((request, response) => { ? tree(url.searchParams.get("path") ?? "") : path === null ? undefined - : routes[path]; + : routes.get(path); if (body === undefined) { response.statusCode = 404; response.end(); diff --git a/apps/web/lib/git-messages.ts b/apps/web/lib/git-messages.ts index 5e22011..6808738 100644 --- a/apps/web/lib/git-messages.ts +++ b/apps/web/lib/git-messages.ts @@ -217,19 +217,27 @@ export function gitRoute(pathname: string): string | null { if (path === "/") { return "/git"; } - const [, name, view, extra, ...rest] = path.split("/"); - if (!name || !repositoryName.test(name) || name.endsWith(".git")) { - return null; - } - if (view === undefined) { + const [, name = "", ...view] = path.split("/"); + return isRepositoryName(name) ? viewRoute(name, view) : null; +} + +/** Whether a path segment names a repository, not its .git clone address. */ +function isRepositoryName(name: string): boolean { + return repositoryName.test(name) && !name.endsWith(".git"); +} + +/** The views of one repository: its files, a tree, its log, refs or a commit. */ +const repositoryViews = new Set(["tree", "log", "refs"]); + +function viewRoute(name: string, view: string[]): string | null { + const [kind, commit] = view; + if (kind === undefined) { return `/git/${name}`; } - if (rest.length === 0 && extra === undefined) { - return view === "tree" || view === "log" || view === "refs" - ? `/git/${name}/${view}` - : null; + if (view.length === 1) { + return repositoryViews.has(kind) ? `/git/${name}/${kind}` : null; } - return view === "commit" && extra && commitId.test(extra) && rest.length === 0 - ? `/git/${name}/commit/${extra}` - : null; + const isCommit = + view.length === 2 && kind === "commit" && commitId.test(commit ?? ""); + return isCommit ? `/git/${name}/commit/${commit}` : null; }