import { useEffect, useState } from "react"; import IntegrationPageTitle from "../components/IntegrationPageTitle"; import { Link } from "react-router-dom"; import { api, type CurrentUser, type GiteaRepo, type IntegrationSummary } from "../api/client"; import { useSortable } from "../hooks/useSortable"; import SortableTh from "../components/SortableTh"; import { usePagination } from "../hooks/usePagination"; import Pagination from "../components/Pagination"; import { downloadCsv } from "../utils/csv"; import { formatDateTime } from "../utils/date"; import { formatAgo } from "../utils/duration"; function runStatusBadge(repo: GiteaRepo) { const run = repo.latestRun; if (!run) return No runs; const state = run.conclusion || run.status; switch (state) { case "success": return Success; case "failure": return Failed; case "cancelled": return Cancelled; case "skipped": return Skipped; case "waiting": case "queued": case "pending": return Queued; case "running": case "in_progress": return Running; default: return {state}; } } export default function Gitea({ user }: { user: CurrentUser }) { const canEdit = user.role === "admin" || user.role === "operator"; const [integrations, setIntegrations] = useState(null); const [selectedId, setSelectedId] = useState(null); const [error, setError] = useState(null); const [repos, setRepos] = useState(null); const [loading, setLoading] = useState(false); const [rerunningRun, setRerunningRun] = useState(null); useEffect(() => { api.integrations .list() .then((res) => { const giteaIntegrations = res.integrations.filter((i) => i.type === "gitea"); setIntegrations(giteaIntegrations); if (!selectedId && giteaIntegrations.length > 0) setSelectedId(giteaIntegrations[0].id); }) .catch((err) => setError(err instanceof Error ? err.message : String(err))); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const selected = integrations?.find((i) => i.id === selectedId) ?? null; function loadRepos(id: number) { setLoading(true); setError(null); api.integrations.gitea .repos(id) .then((res) => setRepos(res.repos)) .catch((err) => setError(err instanceof Error ? err.message : String(err))) .finally(() => setLoading(false)); } useEffect(() => { if (selected?.enabled) { loadRepos(selected.id); } else { setRepos(null); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [selectedId]); async function rerunFailed(repo: GiteaRepo) { if (!selectedId || !repo.latestRun) return; setRerunningRun(repo.latestRun.id); setError(null); try { await api.integrations.gitea.rerunFailed(selectedId, repo.owner, repo.name, repo.latestRun.id); loadRepos(selectedId); } catch (err) { setError(err instanceof Error ? err.message : String(err)); } finally { setRerunningRun(null); } } const { sorted, sortKey, sortDir, requestSort } = useSortable(repos); const { pageItems, page, setPage, pageCount, totalCount } = usePagination(sorted); function exportCsv() { if (!sorted) return; downloadCsv( "gitea-repos.csv", ["Repository", "Private", "Last updated", "Last run", "Branch", "Status"], sorted.map((r) => [ r.fullName, r.private ? "yes" : "no", r.updatedAt ? formatDateTime(new Date(r.updatedAt)) : "", r.latestRun ? `#${r.latestRun.runNumber} ${r.latestRun.displayTitle}` : "", r.latestRun?.headBranch ?? "", r.latestRun ? r.latestRun.conclusion || r.latestRun.status : "", ]), ); } return ( <> {error &&
{error}
} {integrations?.length === 0 ? (
No Gitea integration configured yet. Add one under Integrations.
) : ( <> {integrations && integrations.length > 1 && (
)} {selected && !selected.enabled ? (
"{selected.name}" is disabled. Enable it under Integrations → Manage integrations to see its repositories.
) : (

Repositories

label="Repository" sortKeyName="fullName" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> label="Last updated" sortKeyName="updatedAt" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> {canEdit && } {pageItems?.map((r) => ( {canEdit && ( )} ))} {sorted?.length === 0 && ( )}
Last run Branch StatusActions
{r.fullName} {r.private && Private} {r.updatedAt ? ( <>
{formatAgo(r.updatedAt)}
{formatDateTime(new Date(r.updatedAt))}
) : ( — )}
{r.latestRun ? ( #{r.latestRun.runNumber} {r.latestRun.displayTitle} ) : r.hasActions ? ( "—" ) : ( "Actions disabled" )} {r.latestRun?.headBranch ?? "—"} {runStatusBadge(r)} {r.latestRun && (r.latestRun.conclusion === "failure" || r.latestRun.status === "failure") && ( )}
No repositories visible to this token.
)} )} ); }