198 lines
8.4 KiB
TypeScript
198 lines
8.4 KiB
TypeScript
// "API-Aufrufe" on the list chrome the rest of the product wears: the shared
|
|
// facet filter bar, a ~/ui Table and the same server-side pagination footer the
|
|
// audit viewer uses.
|
|
//
|
|
// It replaces the boilerplate TableSimple/InputFilters/InputSearchWithURL stack
|
|
// for the tenant tab only — the admin log route keeps ApiKeyLogsDetails, which
|
|
// also carries row selection and bulk delete. Filtering is server-side, so every
|
|
// interaction is reported to the route, which turns it into query parameters.
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
import type { FilterablePropertyDto } from "~/application/dtos/data/FilterablePropertyDto";
|
|
import type { PaginationDto } from "~/application/dtos/data/PaginationDto";
|
|
import type { ApiKeyLogDto } from "~/modules/api/dtos/ApiKeyLogDto";
|
|
import { useLocaleFormatters } from "~/locale/format";
|
|
import { BadgeFeedback } from "~/ui/components/BadgeFeedback";
|
|
import { Button } from "~/ui/components/Button";
|
|
import { EmptyState } from "~/ui/components/EmptyState";
|
|
import { Table } from "~/ui/components/Table";
|
|
import { ApiFilterBar } from "./ApiFilterBar";
|
|
|
|
export type ApiLogsActions = {
|
|
onSearchChange: (value: string) => void;
|
|
onToggleProperty: (name: string, value: string) => void;
|
|
onReset: () => void;
|
|
onPage: (page: number) => void;
|
|
};
|
|
|
|
type BadgeTone = React.ComponentProps<typeof BadgeFeedback>["farbe"];
|
|
|
|
/** Same buckets the old SimpleBadge used, mapped onto the kit's feedback tones. */
|
|
export function methodTone(method: string): BadgeTone {
|
|
if (method === "POST") return "subtle-positive";
|
|
if (method === "GET") return "subtle-information";
|
|
if (method === "PUT" || method === "PATCH") return "subtle-warning";
|
|
if (method === "DELETE") return "subtle-negative";
|
|
return "subtle-neutral";
|
|
}
|
|
|
|
export function statusTone(status: number | null): BadgeTone {
|
|
if (status === null) return "subtle-warning";
|
|
if (status >= 200 && status < 300) return "subtle-positive";
|
|
if (status >= 300 && status < 400) return "subtle-warning";
|
|
if (status >= 400) return "subtle-negative";
|
|
return "subtle-neutral";
|
|
}
|
|
|
|
/** Duration buckets of the old ApiCallSpeedBadge, unchanged. */
|
|
export function speedLabelKey(duration: number): string {
|
|
if (duration < 10) return "metrics.speed.lightningFast";
|
|
if (duration < 100) return "metrics.speed.fast";
|
|
if (duration < 1000) return "metrics.speed.average";
|
|
if (duration < 2000) return "metrics.speed.slow";
|
|
return "metrics.speed.verySlow";
|
|
}
|
|
|
|
export function speedTone(duration: number): BadgeTone {
|
|
if (duration < 100) return "subtle-positive";
|
|
if (duration < 1000) return "subtle-warning";
|
|
return "subtle-negative";
|
|
}
|
|
|
|
const CELL_CLASS = "text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-secondary";
|
|
const MUTED_CLASS = "text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-tertiary";
|
|
|
|
export function ApiLogsPage({
|
|
items,
|
|
properties,
|
|
pagination,
|
|
search,
|
|
actions,
|
|
}: {
|
|
items: readonly ApiKeyLogDto[];
|
|
properties: readonly FilterablePropertyDto[];
|
|
pagination: PaginationDto;
|
|
search: string;
|
|
actions: ApiLogsActions;
|
|
}): React.JSX.Element {
|
|
const { t } = useTranslation();
|
|
const formatters = useLocaleFormatters();
|
|
|
|
const duration = (value: number | null) =>
|
|
value === null ? (
|
|
<span className={MUTED_CLASS}>{t("shared.notSet")}</span>
|
|
) : (
|
|
<span className={CELL_CLASS}>{formatters.number(Number(value), { style: "unit", unit: "millisecond", unitDisplay: "short", maximumFractionDigits: 3 })}</span>
|
|
);
|
|
|
|
return (
|
|
<div className="flex w-full flex-col gap-4" data-ui-id="ui.settings.apiLogs">
|
|
<ApiFilterBar
|
|
uiId="ui.settings.apiLogs.filter"
|
|
properties={properties}
|
|
search={search}
|
|
onSearchChange={actions.onSearchChange}
|
|
onToggleProperty={actions.onToggleProperty}
|
|
onReset={actions.onReset}
|
|
/>
|
|
|
|
{items.length === 0 ? (
|
|
<div data-ui-id="ui.settings.apiLogs.empty">
|
|
<EmptyState outlined title={t("models.apiKeyLog.empty.title")} body={t("models.apiKeyLog.empty.description")} />
|
|
</div>
|
|
) : (
|
|
<div className="w-full overflow-x-auto" data-ui-id="ui.settings.apiLogs.table">
|
|
<Table
|
|
header={
|
|
<Table.HeaderRow>
|
|
<Table.HeaderCell>{t("shared.createdAt")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("models.apiKey.alias")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("models.apiKeyLog.ip")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("models.apiKeyLog.endpoint")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("models.apiKeyLog.method")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("models.apiKeyLog.status")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("models.apiKeyLog.duration")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("models.apiKeyLog.speed")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("models.apiKeyLog.error")}</Table.HeaderCell>
|
|
</Table.HeaderRow>
|
|
}
|
|
>
|
|
{items.map((item) => (
|
|
<Table.Row key={item.id} data-row-id={item.id}>
|
|
<Table.Cell>
|
|
<span className={CELL_CLASS}>
|
|
<time dateTime={new Date(item.createdAt).toISOString()}>{formatters.dateTime(item.createdAt)}</time>
|
|
</span>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
<span className={CELL_CLASS}>{item.apiKey?.alias ?? t("shared.notSet")}</span>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
<span className={item.ip.length > 0 ? CELL_CLASS : MUTED_CLASS}>{item.ip.length > 0 ? item.ip : t("shared.notSet")}</span>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
<span className="font-mono text-body-small-regular-0 text-text-neutral-primary">{item.endpoint}</span>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
<BadgeFeedback farbe={methodTone(item.method)} size="small">
|
|
{item.method}
|
|
</BadgeFeedback>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
{item.status === null ? (
|
|
<span className={MUTED_CLASS}>{t("shared.notSet")}</span>
|
|
) : (
|
|
<BadgeFeedback farbe={statusTone(item.status)} size="small">
|
|
{String(item.status)}
|
|
</BadgeFeedback>
|
|
)}
|
|
</Table.Cell>
|
|
<Table.Cell>{duration(item.duration)}</Table.Cell>
|
|
<Table.Cell>
|
|
{item.duration === null ? (
|
|
<span className={MUTED_CLASS}>{t("shared.notSet")}</span>
|
|
) : (
|
|
<BadgeFeedback farbe={speedTone(Number(item.duration))} size="small">
|
|
{t(speedLabelKey(Number(item.duration)))}
|
|
</BadgeFeedback>
|
|
)}
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
{/* An empty error column is the normal case — a dash reads
|
|
quieter there than a spelled-out "not set". */}
|
|
<span className={item.error ? "text-body-small-regular-0 font-body-small-regular-0 text-text-feedback-negative" : MUTED_CLASS}>
|
|
{item.error || "—"}
|
|
</span>
|
|
</Table.Cell>
|
|
</Table.Row>
|
|
))}
|
|
</Table>
|
|
</div>
|
|
)}
|
|
|
|
{/* Server-side pagination, same shape as the audit viewer's footer. */}
|
|
<div className="flex items-center justify-between" data-ui-id="ui.settings.apiLogs.pagination">
|
|
<span className={CELL_CLASS}>
|
|
{t("shared.pageInfo", {
|
|
page: pagination.page,
|
|
totalPages: Math.max(pagination.totalPages, 1),
|
|
total: formatters.number(pagination.totalItems),
|
|
})}
|
|
</span>
|
|
<div className="flex gap-2">
|
|
{pagination.page > 1 ? (
|
|
<Button variant="tertiary" onClick={() => actions.onPage(pagination.page - 1)} data-ui-id="ui.settings.apiLogs.prev">
|
|
{t("shared.previous")}
|
|
</Button>
|
|
) : null}
|
|
{pagination.page < pagination.totalPages ? (
|
|
<Button variant="tertiary" onClick={() => actions.onPage(pagination.page + 1)} data-ui-id="ui.settings.apiLogs.next">
|
|
{t("shared.next")}
|
|
</Button>
|
|
) : null}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|