import { useEffect, useMemo, useState } from "react"; import { MoreVertical, Edit2, Play, Pencil, 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 in the top-right of the thumbnail (same * hover pattern as module cards) and removes the footer + overflow menu. */ hoverModuleActions?: boolean; onEdit?: () => void; onDelete?: () => void; onPublish?: () => void; } export function VideoCard({ title, duration, status, thumbnailGradient = "from-[#CBD5E1] to-[#94A3B8]", thumbnailUrl, videoUrl, onEdit, onDelete, onPublish, hoverModuleActions = false, }: 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 (