add book_count feature, migrate backoffice to server actions, fix healthcheck

This commit is contained in:
2026-03-05 21:29:48 +01:00
parent 3a96f6ba36
commit ef8a755a83
13 changed files with 222 additions and 79 deletions

View File

@@ -210,6 +210,32 @@ button:hover {
min-width: 66px;
}
.cancel-btn {
background: linear-gradient(95deg, hsl(2 72% 48% / 0.15), hsl(338 82% 62% / 0.2));
border-color: hsl(2 72% 48% / 0.5);
}
.status-pending { color: hsl(45 93% 47%); }
.status-running { color: hsl(192 85% 55%); }
.status-completed { color: hsl(142 60% 45%); }
.status-failed { color: hsl(2 72% 48%); }
.status-cancelled { color: hsl(220 13% 40%); }
.error-hint {
display: inline-block;
margin-left: 6px;
width: 16px;
height: 16px;
line-height: 16px;
text-align: center;
border-radius: 50%;
background: hsl(2 72% 48%);
color: white;
font-size: 11px;
font-weight: bold;
cursor: help;
}
.card {
background: var(--card);
border: 1px solid var(--line);

View File

@@ -1,16 +1,43 @@
import { listJobs } from "../../lib/api";
import { revalidatePath } from "next/cache";
import { listJobs, fetchLibraries, rebuildIndex, cancelJob, IndexJobDto, LibraryDto } from "../../lib/api";
export const dynamic = "force-dynamic";
export default async function JobsPage() {
const jobs = await listJobs().catch(() => []);
const [jobs, libraries] = await Promise.all([
listJobs().catch(() => [] as IndexJobDto[]),
fetchLibraries().catch(() => [] as LibraryDto[])
]);
const libraryMap = new Map(libraries.map(l => [l.id, l.name]));
async function triggerRebuild(formData: FormData) {
"use server";
const libraryId = formData.get("library_id") as string;
await rebuildIndex(libraryId || undefined);
revalidatePath("/jobs");
}
async function cancelJobAction(formData: FormData) {
"use server";
const id = formData.get("id") as string;
await cancelJob(id);
revalidatePath("/jobs");
}
return (
<>
<h1>Index Jobs</h1>
<div className="card">
<form action="/jobs/rebuild" method="post">
<input name="library_id" placeholder="optional library UUID" />
<form action={triggerRebuild}>
<select name="library_id" defaultValue="">
<option value="">All libraries</option>
{libraries.map((lib) => (
<option key={lib.id} value={lib.id}>
{lib.name}
</option>
))}
</select>
<button type="submit">Queue Rebuild</button>
</form>
</div>
@@ -18,20 +45,34 @@ export default async function JobsPage() {
<thead>
<tr>
<th>ID</th>
<th>Library</th>
<th>Type</th>
<th>Status</th>
<th>Created</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{jobs.map((job) => (
<tr key={job.id}>
<td>
<code>{job.id}</code>
<code>{job.id.slice(0, 8)}</code>
</td>
<td>{job.library_id ? libraryMap.get(job.library_id) || job.library_id.slice(0, 8) : "—"}</td>
<td>{job.type}</td>
<td>{job.status}</td>
<td>{job.created_at}</td>
<td>
<span className={`status-${job.status}`}>{job.status}</span>
{job.error_opt && <span className="error-hint" title={job.error_opt}>!</span>}
</td>
<td>{new Date(job.created_at).toLocaleString()}</td>
<td>
{job.status === "pending" || job.status === "running" ? (
<form action={cancelJobAction}>
<input type="hidden" name="id" value={job.id} />
<button type="submit" className="cancel-btn">Cancel</button>
</form>
) : null}
</td>
</tr>
))}
</tbody>

View File

@@ -1,17 +0,0 @@
import { revalidatePath } from "next/cache";
import { NextResponse } from "next/server";
import { apiFetch } from "../../../lib/api";
export async function POST(req: Request) {
const form = await req.formData();
const libraryId = String(form.get("library_id") || "").trim();
const body = libraryId ? { library_id: libraryId } : {};
await apiFetch("/index/rebuild", {
method: "POST",
body: JSON.stringify(body)
}).catch(() => null);
revalidatePath("/jobs");
return NextResponse.redirect(new URL("/jobs", req.url));
}

View File

@@ -1,27 +0,0 @@
import { revalidatePath } from "next/cache";
import { NextResponse } from "next/server";
import { apiFetch } from "../../../lib/api";
type CreatedToken = { token: string };
export async function POST(req: Request) {
const form = await req.formData();
const name = String(form.get("name") || "").trim();
const scope = String(form.get("scope") || "read").trim();
let created = "";
if (name) {
const res = await apiFetch<CreatedToken>("/admin/tokens", {
method: "POST",
body: JSON.stringify({ name, scope })
}).catch(() => null);
created = res?.token || "";
}
revalidatePath("/tokens");
const url = new URL("/tokens", req.url);
if (created) {
url.searchParams.set("created", created);
}
return NextResponse.redirect(url);
}

View File

@@ -1,4 +1,6 @@
import { listTokens } from "../../lib/api";
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { listTokens, createToken, revokeToken, TokenDto } from "../../lib/api";
export const dynamic = "force-dynamic";
@@ -8,7 +10,25 @@ export default async function TokensPage({
searchParams: Promise<{ created?: string }>;
}) {
const params = await searchParams;
const tokens = await listTokens().catch(() => []);
const tokens = await listTokens().catch(() => [] as TokenDto[]);
async function createTokenAction(formData: FormData) {
"use server";
const name = formData.get("name") as string;
const scope = formData.get("scope") as string;
if (name) {
const result = await createToken(name, scope);
revalidatePath("/tokens");
redirect(`/tokens?created=${encodeURIComponent(result.token)}`);
}
}
async function revokeTokenAction(formData: FormData) {
"use server";
const id = formData.get("id") as string;
await revokeToken(id);
revalidatePath("/tokens");
}
return (
<>
@@ -16,13 +36,13 @@ export default async function TokensPage({
{params.created ? (
<div className="card">
<strong>Token created:</strong>
<strong>Token created (copy it now, it won't be shown again):</strong>
<pre>{params.created}</pre>
</div>
) : null}
<div className="card">
<form action="/tokens/create" method="post">
<form action={createTokenAction}>
<input name="name" placeholder="token name" required />
<select name="scope" defaultValue="read">
<option value="read">read</option>
@@ -52,10 +72,12 @@ export default async function TokensPage({
</td>
<td>{token.revoked_at ? "yes" : "no"}</td>
<td>
<form className="inline" action="/tokens/revoke" method="post">
<input type="hidden" name="id" value={token.id} />
<button type="submit">Revoke</button>
</form>
{!token.revoked_at && (
<form action={revokeTokenAction}>
<input type="hidden" name="id" value={token.id} />
<button type="submit">Revoke</button>
</form>
)}
</td>
</tr>
))}

View File

@@ -1,13 +0,0 @@
import { revalidatePath } from "next/cache";
import { NextResponse } from "next/server";
import { apiFetch } from "../../../lib/api";
export async function POST(req: Request) {
const form = await req.formData();
const id = String(form.get("id") || "").trim();
if (id) {
await apiFetch(`/admin/tokens/${id}`, { method: "DELETE" }).catch(() => null);
}
revalidatePath("/tokens");
return NextResponse.redirect(new URL("/tokens", req.url));
}