import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import useTrans from "@/hooks/use-translations";
import { Model } from "@/back/types/global";
import { ReactNode } from "react";
interface DetailsTableProps {
columns: {
accessorKey: keyof Model;
header: string;
// eslint-disable-next-line no-unused-vars
cell?: (params: { value: unknown }) => ReactNode;
}[];
model: Model;
forceRenderValue?: boolean;
}
function DetailsTable({
columns,
model,
forceRenderValue = false,
}: DetailsTableProps) {
const tableNotKnownTrans = useTrans("back.table_not_known");
return (
{useTrans("back.table_field")}
{useTrans("back.table_value")}
{columns.map(({ accessorKey, header, cell }) => {
const rawValue = model[accessorKey];
const renderValue = (): ReactNode => {
if (rawValue === null || rawValue === undefined) {
return (
{tableNotKnownTrans}
);
}
if (forceRenderValue && cell) {
return cell({ value: rawValue });
}
if (cell) {
return cell({ value: rawValue });
}
if (
typeof rawValue === "string" ||
typeof rawValue === "number" ||
typeof rawValue === "boolean"
) {
return String(rawValue);
}
if (rawValue instanceof Date) {
return rawValue.toLocaleString();
}
return JSON.stringify(rawValue);
};
return (
{header}
{renderValue()}
);
})}
);
}
export default DetailsTable;