Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/components/app/events/LogsTable.tsx
T

117 lines
4.0 KiB
TypeScript

import { useTranslation } from "react-i18next";
import { useMemo } from "react";
import type { Log } from "@toolctrl/db/prisma-browser";
import { LogWithDetails } from "~/utils/db/logs.db.server";
import { PaginationDto } from "~/application/dtos/data/PaginationDto";
import TableSimple from "~/components/ui/tables/TableSimple";
import { RowHeaderDisplayDto } from "~/application/dtos/data/RowHeaderDisplayDto";
import { Link } from "react-router";
import LogDetailsButton from "./LogDetailsButton";
import { absoluteAppPath } from "~/utils/app/CrossHostUrls";
import { useRootData } from "~/utils/data/useRootData";
import { createLocaleFormatters } from "~/locale/format";
import { getLogActionI18nKey } from "./logActionI18n";
export type LogsTableItem = Log & {
user?: LogWithDetails["user"] | { id: string; email: string; firstName?: string | null; lastName?: string | null } | null;
apiKey?: LogWithDetails["apiKey"] | { id: string; alias: string } | null;
tenant?: LogWithDetails["tenant"] | { id: string; name: string; slug?: string | null } | null;
comment?: LogWithDetails["comment"] | { id: string; value: string } | null;
};
interface Props {
items: LogsTableItem[];
withTenant: boolean;
pagination: PaginationDto;
}
export default function LogsTable({ withTenant, items, pagination }: Props) {
const { i18n, t } = useTranslation();
const formatters = useMemo(() => createLocaleFormatters(i18n.language), [i18n.language]);
const rootData = useRootData();
const headers = useMemo(() => {
let headers: RowHeaderDisplayDto<LogsTableItem>[] = [];
if (withTenant) {
headers.push({
name: "tenant",
title: t("models.tenant.object"),
value: (item) => (
<div>
{item.tenant ? (
<Link
to={absoluteAppPath(rootData.routing, item.tenant.slug ?? item.tenant.id)}
className="focus:bg-secondary/90 hover:border-border rounded-md border-b border-dashed border-transparent"
>
{item.tenant.name}
</Link>
) : (
<div>-</div>
)}
</div>
),
breakpoint: "sm",
});
}
headers = [
...headers,
{
name: "action",
title: t("models.log.action"),
value: (item) => (
<div>
{t(getLogActionI18nKey(item.action) ?? item.action, {
defaultValue: item.action,
})}
</div>
),
},
{
name: "url",
title: t("models.log.url"),
value: (item) => <div>{item.url}</div>,
},
{
name: "details",
title: t("models.log.details"),
value: (item) => <LogDetailsButton item={item as LogWithDetails} />,
},
{
name: "createdBy",
title: t("shared.createdBy"),
value: (item) => (
<div>
{item.user && (
<span>
{item.user.firstName} {item.user.lastName} <span className="text-muted-foreground text-xs">({item.user.email})</span>
</span>
)}
{item.apiKey && (
<span>
{t("models.apiKey.object")} <span className="text-muted-foreground text-xs">({item.apiKey.alias})</span>
</span>
)}
</div>
),
},
{
name: "createdAt",
title: t("shared.createdAt"),
value: (item) => formatters.relativeDate(item.createdAt),
formattedValue: (item) => (
<div className="flex flex-col">
<div>{formatters.date(item.createdAt, { dateStyle: "medium" })}</div>
<div className="text-xs">{formatters.relativeDate(item.createdAt)}</div>
</div>
),
className: "text-muted-foreground text-xs",
breakpoint: "sm",
sortable: true,
},
];
return headers;
}, [formatters, rootData.routing, t, withTenant]);
return <TableSimple items={items} headers={headers} pagination={pagination} className={() => "text-sm"} />;
}