Table.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 ? value.avatar : ""} name={value ? value.name : ""} />;
  21. },
  22. meta: {
  23. className: "w-1",
  24. },
  25. }),
  26. columnHelper.accessor((row: AuditLog) => row, {
  27. id: "objectType",
  28. header: intl.formatMessage({ id: "column.event" }),
  29. cell: (info: any) => {
  30. return <EventFormatter row={info.getValue()} />;
  31. },
  32. }),
  33. columnHelper.display({
  34. id: "id",
  35. cell: (info: any) => {
  36. return (
  37. <button
  38. type="button"
  39. className="btn btn-action btn-sm px-1"
  40. onClick={(e) => {
  41. e.preventDefault();
  42. onSelectItem?.(info.row.original.id);
  43. }}
  44. >
  45. <T id="action.view-details" />
  46. </button>
  47. );
  48. },
  49. meta: {
  50. className: "text-end w-1",
  51. },
  52. }),
  53. ],
  54. [columnHelper, onSelectItem],
  55. );
  56. const tableInstance = useReactTable<AuditLog>({
  57. columns,
  58. data,
  59. getCoreRowModel: getCoreRowModel(),
  60. rowCount: data.length,
  61. meta: {
  62. isFetching,
  63. },
  64. enableSortingRemoval: false,
  65. });
  66. return <TableLayout tableInstance={tableInstance} />;
  67. }