Files
LambdaMail/services/webmail/src/app/(admin)/admin/queue/page.tsx
T
Lucas RayanandClaude Opus 5 ee240ae227 #23: show real data, unify the surfaces and make enrolment scannable
The interface was reporting things that were not true. Several screens were
mockups wired to nothing, and a reader with the console in Portuguese was
served a mixture of two languages.

Fabricated data, removed:

- The outbound queue was two invented jobs held in component state. Retry and
  Freeze only edited that local array, so the screen showed the same two rows
  on every deployment and reported success for actions that never happened.
  "Freeze" named an operation the backend has never implemented. It now reads
  /api/v1/admin/queue, which was already serving the real thing, and offers
  retry and cancel, which exist.
- Every dashboard KPI fell back to an invented constant (1420 received, 890
  sent, 3 queued, 18.4% disk), so an unreachable API produced a dashboard of
  plausible traffic for a server that might have delivered nothing. Two of
  those fields were never returned by the endpoint at all, so the fake disk
  figure was what everyone always saw. DMARC did the same with 1250/98%/97%.
- The domains screen showed thirteen DNS records for example.com, all marked
  VERIFIED, and posted {domain_id: "default-domain"} to endpoints expecting a
  real id. DKIM rotation sends "domain", not "domain_id", so its 400 was
  swallowed and a made-up TXT record displayed as though a key had been
  issued: rotation had never once worked from this screen.
- The TLS panel asserted a Let's Encrypt RSA 4096 certificate and verified
  DANE from static markup, on a deployment currently serving a self-signed
  one. It now reads the cert watcher via /api/v1/admin/tls.
- The readiness scorecard printed "10 / 10 all checks passed" regardless of
  what preflight reported, including failures.
- A message trace that found nothing invented a five-step delivery - TLS 1.3
  accepted, SPF pass, spam score 0.8 - for a message that does not exist.
- Both sidebars showed a fixed admin@lambdamail.local with a hardcoded
  "SUPER_ADMIN (2FA)", claiming a second factor whether or not one existed.
- Settings seeded a "[Boletim]" Sieve rule. Saving posts the whole list, so
  the first edit wrote that invented filter into the account's real script.

The reconcile endpoint itself answered {records_verified: 13, status:
"VERIFIED"} for any domain id, including one that does not exist, without
resolving a name. It now reports what it does, which is record the request.

Surfaces:

The root was a chooser listing webmail and the console side by side, which
asked visitors a question they should not have to answer and advertised cookie
names and audiences to anonymous readers. It now redirects into the mail
client. Accounts that may administer the server get a link to the console from
inside the app - a link to /admin/login, so crossing over still costs the
second factor the admin audience requires - and the console gets a plain link
back, since the webmail session was never given up to get there.

Enrolment:

Both enrolment screens printed the base32 secret as text and left the operator
to type 32 characters into their phone. The enrol endpoint already returned an
otpauth:// URI; it simply was not drawn. Added a shared component that renders
it as a QR locally - the seed is already in the page, and sending it to an
image service would put it in another request log for nothing.

The recovery-code step rendered its own "sign in" button while the form's
submit button stayed visible below it: two identical buttons, and no way to
copy the codes it insisted you save. It is now terminal, with copy and
continue.

Language:

translate() had no interpolation, so "Undo Send ({seconds}s remaining)" showed
the placeholder and a second hardcoded countdown was appended after it. It now
substitutes, leaving an unmatched token visible rather than blanking it.

The pt-BR bundle had lost its diacritics - "Destinatario", "Ultimo erro",
"Acoes" - from the ASCII-source rule being applied to the translations, which
are data. Restored, and the three bundles are back in key parity at 270 each,
with roughly 120 strings that were hardcoded in the source now keyed.

Healthchecks:

web had none, despite being the only service users reach directly: a wedged
Next process kept its container running and Traefik kept routing to it. Added,
using node's own fetch since the image ships no curl. rspamd and clamav gained
the timeouts and start_periods whose absence would fail them during startup.
migrate stays without one and says why: it is a one-shot job, and a probe
would report unhealthy exactly when it has succeeded.

Verified: 36 webmail tests and 41 auth tests against a real Postgres, both new
admin/me cases confirmed to fail without the route (404). lang-lint clean,
production build clean, 0 vulnerabilities, compose validates.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-02 19:13:53 -03:00

203 lines
7.9 KiB
TypeScript

