|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { |
| 4 | + Card, |
| 5 | + CardContent, |
| 6 | + CardFooter, |
| 7 | + CardHeader, |
| 8 | + CardTitle, |
| 9 | +} from "@comp/ui/card"; |
| 10 | +import { Badge } from "@comp/ui/badge"; |
| 11 | +import { cn } from "@comp/ui/cn"; |
| 12 | +import { BadgeProps } from "@comp/ui/badge"; |
| 13 | +import { useState } from "react"; |
| 14 | +import { Button } from "@comp/ui/button"; |
| 15 | +import { ArrowRight } from "lucide-react"; |
| 16 | + |
| 17 | +const severityBadgeMap: { |
| 18 | + [key: string]: BadgeProps["variant"]; |
| 19 | +} = { |
| 20 | + critical: "destructive", |
| 21 | + high: "destructive", |
| 22 | + medium: "warning", |
| 23 | + low: "default", |
| 24 | +}; |
| 25 | + |
| 26 | +const severityBorderMap: { |
| 27 | + [key: string]: string; |
| 28 | +} = { |
| 29 | + critical: "border-t-destructive", |
| 30 | + high: "border-t-destructive", |
| 31 | + medium: "border-t-warning", |
| 32 | + low: "border-t-primary", |
| 33 | +}; |
| 34 | + |
| 35 | +export function TestCard({ |
| 36 | + test, |
| 37 | +}: { |
| 38 | + test: { |
| 39 | + id: string; |
| 40 | + title: string | null; |
| 41 | + description: string | null; |
| 42 | + remediation: string | null; |
| 43 | + status: string | null; |
| 44 | + severity: string | null; |
| 45 | + completedAt: Date | null; |
| 46 | + integration: { |
| 47 | + integrationId: string; |
| 48 | + }; |
| 49 | + }; |
| 50 | +}) { |
| 51 | + const [showRemediation, setShowRemediation] = useState(false); |
| 52 | + |
| 53 | + if (showRemediation) { |
| 54 | + return ( |
| 55 | + <Card |
| 56 | + key={test.id} |
| 57 | + className={cn( |
| 58 | + "flex flex-col border-t-4", |
| 59 | + severityBorderMap[ |
| 60 | + test.severity?.toLocaleLowerCase() as keyof typeof severityBorderMap |
| 61 | + ] || "border-t-secondary", |
| 62 | + )} |
| 63 | + > |
| 64 | + <CardHeader> |
| 65 | + <CardTitle>Remediation</CardTitle> |
| 66 | + </CardHeader> |
| 67 | + <CardContent className="break-all break-before-auto text-md"> |
| 68 | + {test.remediation |
| 69 | + ?.split(/\b(https?:\/\/\S+)\b/) |
| 70 | + .map((part, i) => { |
| 71 | + return /^https?:\/\/\S+$/.test(part) ? ( |
| 72 | + <a |
| 73 | + key={i} |
| 74 | + href={part} |
| 75 | + target="_blank" |
| 76 | + rel="noopener noreferrer" |
| 77 | + className="underline break-all" |
| 78 | + > |
| 79 | + {part} |
| 80 | + </a> |
| 81 | + ) : ( |
| 82 | + <span key={i} className="break-all"> |
| 83 | + {part} |
| 84 | + </span> |
| 85 | + ); |
| 86 | + })} |
| 87 | + </CardContent> |
| 88 | + <CardFooter className="flex justify-end py-4"> |
| 89 | + <Button |
| 90 | + size="sm" |
| 91 | + variant="outline" |
| 92 | + onClick={() => setShowRemediation(false)} |
| 93 | + > |
| 94 | + Close |
| 95 | + </Button> |
| 96 | + </CardFooter> |
| 97 | + </Card> |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + return ( |
| 102 | + <Card |
| 103 | + key={test.id} |
| 104 | + className={cn( |
| 105 | + "flex flex-col border-t-4 break-all", |
| 106 | + severityBorderMap[ |
| 107 | + test.severity?.toLocaleLowerCase() as keyof typeof severityBorderMap |
| 108 | + ] || "border-t-secondary", |
| 109 | + )} |
| 110 | + > |
| 111 | + <CardHeader className="flex flex-col gap-2 break-all relative"> |
| 112 | + <div className="flex flex-row items-center justify-between"> |
| 113 | + <div className="text-xs text-muted-foreground"> |
| 114 | + Last Checked: {test.completedAt?.toLocaleString()} |
| 115 | + </div> |
| 116 | + {test.severity && ( |
| 117 | + <Badge |
| 118 | + className="select-none" |
| 119 | + variant={ |
| 120 | + (severityBadgeMap[ |
| 121 | + test.severity.toLowerCase() as keyof typeof severityBadgeMap |
| 122 | + ] || "default") as BadgeProps["variant"] |
| 123 | + } |
| 124 | + > |
| 125 | + {test.severity} |
| 126 | + </Badge> |
| 127 | + )} |
| 128 | + </div> |
| 129 | + <CardTitle>{test.title || "Untitled Test"}</CardTitle> |
| 130 | + </CardHeader> |
| 131 | + <CardContent className="flex flex-col gap-3 text-sm text-muted-foreground h-full"> |
| 132 | + {test.description && <p>{test.description}</p>} |
| 133 | + <span className="flex-grow" /> |
| 134 | + </CardContent> |
| 135 | + <CardFooter className="flex justify-between items-center py-4"> |
| 136 | + <div className="text-xs text-muted-foreground"> |
| 137 | + Status: {test.status} |
| 138 | + </div> |
| 139 | + <Button |
| 140 | + size="sm" |
| 141 | + variant="outline" |
| 142 | + onClick={() => setShowRemediation(true)} |
| 143 | + > |
| 144 | + Remediate <ArrowRight className="ml-2 h-4 w-4" /> |
| 145 | + </Button> |
| 146 | + </CardFooter> |
| 147 | + </Card> |
| 148 | + ); |
| 149 | +} |
0 commit comments