|
| 1 | +import { IconDotsVertical, IconEdit, IconPower, IconTrash } from "@tabler/icons-react"; |
| 2 | +import { createColumnHelper, getCoreRowModel, useReactTable } from "@tanstack/react-table"; |
| 3 | +import { useMemo } from "react"; |
| 4 | +import type { Certificate } from "src/api/backend"; |
| 5 | +import { DomainsFormatter, GravatarFormatter } from "src/components"; |
| 6 | +import { TableLayout } from "src/components/Table/TableLayout"; |
| 7 | +import { intl } from "src/locale"; |
| 8 | +import Empty from "./Empty"; |
| 9 | + |
| 10 | +interface Props { |
| 11 | + data: Certificate[]; |
| 12 | + isFetching?: boolean; |
| 13 | +} |
| 14 | +export default function Table({ data, isFetching }: Props) { |
| 15 | + const columnHelper = createColumnHelper<Certificate>(); |
| 16 | + const columns = useMemo( |
| 17 | + () => [ |
| 18 | + columnHelper.accessor((row: any) => row.owner, { |
| 19 | + id: "owner", |
| 20 | + cell: (info: any) => { |
| 21 | + const value = info.getValue(); |
| 22 | + return <GravatarFormatter url={value.avatar} name={value.name} />; |
| 23 | + }, |
| 24 | + meta: { |
| 25 | + className: "w-1", |
| 26 | + }, |
| 27 | + }), |
| 28 | + columnHelper.accessor((row: any) => row, { |
| 29 | + id: "domainNames", |
| 30 | + header: intl.formatMessage({ id: "column.name" }), |
| 31 | + cell: (info: any) => { |
| 32 | + const value = info.getValue(); |
| 33 | + return <DomainsFormatter domains={value.domainNames} createdOn={value.createdOn} />; |
| 34 | + }, |
| 35 | + }), |
| 36 | + columnHelper.accessor((row: any) => row.provider, { |
| 37 | + id: "provider", |
| 38 | + header: intl.formatMessage({ id: "column.provider" }), |
| 39 | + cell: (info: any) => { |
| 40 | + return info.getValue(); |
| 41 | + }, |
| 42 | + }), |
| 43 | + columnHelper.accessor((row: any) => row.expires_on, { |
| 44 | + id: "expires_on", |
| 45 | + header: intl.formatMessage({ id: "column.expires" }), |
| 46 | + cell: (info: any) => { |
| 47 | + return info.getValue(); |
| 48 | + }, |
| 49 | + }), |
| 50 | + columnHelper.accessor((row: any) => row, { |
| 51 | + id: "id", |
| 52 | + header: intl.formatMessage({ id: "column.status" }), |
| 53 | + cell: (info: any) => { |
| 54 | + return info.getValue(); |
| 55 | + }, |
| 56 | + }), |
| 57 | + columnHelper.display({ |
| 58 | + id: "id", // todo: not needed for a display? |
| 59 | + cell: (info: any) => { |
| 60 | + return ( |
| 61 | + <span className="dropdown"> |
| 62 | + <button |
| 63 | + type="button" |
| 64 | + className="btn dropdown-toggle btn-action btn-sm px-1" |
| 65 | + data-bs-boundary="viewport" |
| 66 | + data-bs-toggle="dropdown" |
| 67 | + > |
| 68 | + <IconDotsVertical /> |
| 69 | + </button> |
| 70 | + <div className="dropdown-menu dropdown-menu-end"> |
| 71 | + <span className="dropdown-header"> |
| 72 | + {intl.formatMessage( |
| 73 | + { |
| 74 | + id: "certificates.actions-title", |
| 75 | + }, |
| 76 | + { id: info.row.original.id }, |
| 77 | + )} |
| 78 | + </span> |
| 79 | + <a className="dropdown-item" href="#"> |
| 80 | + <IconEdit size={16} /> |
| 81 | + {intl.formatMessage({ id: "action.edit" })} |
| 82 | + </a> |
| 83 | + <a className="dropdown-item" href="#"> |
| 84 | + <IconPower size={16} /> |
| 85 | + {intl.formatMessage({ id: "action.disable" })} |
| 86 | + </a> |
| 87 | + <div className="dropdown-divider" /> |
| 88 | + <a className="dropdown-item" href="#"> |
| 89 | + <IconTrash size={16} /> |
| 90 | + {intl.formatMessage({ id: "action.delete" })} |
| 91 | + </a> |
| 92 | + </div> |
| 93 | + </span> |
| 94 | + ); |
| 95 | + }, |
| 96 | + meta: { |
| 97 | + className: "text-end w-1", |
| 98 | + }, |
| 99 | + }), |
| 100 | + ], |
| 101 | + [columnHelper], |
| 102 | + ); |
| 103 | + |
| 104 | + const tableInstance = useReactTable<Certificate>({ |
| 105 | + columns, |
| 106 | + data, |
| 107 | + getCoreRowModel: getCoreRowModel(), |
| 108 | + rowCount: data.length, |
| 109 | + meta: { |
| 110 | + isFetching, |
| 111 | + }, |
| 112 | + enableSortingRemoval: false, |
| 113 | + }); |
| 114 | + |
| 115 | + return <TableLayout tableInstance={tableInstance} emptyState={<Empty tableInstance={tableInstance} />} />; |
| 116 | +} |
0 commit comments