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