user-contracts.tsx304 lines, 9.5 KB
| 1 | import { ContractAnalysis } from "@/interfaces/contract.interface"; |
| 2 | import { api } from "@/lib/api"; |
| 3 | import { useQuery } from "@tanstack/react-query"; |
| 4 | import { useState } from "react"; |
| 5 | |
| 6 | import { |
| 7 | ColumnDef, |
| 8 | flexRender, |
| 9 | getCoreRowModel, |
| 10 | getPaginationRowModel, |
| 11 | getSortedRowModel, |
| 12 | SortingState, |
| 13 | useReactTable, |
| 14 | } from "@tanstack/react-table"; |
| 15 | import { Button } from "../ui/button"; |
| 16 | import { Badge } from "../ui/badge"; |
| 17 | import { Card, CardContent, CardHeader, CardTitle } from "../ui/card"; |
| 18 | import { |
| 19 | Table, |
| 20 | TableBody, |
| 21 | TableCell, |
| 22 | TableHead, |
| 23 | TableHeader, |
| 24 | TableRow, |
| 25 | } from "../ui/table"; |
| 26 | import { UploadModal } from "../modals/upload-modal"; |
| 27 | import { cn } from "@/lib/utils"; |
| 28 | import { |
| 29 | DropdownMenu, |
| 30 | DropdownMenuContent, |
| 31 | DropdownMenuItem, |
| 32 | DropdownMenuSeparator, |
| 33 | DropdownMenuTrigger, |
| 34 | } from "../ui/dropdown-menu"; |
| 35 | import { MoreHorizontal } from "lucide-react"; |
| 36 | import Link from "next/link"; |
| 37 | import { |
| 38 | AlertDialog, |
| 39 | AlertDialogAction, |
| 40 | AlertDialogCancel, |
| 41 | AlertDialogContent, |
| 42 | AlertDialogDescription, |
| 43 | AlertDialogFooter, |
| 44 | AlertDialogHeader, |
| 45 | AlertDialogTitle, |
| 46 | AlertDialogTrigger, |
| 47 | } from "@/components/ui/alert-dialog"; |
| 48 | |
| 49 | export default function UserContracts() { |
| 50 | const { data: contracts } = useQuery<ContractAnalysis[]>({ |
| 51 | queryKey: ["user-contracts"], |
| 52 | queryFn: () => fetchUserContracts(), |
| 53 | }); |
| 54 | |
| 55 | const [sorting, setSorting] = useState<SortingState>([]); |
| 56 | const [isUploadModalOpen, setIsUploadModalOpen] = useState(false); |
| 57 | |
| 58 | const contractTypeColors: { [key: string]: string } = { |
| 59 | Employment: "bg-blue-100 text-blue-800 hover:bg-blue-200", |
| 60 | "Non-Disclosure Agreement": |
| 61 | "bg-green-100 text-green-800 hover:bg-green-200", |
| 62 | Sales: "bg-yellow-100 text-yellow-800 hover:bg-yellow-200", |
| 63 | Lease: "bg-emerald-100 text-emerald-800 hover:bg-emerald-200", |
| 64 | Services: "bg-pink-100 text-pink-800 hover:bg-pink-200", |
| 65 | Other: "bg-gray-100 text-gray-800 hover:bg-gray-200", |
| 66 | }; |
| 67 | |
| 68 | const columns: ColumnDef<ContractAnalysis>[] = [ |
| 69 | { |
| 70 | accessorKey: "_id", |
| 71 | header: ({ column }) => { |
| 72 | return <Button variant={"ghost"}>Contract ID</Button>; |
| 73 | }, |
| 74 | cell: ({ row }) => ( |
| 75 | <div className="font-medium">{row.getValue<string>("_id")}</div> |
| 76 | ), |
| 77 | }, |
| 78 | { |
| 79 | accessorKey: "overallScore", |
| 80 | header: ({ column }) => { |
| 81 | return <Button variant={"ghost"}>Overall Score</Button>; |
| 82 | }, |
| 83 | cell: ({ row }) => { |
| 84 | const score = parseFloat(row.getValue("overallScore")); |
| 85 | return ( |
| 86 | <Badge |
| 87 | className="rounded-md" |
| 88 | variant={ |
| 89 | score > 75 ? "success" : score < 50 ? "destructive" : "secondary" |
| 90 | } |
| 91 | > |
| 92 | {score.toFixed(2)} Overall Score |
| 93 | </Badge> |
| 94 | ); |
| 95 | }, |
| 96 | }, |
| 97 | { |
| 98 | accessorKey: "contractType", |
| 99 | header: "Contract Type", |
| 100 | cell: ({ row }) => { |
| 101 | const contractType = row.getValue("contractType") as string; |
| 102 | const colorClass = |
| 103 | contractTypeColors[contractType] || contractTypeColors["Other"]; |
| 104 | return ( |
| 105 | <Badge className={cn("rounded-md", colorClass)}>{contractType}</Badge> |
| 106 | ); |
| 107 | }, |
| 108 | }, |
| 109 | { |
| 110 | id: "actions", |
| 111 | cell: ({ row }) => { |
| 112 | const contract = row.original; |
| 113 | |
| 114 | return ( |
| 115 | <DropdownMenu> |
| 116 | <DropdownMenuTrigger asChild> |
| 117 | <Button variant={"ghost"} className="size-8 p-0"> |
| 118 | <span className="sr-only">Open menu</span> |
| 119 | <MoreHorizontal className="size-4" /> |
| 120 | </Button> |
| 121 | </DropdownMenuTrigger> |
| 122 | <DropdownMenuContent align="end"> |
| 123 | <DropdownMenuItem> |
| 124 | <Link href={`/dashboard/contract/${contract._id}`}> |
| 125 | View Details |
| 126 | </Link> |
| 127 | </DropdownMenuItem> |
| 128 | <DropdownMenuSeparator /> |
| 129 | <AlertDialog> |
| 130 | <AlertDialogTrigger asChild> |
| 131 | <DropdownMenuItem onSelect={(e) => e.preventDefault()}> |
| 132 | <span className="text-destructive">Delete Contract</span> |
| 133 | </DropdownMenuItem> |
| 134 | </AlertDialogTrigger> |
| 135 | <AlertDialogContent> |
| 136 | <AlertDialogHeader> |
| 137 | <AlertDialogTitle> |
| 138 | Are you absolutely sure? |
| 139 | </AlertDialogTitle> |
| 140 | <AlertDialogDescription> |
| 141 | This action cannot be undone. This will permanently delete |
| 142 | your contract and remove your data from our servers. |
| 143 | </AlertDialogDescription> |
| 144 | </AlertDialogHeader> |
| 145 | <AlertDialogFooter> |
| 146 | <AlertDialogCancel>Cancel</AlertDialogCancel> |
| 147 | <AlertDialogAction>Continue</AlertDialogAction> |
| 148 | </AlertDialogFooter> |
| 149 | </AlertDialogContent> |
| 150 | </AlertDialog> |
| 151 | </DropdownMenuContent> |
| 152 | </DropdownMenu> |
| 153 | ); |
| 154 | }, |
| 155 | }, |
| 156 | ]; |
| 157 | |
| 158 | const table = useReactTable({ |
| 159 | data: contracts ?? [], |
| 160 | columns, |
| 161 | getCoreRowModel: getCoreRowModel(), |
| 162 | getPaginationRowModel: getPaginationRowModel(), |
| 163 | onSortingChange: setSorting, |
| 164 | getSortedRowModel: getSortedRowModel(), |
| 165 | state: { |
| 166 | sorting, |
| 167 | }, |
| 168 | }); |
| 169 | |
| 170 | const totalContracts = contracts?.length || 0; |
| 171 | const averageScore = |
| 172 | totalContracts > 0 |
| 173 | ? (contracts?.reduce( |
| 174 | (sum, contract) => sum + (contract.overallScore ?? 0), |
| 175 | 0 |
| 176 | ) ?? 0) / totalContracts |
| 177 | : 0; |
| 178 | |
| 179 | const highRiskContracts = |
| 180 | contracts?.filter((contract) => |
| 181 | contract.risks.some((risk) => risk.severity === "high") |
| 182 | ).length ?? 0; |
| 183 | |
| 184 | return ( |
| 185 | <div className="container mx-auto p-6 space-y-8"> |
| 186 | <div className="flex justify-between items-center"> |
| 187 | <h1 className="text-3xl font-bold">Your Contracts</h1> |
| 188 | <Button onClick={() => setIsUploadModalOpen(true)}>New Contract</Button> |
| 189 | </div> |
| 190 | |
| 191 | <div className="grid grid-cols-1 md:grid-cols-3 gap-6"> |
| 192 | <Card> |
| 193 | <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2"> |
| 194 | <CardTitle className="text-sm font-medium"> |
| 195 | Total Contracts |
| 196 | </CardTitle> |
| 197 | </CardHeader> |
| 198 | <CardContent> |
| 199 | <div className="text-2xl font-bold">{totalContracts}</div> |
| 200 | </CardContent> |
| 201 | </Card> |
| 202 | <Card> |
| 203 | <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2"> |
| 204 | <CardTitle className="text-sm font-medium"> |
| 205 | Total Contracts |
| 206 | </CardTitle> |
| 207 | </CardHeader> |
| 208 | <CardContent> |
| 209 | <div className="text-2xl font-bold">{averageScore.toFixed(2)}</div> |
| 210 | </CardContent> |
| 211 | </Card> |
| 212 | <Card> |
| 213 | <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2"> |
| 214 | <CardTitle className="text-sm font-medium"> |
| 215 | High Risk Contracts |
| 216 | </CardTitle> |
| 217 | </CardHeader> |
| 218 | <CardContent> |
| 219 | <div className="text-2xl font-bold">{highRiskContracts}</div> |
| 220 | </CardContent> |
| 221 | </Card> |
| 222 | </div> |
| 223 | |
| 224 | <div className="rounded-md border"> |
| 225 | <Table> |
| 226 | <TableHeader> |
| 227 | {table.getHeaderGroups().map((headerGroup) => ( |
| 228 | <TableRow key={headerGroup.id}> |
| 229 | {headerGroup.headers.map((header) => { |
| 230 | return ( |
| 231 | <TableHead key={header.id}> |
| 232 | {header.isPlaceholder |
| 233 | ? null |
| 234 | : flexRender( |
| 235 | header.column.columnDef.header, |
| 236 | header.getContext() |
| 237 | )} |
| 238 | </TableHead> |
| 239 | ); |
| 240 | })} |
| 241 | </TableRow> |
| 242 | ))} |
| 243 | </TableHeader> |
| 244 | <TableBody> |
| 245 | {table.getRowModel().rows?.length ? ( |
| 246 | table.getRowModel().rows.map((row) => ( |
| 247 | <TableRow |
| 248 | key={row.id} |
| 249 | data-state={row.getIsSelected() && "selected"} |
| 250 | > |
| 251 | {row.getVisibleCells().map((cell) => ( |
| 252 | <TableCell key={cell.id}> |
| 253 | {flexRender( |
| 254 | cell.column.columnDef.cell, |
| 255 | cell.getContext() |
| 256 | )} |
| 257 | </TableCell> |
| 258 | ))} |
| 259 | </TableRow> |
| 260 | )) |
| 261 | ) : ( |
| 262 | <TableRow> |
| 263 | <TableCell |
| 264 | colSpan={columns.length} |
| 265 | className="h-24 text-center" |
| 266 | > |
| 267 | No results. |
| 268 | </TableCell> |
| 269 | </TableRow> |
| 270 | )} |
| 271 | </TableBody> |
| 272 | </Table> |
| 273 | </div> |
| 274 | <div className="flex items-center justify-end space-x-2 py-4"> |
| 275 | <Button |
| 276 | variant={"outline"} |
| 277 | size={"sm"} |
| 278 | onClick={() => table.setPageIndex(0)} |
| 279 | disabled={!table.getCanPreviousPage()} |
| 280 | > |
| 281 | Previous |
| 282 | </Button> |
| 283 | <Button |
| 284 | variant={"outline"} |
| 285 | size={"sm"} |
| 286 | onClick={() => table.nextPage()} |
| 287 | disabled={!table.getCanNextPage()} |
| 288 | > |
| 289 | Next |
| 290 | </Button> |
| 291 | </div> |
| 292 | <UploadModal |
| 293 | isOpen={isUploadModalOpen} |
| 294 | onClose={() => setIsUploadModalOpen(false)} |
| 295 | onUploadComplete={() => table.reset()} |
| 296 | /> |
| 297 | </div> |
| 298 | ); |
| 299 | } |
| 300 | |
| 301 | async function fetchUserContracts(): Promise<ContractAnalysis[]> { |
| 302 | const response = await api.get("/contracts/user-contracts"); |
| 303 | return response.data; |
| 304 | } |