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; // Categorize and organize submissions - single pass for efficiency const { displayedFeaturedApps, displayedTemplateApps, moreApps } = useMemo(() => { const DISPLAY_LIMIT = 6; // Single pass categorization const templates: typeof submissions = []; const featured: typeof submissions = []; const approved: typeof submissions = []; for (const app of submissions) { if (app.isHidden) continue; const isTemplate = app.appTags.includes('Template'); const isFeatured = app.isFeatured; const isApproved = app.isApproved; if (isTemplate) { templates.push(app); } else if (isFeatured) { featured.push(app); } else if (isApproved) { approved.push(app); } } // Shuffle each category once const shuffledTemplates = shuffleArray(templates); const shuffledFeatured = shuffleArray(featured); const shuffledApproved = shuffleArray(approved); // Split into display and more sections return { displayedFeaturedApps: shuffledFeatured.slice(0, DISPLAY_LIMIT), displayedTemplateApps: shuffledTemplates.slice(0, DISPLAY_LIMIT), moreApps: [ ...shuffledFeatured.slice(DISPLAY_LIMIT), ...shuffledTemplates.slice(DISPLAY_LIMIT), ...shuffledApproved, ], }; }, [submissions]); // Don't show showcase if disabled in settings if (!config.showcaseEnabled) { return null; } // Don't show showcase if no apps exist if (isLoading) { return (
{t('noAppsFound')}
{t('featuredAppsDescription')}
{t('templatesDescription')}