job-card.tsx349 lines, 12 KB
| 1 | import { Badge } from "@/components/ui/badge"; |
| 2 | import { Button } from "@/components/ui/button"; |
| 3 | import { Card, CardContent } from "@/components/ui/card"; |
| 4 | import { Progress } from "@/components/ui/progress"; |
| 5 | import { JobStatus } from "@/lib/api/types"; |
| 6 | import { cn } from "@/lib/utils"; |
| 7 | import { CheckCircledIcon, ClockIcon } from "@radix-ui/react-icons"; |
| 8 | import { |
| 9 | AlertCircleIcon, |
| 10 | AlertTriangle, |
| 11 | Clock, |
| 12 | ExternalLink, |
| 13 | Loader2Icon, |
| 14 | XCircleIcon, |
| 15 | Youtube, |
| 16 | Trash2, |
| 17 | RefreshCw, |
| 18 | Ban, |
| 19 | } from "lucide-react"; |
| 20 | import Image from "next/image"; |
| 21 | import Link from "next/link"; |
| 22 | import { formatDistanceToNow } from "date-fns"; |
| 23 | |
| 24 | interface JobCardProps { |
| 25 | job: JobStatus & { thumbnail?: string }; |
| 26 | onRetry?: (jobId: string | number) => void; |
| 27 | onDelete?: (jobId: string | number) => void; |
| 28 | onCancel?: (jobId: string | number) => void; |
| 29 | } |
| 30 | |
| 31 | const stateBorderColors = { |
| 32 | waiting: "border-l-4 border-yellow-400", |
| 33 | active: "border-l-4 border-blue-400", |
| 34 | completed: "border-l-4 border-green-400", |
| 35 | failed: "border-l-4 border-red-400", |
| 36 | delayed: "border-l-4 border-orange-400", |
| 37 | }; |
| 38 | |
| 39 | const stateIconColors = { |
| 40 | waiting: "text-yellow-500", |
| 41 | active: "text-blue-500", |
| 42 | completed: "text-green-500", |
| 43 | failed: "text-red-500", |
| 44 | delayed: "text-orange-500", |
| 45 | }; |
| 46 | |
| 47 | const stateBadgeStyles = { |
| 48 | waiting: "bg-yellow-100 text-yellow-800 border-yellow-200", |
| 49 | active: "bg-blue-100 text-blue-800 border-blue-200", |
| 50 | completed: "bg-green-100 text-green-800 border-green-200", |
| 51 | failed: "bg-red-100 text-red-800 border-red-200", |
| 52 | delayed: "bg-orange-100 text-orange-800 border-orange-200", |
| 53 | }; |
| 54 | |
| 55 | const stateMessages = { |
| 56 | waiting: "Queued", |
| 57 | active: "Processing", |
| 58 | completed: "Completed", |
| 59 | failed: "Failed", |
| 60 | delayed: "Delayed", |
| 61 | }; |
| 62 | |
| 63 | const StateIcon = ({ |
| 64 | state, |
| 65 | className, |
| 66 | }: { |
| 67 | state: JobStatus["state"]; |
| 68 | className?: string; |
| 69 | }) => { |
| 70 | const Icon = |
| 71 | { |
| 72 | completed: CheckCircledIcon, |
| 73 | failed: XCircleIcon, |
| 74 | active: Loader2Icon, |
| 75 | waiting: ClockIcon, |
| 76 | delayed: AlertCircleIcon, |
| 77 | }[state] || Clock; |
| 78 | |
| 79 | return ( |
| 80 | <Icon |
| 81 | className={cn( |
| 82 | stateIconColors[state], |
| 83 | state === "active" && "animate-spin", |
| 84 | className |
| 85 | )} |
| 86 | /> |
| 87 | ); |
| 88 | }; |
| 89 | |
| 90 | export function JobCard({ job, onRetry, onDelete, onCancel }: JobCardProps) { |
| 91 | const videoInfo = job.result?.videoInfo; |
| 92 | |
| 93 | return ( |
| 94 | <Card |
| 95 | className={cn( |
| 96 | "overflow-hidden transition-all hover:shadow-lg", |
| 97 | stateBorderColors[job.state] |
| 98 | )} |
| 99 | > |
| 100 | <CardContent className="p-4 sm:p-6"> |
| 101 | <div className="flex flex-col sm:flex-row items-start gap-4"> |
| 102 | <div className="relative flex-shrink-0 w-full sm:w-32 h-20 rounded-md overflow-hidden bg-muted flex items-center justify-center"> |
| 103 | {job.thumbnail ? ( |
| 104 | <Image |
| 105 | src={job.thumbnail} |
| 106 | alt={videoInfo?.title || "Video thumbnail"} |
| 107 | fill |
| 108 | className="object-cover" |
| 109 | sizes="(max-width: 640px) 100vw, 128px" |
| 110 | /> |
| 111 | ) : ( |
| 112 | <Youtube className="size-8 text-muted-foreground/60" /> |
| 113 | )} |
| 114 | <div className="absolute inset-0 bg-gradient-to-t from-black/30 via-black/10 to-transparent"></div> |
| 115 | <div className="absolute top-1 right-1 p-1 bg-card/80 backdrop-blur-sm rounded-full"> |
| 116 | <StateIcon state={job.state} className="size-4" /> |
| 117 | </div> |
| 118 | </div> |
| 119 | |
| 120 | <div className="flex-1 min-w-0"> |
| 121 | <div className="flex flex-wrap items-center gap-2 mb-1"> |
| 122 | <h3 className="text-lg font-semibold leading-tight truncate flex-1"> |
| 123 | {videoInfo?.title || "Untitled Video"} |
| 124 | </h3> |
| 125 | <Badge |
| 126 | variant={"outline"} |
| 127 | className={cn( |
| 128 | "capitalize text-xs px-2 py-0.5", |
| 129 | stateBadgeStyles[job.state] |
| 130 | )} |
| 131 | > |
| 132 | {stateMessages[job.state]} |
| 133 | </Badge> |
| 134 | </div> |
| 135 | |
| 136 | {videoInfo?.description && ( |
| 137 | <p className="text-sm text-muted-foreground line-clamp-2 mb-3"> |
| 138 | {videoInfo.description} |
| 139 | </p> |
| 140 | )} |
| 141 | |
| 142 | <div className="flex flex-wrap items-center gap-x-3 gap-y-1 text-xs text-muted-foreground mb-3"> |
| 143 | {videoInfo?.author && ( |
| 144 | <span className="inline-flex items-center gap-1"> |
| 145 | <Youtube className="size-3" /> {videoInfo.author} |
| 146 | </span> |
| 147 | )} |
| 148 | {videoInfo?.duration && ( |
| 149 | <> |
| 150 | {videoInfo?.author && <span className="opacity-50">•</span>} |
| 151 | <span className="inline-flex items-center gap-1"> |
| 152 | <Clock className="size-3" /> |
| 153 | {Math.floor(videoInfo.duration / 60)} min{" "} |
| 154 | {Math.floor(videoInfo.duration % 60)} sec |
| 155 | </span> |
| 156 | </> |
| 157 | )} |
| 158 | {job.timestamp && typeof job.timestamp === "number" && ( |
| 159 | <> |
| 160 | {(videoInfo?.author || videoInfo?.duration) && ( |
| 161 | <span className="opacity-50">•</span> |
| 162 | )} |
| 163 | <span> |
| 164 | Added:{" "} |
| 165 | {formatDistanceToNow(new Date(job.timestamp), { |
| 166 | addSuffix: true, |
| 167 | })} |
| 168 | </span> |
| 169 | </> |
| 170 | )} |
| 171 | </div> |
| 172 | |
| 173 | {/* --- State-Specific Information --- */} |
| 174 | |
| 175 | {/* Waiting State Info */} |
| 176 | {job.state === "waiting" && ( |
| 177 | <div className="text-xs text-muted-foreground mt-2 space-y-0.5"> |
| 178 | {job.timestamp && typeof job.timestamp === "number" && ( |
| 179 | <div> |
| 180 | Queued{" "} |
| 181 | {formatDistanceToNow(new Date(job.timestamp), { |
| 182 | addSuffix: true, |
| 183 | })} |
| 184 | .{job.attemptsMade > 0 && ` (Attempt ${job.attemptsMade})`} |
| 185 | </div> |
| 186 | )} |
| 187 | <p className="text-muted-foreground/80"> |
| 188 | Will be processed soon. |
| 189 | </p> |
| 190 | </div> |
| 191 | )} |
| 192 | |
| 193 | {/* Active State Info */} |
| 194 | {job.state === "active" && ( |
| 195 | <div className="mt-2 space-y-1"> |
| 196 | <div className="flex items-center justify-between text-xs"> |
| 197 | <span className="text-muted-foreground/80"> |
| 198 | Processing ({job.progress.toFixed(0)}%) |
| 199 | {job.attemptsMade > 0 && ` (Attempt ${job.attemptsMade})`} |
| 200 | </span> |
| 201 | </div> |
| 202 | <Progress |
| 203 | value={job.progress} |
| 204 | className="h-1" |
| 205 | indicatorClassName={cn( |
| 206 | "transition-all duration-500", |
| 207 | "bg-blue-500" |
| 208 | )} |
| 209 | /> |
| 210 | <p className="text-xs text-muted-foreground/80 pt-0.5"> |
| 211 | Processing may take a few minutes depending on video length. |
| 212 | </p> |
| 213 | </div> |
| 214 | )} |
| 215 | |
| 216 | {/* Delayed State Info */} |
| 217 | {job.state === "delayed" && ( |
| 218 | <div className="text-xs text-amber-700 bg-amber-50 border border-amber-200 p-2 rounded-md mt-2 space-y-0.5"> |
| 219 | <div> |
| 220 | Job is currently delayed |
| 221 | {job.timestamp && |
| 222 | typeof job.timestamp === "number" && |
| 223 | ` (since ${formatDistanceToNow(new Date(job.timestamp), { |
| 224 | addSuffix: true, |
| 225 | })})`} |
| 226 | .{/* Display attempts correctly */} |
| 227 | {job.attemptsMade > 0 && ` (Attempt ${job.attemptsMade})`} |
| 228 | </div> |
| 229 | <p className="text-amber-600/90">Will resume automatically.</p> |
| 230 | </div> |
| 231 | )} |
| 232 | |
| 233 | {/* Completed State Info */} |
| 234 | {job.state === "completed" && ( |
| 235 | <div className="text-xs text-green-700 mt-2 space-y-0.5"> |
| 236 | {job.finishedOn && typeof job.finishedOn === "number" && ( |
| 237 | <div> |
| 238 | Completed{" "} |
| 239 | {formatDistanceToNow(new Date(job.finishedOn), { |
| 240 | addSuffix: true, |
| 241 | })} |
| 242 | . |
| 243 | </div> |
| 244 | )} |
| 245 | <p className="text-green-600/90"> |
| 246 | Summary & analysis are ready. |
| 247 | </p> |
| 248 | </div> |
| 249 | )} |
| 250 | |
| 251 | {/* Failed State Info */} |
| 252 | {job.state === "failed" && ( |
| 253 | <div className="mt-2 space-y-2"> |
| 254 | {job.failedReason && ( |
| 255 | <div className="text-xs text-red-600 bg-red-50 border border-red-200 p-2 rounded-md flex items-start gap-2"> |
| 256 | <AlertTriangle className="size-4 flex-shrink-0 mt-0.5" /> |
| 257 | <p className="line-clamp-3"> |
| 258 | <strong>Failure Reason:</strong> {job.failedReason} |
| 259 | </p> |
| 260 | </div> |
| 261 | )} |
| 262 | {job.finishedOn && typeof job.finishedOn === "number" && ( |
| 263 | <div className="text-xs text-red-700"> |
| 264 | Failed{" "} |
| 265 | {formatDistanceToNow(new Date(job.finishedOn), { |
| 266 | addSuffix: true, |
| 267 | })} |
| 268 | {job.attemptsMade > 0 && ` on attempt ${job.attemptsMade}`}. |
| 269 | </div> |
| 270 | )} |
| 271 | {/* Helpful text for failed state */} |
| 272 | {onRetry && ( |
| 273 | <p className="text-xs text-muted-foreground/90"> |
| 274 | Try retrying the job if the issue seems temporary. |
| 275 | </p> |
| 276 | )} |
| 277 | </div> |
| 278 | )} |
| 279 | |
| 280 | {/* --- Action Buttons --- */} |
| 281 | <div className="mt-4 flex items-center justify-end gap-2"> |
| 282 | {videoInfo?.videoUrl && ( |
| 283 | <Button variant="outline" size="sm" asChild> |
| 284 | <a |
| 285 | href={videoInfo.videoUrl} |
| 286 | target="_blank" |
| 287 | rel="noopener noreferrer" |
| 288 | className="inline-flex items-center gap-1.5" |
| 289 | > |
| 290 | <Youtube className="h-3.5 w-3.5" /> |
| 291 | <span>Watch</span> |
| 292 | </a> |
| 293 | </Button> |
| 294 | )} |
| 295 | {job.state === "completed" && job.videoStatus?.id && ( |
| 296 | <Button variant="default" size="sm" asChild> |
| 297 | <Link |
| 298 | href={`/dashboard/videos/${job.videoStatus.id}`} |
| 299 | className="inline-flex items-center gap-1.5" |
| 300 | > |
| 301 | <ExternalLink className="h-3.5 w-3.5" /> |
| 302 | <span>View Summary</span> |
| 303 | </Link> |
| 304 | </Button> |
| 305 | )} |
| 306 | {job.state === "failed" && onRetry && ( |
| 307 | <Button |
| 308 | variant="outline" |
| 309 | size="sm" |
| 310 | onClick={() => onRetry(job.id)} |
| 311 | className="text-amber-600 border-amber-300 hover:bg-amber-50 hover:text-amber-700" |
| 312 | > |
| 313 | <RefreshCw className="h-3.5 w-3.5 mr-1.5" /> |
| 314 | Retry |
| 315 | </Button> |
| 316 | )} |
| 317 | {(job.state === "completed" || job.state === "failed") && |
| 318 | onDelete && ( |
| 319 | <Button |
| 320 | variant="outline" |
| 321 | size="sm" |
| 322 | onClick={() => onDelete(job.id)} |
| 323 | className="text-red-600 border-red-300 hover:bg-red-50 hover:text-red-700" |
| 324 | > |
| 325 | <Trash2 className="h-3.5 w-3.5 mr-1.5" /> |
| 326 | Delete |
| 327 | </Button> |
| 328 | )} |
| 329 | {(job.state === "active" || |
| 330 | job.state === "waiting" || |
| 331 | job.state === "delayed") && |
| 332 | onCancel && ( |
| 333 | <Button |
| 334 | variant="outline" |
| 335 | size="sm" |
| 336 | onClick={() => onCancel(job.id)} |
| 337 | className="text-gray-600 border-gray-300 hover:bg-gray-100 hover:text-gray-700" |
| 338 | > |
| 339 | <Ban className="h-3.5 w-3.5 mr-1.5" /> |
| 340 | Cancel |
| 341 | </Button> |
| 342 | )} |
| 343 | </div> |
| 344 | </div> |
| 345 | </div> |
| 346 | </CardContent> |
| 347 | </Card> |
| 348 | ); |
| 349 | } |