import React, { useState, useMemo } from "react"; import { Button } from "@/components/ui/button"; import { useCurrentUser } from "@/hooks/useCurrentUser"; import { useAppSubmissions } from "@/hooks/useAppSubmissions"; import { AppShowcaseCard } from "@/components/AppShowcaseCard"; import { Collapsible, CollapsibleContent, CollapsibleTrigger, } from "@/components/ui/collapsible"; import { nip19 } from 'nostr-tools'; import { shuffleArray } from "@/lib/utils"; import { Skeleton } from "@/components/ui/skeleton"; import { Card, CardContent } from "@/components/ui/card"; import { useAppContext } from "@/hooks/useAppContext"; import { useTranslation } from 'react-i18next'; import { Link } from 'react-router-dom'; import { Settings } from 'lucide-react'; export function AppShowcase() { const { t } = useTranslation(); const { user } = useCurrentUser(); const { config } = useAppContext(); const { data: submissions = [], isLoading } = useAppSubmissions(); const [showAllApps, setShowAllApps] = useState(false); // Get moderator hex from config const MODERATOR_HEX = useMemo(() => { try { const decoded = nip19.decode(config.showcaseModerator); return decoded.type === 'npub' ? decoded.data : ''; } catch { return ''; } }, [config.showcaseModerator]); const isModerator = user?.pubkey === MODERATOR_HEX; // Filter and shuffle submissions - memoized with stable dependency const { templateApps, featuredApps, approvedApps } = useMemo(() => { // Filter visible submissions const visibleSubmissions = submissions.filter(app => !app.isHidden); // Separate template apps from other apps const templates = shuffleArray(visibleSubmissions.filter(app => app.appTags.includes('Template'))); const nonTemplateSubmissions = visibleSubmissions.filter(app => !app.appTags.includes('Template')); // Shuffle featured and approved apps for random display order const featured = shuffleArray(nonTemplateSubmissions.filter(app => app.isFeatured)); const approved = shuffleArray(nonTemplateSubmissions.filter(app => app.isApproved && !app.isFeatured)); return { templateApps: templates, featuredApps: featured, approvedApps: approved, }; }, [submissions]); // Only depend on submissions, not intermediate filtered arrays // Don't show showcase if disabled in settings if (!config.showcaseEnabled) { return null; } // Don't show showcase if no apps exist if (isLoading) { return (
{Array.from({ length: 6 }).map((_, i) => (
))}
); } if (!submissions.length) { return (

{t('noAppsFound')}

); } return (
{/* Apps Grid */}
{/* Featured Apps */} {featuredApps.length > 0 && (
⭐

{t('featuredApps')}

{t('featuredAppsDescription')}

{featuredApps.slice(0, 6).map((app) => ( ))}
)} {/* Templates Section */} {templateApps.length > 0 && (
📋

{t('templates')}

{t('templatesDescription')}

{templateApps.slice(0, 6).map((app) => ( ))}
)} {/* More Apps (Approved) - Collapsible */} {approvedApps.length > 0 && (
{approvedApps.map((app) => ( ))}
)}
); }