import React, { useEffect, useState } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import {
Play,
X,
ExternalLink,
Video,
Maximize2,
Tv,
Monitor
} from 'lucide-react';
import { PageHeader } from '../components/ui/PageHeader';
import { PageLayout } from '../components/layout/PageLayout';
import { getShowcaseContent } from '../services/ecosystem-api';
import type { ContentShowcase } from '../services/ecosystem-api';
// ── Helper: Extract YouTube video ID ──
const extractYouTubeVideoId = (url: string): string | null => {
const patterns = [
/(?:youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/|youtube\.com\/shorts\/)([a-zA-Z0-9_-]{11})/,
/(?:youtube-nocookie\.com\/embed\/)([a-zA-Z0-9_-]{11})/,
];
for (const pattern of patterns) {
const match = url.match(pattern);
if (match) return match[1];
}
return null;
};
// ── Helper: Extract Instagram post/reel ID ──
const extractInstagramId = (url: string): string | null => {
const patterns = [
/instagram\.com\/p\/([a-zA-Z0-9_-]+)/,
/instagram\.com\/reel\/([a-zA-Z0-9_-]+)/,
/instagram\.com\/tv\/([a-zA-Z0-9_-]+)/
];
for (const pattern of patterns) {
const match = url.match(pattern);
if (match) return match[1];
}
return null;
};
// ── YouTube Player Embed Component ──
const YouTubeEmbed: React.FC<{ videoId: string }> = ({ videoId }) => {
return (
);
};
// ── Instagram Player Embed Component ──
const InstagramEmbed: React.FC<{ postId: string; size: 'compact' | 'theater' | 'cinema' }> = ({ postId, size }) => {
const maxWidthClass = size === 'compact' ? 'max-w-[360px]' : size === 'theater' ? 'max-w-[420px]' : 'max-w-[480px]';
return (
);
};
// ── Twitter/X Embed Component ──
const TwitterEmbed: React.FC<{ url: string }> = ({ url }) => {
useEffect(() => {
if (!document.getElementById('twitter-wjs')) {
const script = document.createElement('script');
script.id = 'twitter-wjs';
script.src = 'https://platform.twitter.com/widgets.js';
script.async = true;
script.charset = 'utf-8';
document.body.appendChild(script);
} else {
try {
(window as any).twttr?.widgets?.load();
} catch (err) {
console.error('Failed to reload twitter widgets:', err);
}
}
}, [url]);
return (
);
};
// ── Collapsible Video Description Component ──
const VideoDescription: React.FC<{ text: string }> = ({ text }) => {
const [isExpanded, setIsExpanded] = useState(false);
const shouldCollapse = text.length > 180 || text.includes('\n');
return (
{text}
{shouldCollapse && (
)}
);
};
export const ShowcasePage: React.FC = () => {
const [items, setItems] = useState([]);
const [loading, setLoading] = useState(true);
const [playingVideoId, setPlayingVideoId] = useState(null);
// Resizable Lightbox state: compact | theater | cinema
const [lightboxSize, setLightboxSize] = useState<'compact' | 'theater' | 'cinema'>('compact');
useEffect(() => {
const fetchItems = async () => {
try {
const data = await getShowcaseContent();
setItems(data);
} catch (err) {
console.error('Failed to load showcase content:', err);
} finally {
setLoading(false);
}
};
fetchItems();
}, []);
const activeItem = items.find(item => item.id === playingVideoId);
const headerNode = (
);
return (
Media Showcase
YouTube, Instagram & Twitter/X Gallery
{items.length} Videos Available
{loading ? (
) : items.length === 0 ? (
No Video Showcase Content
There are no featured videos available in the showcase right now. Check back later!
) : (
{items.map((item) => {
const ytId = extractYouTubeVideoId(item.youtubeUrl);
const isIg = item.youtubeUrl.includes('instagram.com');
const isTw = item.youtubeUrl.includes('twitter.com') || item.youtubeUrl.includes('x.com');
const thumbnail = item.thumbnailUrl || (ytId ? `https://img.youtube.com/vi/${ytId}/hqdefault.jpg` : null);
return (
{/* Video Player / Thumbnail */}
{thumbnail ? (

) : (
/* Platform Fallback Gradients */
{isIg && (
)}
{isTw && (
)}
{!isIg && !isTw && (
)}
)}
{/* Dark gradient overlay */}
{/* Play button overlay */}
{/* Platform Tag */}
{isIg ? 'Instagram' : isTw ? 'Twitter / X' : 'YouTube'}
{/* Content Info */}
{item.title}
{item.description && (
)}
{/* Redirect CTA */}
{item.redirectUrl && (
)}
);
})}
)}
{/* ── Immersive Lightbox Media Modal ── */}
{activeItem && (
setPlayingVideoId(null)}
>
{/* Modal Container */}
e.stopPropagation()}
>
{/* Media Container */}
{(() => {
const ytId = extractYouTubeVideoId(activeItem.youtubeUrl);
if (ytId) {
return
;
}
const igId = extractInstagramId(activeItem.youtubeUrl);
if (igId) {
return
;
}
if (activeItem.youtubeUrl.includes('twitter.com') || activeItem.youtubeUrl.includes('x.com')) {
return
;
}
return (
Unsupported media format. Please visit direct link.
);
})()}
{/* Info Container */}
{/* Toolbar with Resizer Preset Buttons */}
{(() => {
if (activeItem.youtubeUrl.includes('youtube.com') || activeItem.youtubeUrl.includes('youtu.be')) return 'YouTube';
if (activeItem.youtubeUrl.includes('instagram.com')) return 'Instagram';
if (activeItem.youtubeUrl.includes('twitter.com') || activeItem.youtubeUrl.includes('x.com')) return 'Twitter / X';
return 'Media';
})()}
{activeItem.title}
{activeItem.description && (
)}
{activeItem.redirectUrl && (
)}
)}
);
};
export default ShowcasePage;