|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { |
| 4 | + type ColumnDef, |
| 5 | + type ColumnFiltersState, |
| 6 | + type SortingState, |
| 7 | + type VisibilityState, |
| 8 | + getCoreRowModel, |
| 9 | + getFacetedRowModel, |
| 10 | + getFacetedUniqueValues, |
| 11 | + getFilteredRowModel, |
| 12 | + getPaginationRowModel, |
| 13 | + getSortedRowModel, |
| 14 | + useReactTable, |
| 15 | +} from "@tanstack/react-table"; |
| 16 | +import * as React from "react"; |
| 17 | + |
| 18 | +import { dayjsExt } from "@/common/dayjs"; |
| 19 | +import { Checkbox } from "@/components/ui/checkbox"; |
| 20 | + |
| 21 | +import { Avatar, AvatarImage } from "@/components/ui/avatar"; |
| 22 | +import { DataTable } from "@/components/ui/data-table/data-table"; |
| 23 | +import { DataTableBody } from "@/components/ui/data-table/data-table-body"; |
| 24 | +import { DataTableContent } from "@/components/ui/data-table/data-table-content"; |
| 25 | +import { DataTableHeader } from "@/components/ui/data-table/data-table-header"; |
| 26 | +import { DataTablePagination } from "@/components/ui/data-table/data-table-pagination"; |
| 27 | +import type { RouterOutputs } from "@/trpc/shared"; |
| 28 | + |
| 29 | +import { Button } from "@/components/ui/button"; |
| 30 | +import { SortButton } from "@/components/ui/data-table/data-table-buttons"; |
| 31 | +import { |
| 32 | + DropdownMenuContent, |
| 33 | + DropdownMenuItem, |
| 34 | + DropdownMenuLabel, |
| 35 | + DropdownMenuSeparator, |
| 36 | +} from "@/components/ui/dropdown-menu"; |
| 37 | +import { formatCurrency, formatNumber } from "@/lib/utils"; |
| 38 | +import { getPresignedGetUrl } from "@/server/file-uploads"; |
| 39 | +import { api } from "@/trpc/react"; |
| 40 | +import { |
| 41 | + DropdownMenu, |
| 42 | + DropdownMenuTrigger, |
| 43 | +} from "@radix-ui/react-dropdown-menu"; |
| 44 | +import { RiFileDownloadLine, RiMoreLine } from "@remixicon/react"; |
| 45 | +import { useRouter } from "next/navigation"; |
| 46 | +import { toast } from "sonner"; |
| 47 | +import { ShareTableToolbar } from "./share-table-toolbar"; |
| 48 | + |
| 49 | +type Share = RouterOutputs["securities"]["getShares"]["data"]; |
| 50 | + |
| 51 | +type SharesType = { |
| 52 | + shares: Share; |
| 53 | +}; |
| 54 | + |
| 55 | +const humanizeShareStatus = (type: string) => { |
| 56 | + switch (type) { |
| 57 | + case "ACTIVE": |
| 58 | + return "Active"; |
| 59 | + case "DRAFT": |
| 60 | + return "Draft"; |
| 61 | + case "SIGNED": |
| 62 | + return "Signed"; |
| 63 | + case "PENDING": |
| 64 | + return "Pending"; |
| 65 | + default: |
| 66 | + return ""; |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +const StatusColorProvider = (type: string) => { |
| 71 | + switch (type) { |
| 72 | + case "ACTIVE": |
| 73 | + return "bg-green-50 text-green-600 ring-green-600/20"; |
| 74 | + case "DRAFT": |
| 75 | + return "bg-yellow-50 text-yellow-600 ring-yellow-600/20"; |
| 76 | + case "SIGNED": |
| 77 | + return "bg-blue-50 text-blue-600 ring-blue-600/20"; |
| 78 | + case "PENDING": |
| 79 | + return "bg-gray-50 text-gray-600 ring-gray-600/20"; |
| 80 | + default: |
| 81 | + return ""; |
| 82 | + } |
| 83 | +}; |
| 84 | + |
| 85 | +export const columns: ColumnDef<Share[number]>[] = [ |
| 86 | + { |
| 87 | + id: "select", |
| 88 | + header: ({ table }) => ( |
| 89 | + <Checkbox |
| 90 | + checked={ |
| 91 | + table.getIsAllPageRowsSelected() || |
| 92 | + (table.getIsSomePageRowsSelected() && "indeterminate") |
| 93 | + } |
| 94 | + onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)} |
| 95 | + aria-label="Select all" |
| 96 | + /> |
| 97 | + ), |
| 98 | + cell: ({ row }) => ( |
| 99 | + <Checkbox |
| 100 | + checked={row.getIsSelected()} |
| 101 | + onCheckedChange={(value) => row.toggleSelected(!!value)} |
| 102 | + aria-label="Select row" |
| 103 | + /> |
| 104 | + ), |
| 105 | + enableSorting: false, |
| 106 | + enableHiding: false, |
| 107 | + }, |
| 108 | + { |
| 109 | + id: "stakeholderName", |
| 110 | + accessorKey: "stakeholder.name", |
| 111 | + header: ({ column }) => { |
| 112 | + return ( |
| 113 | + <SortButton |
| 114 | + label="Stakeholder" |
| 115 | + onClick={() => column.toggleSorting(column.getIsSorted() === "asc")} |
| 116 | + /> |
| 117 | + ); |
| 118 | + }, |
| 119 | + cell: ({ row }) => ( |
| 120 | + <div className="flex"> |
| 121 | + <Avatar className="rounded-full"> |
| 122 | + <AvatarImage src={"/placeholders/user.svg"} /> |
| 123 | + </Avatar> |
| 124 | + <div className="ml-2 pt-2"> |
| 125 | + <p>{row?.original?.stakeholder?.name}</p> |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + ), |
| 129 | + }, |
| 130 | + { |
| 131 | + id: "status", |
| 132 | + accessorKey: "status", |
| 133 | + header: ({ column }) => ( |
| 134 | + <SortButton |
| 135 | + label="Status" |
| 136 | + onClick={() => column.toggleSorting(column?.getIsSorted() === "asc")} |
| 137 | + /> |
| 138 | + ), |
| 139 | + cell: ({ row }) => { |
| 140 | + const status = row.original?.status; |
| 141 | + return ( |
| 142 | + <span |
| 143 | + className={`inline-flex items-center rounded-md ${StatusColorProvider( |
| 144 | + status, |
| 145 | + )} px-2 py-1 text-xs text-center font-medium ring-1 ring-inset `} |
| 146 | + > |
| 147 | + {humanizeShareStatus(status)} |
| 148 | + </span> |
| 149 | + ); |
| 150 | + }, |
| 151 | + }, |
| 152 | + { |
| 153 | + id: "shareClass", |
| 154 | + accessorKey: "shareClass.classType", |
| 155 | + |
| 156 | + header: ({ column }) => ( |
| 157 | + <SortButton |
| 158 | + label="Share class" |
| 159 | + onClick={() => column.toggleSorting(column.getIsSorted() === "asc")} |
| 160 | + /> |
| 161 | + ), |
| 162 | + cell: ({ row }) => ( |
| 163 | + <div className="text-center">{row.original.shareClass.classType}</div> |
| 164 | + ), |
| 165 | + }, |
| 166 | + { |
| 167 | + id: "quantity", |
| 168 | + accessorKey: "quantity", |
| 169 | + header: ({ column }) => ( |
| 170 | + <div className="flex justify-end"> |
| 171 | + <SortButton |
| 172 | + label="Quantity" |
| 173 | + onClick={() => column.toggleSorting(column.getIsSorted() === "asc")} |
| 174 | + /> |
| 175 | + </div> |
| 176 | + ), |
| 177 | + cell: ({ row }) => { |
| 178 | + const quantity = row.original.quantity; |
| 179 | + return ( |
| 180 | + <div className="text-center"> |
| 181 | + {quantity ? formatNumber(quantity) : null} |
| 182 | + </div> |
| 183 | + ); |
| 184 | + }, |
| 185 | + }, |
| 186 | + { |
| 187 | + id: "pricePerShare", |
| 188 | + accessorKey: "pricePerShare", |
| 189 | + header: ({ column }) => ( |
| 190 | + <div className="flex justify-end"> |
| 191 | + <SortButton |
| 192 | + label="Unit price" |
| 193 | + onClick={() => column.toggleSorting(column.getIsSorted() === "asc")} |
| 194 | + /> |
| 195 | + </div> |
| 196 | + ), |
| 197 | + cell: ({ row }) => { |
| 198 | + const price = row.original.pricePerShare; |
| 199 | + return ( |
| 200 | + <div className="text-center"> |
| 201 | + {price ? formatCurrency(price, "USD") : null} |
| 202 | + </div> |
| 203 | + ); |
| 204 | + }, |
| 205 | + }, |
| 206 | + { |
| 207 | + id: "issueDate", |
| 208 | + accessorKey: "issueDate", |
| 209 | + header: ({ column }) => ( |
| 210 | + <SortButton |
| 211 | + label="Issued" |
| 212 | + onClick={() => column.toggleSorting(column.getIsSorted() === "asc")} |
| 213 | + /> |
| 214 | + ), |
| 215 | + cell: ({ row }) => ( |
| 216 | + <div className="text-center"> |
| 217 | + {dayjsExt(row.original.issueDate).format("DD/MM/YYYY")} |
| 218 | + </div> |
| 219 | + ), |
| 220 | + }, |
| 221 | + { |
| 222 | + id: "boardApprovalDate", |
| 223 | + accessorKey: "boardApprovalDate", |
| 224 | + header: ({ column }) => ( |
| 225 | + <div className="flex justify-end"> |
| 226 | + <SortButton |
| 227 | + label="Board Approved" |
| 228 | + onClick={() => column.toggleSorting(column.getIsSorted() === "asc")} |
| 229 | + /> |
| 230 | + </div> |
| 231 | + ), |
| 232 | + cell: ({ row }) => ( |
| 233 | + <div className="text-center"> |
| 234 | + {dayjsExt(row.original.boardApprovalDate).format("DD/MM/YYYY")} |
| 235 | + </div> |
| 236 | + ), |
| 237 | + }, |
| 238 | + { |
| 239 | + id: "Documents", |
| 240 | + enableHiding: false, |
| 241 | + header: ({ column }) => { |
| 242 | + return ( |
| 243 | + <SortButton |
| 244 | + label="Documents" |
| 245 | + onClick={() => column.toggleSorting(column.getIsSorted() === "asc")} |
| 246 | + /> |
| 247 | + ); |
| 248 | + }, |
| 249 | + cell: ({ row }) => { |
| 250 | + const documents = row?.original?.documents; |
| 251 | + |
| 252 | + const openFileOnTab = async (key: string) => { |
| 253 | + const fileUrl = await getPresignedGetUrl(key); |
| 254 | + window.open(fileUrl.url, "_blank"); |
| 255 | + }; |
| 256 | + |
| 257 | + return ( |
| 258 | + <DropdownMenu> |
| 259 | + <div className="items-end justify-end text-center"> |
| 260 | + <DropdownMenuTrigger className="outline-none" asChild> |
| 261 | + <Button variant={"outline"} className="h-7 w-12 px-2"> |
| 262 | + View |
| 263 | + </Button> |
| 264 | + </DropdownMenuTrigger> |
| 265 | + </div> |
| 266 | + <DropdownMenuContent align="end"> |
| 267 | + <DropdownMenuLabel>Documents</DropdownMenuLabel> |
| 268 | + {documents?.map((doc) => ( |
| 269 | + <DropdownMenuItem |
| 270 | + key={doc.id} |
| 271 | + className="hover:cursor-pointer" |
| 272 | + onClick={async () => { |
| 273 | + await openFileOnTab(doc.bucket.key); |
| 274 | + }} |
| 275 | + > |
| 276 | + <RiFileDownloadLine |
| 277 | + type={doc.bucket.mimeType} |
| 278 | + className="mx-3 cursor-pointer text-muted-foreground hover:text-primary/80" |
| 279 | + /> |
| 280 | + {doc.name.slice(0, 12)} |
| 281 | + <p className="mx-4 rounded-full bg-slate-100 text-xs text-slate-500"> |
| 282 | + {doc?.uploader?.user?.name} |
| 283 | + </p> |
| 284 | + </DropdownMenuItem> |
| 285 | + ))} |
| 286 | + </DropdownMenuContent> |
| 287 | + </DropdownMenu> |
| 288 | + ); |
| 289 | + }, |
| 290 | + }, |
| 291 | + { |
| 292 | + id: "actions", |
| 293 | + enableHiding: false, |
| 294 | + cell: ({ row }) => { |
| 295 | + // eslint-disable-next-line react-hooks/rules-of-hooks |
| 296 | + const router = useRouter(); |
| 297 | + // eslint-disable-next-line react-hooks/rules-of-hooks |
| 298 | + const share = row.original; |
| 299 | + |
| 300 | + const deleteShareMutation = api.securities.deleteShare.useMutation({ |
| 301 | + onSuccess: () => { |
| 302 | + toast.success("🎉 Successfully deleted the stakeholder"); |
| 303 | + router.refresh(); |
| 304 | + }, |
| 305 | + onError: () => { |
| 306 | + toast.error("Failed deleting the share"); |
| 307 | + }, |
| 308 | + }); |
| 309 | + |
| 310 | + const updateAction = "Update Share"; |
| 311 | + const deleteAction = "Delete Share"; |
| 312 | + |
| 313 | + const handleDeleteShare = async () => { |
| 314 | + await deleteShareMutation.mutateAsync({ shareId: share.id }); |
| 315 | + }; |
| 316 | + |
| 317 | + return ( |
| 318 | + <DropdownMenu> |
| 319 | + <div className="items-end justify-end text-right"> |
| 320 | + <DropdownMenuTrigger |
| 321 | + className="border-0 border-white outline-none focus:outline-none" |
| 322 | + asChild |
| 323 | + > |
| 324 | + <Button variant="ghost" className="h-8 w-8 p-0"> |
| 325 | + <> |
| 326 | + <span className="sr-only">Open menu</span> |
| 327 | + <RiMoreLine aria-hidden className="h-4 w-4" /> |
| 328 | + </> |
| 329 | + </Button> |
| 330 | + </DropdownMenuTrigger> |
| 331 | + </div> |
| 332 | + <DropdownMenuContent align="end"> |
| 333 | + <DropdownMenuLabel>Actions</DropdownMenuLabel> |
| 334 | + <DropdownMenuSeparator /> |
| 335 | + <DropdownMenuItem>{updateAction}</DropdownMenuItem> |
| 336 | + <DropdownMenuItem |
| 337 | + onSelect={handleDeleteShare} |
| 338 | + className="text-red-500" |
| 339 | + > |
| 340 | + {deleteAction} |
| 341 | + </DropdownMenuItem> |
| 342 | + </DropdownMenuContent> |
| 343 | + </DropdownMenu> |
| 344 | + ); |
| 345 | + }, |
| 346 | + }, |
| 347 | +]; |
| 348 | + |
| 349 | +const ShareTable = ({ shares }: SharesType) => { |
| 350 | + const [sorting, setSorting] = React.useState<SortingState>([]); |
| 351 | + const [columnFilters, setColumnFilters] = React.useState<ColumnFiltersState>( |
| 352 | + [], |
| 353 | + ); |
| 354 | + const [columnVisibility, setColumnVisibility] = |
| 355 | + React.useState<VisibilityState>({}); |
| 356 | + const [rowSelection, setRowSelection] = React.useState({}); |
| 357 | + |
| 358 | + const table = useReactTable({ |
| 359 | + data: shares ?? [], |
| 360 | + columns: columns, |
| 361 | + enableRowSelection: true, |
| 362 | + onRowSelectionChange: setRowSelection, |
| 363 | + onSortingChange: setSorting, |
| 364 | + onColumnFiltersChange: setColumnFilters, |
| 365 | + onColumnVisibilityChange: setColumnVisibility, |
| 366 | + getCoreRowModel: getCoreRowModel(), |
| 367 | + getFilteredRowModel: getFilteredRowModel(), |
| 368 | + getPaginationRowModel: getPaginationRowModel(), |
| 369 | + getSortedRowModel: getSortedRowModel(), |
| 370 | + getFacetedRowModel: getFacetedRowModel(), |
| 371 | + getFacetedUniqueValues: getFacetedUniqueValues(), |
| 372 | + state: { |
| 373 | + sorting, |
| 374 | + columnFilters, |
| 375 | + columnVisibility, |
| 376 | + rowSelection, |
| 377 | + }, |
| 378 | + }); |
| 379 | + |
| 380 | + return ( |
| 381 | + <div className="w-full p-6"> |
| 382 | + <DataTable table={table}> |
| 383 | + <ShareTableToolbar /> |
| 384 | + <DataTableContent> |
| 385 | + <DataTableHeader /> |
| 386 | + <DataTableBody /> |
| 387 | + </DataTableContent> |
| 388 | + <DataTablePagination /> |
| 389 | + </DataTable> |
| 390 | + </div> |
| 391 | + ); |
| 392 | +}; |
| 393 | + |
| 394 | +export default ShareTable; |
0 commit comments