peppa/apps/web/app/(app)/gitea/page.tsx

165 lines
7.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// glossary-ignore-file: superadmin-only page about the platform's git server — «репозиторий» is the literal object here
import Link from "next/link";
import { notFound } from "next/navigation";
import { ExternalLink, GitBranch, Lock } from "lucide-react";
import { giteaServerOverview, isSuperadmin, type GiteaServerRepo } from "@peppa/core";
import { ago, Empty, Flash, Notice, PageHeader, Section } from "@/components/common";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Card } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Workspace } from "@/components/Workspace";
import { requireUser } from "@/lib/auth";
import { makeRepoPrivateAction } from "./actions";
import { db } from "@/lib/db";
import { getT } from "@/lib/i18n";
import type { Translator } from "@peppa/shared/i18n";
export async function generateMetadata() {
const t = await getT();
return { title: t("Gitea — FORGE") };
}
type Params = { q?: string; show?: string; closed?: string; error?: string };
type Show = "all" | "linked" | "unlinked" | "public";
function RepoRow({ r, t, q, show }: { r: GiteaServerRepo; t: Translator; q: string; show: Show }) {
return (
<li className="flex flex-col gap-1 border-b border-border px-4 py-3 last:border-b-0">
<div className="flex flex-wrap items-center gap-2">
<a href={r.htmlUrl} target="_blank" rel="noreferrer" className="font-mono text-sm font-medium">
{r.fullName}
<ExternalLink className="ml-1 inline size-3" />
</a>
{r.private ? (
<Badge variant="secondary">
<Lock className="size-3" />
{t("приватный")}
</Badge>
) : (
<Badge variant="info">{t("публичный")}</Badge>
)}
{r.empty && <Badge variant="warn">{t("пустой")}</Badge>}
{r.archived && <Badge variant="secondary">{t("в архиве")}</Badge>}
{r.fork && <Badge variant="secondary">{t("форк")}</Badge>}
{r.project ? (
<Link href={`/p/${r.project.key}`} className="ml-auto text-xs">
{t("проект {key} · {name}", { key: r.project.key, name: r.project.name })}
</Link>
) : (
<span className="ml-auto text-xs text-muted-foreground">{t("не подключён к проекту")}</span>
)}
</div>
{r.description && <div className="text-sm text-muted-foreground">{r.description}</div>}
{!r.private && (
// PEP-280: one repository at a time; opening it again is the owner's decision in the project settings.
<form action={makeRepoPrivateAction} className="mt-1">
<input type="hidden" name="repo" value={r.fullName} />
{q && <input type="hidden" name="q" value={q} />}
{show !== "all" && <input type="hidden" name="show" value={show} />}
<Button type="submit" size="sm" variant="outline">
<Lock className="size-3" />
{t("Сделать закрытым")}
</Button>
</form>
)}
<div className="text-xs text-muted-foreground">
{t("ветка {branch} · обновлён {when} · {size}", { branch: r.defaultBranch || "—", when: ago(r.updatedAt, t), size: r.sizeKb >= 1024 ? `${(r.sizeKb / 1024).toFixed(1)} MB` : `${r.sizeKb} KB` })}
</div>
</li>
);
}
export default async function GiteaPage({ searchParams }: { searchParams: Promise<Params> }) {
const user = await requireUser();
const t = await getT();
if (!(await isSuperadmin(db, user))) notFound();
const q = await searchParams;
const o = await giteaServerOverview(db);
const needle = (q.q ?? "").trim().toLowerCase();
const show: Show = q.show === "linked" || q.show === "unlinked" || q.show === "public" ? q.show : "all";
const repos = o.repos.filter(
(r) =>
(!needle || r.fullName.toLowerCase().includes(needle) || r.description.toLowerCase().includes(needle)) &&
(show === "all" || (show === "public" ? !r.private : (show === "linked") === !!r.project)),
);
const open = o.repos.filter((r) => !r.private).length;
const owners = new Map<string, number>();
for (const r of o.repos) owners.set(r.owner, (owners.get(r.owner) ?? 0) + 1);
const linked = o.repos.filter((r) => r.project).length;
return (
<Workspace>
<div className="max-w-4xl">
<PageHeader title={<><GitBranch className="mr-1 inline size-5 text-primary" />Gitea</>}>
{t("Git-сервер платформы и репозитории на нём — как их видит бот FORGE. Раздел видят только суперадмины.")}
</PageHeader>
<Section title={t("Сервер")}>
<Card className="flex flex-col gap-1 p-4 text-sm">
<div>
{t("Адрес")}:{" "}
{o.url ? (
<a href={o.url} target="_blank" rel="noreferrer" className="font-mono">
{o.url}
</a>
) : (
"—"
)}
</div>
<div>
{t("Бот")}: <span className="font-mono">{o.bot?.login ?? "—"}</span>
{o.bot && (o.bot.admin ? <Badge variant="ok" className="ml-2">{t("админ сервера — видит все репозитории")}</Badge> : <Badge variant="warn" className="ml-2">{t("не админ — видит публичные и свои репозитории")}</Badge>)}
</div>
{o.repos.length > 0 && (
<div className="text-muted-foreground">
{t("Репозиториев: {n} · подключены к проектам: {linked}", { n: o.repos.length, linked })} ·{" "}
{[...owners].sort((a, b) => b[1] - a[1]).map(([owner, n]) => `${owner} ${n}`).join(", ")}
</div>
)}
{o.repos.length > 0 && (
<div>
{open > 0 ? (
<Link href="/gitea?show=public">{t("Публичных: {n} — их видит любой, у кого есть доступ к Gitea", { n: open })}</Link>
) : (
<span className="text-muted-foreground">{t("Публичных нет")}</span>
)}
</div>
)}
</Card>
</Section>
{q.closed && <Notice>{t("{repo} теперь закрытый: его видят только владелец, участники и бот", { repo: q.closed })}</Notice>}
<Flash error={q.error} />
{o.error && <Notice>{t("Gitea не ответила: {error}", { error: o.error })}</Notice>}
<Section title={t("Репозитории")} count={repos.length}>
<form className="mb-3 flex flex-wrap items-center gap-2" action="/gitea">
<Input name="q" defaultValue={q.q ?? ""} placeholder={t("Поиск по имени или описанию")} className="h-8 max-w-xs" />
<select name="show" defaultValue={show} className="h-8 rounded-md border border-input bg-background px-2 text-sm">
<option value="all">{t("все")}</option>
<option value="linked">{t("подключённые к проектам")}</option>
<option value="unlinked">{t("без проекта")}</option>
<option value="public">{t("публичные")}</option>
</select>
<Button type="submit" size="sm">
{t("Показать")}
</Button>
</form>
{repos.length === 0 ? (
<Empty>{t("Репозиториев не найдено.")}</Empty>
) : (
<Card className="p-0">
<ul>
{repos.map((r) => (
<RepoRow key={r.fullName} r={r} t={t} q={q.q ?? ""} show={show} />
))}
</ul>
</Card>
)}
</Section>
</div>
</Workspace>
);
}