page.tsx491 lines, 19 KB
| 1 | "use client"; |
| 2 | |
| 3 | import { useParams } from "next/navigation"; |
| 4 | import { useVideo } from "@/lib/hooks/queries/videos"; |
| 5 | import { Button } from "@/components/ui/button"; |
| 6 | import { |
| 7 | Card, |
| 8 | CardContent, |
| 9 | CardDescription, |
| 10 | CardHeader, |
| 11 | CardTitle, |
| 12 | } from "@/components/ui/card"; |
| 13 | import { Badge } from "@/components/ui/badge"; |
| 14 | import { Skeleton } from "@/components/ui/skeleton"; |
| 15 | import { |
| 16 | ArrowLeft, |
| 17 | FileText, |
| 18 | BarChart, |
| 19 | Clock, |
| 20 | Calendar, |
| 21 | ExternalLink, |
| 22 | ListOrdered, |
| 23 | AlertCircle, |
| 24 | Info, |
| 25 | Music, |
| 26 | Sparkles, |
| 27 | Clipboard, |
| 28 | } from "lucide-react"; |
| 29 | import Link from "next/link"; |
| 30 | import { VideoCard } from "../../components/video-card"; |
| 31 | import { format } from "date-fns"; |
| 32 | import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; |
| 33 | import { cn } from "@/lib/utils"; |
| 34 | import { toast } from "sonner"; |
| 35 | |
| 36 | // Helper function to format seconds into MM:SS format |
| 37 | function formatTime(seconds: number): string { |
| 38 | if (isNaN(seconds) || seconds < 0) return "0:00"; |
| 39 | const minutes = Math.floor(seconds / 60); |
| 40 | const remainingSeconds = Math.floor(seconds % 60); |
| 41 | return `${minutes}:${remainingSeconds.toString().padStart(2, "0")}`; |
| 42 | } |
| 43 | |
| 44 | // Skeleton for the details page |
| 45 | function VideoDetailsSkeleton() { |
| 46 | return ( |
| 47 | <div className="space-y-6"> |
| 48 | {/* Back link skeleton */} |
| 49 | <Skeleton className="h-5 w-32" /> |
| 50 | {/* Video Card Skeleton */} |
| 51 | <Card className="overflow-hidden"> |
| 52 | <div className="p-4 sm:p-6"> |
| 53 | <div className="flex flex-col sm:flex-row items-start gap-4"> |
| 54 | <Skeleton className="flex-shrink-0 w-full sm:w-40 h-24 rounded-md" /> |
| 55 | <div className="flex-1 min-w-0 space-y-2"> |
| 56 | <Skeleton className="h-5 w-3/4 mb-1" /> |
| 57 | <Skeleton className="h-4 w-full" /> |
| 58 | <Skeleton className="h-4 w-5/6" /> |
| 59 | <div className="flex gap-3 pt-1"> |
| 60 | <Skeleton className="h-3 w-20" /> |
| 61 | <Skeleton className="h-3 w-16" /> |
| 62 | <Skeleton className="h-3 w-24" /> |
| 63 | </div> |
| 64 | <div className="flex gap-4 pt-1"> |
| 65 | <Skeleton className="h-4 w-28" /> |
| 66 | <Skeleton className="h-4 w-28" /> |
| 67 | </div> |
| 68 | <div className="flex justify-end pt-2"> |
| 69 | <Skeleton className="h-8 w-20 mr-2" /> |
| 70 | <Skeleton className="h-8 w-24" /> |
| 71 | </div> |
| 72 | </div> |
| 73 | </div> |
| 74 | </div> |
| 75 | </Card> |
| 76 | {/* Tabs Skeleton */} |
| 77 | <Skeleton className="h-10 w-full rounded-md" /> |
| 78 | {/* Tab Content Skeleton */} |
| 79 | <Card> |
| 80 | <CardHeader> |
| 81 | <Skeleton className="h-6 w-1/4 mb-4" /> |
| 82 | <Skeleton className="h-4 w-full" /> |
| 83 | <Skeleton className="h-4 w-full" /> |
| 84 | <Skeleton className="h-4 w-3/4" /> |
| 85 | </CardHeader> |
| 86 | <CardContent> |
| 87 | <Skeleton className="h-4 w-1/2" /> |
| 88 | </CardContent> |
| 89 | </Card> |
| 90 | </div> |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | export default function VideoDetailsPage() { |
| 95 | const { id } = useParams(); |
| 96 | const { data: video, isLoading, error, refetch } = useVideo(id as string); |
| 97 | |
| 98 | if (error) { |
| 99 | // Consistent error state |
| 100 | return ( |
| 101 | <div className="flex flex-col items-center justify-center min-h-[400px] text-center p-6 bg-red-50 border border-red-200 rounded-lg shadow-sm"> |
| 102 | <AlertCircle className="size-12 text-red-500 mb-4" /> |
| 103 | <h2 className="text-xl font-semibold text-red-700 mb-2"> |
| 104 | Failed to Load Video |
| 105 | </h2> |
| 106 | <p className="text-red-600 mb-6 max-w-md"> |
| 107 | We couldn't fetch the details for this video. It might have been |
| 108 | removed or there was a network issue. |
| 109 | </p> |
| 110 | <div className="flex gap-3"> |
| 111 | <Button |
| 112 | onClick={() => refetch()} |
| 113 | variant={"destructive"} |
| 114 | className="gap-2" |
| 115 | > |
| 116 | <AlertCircle className="size-4" /> |
| 117 | Try Again |
| 118 | </Button> |
| 119 | <Button variant="outline" asChild> |
| 120 | <Link href="/dashboard/videos" className="gap-2"> |
| 121 | <ArrowLeft className="size-4" /> |
| 122 | Back to Videos |
| 123 | </Link> |
| 124 | </Button> |
| 125 | </div> |
| 126 | </div> |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | if (isLoading || !video) { |
| 131 | return <VideoDetailsSkeleton />; |
| 132 | } |
| 133 | |
| 134 | const hasTranscription = !!video.transcription; |
| 135 | const hasAnalysis = !!video.analysis; |
| 136 | |
| 137 | return ( |
| 138 | <div className="space-y-6 lg:space-y-8"> |
| 139 | {/* Back Link */} |
| 140 | <Button |
| 141 | variant="ghost" |
| 142 | size="sm" |
| 143 | asChild |
| 144 | className="-ml-2 text-muted-foreground hover:text-foreground" |
| 145 | > |
| 146 | <Link href="/dashboard/videos"> |
| 147 | <ArrowLeft className="mr-2 h-4 w-4" /> |
| 148 | Back to Videos |
| 149 | </Link> |
| 150 | </Button> |
| 151 | |
| 152 | {/* Video Card Display */} |
| 153 | <VideoCard video={video} /> |
| 154 | |
| 155 | {/* Tabs Section */} |
| 156 | <Tabs |
| 157 | defaultValue={ |
| 158 | hasTranscription |
| 159 | ? "transcription" |
| 160 | : hasAnalysis |
| 161 | ? "analysis" |
| 162 | : "details" |
| 163 | } |
| 164 | className="w-full" |
| 165 | > |
| 166 | <TabsList className="grid w-full grid-cols-3 mb-6"> |
| 167 | <TabsTrigger value="transcription" disabled={!hasTranscription}> |
| 168 | <FileText className="h-4 w-4 mr-2" /> |
| 169 | Transcription |
| 170 | </TabsTrigger> |
| 171 | <TabsTrigger value="analysis" disabled={!hasAnalysis}> |
| 172 | <BarChart className="h-4 w-4 mr-2" /> |
| 173 | Analysis |
| 174 | </TabsTrigger> |
| 175 | <TabsTrigger value="details"> |
| 176 | <Info className="h-4 w-4 mr-2" /> |
| 177 | Details |
| 178 | </TabsTrigger> |
| 179 | </TabsList> |
| 180 | |
| 181 | {/* Transcription Tab Content */} |
| 182 | <TabsContent value="transcription"> |
| 183 | {hasTranscription && video.transcription ? ( |
| 184 | <Card> |
| 185 | <CardHeader> |
| 186 | <div className="flex flex-wrap items-center justify-between gap-2"> |
| 187 | <CardTitle className="text-xl">Transcription</CardTitle> |
| 188 | <div className="flex items-center gap-4 text-sm text-muted-foreground"> |
| 189 | <div className="inline-flex items-center gap-1.5"> |
| 190 | <Calendar className="h-4 w-4" /> |
| 191 | {format(new Date(video.transcription.createdAt), "PPP")} |
| 192 | </div> |
| 193 | </div> |
| 194 | </div> |
| 195 | <CardDescription> |
| 196 | Confidence: {Math.round(video.transcription.confidence * 100)} |
| 197 | % |
| 198 | {video.transcription.isMusic && ( |
| 199 | <Badge |
| 200 | variant="secondary" |
| 201 | className="ml-3 bg-purple-100 text-purple-800" |
| 202 | > |
| 203 | <Music className="h-3 w-3 mr-1" /> Contains Music |
| 204 | </Badge> |
| 205 | )} |
| 206 | </CardDescription> |
| 207 | </CardHeader> |
| 208 | <CardContent> |
| 209 | <div className="prose prose-sm max-w-none h-[400px] overflow-y-auto bg-muted/30 p-4 rounded-md border"> |
| 210 | <p className="whitespace-pre-line"> |
| 211 | {video.transcription.text} |
| 212 | </p> |
| 213 | </div> |
| 214 | </CardContent> |
| 215 | </Card> |
| 216 | ) : ( |
| 217 | <Card className="flex flex-col items-center justify-center p-10 min-h-[200px]"> |
| 218 | <FileText className="h-10 w-10 text-muted-foreground mb-3" /> |
| 219 | <p className="font-medium text-center"> |
| 220 | No Transcription Available |
| 221 | </p> |
| 222 | <p className="text-sm text-muted-foreground text-center mt-1"> |
| 223 | This video hasn't been transcribed yet. |
| 224 | </p> |
| 225 | </Card> |
| 226 | )} |
| 227 | </TabsContent> |
| 228 | |
| 229 | {/* Analysis Tab Content */} |
| 230 | <TabsContent value="analysis"> |
| 231 | {hasAnalysis && video.analysis ? ( |
| 232 | <div className="grid grid-cols-1 lg:grid-cols-2 gap-6"> |
| 233 | {/* Summary Card (Span 2 cols) */} |
| 234 | <Card className="lg:col-span-2"> |
| 235 | <CardHeader> |
| 236 | <CardTitle className="text-xl">Summary</CardTitle> |
| 237 | </CardHeader> |
| 238 | <CardContent> |
| 239 | <p className="text-muted-foreground whitespace-pre-line"> |
| 240 | {video.analysis.summary} |
| 241 | </p> |
| 242 | </CardContent> |
| 243 | </Card> |
| 244 | |
| 245 | {/* Chapters Card (NEW) */} |
| 246 | {video.analysis.chapters && |
| 247 | video.analysis.chapters.length > 0 && ( |
| 248 | <Card> |
| 249 | <CardHeader> |
| 250 | <div className="flex justify-between items-center"> |
| 251 | <CardTitle className="text-xl flex items-center gap-2"> |
| 252 | <ListOrdered className="h-5 w-5" /> Chapters |
| 253 | </CardTitle> |
| 254 | <Button |
| 255 | variant="outline" |
| 256 | size="sm" |
| 257 | onClick={() => { |
| 258 | // Format chapters for copying |
| 259 | const formattedChapters = |
| 260 | video.analysis?.chapters |
| 261 | ?.map( |
| 262 | (chapter, index) => |
| 263 | `${formatTime(chapter.startTime)} - ${ |
| 264 | chapter.title |
| 265 | }` |
| 266 | ) |
| 267 | .join("\n") || ""; |
| 268 | |
| 269 | // Copy to clipboard |
| 270 | navigator.clipboard |
| 271 | .writeText(formattedChapters) |
| 272 | .then(() => { |
| 273 | toast.success("Chapters copied to clipboard", { |
| 274 | description: |
| 275 | "You can now paste them anywhere", |
| 276 | duration: 3000, |
| 277 | }); |
| 278 | }) |
| 279 | .catch((err) => { |
| 280 | console.error("Failed to copy chapters: ", err); |
| 281 | toast.error("Failed to copy chapters"); |
| 282 | }); |
| 283 | }} |
| 284 | className="flex items-center gap-1.5 text-sm" |
| 285 | > |
| 286 | <Clipboard className="h-4 w-4" /> |
| 287 | Copy |
| 288 | </Button> |
| 289 | </div> |
| 290 | </CardHeader> |
| 291 | <CardContent> |
| 292 | {/* Scrollable list for chapters */} |
| 293 | <ul className="space-y-3 max-h-[300px] overflow-y-auto pr-2"> |
| 294 | {video.analysis.chapters.map((chapter, index) => ( |
| 295 | <li |
| 296 | key={index} |
| 297 | className="flex items-center justify-between gap-4 text-sm border-b pb-2 last:border-b-0 last:pb-0" |
| 298 | > |
| 299 | <span className="flex-1 font-medium text-foreground hover:text-primary transition-colors cursor-pointer"> |
| 300 | {/* TODO: Add onClick to seek video if player is integrated */} |
| 301 | {chapter.title} |
| 302 | </span> |
| 303 | <Badge |
| 304 | variant="outline" |
| 305 | className="whitespace-nowrap font-mono text-xs" |
| 306 | > |
| 307 | {formatTime(chapter.startTime)} -{" "} |
| 308 | {formatTime(chapter.endTime)} |
| 309 | </Badge> |
| 310 | </li> |
| 311 | ))} |
| 312 | </ul> |
| 313 | </CardContent> |
| 314 | </Card> |
| 315 | )} |
| 316 | |
| 317 | {/* Key Points Card */} |
| 318 | {video.analysis.keyPoints && |
| 319 | video.analysis.keyPoints.length > 0 && ( |
| 320 | <Card> |
| 321 | <CardHeader> |
| 322 | <CardTitle className="text-xl">Key Points</CardTitle> |
| 323 | </CardHeader> |
| 324 | <CardContent> |
| 325 | {/* Scrollable list for points */} |
| 326 | <ul className="space-y-3 max-h-[300px] overflow-y-auto pr-2"> |
| 327 | {video.analysis.keyPoints.map((point, index) => ( |
| 328 | <li |
| 329 | key={index} |
| 330 | className="flex items-start gap-3 text-sm border-b pb-2 last:border-b-0 last:pb-0" |
| 331 | > |
| 332 | <span className="font-semibold text-primary mt-0.5"> |
| 333 | {index + 1}. |
| 334 | </span> |
| 335 | <span className="text-muted-foreground flex-1"> |
| 336 | {point} |
| 337 | </span> |
| 338 | </li> |
| 339 | ))} |
| 340 | </ul> |
| 341 | </CardContent> |
| 342 | </Card> |
| 343 | )} |
| 344 | |
| 345 | {/* Topics & Tags Card (Span 2 cols) */} |
| 346 | <Card className="lg:col-span-2"> |
| 347 | <CardHeader> |
| 348 | <CardTitle className="text-xl">Topics & Tags</CardTitle> |
| 349 | </CardHeader> |
| 350 | <CardContent className="space-y-6"> |
| 351 | {/* Sentiment */} |
| 352 | <div> |
| 353 | <h4 className="font-medium mb-2 text-sm text-muted-foreground flex items-center gap-1.5"> |
| 354 | <Sparkles className="w-4 h-4" /> Sentiment |
| 355 | </h4> |
| 356 | <Badge |
| 357 | variant="secondary" |
| 358 | className="capitalize text-base px-3 py-1" |
| 359 | > |
| 360 | {video.analysis.sentiment} |
| 361 | </Badge> |
| 362 | </div> |
| 363 | |
| 364 | {/* Topics */} |
| 365 | <div> |
| 366 | <h4 className="font-medium mb-2 text-sm text-muted-foreground"> |
| 367 | Topics |
| 368 | </h4> |
| 369 | <div className="flex flex-wrap gap-2"> |
| 370 | {video.analysis.topics && |
| 371 | video.analysis.topics.map((topic, index) => ( |
| 372 | <Badge key={index} variant="secondary"> |
| 373 | {topic} |
| 374 | </Badge> |
| 375 | ))} |
| 376 | </div> |
| 377 | </div> |
| 378 | |
| 379 | {/* Tags */} |
| 380 | <div> |
| 381 | <h4 className="font-medium mb-2 text-sm text-muted-foreground"> |
| 382 | Suggested Tags |
| 383 | </h4> |
| 384 | <div className="flex flex-wrap gap-2"> |
| 385 | {video.analysis.suggestedTags && |
| 386 | video.analysis.suggestedTags.map((tag, index) => ( |
| 387 | <Badge key={index} variant="outline"> |
| 388 | {tag} |
| 389 | </Badge> |
| 390 | ))} |
| 391 | </div> |
| 392 | </div> |
| 393 | </CardContent> |
| 394 | </Card> |
| 395 | </div> |
| 396 | ) : ( |
| 397 | <Card className="flex flex-col items-center justify-center p-10 min-h-[200px]"> |
| 398 | <BarChart className="h-10 w-10 text-muted-foreground mb-3" /> |
| 399 | <p className="font-medium text-center">No Analysis Available</p> |
| 400 | <p className="text-sm text-muted-foreground text-center mt-1"> |
| 401 | This video hasn't been analyzed yet. |
| 402 | </p> |
| 403 | </Card> |
| 404 | )} |
| 405 | </TabsContent> |
| 406 | |
| 407 | {/* Details Tab Content */} |
| 408 | <TabsContent value="details"> |
| 409 | <Card> |
| 410 | <CardHeader> |
| 411 | <CardTitle className="text-xl">Video Information</CardTitle> |
| 412 | <CardDescription> |
| 413 | Core details about the source video. |
| 414 | </CardDescription> |
| 415 | </CardHeader> |
| 416 | <CardContent> |
| 417 | {/* Using a more structured grid for details */} |
| 418 | <dl className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-x-6 gap-y-4"> |
| 419 | <div className="border-b pb-2 sm:border-none sm:pb-0"> |
| 420 | <dt className="text-xs font-medium text-muted-foreground uppercase tracking-wider"> |
| 421 | Duration |
| 422 | </dt> |
| 423 | <dd className="mt-1 text-sm"> |
| 424 | {Math.floor(video.duration / 60)}m{" "} |
| 425 | {Math.floor(video.duration % 60)}s |
| 426 | </dd> |
| 427 | </div> |
| 428 | <div className="border-b pb-2 sm:border-none sm:pb-0"> |
| 429 | <dt className="text-xs font-medium text-muted-foreground uppercase tracking-wider"> |
| 430 | Author/Channel |
| 431 | </dt> |
| 432 | <dd className="mt-1 text-sm truncate"> |
| 433 | {video.author || "N/A"} |
| 434 | </dd> |
| 435 | </div> |
| 436 | <div className="border-b pb-2 sm:border-none sm:pb-0"> |
| 437 | <dt className="text-xs font-medium text-muted-foreground uppercase tracking-wider"> |
| 438 | Status |
| 439 | </dt> |
| 440 | <dd className="mt-1 text-sm"> |
| 441 | <Badge variant="secondary" className="capitalize"> |
| 442 | {video.status} |
| 443 | </Badge> |
| 444 | </dd> |
| 445 | </div> |
| 446 | <div className="border-b pb-2 sm:border-none sm:pb-0"> |
| 447 | <dt className="text-xs font-medium text-muted-foreground uppercase tracking-wider"> |
| 448 | Date Added |
| 449 | </dt> |
| 450 | <dd className="mt-1 text-sm"> |
| 451 | {format(new Date(video.createdAt), "PPp")} |
| 452 | </dd> |
| 453 | </div> |
| 454 | <div className="border-b pb-2 sm:border-none sm:pb-0"> |
| 455 | <dt className="text-xs font-medium text-muted-foreground uppercase tracking-wider"> |
| 456 | Last Updated |
| 457 | </dt> |
| 458 | <dd className="mt-1 text-sm"> |
| 459 | {format(new Date(video.updatedAt), "PPp")} |
| 460 | </dd> |
| 461 | </div> |
| 462 | <div className="sm:col-span-1 md:col-span-1"> |
| 463 | <dt className="text-xs font-medium text-muted-foreground uppercase tracking-wider"> |
| 464 | YouTube URL |
| 465 | </dt> |
| 466 | <dd className="mt-1 text-sm"> |
| 467 | <Button |
| 468 | variant="link" |
| 469 | asChild |
| 470 | className="p-0 h-auto text-blue-600 hover:text-blue-700" |
| 471 | > |
| 472 | <a |
| 473 | href={video.url} |
| 474 | target="_blank" |
| 475 | rel="noopener noreferrer" |
| 476 | className="inline-flex items-center gap-1" |
| 477 | > |
| 478 | Watch on YouTube |
| 479 | <ExternalLink className="h-3.5 w-3.5" /> |
| 480 | </a> |
| 481 | </Button> |
| 482 | </dd> |
| 483 | </div> |
| 484 | </dl> |
| 485 | </CardContent> |
| 486 | </Card> |
| 487 | </TabsContent> |
| 488 | </Tabs> |
| 489 | </div> |
| 490 | ); |
| 491 | } |