"use client";
import React, { useCallback, useEffect, useState } from "react";
import { RefreshCw } from "lucide-react";
import { useTranslations } from "../../../../i18n/provider";
/**
* The outbound queue, read from the database.
*
* This screen used to be a fixture: two invented jobs ("job-101",
* user@example.com -> partner@remotehost.com) held in component state, whose
* Retry and Freeze buttons only edited that local array. It showed the same two
* rows on every deployment, never displayed a real stuck message, and reported
* success for actions that had not happened. /api/v1/admin/queue was already
* serving the real thing.
*/
/** Mirrors the outbound_jobs row shape returned by the queue endpoint. */
interface QueueJob {
id: string;
envelope_from: string;
envelope_to: string;
destination_domain: string;
status: string;
attempt: number;
next_attempt_at: string | null;
last_smtp_code: number | null;
last_error: string | null;
}
interface QueueSummary {
by_status: Record<string, number>;
recent: QueueJob[];
}
/** Only failure states earn a warning colour; the rest are ordinary progress. */
function statusClass(status: string): string {
if (status === "BOUNCED" || status === "FROZEN") return "badge-danger";
if (status === "DEFERRED") return "badge-warning";
return "badge-verified";
}
export default function AdminQueuePage() {
const t = useTranslations();
const [summary, setSummary] = useState<QueueSummary | null>(null);
const [message, setMessage] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
const [loading, setLoading] = useState(true);
const load = useCallback(async () => {
setLoading(true);
try {
const res = await fetch("/api/v1/admin/queue");
if (!res.ok) throw new Error();
setSummary(await res.json());
setError(null);
} catch {
// Left null so the table renders empty rather than showing stale or
// invented rows while the service is unreachable.
setSummary(null);
setError(t("errors.loadFailed"));
} finally {
setLoading(false);
}
}, [t]);
useEffect(() => {
void load();
}, [load]);
/**
* Retry and cancel are the two actions the service actually implements.
* The old "Freeze" button named a third that has never existed anywhere in
* the backend, so pressing it changed nothing but the colour of a cell.
*/
const act = async (id: string, action: "retry" | "cancel") => {
setMessage(null);
setError(null);
const res = await fetch(`/api/v1/admin/queue/${encodeURIComponent(id)}/${action}`, {
method: "POST",
});
if (!res.ok) {
setError(t("errors.serverError"));
return;
}
setMessage(t(action === "retry" ? "admin.jobRetried" : "admin.jobCancelled", { id }));
// Re-read rather than patching local state, so what is on screen is what
// the queue now holds.
await load();
};
const jobs = summary?.recent ?? [];
return (
<div className="p-6 md:p-8 space-y-6 max-w-7xl mx-auto">
<div className="flex flex-col md:flex-row md:items-center justify-between gap-4">
<div>
<h1 className="text-2xl font-bold text-white mb-1">{t("admin.queueTitle")}</h1>
<p className="text-xs text-slate-400">{t("admin.queueSubtitle")}</p>
</div>
<button
type="button"
onClick={() => void load()}
disabled={loading}
className="flex items-center gap-2 self-start rounded-xl border border-slate-700 px-3.5 py-2 text-xs font-medium text-slate-300 transition-colors hover:border-emerald-500/50 hover:text-white disabled:opacity-50"
>
<RefreshCw className={`w-3.5 h-3.5 ${loading ? "animate-spin" : ""}`} />
<span>{t("common.refresh")}</span>
</button>
</div>
{/* Counts per status, straight from the by_status aggregate. */}
{summary && Object.keys(summary.by_status).length > 0 && (
<div className="flex flex-wrap gap-2">
{Object.entries(summary.by_status).map(([status, count]) => (
<span
key={status}
className="rounded-lg border border-slate-800 bg-slate-900/70 px-3 py-1.5 text-xs text-slate-300"
>
<span className="font-mono font-bold text-white">{count}</span> {status}
</span>
))}
</div>
)}
{message && (
<div className="p-4 rounded-xl bg-emerald-500/10 border border-emerald-500/30 text-emerald-300 text-xs">
{message}
</div>
)}
{error && (
<div className="p-4 rounded-xl bg-rose-500/10 border border-rose-500/30 text-rose-300 text-xs">
{error}
</div>
)}
<div className="glass-panel rounded-2xl border border-slate-800 overflow-hidden">
<div className="p-4 border-b border-slate-800 bg-slate-900/60 font-bold text-xs text-slate-300">
{t("admin.queueTitle")} ({jobs.length})
</div>
{jobs.length === 0 && !loading ? (
<div className="p-8 text-center text-xs text-slate-500">{t("admin.queueEmpty")}</div>
) : (
<div className="overflow-x-auto">
<table className="w-full text-left border-collapse text-xs">
<thead>
<tr className="border-b border-slate-800 bg-slate-900/40 text-slate-400">
<th className="p-3">{t("ui.jobId")}</th>
<th className="p-3">{t("ui.sender")}</th>
<th className="p-3">{t("ui.recipient")}</th>
<th className="p-3">{t("ui.destination")}</th>
<th className="p-3">{t("ui.attempts")}</th>
<th className="p-3">{t("ui.lastError")}</th>
<th className="p-3">{t("common.status")}</th>
<th className="p-3">{t("ui.actions")}</th>
</tr>
</thead>
<tbody className="divide-y divide-slate-800/60 font-mono">
{jobs.map((j) => (
<tr key={j.id} className="hover:bg-slate-900/30">
<td className="p-3 font-bold text-slate-200">{j.id.slice(0, 8)}</td>
<td className="p-3 text-slate-300">{j.envelope_from}</td>
<td className="p-3 text-slate-300">{j.envelope_to}</td>
<td className="p-3 text-emerald-400">{j.destination_domain}</td>
<td className="p-3 text-slate-400">{j.attempt}</td>
<td className="p-3 text-slate-400 truncate max-w-xs" title={j.last_error ?? ""}>
{j.last_error
? `${j.last_smtp_code ? `${j.last_smtp_code} ` : ""}${j.last_error}`
: t("common.none")}
</td>
<td className="p-3">
<span className={`${statusClass(j.status)} px-2 py-0.5 rounded text-[10px]`}>
{j.status}
</span>
</td>
<td className="p-3 flex items-center gap-2">
<button
type="button"
onClick={() => void act(j.id, "retry")}
className="px-2 py-1 rounded bg-indigo-600/20 hover:bg-indigo-600/30 text-indigo-300 text-[10px] font-bold"
>
{t("admin.retryJob")}
</button>
<button
type="button"
onClick={() => void act(j.id, "cancel")}
className="px-2 py-1 rounded bg-red-600/20 hover:bg-red-600/30 text-red-300 text-[10px] font-bold"
>
{t("admin.cancelJob")}
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
</div>
);
}