"use client";
import { useQuery } from "@tanstack/react-query";
import { Key, Loader2 } from "lucide-react";
import { Badge } from "@/components/ui/badge";
import { EmptyState } from "@/components/ui/empty-state";
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "@/components/ui/tooltip";
import { formatDate, formatDateTime } from "@/lib/format";
import { useTranslations } from "@/lib/i18n/context";
import { type ApiTokenInfo } from "./_shared";
/**
* Wraps a string in `truncate` + tooltip so a long token name /
* username / permission cannot push the row past its bounds.
*
* v1.4.19 phase A7 (4th attempt) — earlier fixes (column-hide,
* mobile card-list, mobile-strip `no-scrollbar`) cleared every
* scrollbar Marc reported except a residual painted bar he kept
* seeing at the bottom-right. Production probe confirmed no
* remaining horizontal-overflow culprits, but the cards still
* carried `break-all` next to `truncate` (dead code; `white-space:
* nowrap` from `truncate` beats `word-break: break-all`) and the
* desktop table cells had no upper-bound on width — a long token
* name on a 768-1024 px viewport could spill into the next column.
*
* Switching every potentially-long cell to a hover tooltip means
* the visible row stays inside the card no matter what the data
* looks like. The native `title` attribute is a graceful fallback
* for screen readers and right-click → "view full name" on desktop;
* the radix tooltip handles hover and keyboard focus.
*/
function TruncatedCell({
value,
className,
}: {
value: string;
className?: string;
}) {
return (
{value}
{value}
);
}
/**
* F-18 (v1.4.19): the auto-login flow names tokens
* "web auto-login 2026-05-05T19:46:20.603Z" / "iOS auto-login …" by
* suffixing the issuing call's `Date.now().toISOString()`. That suffix
* makes the admin list look like debug output. Reformat to
* "iOS auto-login · 05.05.2026 19:46" while leaving any non-ISO suffix
* (manual names, device fingerprints, etc.) untouched.
*/
const TOKEN_NAME_ISO_RE =
/^(.+?)\s+(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z)(.*)$/;
function formatTokenName(name: string): string {
const match = TOKEN_NAME_ISO_RE.exec(name);
if (!match) return name;
const [, prefix, iso, rest] = match;
const d = new Date(iso);
if (Number.isNaN(d.getTime())) return name;
const dd = String(d.getUTCDate()).padStart(2, "0");
const mm = String(d.getUTCMonth() + 1).padStart(2, "0");
const yyyy = d.getUTCFullYear();
const hh = String(d.getUTCHours()).padStart(2, "0");
const mi = String(d.getUTCMinutes()).padStart(2, "0");
const formatted = `${prefix} · ${dd}.${mm}.${yyyy} ${hh}:${mi}`;
return rest && rest.trim() ? `${formatted} ${rest.trim()}` : formatted;
}
export function ApiTokenOverviewSection() {
const { t } = useTranslations();
// v1.4.19 phase A7 — `/admin/api-tokens` is a dedicated single-
// section route. The previous pattern carried a "Collapse / Expand"
// toggle as an escape hatch from the v1.4 shared admin page where
// 13 sections lived together, but on a route that only renders this
// one card the toggle hides the entire surface. Marc reported it
// as "sinnlos" — gone.
const { data: tokens, isLoading } = useQuery({
queryKey: ["admin", "tokens"],
queryFn: async () => {
const res = await fetch("/api/admin/tokens");
if (!res.ok) throw new Error("Failed");
return (await res.json()).data as ApiTokenInfo[];
},
});
return (
{isLoading ? (
) : !tokens?.length ? (
// v1.4.15 phase-C5: empty state explains where tokens come
// from (native client sign-in / Settings → Account) so a
// fresh admin doesn't think they need to create them here.
}
title={t("admin.tokensEmptyTitle")}
description={t("admin.tokensEmptyDescription")}
/>
) : (
<>
{/* Desktop table — verbatim layout for md+. The wrapping
`hidden md:block` swaps it out for the card-list below
on mobile, mirroring the pattern used in
`
`. `table-fixed` + per-column
widths give every cell a hard upper bound so a long
token name cannot expand the row past the card. The
inner `overflow-x-auto` is a belt-and-suspenders guard
for medium viewports with extra-long permission
lists. */}
|
{t("admin.tokenUser")}
|
{t("admin.tokenName")}
|
{t("admin.tokenPermissions")}
|
{t("admin.tokenStatus")}
|
{t("admin.tokenLastUsed")}
|
{t("admin.tokenCreated")}
|
{tokens.map((token, i) => {
const isExpired =
token.expiresAt &&
new Date(token.expiresAt) < new Date();
return (
|
|
|
{token.permissions.map((p) => (
{p === "*" ? t("admin.tokenPermissionAll") : p}
))}
|
{token.revoked ? (
{t("settings.tokenRevoked")}
) : isExpired ? (
{t("settings.tokenExpired")}
) : (
{t("common.active")}
)}
|
{token.lastUsedAt
? formatDateTime(token.lastUsedAt)
: t("admin.tokenNeverUsed")}
|
{formatDate(token.createdAt)}
|
);
})}
{/* Mobile card list — each token renders as a self-
contained card with a stacked meta layout. No
horizontal scroll: every cell wraps within the card
width. Mirrors ``. */}
{tokens.map((token) => {
const isExpired =
token.expiresAt && new Date(token.expiresAt) < new Date();
return (
-
{token.revoked ? (
{t("settings.tokenRevoked")}
) : isExpired ? (
{t("settings.tokenExpired")}
) : (
{t("common.active")}
)}
{token.permissions.map((p) => (
{p === "*" ? t("admin.tokenPermissionAll") : p}
))}
{t("admin.tokenLastUsed")}:{" "}
{token.lastUsedAt
? formatDateTime(token.lastUsedAt)
: t("admin.tokenNeverUsed")}
{t("admin.tokenCreated")}: {formatDate(token.createdAt)}
);
})}
>
)}
);
}