|
1 | 1 | 'use client' |
2 | 2 |
|
3 | | -import { useEffect, useRef, useState } from 'react' |
| 3 | +import { useEffect, useRef, useState, useCallback } from 'react' |
4 | 4 | import { useParams, useSearchParams, useRouter } from 'next/navigation' |
5 | 5 | import Link from 'next/link' |
6 | 6 | import VideoPlayer from '@/components/VideoPlayer' |
7 | 7 | import CommentSection from '@/components/CommentSection' |
8 | | -import VideoSidebar from '@/components/VideoSidebar' |
| 8 | +import ThumbnailGrid from '@/components/ThumbnailGrid' |
| 9 | +import ThumbnailReel from '@/components/ThumbnailReel' |
9 | 10 | import ProjectInfo from '@/components/ProjectInfo' |
10 | 11 | import { Card, CardContent } from '@/components/ui/card' |
11 | 12 | import { Button } from '@/components/ui/button' |
@@ -45,6 +46,9 @@ export default function AdminSharePage() { |
45 | 46 | displayVideos: any[] |
46 | 47 | displayLabel: string |
47 | 48 | } | null>(null) |
| 49 | + const [viewState, setViewState] = useState<'grid' | 'player'>('grid') |
| 50 | + const [thumbnailsByName, setThumbnailsByName] = useState<Map<string, string>>(new Map()) |
| 51 | + const [thumbnailsLoading, setThumbnailsLoading] = useState(true) |
48 | 52 | const tokenCacheRef = useRef<Map<string, any>>(new Map()) |
49 | 53 | const sessionIdRef = useRef<string>(`admin:${Date.now()}`) |
50 | 54 |
|
@@ -321,11 +325,89 @@ export default function AdminSharePage() { |
321 | 325 | } |
322 | 326 | }, [activeVideosRaw]) |
323 | 327 |
|
324 | | - // Handle video selection (identical to public share) |
325 | | - const handleVideoSelect = (videoName: string) => { |
| 328 | + // Fetch thumbnails for all video groups (for grid and reel display) |
| 329 | + useEffect(() => { |
| 330 | + let isMounted = true |
| 331 | + const sessionId = sessionIdRef.current |
| 332 | + |
| 333 | + async function fetchThumbnails() { |
| 334 | + if (!project?.videosByName || !id) { |
| 335 | + return |
| 336 | + } |
| 337 | + |
| 338 | + setThumbnailsLoading(true) |
| 339 | + const newThumbnails = new Map<string, string>() |
| 340 | + |
| 341 | + try { |
| 342 | + await Promise.all( |
| 343 | + Object.entries(project.videosByName).map(async ([name, videos]: [string, any[]]) => { |
| 344 | + // Find a video with a thumbnail |
| 345 | + const videoWithThumb = videos.find((v: any) => v.thumbnailPath) |
| 346 | + if (videoWithThumb) { |
| 347 | + const responseThumbnail = await apiFetch( |
| 348 | + `/api/admin/video-token?videoId=${videoWithThumb.id}&projectId=${id}&quality=thumbnail&sessionId=${sessionId}` |
| 349 | + ) |
| 350 | + if (responseThumbnail.ok && isMounted) { |
| 351 | + const dataThumbnail = await responseThumbnail.json() |
| 352 | + newThumbnails.set(name, `/api/content/${dataThumbnail.token}`) |
| 353 | + } |
| 354 | + } |
| 355 | + }) |
| 356 | + ) |
| 357 | + |
| 358 | + if (isMounted) { |
| 359 | + setThumbnailsByName(newThumbnails) |
| 360 | + } |
| 361 | + } catch (error) { |
| 362 | + // Failed to load thumbnails |
| 363 | + } finally { |
| 364 | + if (isMounted) { |
| 365 | + setThumbnailsLoading(false) |
| 366 | + } |
| 367 | + } |
| 368 | + } |
| 369 | + |
| 370 | + fetchThumbnails() |
| 371 | + |
| 372 | + return () => { |
| 373 | + isMounted = false |
| 374 | + } |
| 375 | + }, [project?.videosByName, id]) |
| 376 | + |
| 377 | + // Determine initial view state based on video count and URL params |
| 378 | + useEffect(() => { |
| 379 | + if (!project?.videosByName) return |
| 380 | + |
| 381 | + const videoNames = Object.keys(project.videosByName) |
| 382 | + const hasMultiple = videoNames.length > 1 |
| 383 | + |
| 384 | + // If single video, always go to player |
| 385 | + if (!hasMultiple) { |
| 386 | + setViewState('player') |
| 387 | + return |
| 388 | + } |
| 389 | + |
| 390 | + // If URL specifies a video, go to player |
| 391 | + if (urlVideoName && project.videosByName[urlVideoName]) { |
| 392 | + setViewState('player') |
| 393 | + return |
| 394 | + } |
| 395 | + |
| 396 | + // Multiple videos without URL param: show grid |
| 397 | + setViewState('grid') |
| 398 | + }, [project?.videosByName, urlVideoName]) |
| 399 | + |
| 400 | + // Handle video selection |
| 401 | + const handleVideoSelect = useCallback((videoName: string) => { |
326 | 402 | setActiveVideoName(videoName) |
327 | 403 | setActiveVideosRaw(project.videosByName[videoName]) |
328 | | - } |
| 404 | + setViewState('player') |
| 405 | + }, [project?.videosByName]) |
| 406 | + |
| 407 | + // Handle back to grid |
| 408 | + const handleBackToGrid = useCallback(() => { |
| 409 | + setViewState('grid') |
| 410 | + }, []) |
329 | 411 |
|
330 | 412 | // Show loading state while project loads |
331 | 413 | if (loading) { |
@@ -380,15 +462,58 @@ export default function AdminSharePage() { |
380 | 462 | return project.companyName || primaryRecipient?.name || primaryRecipient?.email || 'Client' |
381 | 463 | })() |
382 | 464 |
|
| 465 | + // Show thumbnail grid for multi-video projects when in grid view |
| 466 | + if (viewState === 'grid' && hasMultipleVideos) { |
| 467 | + return ( |
| 468 | + <div className="flex-1 min-h-0 bg-background flex flex-col overflow-hidden"> |
| 469 | + <div className="flex-1 overflow-y-auto"> |
| 470 | + <div className="max-w-screen-2xl mx-auto w-full px-3 sm:px-4 lg:px-6 py-3 sm:py-6"> |
| 471 | + {/* Header */} |
| 472 | + <div className="mb-6 flex flex-wrap items-center gap-4"> |
| 473 | + <Button |
| 474 | + variant="ghost" |
| 475 | + size="default" |
| 476 | + className="px-3" |
| 477 | + onClick={() => router.push(projectUrl)} |
| 478 | + > |
| 479 | + <ArrowLeft className="w-4 h-4 mr-2" /> |
| 480 | + <span className="hidden sm:inline">Back to Project</span> |
| 481 | + <span className="sm:hidden">Back</span> |
| 482 | + </Button> |
| 483 | + <div className="flex-1 min-w-0"> |
| 484 | + <h1 className="text-2xl sm:text-3xl font-bold text-foreground truncate"> |
| 485 | + {project.title} |
| 486 | + </h1> |
| 487 | + <p className="text-muted-foreground text-sm mt-1"> |
| 488 | + Share View |
| 489 | + </p> |
| 490 | + </div> |
| 491 | + </div> |
| 492 | + |
| 493 | + <ThumbnailGrid |
| 494 | + videosByName={project.videosByName} |
| 495 | + thumbnailsByName={thumbnailsByName} |
| 496 | + thumbnailsLoading={thumbnailsLoading} |
| 497 | + onVideoSelect={handleVideoSelect} |
| 498 | + projectTitle={project.title} |
| 499 | + /> |
| 500 | + </div> |
| 501 | + </div> |
| 502 | + </div> |
| 503 | + ) |
| 504 | + } |
| 505 | + |
383 | 506 | return ( |
384 | | - <div className="flex-1 min-h-0 bg-background flex flex-col lg:flex-row overflow-hidden"> |
385 | | - {/* Video Sidebar */} |
386 | | - {project.videosByName && hasMultipleVideos && ( |
387 | | - <VideoSidebar |
| 507 | + <div className="flex-1 min-h-0 bg-background flex flex-col overflow-hidden"> |
| 508 | + {/* Thumbnail Reel for multi-video projects */} |
| 509 | + {hasMultipleVideos && ( |
| 510 | + <ThumbnailReel |
388 | 511 | videosByName={project.videosByName} |
| 512 | + thumbnailsByName={thumbnailsByName} |
389 | 513 | activeVideoName={activeVideoName} |
390 | 514 | onVideoSelect={handleVideoSelect} |
391 | | - className="w-64 flex-shrink-0" |
| 515 | + onBackToGrid={handleBackToGrid} |
| 516 | + showBackButton={true} |
392 | 517 | /> |
393 | 518 | )} |
394 | 519 |
|
|
0 commit comments