import React, { useState } from 'react'; import { Link } from 'react-router-dom'; import { Button } from '@/components/ui/button'; import { Card, CardContent } from '@/components/ui/card'; import { ExternalLink, Edit, Star, StarOff, Eye, EyeOff, MoreVertical, CheckCircle, XCircle, User, Home, Contact } from 'lucide-react'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; import type { AppSubmission } from '@/hooks/useAppSubmissions'; import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useNostrPublish } from '@/hooks/useNostrPublish'; import { useNostr } from '@nostrify/react'; import { useAuthor } from '@/hooks/useAuthor'; import { nip19 } from 'nostr-tools'; import { useToast } from '@/hooks/useToast'; import { useQueryClient } from '@tanstack/react-query'; import type { NostrEvent } from '@nostrify/nostrify'; interface AppShowcaseCardProps { app: AppSubmission; onEdit?: (app: AppSubmission) => void; showModerationControls?: boolean; hideApprovalStatus?: boolean; // Hide pending/approval badges } export function AppShowcaseCard({ app, onEdit, showModerationControls, hideApprovalStatus: _hideApprovalStatus }: AppShowcaseCardProps) { const { user } = useCurrentUser(); const { nostr } = useNostr(); const { mutate: publishEvent, isPending } = useNostrPublish(); const queryClient = useQueryClient(); const [imageError, setImageError] = useState(false); const { toast } = useToast(); // Get author pubkey from authorNpub field const authorPubkey = React.useMemo(() => { if (!app.authorNpub) return app.pubkey; // Fallback to event pubkey try { // If it's already a hex key, use it directly if (app.authorNpub.length === 64 && /^[0-9a-f]+$/i.test(app.authorNpub)) { return app.authorNpub; } // Otherwise try to decode as npub const decoded = nip19.decode(app.authorNpub); return decoded.type === 'npub' ? decoded.data : app.pubkey; } catch { return app.pubkey; // Fallback to event pubkey if decode fails } }, [app.authorNpub, app.pubkey]); const { data: authorData } = useAuthor(authorPubkey); const isOwner = user?.pubkey === app.pubkey; const MODERATOR_NPUB = 'npub1jvnpg4c6ljadf5t6ry0w9q0rnm4mksde87kglkrc993z46c39axsgq89sc'; const MODERATOR_HEX = (() => { try { const decoded = nip19.decode(MODERATOR_NPUB); return decoded.type === 'npub' ? decoded.data : ''; } catch { return ''; } })(); const isModerator = user?.pubkey === MODERATOR_HEX; // Check if this is a Halloween submission by looking for Halloween tags const isHalloweenSubmission = app.appTags.includes('Halloween Hackathon 2025'); // Get contact info from Halloween submissions (these have lightning/email tags) const getContactInfo = () => { if (!isHalloweenSubmission) return null; // Look for lightning and email tags in the event tags const lightningTag = app.tags?.find((tag: string[]) => tag[0] === 'lightning')?.[1]; const emailTag = app.tags?.find((tag: string[]) => tag[0] === 'email')?.[1]; return { lightning: lightningTag, email: emailTag }; }; const handleCopyContactInfo = () => { const contactInfo = getContactInfo(); if (!contactInfo) return; const info: string[] = []; if (contactInfo.lightning) info.push(`Lightning: ${contactInfo.lightning}`); if (contactInfo.email) info.push(`Email: ${contactInfo.email}`); const textToCopy = info.join('\n'); navigator.clipboard.writeText(textToCopy); toast({ title: 'Contact Info Copied!', description: `Copied ${info.length} contact method(s) to clipboard` }); }; const handleModerationAction = async (action: 'feature' | 'unfeature' | 'approve' | 'unapprove' | 'hide' | 'unhide' | 'homepage' | 'unhomepage') => { if (!isModerator || !user) return; const dTag = app.tags.find(tag => tag[0] === 'd')?.[1]; if (!dTag) { toast({ title: 'Error', description: 'Cannot perform moderation action: missing app identifier', variant: 'destructive' }); return; } const appCoordinate = `${app.kind}:${app.pubkey}:${dTag}`; try { if (action === 'homepage' || action === 'unhomepage') { // Get current homepage apps list const currentHomepageLists = await nostr.query([{ kinds: [30267], authors: [MODERATOR_HEX], '#d': ['soapbox-homepage-apps'], limit: 1 }]); const currentHomepageList = currentHomepageLists.reduce((latest: NostrEvent | null, current: NostrEvent) => !latest || current.created_at > latest.created_at ? current : latest, null as NostrEvent | null ); // Get current homepage app coordinates const currentHomepageApps = new Set(); if (currentHomepageList) { currentHomepageList.tags .filter(tag => tag[0] === 'a') .forEach(tag => currentHomepageApps.add(tag[1])); } // Update the homepage set if (action === 'homepage') { currentHomepageApps.add(appCoordinate); } else { currentHomepageApps.delete(appCoordinate); } // Publish updated homepage apps list publishEvent({ kind: 30267, content: 'Soapbox homepage apps curated by moderators', tags: [ ['d', 'soapbox-homepage-apps'], ['title', 'Soapbox Homepage Apps'], ['description', 'Apps displayed on the Soapbox homepage'], ...Array.from(currentHomepageApps).map(coord => ['a', coord]) ], created_at: Math.floor(Date.now() / 1000) }); // If adding to homepage, also ensure it's approved if (action === 'homepage') { // Get current approved apps list const currentApprovedLists = await nostr.query([{ kinds: [30267], authors: [MODERATOR_HEX], '#d': ['soapbox-approved-apps'], limit: 1 }]); const currentApprovedList = currentApprovedLists.reduce((latest: NostrEvent | null, current: NostrEvent) => !latest || current.created_at > latest.created_at ? current : latest, null as NostrEvent | null ); // Get current approved app coordinates const currentApprovedApps = new Set(); if (currentApprovedList) { currentApprovedList.tags .filter(tag => tag[0] === 'a') .forEach(tag => currentApprovedApps.add(tag[1])); } // Add to approved list if not already there currentApprovedApps.add(appCoordinate); // Publish updated approved apps list publishEvent({ kind: 30267, content: 'Soapbox approved apps curated by moderators', tags: [ ['d', 'soapbox-approved-apps'], ['title', 'Soapbox Approved Apps'], ['description', 'Apps approved for the Soapbox showcase'], ...Array.from(currentApprovedApps).map(coord => ['a', coord]) ], created_at: Math.floor(Date.now() / 1000) }); } } else if (action === 'feature' || action === 'unfeature') { // Get current featured apps list const currentFeaturedLists = await nostr.query([{ kinds: [30267], authors: [MODERATOR_HEX], '#d': ['soapbox-featured-apps'], limit: 1 }]); const currentFeaturedList = currentFeaturedLists.reduce((latest: NostrEvent | null, current: NostrEvent) => !latest || current.created_at > latest.created_at ? current : latest, null as NostrEvent | null ); // Get current featured app coordinates const currentFeaturedApps = new Set(); if (currentFeaturedList) { currentFeaturedList.tags .filter(tag => tag[0] === 'a') .forEach(tag => currentFeaturedApps.add(tag[1])); } // Update the featured set if (action === 'feature') { currentFeaturedApps.add(appCoordinate); } else { currentFeaturedApps.delete(appCoordinate); } // Publish updated featured apps list publishEvent({ kind: 30267, content: 'Soapbox featured apps curated by moderators', tags: [ ['d', 'soapbox-featured-apps'], ['title', 'Soapbox Featured Apps'], ['description', 'Apps featured in the Soapbox showcase'], ...Array.from(currentFeaturedApps).map(coord => ['a', coord]) ], created_at: Math.floor(Date.now() / 1000) }); // If featuring an app, also ensure it's approved if (action === 'feature') { // Get current approved apps list const currentApprovedLists = await nostr.query([{ kinds: [30267], authors: [MODERATOR_HEX], '#d': ['soapbox-approved-apps'], limit: 1 }]); const currentApprovedList = currentApprovedLists.reduce((latest: NostrEvent | null, current: NostrEvent) => !latest || current.created_at > latest.created_at ? current : latest, null as NostrEvent | null ); // Get current approved app coordinates const currentApprovedApps = new Set(); if (currentApprovedList) { currentApprovedList.tags .filter(tag => tag[0] === 'a') .forEach(tag => currentApprovedApps.add(tag[1])); } // Add to approved list if not already there currentApprovedApps.add(appCoordinate); // Publish updated approved apps list publishEvent({ kind: 30267, content: 'Soapbox approved apps curated by moderators', tags: [ ['d', 'soapbox-approved-apps'], ['title', 'Soapbox Approved Apps'], ['description', 'Apps approved for the Soapbox showcase'], ...Array.from(currentApprovedApps).map(coord => ['a', coord]) ], created_at: Math.floor(Date.now() / 1000) }); } } else if (action === 'approve' || action === 'unapprove') { // Get current approved apps list const currentApprovedLists = await nostr.query([{ kinds: [30267], authors: [MODERATOR_HEX], '#d': ['soapbox-approved-apps'], limit: 1 }]); const currentApprovedList = currentApprovedLists.reduce((latest: NostrEvent | null, current: NostrEvent) => !latest || current.created_at > latest.created_at ? current : latest, null as NostrEvent | null ); // Get current approved app coordinates const currentApprovedApps = new Set(); if (currentApprovedList) { currentApprovedList.tags .filter(tag => tag[0] === 'a') .forEach(tag => currentApprovedApps.add(tag[1])); } // Update the approved set if (action === 'approve') { currentApprovedApps.add(appCoordinate); } else { currentApprovedApps.delete(appCoordinate); } // Publish updated approved apps list publishEvent({ kind: 30267, content: 'Soapbox approved apps curated by moderators', tags: [ ['d', 'soapbox-approved-apps'], ['title', 'Soapbox Approved Apps'], ['description', 'Apps approved for the Soapbox showcase'], ...Array.from(currentApprovedApps).map(coord => ['a', coord]) ], created_at: Math.floor(Date.now() / 1000) }); } else if (action === 'hide' || action === 'unhide') { // Create NIP-56 report event publishEvent({ kind: 1984, content: `App ${action}den by moderator: "${app.appName}"`, tags: [ ['a', appCoordinate, action === 'hide' ? 'spam' : 'other'], ['p', app.pubkey], ['L', 'soapbox.moderation'], ['l', action === 'hide' ? 'hidden' : 'unhidden', 'soapbox.moderation'] ], created_at: Math.floor(Date.now() / 1000) }); } // Invalidate queries to refresh the data queryClient.invalidateQueries({ queryKey: ['app-submissions'] }); // If this is a Halloween app, also invalidate Halloween queries if (isHalloweenSubmission) { queryClient.invalidateQueries({ queryKey: ['halloween-submissions'] }); queryClient.invalidateQueries({ queryKey: ['halloween-submissions-with-contacts'] }); } toast({ title: 'Success!', description: `App "${app.appName}" ${action}d successfully` }); } catch (error) { console.error('Moderation action failed:', error); toast({ title: 'Error', description: `Failed to ${action} app "${app.appName}"`, variant: 'destructive' }); } }; return (
{/* App Screenshot */} {!imageError ? ( {`${app.appName} setImageError(true)} /> ) : (

Screenshot unavailable

)}
{/* Actions Menu */} {(isOwner || showModerationControls) && (
{isOwner && ( onEdit?.(app)}> Edit App )} {showModerationControls && isModerator && ( <> {isHalloweenSubmission && getContactInfo() && (getContactInfo()?.lightning || getContactInfo()?.email) && ( View Contact Info )} {!app.isApproved && ( handleModerationAction('approve')} disabled={isPending} > Approve )} {app.isApproved && !app.isFeatured && ( handleModerationAction('unapprove')} disabled={isPending} > Unapprove )} handleModerationAction(app.isFeatured ? 'unfeature' : 'feature')} disabled={isPending} > {app.isFeatured ? ( <> Un-feature ) : ( <> Feature )} handleModerationAction(app.isHomepage ? 'unhomepage' : 'homepage')} disabled={isPending} > {app.isHomepage ? ( <> Remove from Homepage ) : ( <> Add to Homepage )} handleModerationAction(app.isHidden ? 'unhide' : 'hide')} disabled={isPending} > {app.isHidden ? ( <> Unhide ) : ( <> Hide )} )}
)}
{/* App Icon */}
{`${app.appName} { const target = e.target as HTMLImageElement; target.style.display = 'none'; }} />

{app.appName.length > 50 ? `${app.appName.slice(0, 50)}...` : app.appName}

{app.description}

{/* Author Information */}
{authorData?.metadata?.picture ? ( {`${authorData.metadata.name { const target = e.target as HTMLImageElement; target.style.display = 'none'; target.parentElement!.innerHTML = '
'; }} /> ) : (
)}
by{' '} {authorData?.metadata?.name || authorData?.metadata?.display_name || 'Anonymous'}
{/* Spacer to push action links to bottom */}
{/* Action Links */}
); }