fix(dashboard): play UGC gallery clips in a modal

Cards were mounting every mp4 with preload=metadata, so the grid stalled,
and click did nothing except a hidden View link that opened another tab.
Posters only until click; playback stays on the same screen.
This commit is contained in:
victormanuel.caverogracia 2026-09-01 10:27:27 +02:00
parent 5fe2251a4d
commit 9d69bea940

View file

@ -1,52 +1,59 @@
import React, { useState, useEffect, useRef } from 'react';
import React, { useState, useEffect } from 'react';
import { Film, Download, Copy, Check, ExternalLink, Loader2, Play, User } from 'lucide-react';
import { getApiUrl } from '../config';
import SegmentedControl from './ui/SegmentedControl';
import Modal from './ui/Modal';
export default function UGCGallery() {
const [tab, setTab] = useState('videos');
const [videos, setVideos] = useState([]);
const [avatars, setAvatars] = useState([]);
const [loading, setLoading] = useState(true);
const [loadingVideos, setLoadingVideos] = useState(true);
const [loadingAvatars, setLoadingAvatars] = useState(false);
const [avatarsLoaded, setAvatarsLoaded] = useState(false);
const [copied, setCopied] = useState('');
const [selected, setSelected] = useState(null);
useEffect(() => {
setLoading(true);
Promise.all([
fetch(getApiUrl('/api/saasshorts/gallery?limit=100')).then(r => r.ok ? r.json() : { videos: [] }),
fetch(getApiUrl('/api/saasshorts/actor-gallery')).then(r => r.ok ? r.json() : { images: [] }),
])
.then(([vData, aData]) => {
setVideos(vData.videos || []);
setAvatars(aData.images || []);
})
setLoadingVideos(true);
fetch(getApiUrl('/api/saasshorts/gallery?limit=100'))
.then((r) => (r.ok ? r.json() : { videos: [] }))
.then((d) => setVideos(d.videos || []))
.catch(() => {})
.finally(() => setLoading(false));
.finally(() => setLoadingVideos(false));
}, []);
useEffect(() => {
if (tab !== 'avatars' || avatarsLoaded) return;
setLoadingAvatars(true);
fetch(getApiUrl('/api/saasshorts/actor-gallery'))
.then((r) => (r.ok ? r.json() : { images: [] }))
.then((d) => setAvatars(d.images || []))
.catch(() => {})
.finally(() => {
setLoadingAvatars(false);
setAvatarsLoaded(true);
});
}, [tab, avatarsLoaded]);
const handleCopy = (text, id) => {
navigator.clipboard.writeText(text);
setCopied(id);
setTimeout(() => setCopied(''), 2000);
};
if (loading) {
return (
<div className="flex items-center justify-center h-64">
<Loader2 size={24} className="animate-spin text-brass" />
<span className="ml-2 text-muted lowercase">Loading gallery...</span>
</div>
);
}
const loading = (tab === 'videos' && loadingVideos) || (tab === 'avatars' && loadingAvatars);
return (
<div className="space-y-6">
{/* Header */}
<div className="flex flex-wrap items-end justify-between gap-3">
<div>
<p className="eyebrow mb-1">04 · UGC GALLERY</p>
<h2 className="font-display lowercase text-2xl md:text-3xl text-ink">ugc gallery</h2>
<p className="readout mt-2">{videos.length} videos · {avatars.length} avatars</p>
<p className="readout mt-2">
{loadingVideos ? '…' : videos.length} videos
{avatarsLoaded ? ` · ${avatars.length} avatars` : ''}
</p>
</div>
<a
href={getApiUrl('/gallery')}
@ -58,21 +65,24 @@ export default function UGCGallery() {
</a>
</div>
{/* Tabs */}
<div className="max-w-xs w-full">
<SegmentedControl
size="sm"
value={tab}
onChange={setTab}
options={[
{ value: 'videos', label: `Videos (${videos.length})`, icon: <Film size={14} /> },
{ value: 'avatars', label: `Avatars (${avatars.length})`, icon: <User size={14} /> },
{ value: 'videos', label: `Videos (${loadingVideos ? '…' : videos.length})`, icon: <Film size={14} /> },
{ value: 'avatars', label: `Avatars (${avatarsLoaded ? avatars.length : '…'})`, icon: <User size={14} /> },
]}
/>
</div>
{/* Videos Tab */}
{tab === 'videos' && (
{loading ? (
<div className="flex items-center justify-center h-64">
<Loader2 size={24} className="animate-spin text-brass" />
<span className="ml-2 text-muted lowercase">Loading gallery...</span>
</div>
) : tab === 'videos' ? (
videos.length === 0 ? (
<div className="text-center py-16">
<Film size={40} className="mx-auto text-muted opacity-40 mb-3" />
@ -81,15 +91,17 @@ export default function UGCGallery() {
) : (
<div className="grid grid-cols-2 sm:grid-cols-4 lg:grid-cols-5 xl:grid-cols-6 gap-3">
{videos.map((video) => (
<VideoCard key={video.video_id} video={video} copied={copied} onCopy={handleCopy} />
<VideoCard
key={video.video_id}
video={video}
copied={copied}
onCopy={handleCopy}
onOpen={() => setSelected(video)}
/>
))}
</div>
)
)}
{/* Avatars Tab */}
{tab === 'avatars' && (
avatars.length === 0 ? (
) : avatars.length === 0 ? (
<div className="text-center py-16">
<User size={40} className="mx-auto text-muted opacity-40 mb-3" />
<p className="text-sm text-muted lowercase">No avatars yet. Generate actors from AI Shorts.</p>
@ -100,7 +112,39 @@ export default function UGCGallery() {
<AvatarCard key={avatar.key || i} avatar={avatar} copied={copied} onCopy={handleCopy} />
))}
</div>
)
)}
{selected && (
<Modal
isOpen
onClose={() => setSelected(null)}
size="sm"
eyebrow={selected.video_mode === 'lowcost' ? 'LOW COST' : 'PREMIUM'}
title={selected.title || 'Untitled'}
>
<video
src={selected.video_url}
poster={selected.actor_url}
controls
autoPlay
playsInline
className="w-full rounded-input bg-black aspect-[9/16] object-contain max-h-[60vh]"
/>
<p className="readout mt-3">
{selected.duration?.toFixed(0)}s
{selected.cost_estimate?.total != null ? ` · $${selected.cost_estimate.total.toFixed(2)}` : ''}
</p>
{selected.caption && (
<p className="text-sm text-muted mt-2 leading-relaxed">{selected.caption}</p>
)}
<a
href={selected.video_url}
download
className="btn-ghost w-full mt-4 justify-center text-sm"
>
<Download size={14} /> Download
</a>
</Modal>
)}
</div>
);
@ -110,13 +154,14 @@ function AvatarCard({ avatar, copied, onCopy }) {
return (
<div className="group card overflow-hidden transition-colors hover:border-rule2">
<div className="aspect-[3/4] bg-black">
<img src={avatar.url} alt="Avatar" className="w-full h-full object-cover" />
<img src={avatar.url} alt="Avatar" loading="lazy" decoding="async" className="w-full h-full object-cover" />
</div>
<div className="p-2 space-y-1">
{avatar.description ? (
<div className="relative pr-4">
<p className="text-micro text-muted line-clamp-2">{avatar.description}</p>
<button
type="button"
onClick={() => onCopy(avatar.description, `avatar-${avatar.key}`)}
className="absolute top-0 right-0 p-0.5 text-muted hover:text-brass transition-colors"
title="Copy prompt"
@ -139,56 +184,38 @@ function AvatarCard({ avatar, copied, onCopy }) {
);
}
function VideoCard({ video, copied, onCopy }) {
const videoRef = useRef(null);
const [playing, setPlaying] = useState(false);
const handleMouseEnter = () => {
if (videoRef.current) {
videoRef.current.play().catch(() => {});
setPlaying(true);
}
};
const handleMouseLeave = () => {
if (videoRef.current) {
videoRef.current.pause();
videoRef.current.currentTime = 0;
setPlaying(false);
}
};
function VideoCard({ video, copied, onCopy, onOpen }) {
const mode = video.video_mode;
const caption = video.caption || '';
const hashtags = (video.hashtags || []).join(' ');
return (
<div className="group card overflow-hidden transition-colors hover:border-rule2">
<div
className="relative aspect-[9/16] bg-black cursor-pointer"
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
<button
type="button"
onClick={onOpen}
className="relative aspect-[9/16] w-full p-0 border-0 bg-black overflow-hidden cursor-pointer"
>
<video
ref={videoRef}
src={video.video_url}
poster={video.actor_url}
muted
playsInline
preload="metadata"
{video.actor_url ? (
<img
src={video.actor_url}
alt=""
loading="lazy"
decoding="async"
className="w-full h-full object-cover"
/>
{!playing && (
<div className="absolute inset-0 flex items-center justify-center bg-black/20">
<Play size={20} className="text-white/70" />
</div>
) : (
<div className="w-full h-full bg-paper3" />
)}
<div className="absolute inset-0 flex items-center justify-center bg-black/20 group-hover:bg-black/30 transition-colors">
<Play size={20} className="text-white/80" />
</div>
<div className="absolute top-1.5 right-1.5">
<span className={`${mode === 'lowcost' ? 'badge-ok' : 'badge-brass'} bg-black/70`}>
{mode === 'lowcost' ? 'LOW COST' : 'PREMIUM'}
</span>
</div>
</div>
</button>
<div className="p-2 space-y-1">
<h3 className="text-xs font-semibold text-ink truncate">{video.title || 'Untitled'}</h3>
@ -199,6 +226,7 @@ function VideoCard({ video, copied, onCopy }) {
<div className="relative pr-4">
<p className="text-micro text-muted line-clamp-2">{caption}</p>
<button
type="button"
onClick={() => onCopy(`${caption}\n${hashtags}`, `caption-${video.video_id}`)}
className="absolute top-0 right-0 p-0.5 text-muted hover:text-brass transition-colors"
title="Copy caption"
@ -207,23 +235,13 @@ function VideoCard({ video, copied, onCopy }) {
</button>
</div>
)}
<div className="flex gap-1 pt-0.5">
<a
href={video.video_url}
download
className="flex-1 text-center text-micro lowercase bg-paper3 hover:brightness-110 text-muted hover:text-ink2 py-1 rounded-full transition-all"
className="block text-center text-micro lowercase bg-paper3 hover:brightness-110 text-muted hover:text-ink2 py-1 rounded-full transition-all"
>
<Download size={10} className="inline mr-0.5" />Download
</a>
<a
href={getApiUrl(`/video/${video.video_id}`)}
target="_blank"
rel="noopener noreferrer"
className="flex-1 text-center text-micro lowercase bg-paper3 hover:brightness-110 text-brass py-1 rounded-full transition-all"
>
<ExternalLink size={10} className="inline mr-0.5" />View
</a>
</div>
</div>
</div>
);