import { useEffect, useMemo, useState } from "react"; import { BookOpen, Calendar, Edit2, MoreVertical, Pencil, Play, Trash2, } from "lucide-react"; import { Button } from "../../../components/ui/button"; import { Dialog, DialogContent, DialogHeader, DialogTitle, } from "../../../components/ui/dialog"; import { isAdminOrSuperAdminRole } from "../../../lib/sessionRole"; import { cn } from "../../../lib/utils"; import { applyShortPreviewToEmbedUrl, DEFAULT_PREVIEW_MAX_SECONDS, formatPreviewLength, getVideoPreview, } from "../../../lib/videoPreview"; import { PreviewLimitedFileVideo } from "./PreviewLimitedFileVideo"; interface VideoCardProps { id?: string | number; title: string; /** Omits the duration chip when not provided (e.g. API has no length yet). */ duration?: string; /** When omitted, shows a neutral "Lesson" chip and no Publish button. */ status?: "Draft" | "Published"; thumbnailGradient?: string; thumbnailUrl?: string | null; /** * When set, the hover play control opens a preview (Vimeo, YouTube, or direct * video file) in a dialog. */ videoUrl?: string; /** * When true, shows edit/delete (and optional view practices) in the top-right * of the thumbnail on hover, and removes the footer + overflow menu. */ hoverModuleActions?: boolean; onEdit?: () => void; onDelete?: () => void; /** When set (e.g. on module lesson cards), shows an "Add practice" control scoped to this lesson. */ onAddPractice?: () => void; /** When set with hoverModuleActions, shows a book icon next to edit/delete on thumbnail hover. */ onViewPractices?: () => void; onPublish?: () => void; /** Shown under title on module lesson cards; reserved height keeps grid rows even. */ description?: string | null; } export function VideoCard({ title, duration, status, thumbnailGradient = "from-[#CBD5E1] to-[#94A3B8]", thumbnailUrl, videoUrl, onEdit, onDelete, onPublish, onAddPractice, onViewPractices, hoverModuleActions = false, description, }: VideoCardProps) { const [thumbFailed, setThumbFailed] = useState(false); const [previewOpen, setPreviewOpen] = useState(false); /** Iframe players ignore URL limits in many cases — unmount after real time. */ const [iframeSessionDone, setIframeSessionDone] = useState(false); const [iframeSessionKey, setIframeSessionKey] = useState(0); const useGradient = !thumbnailUrl?.trim() || thumbFailed; const videoPreview = useMemo( () => (videoUrl?.trim() ? getVideoPreview(videoUrl) : { kind: "none" as const }), [videoUrl], ); const limitedEmbedSrc = useMemo(() => { if (videoPreview.kind !== "iframe") return null; return applyShortPreviewToEmbedUrl( videoPreview.src, videoPreview.label, DEFAULT_PREVIEW_MAX_SECONDS, ); }, [videoPreview]); const canPreview = Boolean(videoUrl?.trim()); const previewLengthLabel = formatPreviewLength( DEFAULT_PREVIEW_MAX_SECONDS, ); useEffect(() => { if (!previewOpen) { setIframeSessionDone(false); return; } if (videoPreview.kind !== "iframe" || !limitedEmbedSrc) { return; } if (iframeSessionDone) { return; } const ms = DEFAULT_PREVIEW_MAX_SECONDS * 1000; const id = window.setTimeout(() => { setIframeSessionDone(true); }, ms); return () => window.clearTimeout(id); }, [ previewOpen, videoPreview.kind, limitedEmbedSrc, iframeSessionDone, ]); const handlePreviewOpenChange = (open: boolean) => { setPreviewOpen(open); if (!open) { setIframeSessionDone(false); setIframeSessionKey((k) => k + 1); } }; return (
{/* Thumbnail */}
{hoverModuleActions && (onEdit || onDelete || onViewPractices) ? (
{onViewPractices ? ( ) : null} {onEdit ? ( ) : null} {onDelete ? ( ) : null}
) : null} {!useGradient && thumbnailUrl ? ( setThumbFailed(true)} /> ) : null} {/* Duration Badge */} {duration ? (
{duration}
) : null} {/* Play: opens preview dialog when videoUrl is set */} {canPreview ? ( ) : (
)}
e.preventDefault()} >

Short preview

{title}

The player closes automatically after {previewLengthLabel} in this window (YouTube/Vimeo can’t be trimmed reliably). For the full lesson, use your LMS app.

{videoPreview.kind === "iframe" && limitedEmbedSrc ? ( iframeSessionDone ? (

Preview time in this window has ended

The embed is removed after {previewLengthLabel} of real time so the full video is not available here.

{videoUrl && isAdminOrSuperAdminRole() ? ( Open full video in new tab ) : null}
) : (