|
| 1 | +import { Img } from "@/components/blocks/Img"; |
| 2 | +import { Button } from "@/components/ui/button"; |
| 3 | +import { |
| 4 | + Table, |
| 5 | + TableBody, |
| 6 | + TableCell, |
| 7 | + TableContainer, |
| 8 | + TableHead, |
| 9 | + TableHeader, |
| 10 | + TableRow, |
| 11 | +} from "@/components/ui/table"; |
| 12 | +import { ToolTipLabel } from "@/components/ui/tooltip"; |
| 13 | +import { TrackedLinkTW } from "@/components/ui/tracked-link"; |
| 14 | +import { replaceDeployerAddress } from "components/explore/publisher"; |
| 15 | +import { useTrack } from "hooks/analytics/useTrack"; |
| 16 | +import { replaceIpfsUrl } from "lib/sdk"; |
| 17 | +import { ShieldCheckIcon } from "lucide-react"; |
| 18 | +import Link from "next/link"; |
| 19 | +import { useMemo } from "react"; |
| 20 | +import { type Column, type Row, useTable } from "react-table"; |
| 21 | +import type { PublishedContractDetails } from "../../../../../components/contract-components/hooks"; |
| 22 | + |
| 23 | +interface PublishedContractTableProps { |
| 24 | + contractDetails: ContractDataInput[]; |
| 25 | + footer?: React.ReactNode; |
| 26 | + publisherEnsName: string | undefined; |
| 27 | +} |
| 28 | + |
| 29 | +type ContractDataInput = PublishedContractDetails; |
| 30 | +type ContractDataRow = ContractDataInput["metadata"] & { |
| 31 | + id: string; |
| 32 | +}; |
| 33 | + |
| 34 | +function convertContractDataToRowData( |
| 35 | + input: ContractDataInput, |
| 36 | +): ContractDataRow { |
| 37 | + return { |
| 38 | + id: input.contractId, |
| 39 | + ...input.metadata, |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +export function PublishedContractTable(props: PublishedContractTableProps) { |
| 44 | + const { contractDetails, footer, publisherEnsName } = props; |
| 45 | + const trackEvent = useTrack(); |
| 46 | + const rows = useMemo( |
| 47 | + () => contractDetails.map(convertContractDataToRowData), |
| 48 | + [contractDetails], |
| 49 | + ); |
| 50 | + |
| 51 | + const tableColumns: Column<ContractDataRow>[] = useMemo(() => { |
| 52 | + const cols: Column<ContractDataRow>[] = [ |
| 53 | + { |
| 54 | + Header: "Logo", |
| 55 | + accessor: (row) => row.logo, |
| 56 | + // biome-ignore lint/suspicious/noExplicitAny: <explanation> |
| 57 | + Cell: (cell: any) => ( |
| 58 | + <Img |
| 59 | + alt="" |
| 60 | + src={cell.value ? replaceIpfsUrl(cell.value) : ""} |
| 61 | + fallback={ |
| 62 | + <div className="size-8 rounded-full border border-border bg-muted" /> |
| 63 | + } |
| 64 | + className="size-8" |
| 65 | + /> |
| 66 | + ), |
| 67 | + }, |
| 68 | + { |
| 69 | + Header: "Name", |
| 70 | + accessor: (row) => row.name, |
| 71 | + // biome-ignore lint/suspicious/noExplicitAny: FIXME |
| 72 | + Cell: (cell: any) => { |
| 73 | + return ( |
| 74 | + <Link |
| 75 | + href={replaceDeployerAddress( |
| 76 | + `/${publisherEnsName || cell.row.original.publisher}/${cell.row.original.id}`, |
| 77 | + )} |
| 78 | + className="whitespace-nowrap text-foreground before:absolute before:inset-0" |
| 79 | + > |
| 80 | + {cell.value} |
| 81 | + </Link> |
| 82 | + ); |
| 83 | + }, |
| 84 | + }, |
| 85 | + { |
| 86 | + Header: "Description", |
| 87 | + accessor: (row) => row.description, |
| 88 | + // biome-ignore lint/suspicious/noExplicitAny: FIXME |
| 89 | + Cell: (cell: any) => ( |
| 90 | + <span className="line-clamp-2 text-muted-foreground"> |
| 91 | + {cell.value} |
| 92 | + </span> |
| 93 | + ), |
| 94 | + }, |
| 95 | + { |
| 96 | + Header: "Version", |
| 97 | + accessor: (row) => row.version, |
| 98 | + // biome-ignore lint/suspicious/noExplicitAny: FIXME |
| 99 | + Cell: (cell: any) => ( |
| 100 | + <span className="text-muted-foreground">{cell.value}</span> |
| 101 | + ), |
| 102 | + }, |
| 103 | + { |
| 104 | + id: "audit-badge", |
| 105 | + accessor: (row) => ({ audit: row.audit }), |
| 106 | + // biome-ignore lint/suspicious/noExplicitAny: FIXME |
| 107 | + Cell: (cell: any) => ( |
| 108 | + <span className="flex items-center gap-2"> |
| 109 | + {cell.value.audit ? ( |
| 110 | + <ToolTipLabel label="View Contract Audit"> |
| 111 | + <Button |
| 112 | + asChild |
| 113 | + variant="ghost" |
| 114 | + className="relative z-10 h-auto w-auto p-2" |
| 115 | + > |
| 116 | + <TrackedLinkTW |
| 117 | + href={replaceIpfsUrl(cell.value.audit)} |
| 118 | + category="deploy" |
| 119 | + label="audited" |
| 120 | + aria-label="View Contract Audit" |
| 121 | + target="_blank" |
| 122 | + onClick={(e) => { |
| 123 | + e.stopPropagation(); |
| 124 | + trackEvent({ |
| 125 | + category: "visit-audit", |
| 126 | + action: "click", |
| 127 | + label: cell.value.audit, |
| 128 | + }); |
| 129 | + }} |
| 130 | + > |
| 131 | + <ShieldCheckIcon className="size-5 text-success-text" /> |
| 132 | + </TrackedLinkTW> |
| 133 | + </Button> |
| 134 | + </ToolTipLabel> |
| 135 | + ) : null} |
| 136 | + </span> |
| 137 | + ), |
| 138 | + }, |
| 139 | + ]; |
| 140 | + |
| 141 | + return cols; |
| 142 | + }, [trackEvent, publisherEnsName]); |
| 143 | + |
| 144 | + const tableInstance = useTable({ |
| 145 | + columns: tableColumns, |
| 146 | + data: rows, |
| 147 | + }); |
| 148 | + |
| 149 | + return ( |
| 150 | + <TableContainer> |
| 151 | + <Table {...tableInstance.getTableProps()}> |
| 152 | + <TableHeader> |
| 153 | + {tableInstance.headerGroups.map((headerGroup) => { |
| 154 | + const { key, ...rowProps } = headerGroup.getHeaderGroupProps(); |
| 155 | + return ( |
| 156 | + <TableRow {...rowProps} key={key}> |
| 157 | + {headerGroup.headers.map((column, columnIndex) => ( |
| 158 | + <TableHead |
| 159 | + {...column.getHeaderProps()} |
| 160 | + // biome-ignore lint/suspicious/noArrayIndexKey: FIXME |
| 161 | + key={columnIndex} |
| 162 | + > |
| 163 | + <span className="text-muted-foreground"> |
| 164 | + {column.render("Header")} |
| 165 | + </span> |
| 166 | + </TableHead> |
| 167 | + ))} |
| 168 | + </TableRow> |
| 169 | + ); |
| 170 | + })} |
| 171 | + </TableHeader> |
| 172 | + |
| 173 | + <TableBody {...tableInstance.getTableBodyProps()} className="relative"> |
| 174 | + {tableInstance.rows.map((row) => { |
| 175 | + tableInstance.prepareRow(row); |
| 176 | + return <ContractTableRow row={row} key={row.getRowProps().key} />; |
| 177 | + })} |
| 178 | + </TableBody> |
| 179 | + </Table> |
| 180 | + {footer} |
| 181 | + </TableContainer> |
| 182 | + ); |
| 183 | +} |
| 184 | + |
| 185 | +function ContractTableRow(props: { |
| 186 | + row: Row<ContractDataRow>; |
| 187 | +}) { |
| 188 | + const { row } = props; |
| 189 | + const { key, ...rowProps } = row.getRowProps(); |
| 190 | + return ( |
| 191 | + <> |
| 192 | + <TableRow |
| 193 | + className="relative cursor-pointer hover:bg-muted/50" |
| 194 | + {...rowProps} |
| 195 | + key={key} |
| 196 | + > |
| 197 | + {row.cells.map((cell) => ( |
| 198 | + <TableCell {...cell.getCellProps()} key={cell.getCellProps().key}> |
| 199 | + {cell.render("Cell")} |
| 200 | + </TableCell> |
| 201 | + ))} |
| 202 | + </TableRow> |
| 203 | + </> |
| 204 | + ); |
| 205 | +} |
0 commit comments