Files
Homelab-manager/web/src/pages/Gitea.tsx
T
bobbanandClaude Sonnet 5.5 75f0a3d881 Add an "Open admin" button to the integration pages
Each integration page now has a button next to its title that opens the
product's own admin panel in a new tab, using the address entered on the
integration. Tailscale links to its admin console. Only http(s) addresses
become links.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 01:44:38 +02:00

246 lines
9.8 KiB
TypeScript

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 <span className="badge bg-secondary-lt text-secondary">No runs</span>;
const state = run.conclusion || run.status;
switch (state) {
case "success":
return <span className="badge bg-green-lt text-green">Success</span>;
case "failure":
return <span className="badge bg-red-lt text-red">Failed</span>;
case "cancelled":
return <span className="badge bg-secondary-lt text-secondary">Cancelled</span>;
case "skipped":
return <span className="badge bg-secondary-lt text-secondary">Skipped</span>;
case "waiting":
case "queued":
case "pending":
return <span className="badge bg-yellow-lt text-yellow">Queued</span>;
case "running":
case "in_progress":
return <span className="badge bg-blue-lt text-blue">Running</span>;
default:
return <span className="badge bg-secondary-lt text-secondary">{state}</span>;
}
}
export default function Gitea({ user }: { user: CurrentUser }) {
const canEdit = user.role === "admin" || user.role === "operator";
const [integrations, setIntegrations] = useState<IntegrationSummary[] | null>(null);
const [selectedId, setSelectedId] = useState<number | null>(null);
const [error, setError] = useState<string | null>(null);
const [repos, setRepos] = useState<GiteaRepo[] | null>(null);
const [loading, setLoading] = useState(false);
const [rerunningRun, setRerunningRun] = useState<number | null>(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 (
<>
<IntegrationPageTitle title="Gitea" integration={selected} />
{error && <div className="alert alert-danger">{error}</div>}
{integrations?.length === 0 ? (
<div className="card">
<div className="card-body text-secondary">
No Gitea integration configured yet. <Link to="/integrations">Add one under Integrations</Link>.
</div>
</div>
) : (
<>
{integrations && integrations.length > 1 && (
<div className="mb-3" style={{ maxWidth: 320 }}>
<select
className="form-select"
value={selectedId ?? ""}
onChange={(e) => setSelectedId(Number(e.target.value))}
>
{integrations.map((i) => (
<option key={i.id} value={i.id} disabled={!i.enabled}>
{i.name}
{!i.enabled ? " — disabled" : ""}
</option>
))}
</select>
</div>
)}
{selected && !selected.enabled ? (
<div className="card">
<div className="card-body text-secondary">
"{selected.name}" is disabled. Enable it under Integrations → Manage integrations to see its
repositories.
</div>
</div>
) : (
<div className="card">
<div className="card-header">
<h3 className="card-title">Repositories</h3>
<div className="card-actions">
<button className="btn btn-sm btn-outline-secondary" onClick={exportCsv} disabled={!sorted || sorted.length === 0}>
Export CSV
</button>
<button
className="btn btn-sm btn-outline-secondary"
onClick={() => selected && loadRepos(selected.id)}
disabled={loading}
>
{loading ? "Refreshing…" : "Refresh"}
</button>
</div>
</div>
<div className="table-responsive">
<table className="table table-vcenter card-table">
<thead>
<tr>
<SortableTh<GiteaRepo> label="Repository" sortKeyName="fullName" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<SortableTh<GiteaRepo> label="Last updated" sortKeyName="updatedAt" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<th>Last run</th>
<th>Branch</th>
<th>Status</th>
{canEdit && <th className="w-1">Actions</th>}
</tr>
</thead>
<tbody>
{pageItems?.map((r) => (
<tr key={r.fullName}>
<td>
<a href={r.htmlUrl} target="_blank" rel="noopener noreferrer">
{r.fullName}
</a>
{r.private && <span className="badge bg-secondary-lt ms-2">Private</span>}
</td>
<td>
{r.updatedAt ? (
<>
<div>{formatAgo(r.updatedAt)}</div>
<div className="text-secondary small">{formatDateTime(new Date(r.updatedAt))}</div>
</>
) : (
<span className="text-secondary">—</span>
)}
</td>
<td className="text-secondary">
{r.latestRun ? (
<a href={r.latestRun.htmlUrl} target="_blank" rel="noopener noreferrer">
#{r.latestRun.runNumber} {r.latestRun.displayTitle}
</a>
) : r.hasActions ? (
"—"
) : (
"Actions disabled"
)}
</td>
<td className="text-secondary">{r.latestRun?.headBranch ?? "—"}</td>
<td>{runStatusBadge(r)}</td>
{canEdit && (
<td>
{r.latestRun && (r.latestRun.conclusion === "failure" || r.latestRun.status === "failure") && (
<button
className="btn btn-sm"
onClick={() => rerunFailed(r)}
disabled={rerunningRun === r.latestRun.id}
>
{rerunningRun === r.latestRun.id ? "Re-running…" : "Re-run failed"}
</button>
)}
</td>
)}
</tr>
))}
{sorted?.length === 0 && (
<tr>
<td colSpan={canEdit ? 6 : 5} className="text-secondary text-center">
No repositories visible to this token.
</td>
</tr>
)}
</tbody>
</table>
</div>
<Pagination page={page} pageCount={pageCount} totalCount={totalCount} onPageChange={setPage} />
</div>
)}
</>
)}
</>
);
}