contract-analysis-results.tsx365 lines, 12 KB
| 1 | import { ContractAnalysis } from "@/interfaces/contract.interface"; |
| 2 | import { ReactNode, useState } from "react"; |
| 3 | import { |
| 4 | Card, |
| 5 | CardContent, |
| 6 | CardDescription, |
| 7 | CardHeader, |
| 8 | CardTitle, |
| 9 | } from "../ui/card"; |
| 10 | import { ArrowDown, ArrowUp, Minus } from "lucide-react"; |
| 11 | import OverallScoreChart from "./chart"; |
| 12 | import { Tabs, TabsContent, TabsList, TabsTrigger } from "../ui/tabs"; |
| 13 | import { motion } from "framer-motion"; |
| 14 | import { Badge } from "../ui/badge"; |
| 15 | import { Button } from "../ui/button"; |
| 16 | import { Accordion, AccordionItem, AccordionTrigger } from "../ui/accordion"; |
| 17 | import { AccordionContent } from "@radix-ui/react-accordion"; |
| 18 | |
| 19 | interface IContractAnalysisResultsProps { |
| 20 | analysisResults: ContractAnalysis; |
| 21 | isActive: boolean; |
| 22 | contractId: string; |
| 23 | onUpgrade: () => void; |
| 24 | } |
| 25 | |
| 26 | export default function ContractAnalysisResults({ |
| 27 | analysisResults, |
| 28 | isActive, |
| 29 | onUpgrade, |
| 30 | }: IContractAnalysisResultsProps) { |
| 31 | const [activeTab, setActiveTab] = useState("summary"); |
| 32 | |
| 33 | if (!analysisResults) { |
| 34 | return <div>No results</div>; |
| 35 | } |
| 36 | |
| 37 | const getScore = () => { |
| 38 | const score = analysisResults.overallScore; //analysisResults.overallScore || |
| 39 | if (score > 70) |
| 40 | return { icon: ArrowUp, color: "text-green-500", text: "Good" }; |
| 41 | if (score < 50) |
| 42 | return { icon: ArrowDown, color: "text-red-500", text: "Bad" }; |
| 43 | return { icon: Minus, color: "text-yellow-500", text: "Average" }; |
| 44 | }; |
| 45 | |
| 46 | const scoreTrend = getScore(); |
| 47 | |
| 48 | const getSeverityColor = (severity: string) => { |
| 49 | switch (severity) { |
| 50 | case "high": |
| 51 | return "bg-red-100 text-red-800"; |
| 52 | case "medium": |
| 53 | return "bg-yellow-100 text-yellow-800"; |
| 54 | case "low": |
| 55 | return "bg-green-100 text-green-800"; |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | const getImpactColor = (impact: string) => { |
| 60 | switch (impact) { |
| 61 | case "high": |
| 62 | return "bg-red-100 text-red-800"; |
| 63 | case "medium": |
| 64 | return "bg-yellow-100 text-yellow-800"; |
| 65 | case "low": |
| 66 | return "bg-green-100 text-green-800"; |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | const renderRisksAndOpportunities = ( |
| 71 | items: Array<{ |
| 72 | risk?: string; |
| 73 | opportunity?: string; |
| 74 | explanation?: string; |
| 75 | severity?: string; |
| 76 | impact?: string; |
| 77 | }>, |
| 78 | type: "risks" | "opportunities" |
| 79 | ) => { |
| 80 | const displayItems = isActive ? items : items.slice(0, 3); |
| 81 | const fakeItems = { |
| 82 | risk: type === "risks" ? "Hidden Risk" : undefined, |
| 83 | opportunity: type === "opportunities" ? "Hidden Opportunity" : undefined, |
| 84 | explanation: "Hidden Explanation", |
| 85 | severity: "low", |
| 86 | impact: "low", |
| 87 | }; |
| 88 | |
| 89 | return ( |
| 90 | <ul className="space-y-4"> |
| 91 | {displayItems.map((item, index) => ( |
| 92 | <motion.li |
| 93 | className="border rounded-lg p-4" |
| 94 | key={index} |
| 95 | initial={{ opacity: 0, y: 20 }} |
| 96 | animate={{ opacity: 1, y: 0 }} |
| 97 | transition={{ duration: 0.3, delay: index * 0.1 }} |
| 98 | > |
| 99 | <div className="flex justify-between items-start mb-2"> |
| 100 | <span className="font-semibold text-lg"> |
| 101 | {type === "risks" ? item.risk : item.opportunity} |
| 102 | </span> |
| 103 | {(item.severity || item.impact) && ( |
| 104 | <Badge |
| 105 | className={ |
| 106 | type === "risks" |
| 107 | ? getSeverityColor(item.severity!) |
| 108 | : getImpactColor(item.impact!) |
| 109 | } |
| 110 | > |
| 111 | {(item.severity || item.impact)?.toUpperCase()} |
| 112 | </Badge> |
| 113 | )} |
| 114 | </div> |
| 115 | <p className="mt-2 text-gray-600"> |
| 116 | {type === "risks" ? item.explanation : item.explanation} |
| 117 | </p> |
| 118 | </motion.li> |
| 119 | ))} |
| 120 | {!isActive && items.length > 3 && ( |
| 121 | <motion.li |
| 122 | className="border rounded-lg p-4 blur-sm" |
| 123 | initial={{ opacity: 0, y: 20 }} |
| 124 | animate={{ opacity: 1, y: 0 }} |
| 125 | transition={{ delay: displayItems.length * 0.1 }} |
| 126 | > |
| 127 | <div className="flex justify-between items-start mb-2"> |
| 128 | <span className="font-semibold text-lg"> |
| 129 | {type === "risks" ? fakeItems.risk : fakeItems.opportunity} |
| 130 | </span> |
| 131 | <Badge> |
| 132 | {(fakeItems.severity || fakeItems.impact)?.toUpperCase()} |
| 133 | </Badge> |
| 134 | </div> |
| 135 | </motion.li> |
| 136 | )} |
| 137 | </ul> |
| 138 | ); |
| 139 | }; |
| 140 | |
| 141 | const renderPremiumAccordition = (content: ReactNode) => { |
| 142 | if (isActive) { |
| 143 | return content; |
| 144 | } |
| 145 | |
| 146 | return ( |
| 147 | <div className="relative"> |
| 148 | <div className="absolute inset-0 bg-white/50 backdrop-blur-sm z-50 flex items-center justify-center"> |
| 149 | <Button onClick={onUpgrade} variant={"outline"}> |
| 150 | Upgrade to Premium |
| 151 | </Button> |
| 152 | </div> |
| 153 | <div className="opacity-50">{content}</div> |
| 154 | </div> |
| 155 | ); |
| 156 | }; |
| 157 | |
| 158 | return ( |
| 159 | <div className="container mx-auto px-4 py-8"> |
| 160 | <div className="flex justify-between items-center mb-6"> |
| 161 | <h1 className="text-3xl font-bold">Analysis Results</h1> |
| 162 | <div className="flex space-x-2">{/* ASK AI BUTTON */}</div> |
| 163 | </div> |
| 164 | |
| 165 | <Card className="mb-6"> |
| 166 | <CardHeader> |
| 167 | <CardTitle>Overal Contract Score</CardTitle> |
| 168 | <CardDescription> |
| 169 | Based on risks and opportunities identified |
| 170 | </CardDescription> |
| 171 | </CardHeader> |
| 172 | <CardContent> |
| 173 | <div className="flex items-center justify-between"> |
| 174 | <div className="w-1/2"> |
| 175 | <div className="flex items-center space-x-4 mb-4"> |
| 176 | <div className="text-4xl font-bold"> |
| 177 | {analysisResults.overallScore ?? 0} |
| 178 | </div> |
| 179 | <div className={`flex items-center ${scoreTrend.color}`}> |
| 180 | <scoreTrend.icon className="size-6 mr-1" /> |
| 181 | <span className="font-semibold">{scoreTrend.text}</span> |
| 182 | </div> |
| 183 | </div> |
| 184 | <div className="space-y-2"> |
| 185 | <div className="flex justify-between text-sm"> |
| 186 | <span>Risk</span> |
| 187 | <span>{100 - analysisResults.overallScore}%</span> |
| 188 | </div> |
| 189 | <div className="flex justify-between text-sm"> |
| 190 | <span>Opportunities</span> |
| 191 | <span>{analysisResults.overallScore}%</span> |
| 192 | </div> |
| 193 | </div> |
| 194 | <p className="text-sm text-gray-600 mt-4"> |
| 195 | This score represents the overall risk and opportunitys |
| 196 | identified in the contract. |
| 197 | </p> |
| 198 | </div> |
| 199 | |
| 200 | <div className="w-1/2 h-48 flex justify-center items-center"> |
| 201 | <div className="w-full h-full max-w-xs"> |
| 202 | <OverallScoreChart |
| 203 | overallScore={analysisResults.overallScore} |
| 204 | /> |
| 205 | </div> |
| 206 | </div> |
| 207 | </div> |
| 208 | </CardContent> |
| 209 | </Card> |
| 210 | |
| 211 | <Tabs value={activeTab} onValueChange={setActiveTab} className="mb-6"> |
| 212 | <TabsList className="grid w-full grid-cols-4"> |
| 213 | <TabsTrigger value="summary">Summary</TabsTrigger> |
| 214 | <TabsTrigger value="risks">Risks</TabsTrigger> |
| 215 | <TabsTrigger value="opportunities">Opportunities</TabsTrigger> |
| 216 | <TabsTrigger value="details">Details</TabsTrigger> |
| 217 | </TabsList> |
| 218 | <TabsContent value="summary"> |
| 219 | <Card> |
| 220 | <CardHeader> |
| 221 | <CardTitle>Contract Summary</CardTitle> |
| 222 | </CardHeader> |
| 223 | <CardContent> |
| 224 | <p className="text-lg leading-relaxed"> |
| 225 | {analysisResults.summary} |
| 226 | </p> |
| 227 | </CardContent> |
| 228 | </Card> |
| 229 | </TabsContent> |
| 230 | <TabsContent value="risks"> |
| 231 | <Card> |
| 232 | <CardHeader> |
| 233 | <CardTitle>Risks</CardTitle> |
| 234 | </CardHeader> |
| 235 | <CardContent> |
| 236 | {renderRisksAndOpportunities(analysisResults.risks, "risks")} |
| 237 | {!isActive && ( |
| 238 | <p className="mt-4 text-center text-sm text-gray-500"> |
| 239 | Upgrade to Premium to see all risks |
| 240 | </p> |
| 241 | )} |
| 242 | </CardContent> |
| 243 | </Card> |
| 244 | </TabsContent> |
| 245 | <TabsContent value="opportunities"> |
| 246 | <Card> |
| 247 | <CardHeader> |
| 248 | <CardTitle>Opportunities</CardTitle> |
| 249 | </CardHeader> |
| 250 | <CardContent> |
| 251 | {renderRisksAndOpportunities( |
| 252 | analysisResults.opportunities, |
| 253 | "opportunities" |
| 254 | )} |
| 255 | {!isActive && ( |
| 256 | <p className="mt-4 text-center text-sm text-gray-500"> |
| 257 | Upgrade to Premium to see all opportunities |
| 258 | </p> |
| 259 | )} |
| 260 | </CardContent> |
| 261 | </Card> |
| 262 | </TabsContent> |
| 263 | <TabsContent value="details"> |
| 264 | {isActive ? ( |
| 265 | <div className="grid md:grid-cols-2 gap-6"> |
| 266 | <Card> |
| 267 | <CardHeader> |
| 268 | <CardTitle>Contract Details</CardTitle> |
| 269 | </CardHeader> |
| 270 | <CardContent> |
| 271 | <ul className="space-y-2"> |
| 272 | {analysisResults.keyClauses?.map((keyClause, index) => ( |
| 273 | <motion.li key={index} className="flex items-center"> |
| 274 | {keyClause} |
| 275 | </motion.li> |
| 276 | ))} |
| 277 | </ul> |
| 278 | </CardContent> |
| 279 | </Card> |
| 280 | <Card> |
| 281 | <CardHeader> |
| 282 | <CardTitle>Recommdations</CardTitle> |
| 283 | </CardHeader> |
| 284 | <CardContent> |
| 285 | <ul className="space-y-2"> |
| 286 | {analysisResults.recommendations?.map( |
| 287 | (recommendation, index) => ( |
| 288 | <motion.li key={index} className="flex items-center"> |
| 289 | {recommendation} |
| 290 | </motion.li> |
| 291 | ) |
| 292 | )} |
| 293 | </ul> |
| 294 | </CardContent> |
| 295 | </Card> |
| 296 | </div> |
| 297 | ) : ( |
| 298 | <Card> |
| 299 | <CardHeader> |
| 300 | <CardTitle>Contract Details</CardTitle> |
| 301 | </CardHeader> |
| 302 | <CardContent> |
| 303 | <p> |
| 304 | Upgrade to Premium to see contract detailed analysis, |
| 305 | including key clauses and recommendations. |
| 306 | </p> |
| 307 | <Button |
| 308 | variant={"outline"} |
| 309 | onClick={onUpgrade} |
| 310 | className="mt-4" |
| 311 | > |
| 312 | Upgrade to Premium |
| 313 | </Button> |
| 314 | </CardContent> |
| 315 | </Card> |
| 316 | )} |
| 317 | </TabsContent> |
| 318 | </Tabs> |
| 319 | |
| 320 | <Accordion type="single" collapsible className="mb-6"> |
| 321 | {renderPremiumAccordition( |
| 322 | <> |
| 323 | <AccordionItem value="contract-details"> |
| 324 | <AccordionTrigger>Contract Details</AccordionTrigger> |
| 325 | <AccordionContent> |
| 326 | <div className="grid md:grid-cols-2 gap-4"> |
| 327 | <div> |
| 328 | <h3 className="font-semibold mb-2"> |
| 329 | Duration and Termination |
| 330 | </h3> |
| 331 | <p>{analysisResults.contractDuration}</p> |
| 332 | <strong>Termination Conditions</strong> |
| 333 | <p>{analysisResults.terminationConditions}</p> |
| 334 | </div> |
| 335 | <div> |
| 336 | <h3 className="font-semibold mb-2">Legal Informatiob</h3> |
| 337 | <p> |
| 338 | <strong>Legal Compliance</strong> |
| 339 | {analysisResults.legalCompliance} |
| 340 | </p> |
| 341 | </div> |
| 342 | </div> |
| 343 | </AccordionContent> |
| 344 | </AccordionItem> |
| 345 | </> |
| 346 | )} |
| 347 | </Accordion> |
| 348 | |
| 349 | <Card> |
| 350 | <CardHeader> |
| 351 | <CardTitle>Negotiation Points</CardTitle> |
| 352 | </CardHeader> |
| 353 | <CardContent> |
| 354 | <ul className="grid md:grid-cols-2 gap-2"> |
| 355 | {analysisResults.negotiationPoints?.map((point, index) => ( |
| 356 | <li className="flex items-center" key={index}> |
| 357 | {point} |
| 358 | </li> |
| 359 | ))} |
| 360 | </ul> |
| 361 | </CardContent> |
| 362 | </Card> |
| 363 | </div> |
| 364 | ); |
| 365 | } |