Table.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { createColumnHelper, getCoreRowModel, useReactTable } from "@tanstack/react-table";
  2. import { useMemo } from "react";
  3. import type { AuditLog } from "src/api/backend";
  4. import { EventFormatter, GravatarFormatter } from "src/components";
  5. import { TableLayout } from "src/components/Table/TableLayout";
  6. import { intl, T } from "src/locale";
  7. interface Props {
  8. data: AuditLog[];
  9. isFetching?: boolean;
  10. onSelectItem?: (id: number) => void;
  11. }
  12. export default function Table({ data, isFetching, onSelectItem }: Props) {
  13. const columnHelper = createColumnHelper<AuditLog>();
  14. const columns = useMemo(
  15. () => [
  16. columnHelper.accessor((row: AuditLog) => row.user, {
  17. id: "user.avatar",
  18. cell: (info: any) => {
  19. const value = info.getValue();
  20. return <GravatarFormatter url={value.avatar} name={value.name} />;
  21. },
  22. meta: {
  23. className: "w-1",
  24. },
  25. }),
  26. columnHelper.accessor((row: AuditLog) => row.user?.name, {
  27. id: "user.name",
  28. header: intl.formatMessage({ id: "column.name" }),
  29. }),
  30. columnHelper.accessor((row: AuditLog) => row, {
  31. id: "objectType",
  32. header: intl.formatMessage({ id: "column.event" }),
  33. cell: (info: any) => {
  34. return <EventFormatter row={info.getValue()} />;
  35. },
  36. }),
  37. columnHelper.display({
  38. id: "id",
  39. cell: (info: any) => {
  40. return (
  41. <button
  42. type="button"
  43. className="btn btn-action btn-sm px-1"
  44. onClick={(e) => {
  45. e.preventDefault();
  46. onSelectItem?.(info.row.original.id);
  47. }}
  48. >
  49. <T id="action.view-details" />
  50. </button>
  51. );
  52. },
  53. meta: {
  54. className: "text-end w-1",
  55. },
  56. }),
  57. ],
  58. [columnHelper, onSelectItem],
  59. );
  60. const tableInstance = useReactTable<AuditLog>({
  61. columns,
  62. data,
  63. getCoreRowModel: getCoreRowModel(),
  64. rowCount: data.length,
  65. meta: {
  66. isFetching,
  67. },
  68. enableSortingRemoval: false,
  69. });
  70. return <TableLayout tableInstance={tableInstance} />;
  71. }