diff --git a/src/AppRouter.tsx b/src/AppRouter.tsx index dd0f5e75..29c2433e 100644 --- a/src/AppRouter.tsx +++ b/src/AppRouter.tsx @@ -21,6 +21,7 @@ import VercelOAuth from "./pages/VercelOAuth"; import { NIP19Page } from "./pages/NIP19Page"; import NotFound from "./pages/NotFound"; import { ProjectView } from "./pages/ProjectView"; +import ShowcasePage from "./pages/ShowcasePage"; import { SettingsLayout } from "./components/SettingsLayout"; export function AppRouter() { @@ -51,6 +52,7 @@ export function AppRouter() { } /> } /> + } /> {/* NIP-19 route for npub1, note1, naddr1, nevent1, nprofile1 */} } /> {/* ADD ALL CUSTOM ROUTES ABOVE THE CATCH-ALL "*" ROUTE */} diff --git a/src/components/AppShowcaseCard.tsx b/src/components/AppShowcaseCard.tsx index 4329163d..15bd46d0 100644 --- a/src/components/AppShowcaseCard.tsx +++ b/src/components/AppShowcaseCard.tsx @@ -370,14 +370,28 @@ export function AppShowcaseCard({ app, onEdit, showModerationControls, hideAppro } }; + // Generate naddr for the app + const appNaddr = React.useMemo(() => { + const dTag = app.tags.find(tag => tag[0] === 'd')?.[1]; + if (!dTag) return null; + + try { + return nip19.naddrEncode({ + kind: app.kind, + pubkey: app.pubkey, + identifier: dTag, + }); + } catch { + return null; + } + }, [app.kind, app.pubkey, app.tags]); + return (
{/* App Screenshot */} - {!imageError ? ( @@ -397,7 +411,7 @@ export function AppShowcaseCard({ app, onEdit, showModerationControls, hideAppro
)} - + {/* Actions Menu */} {(isOwner || showModerationControls) && ( diff --git a/src/pages/ShowcasePage.tsx b/src/pages/ShowcasePage.tsx new file mode 100644 index 00000000..53603f5d --- /dev/null +++ b/src/pages/ShowcasePage.tsx @@ -0,0 +1,949 @@ +import React, { useState } from 'react'; +import { useParams, Link, useNavigate } from 'react-router-dom'; +import { useNostr } from '@nostrify/react'; +import { useQuery } from '@tanstack/react-query'; +import { Button } from '@/components/ui/button'; +import { Card, CardContent, CardHeader } from '@/components/ui/card'; +import { Skeleton } from '@/components/ui/skeleton'; +import { Badge } from '@/components/ui/badge'; +import { CommentsSection } from '@/components/comments/CommentsSection'; +import { useAuthor } from '@/hooks/useAuthor'; +import { useCurrentUser } from '@/hooks/useCurrentUser'; +import { useNostrPublish } from '@/hooks/useNostrPublish'; +import { useToast } from '@/hooks/useToast'; +import { + ExternalLink, + User, + Star, + ArrowLeft, + Edit, + MoreVertical, + StarOff, + Eye, + EyeOff, + CheckCircle, + XCircle, + Home, +} from 'lucide-react'; +import { nip19 } from 'nostr-tools'; +import type { NostrEvent } from '@nostrify/nostrify'; +import { AppLayout } from '@/components/AppLayout'; +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuItem, + DropdownMenuTrigger, +} from '@/components/ui/dropdown-menu'; +import { useQueryClient } from '@tanstack/react-query'; + +// Kind 31733 for app submissions +const APP_SUBMISSION_KIND = 31733; + +// The moderator npub and hex +const MODERATOR_NPUB = 'npub1jvnpg4c6ljadf5t6ry0w9q0rnm4mksde87kglkrc993z46c39axsgq89sc'; +const MODERATOR_HEX = (() => { + try { + const decoded = nip19.decode(MODERATOR_NPUB); + return decoded.type === 'npub' ? decoded.data : ''; + } catch { + return ''; + } +})(); + +export default function ShowcasePage() { + const { id } = useParams<{ id: string }>(); + const navigate = useNavigate(); + const { nostr } = useNostr(); + const { user } = useCurrentUser(); + const { toast } = useToast(); + const { mutate: publishEvent, isPending } = useNostrPublish(); + const queryClient = useQueryClient(); + const [imageError, setImageError] = useState(false); + + // Decode the naddr to get the app submission + const appData = React.useMemo(() => { + if (!id) return null; + try { + const decoded = nip19.decode(id); + if (decoded.type === 'naddr') { + return decoded.data; + } + } catch (error) { + console.error('Failed to decode naddr:', error); + } + return null; + }, [id]); + + // Fetch the app submission event + const { data: appEvent, isLoading } = useQuery({ + queryKey: ['showcase-app', id], + queryFn: async () => { + if (!appData) return null; + + const events = await nostr.query( + [ + { + kinds: [appData.kind], + authors: [appData.pubkey], + '#d': [appData.identifier], + limit: 1, + }, + ], + { signal: AbortSignal.timeout(3000) } + ); + + return events[0] || null; + }, + enabled: !!appData, + }); + + // Fetch moderation lists + const { data: moderationData } = useQuery({ + queryKey: ['showcase-moderation', id], + queryFn: async () => { + if (!appEvent) return null; + + const [moderatorListEvents, reportEvents] = await Promise.all([ + nostr.query( + [ + { + kinds: [30267], + authors: [MODERATOR_HEX], + '#d': [ + 'soapbox-featured-apps', + 'soapbox-approved-apps', + 'soapbox-homepage-apps', + ], + limit: 3, + }, + ], + { signal: AbortSignal.timeout(1000) } + ), + nostr.query( + [ + { + kinds: [1984], + authors: [MODERATOR_HEX], + limit: 1000, + }, + ], + { signal: AbortSignal.timeout(1000) } + ), + ]); + + // Process moderation lists + const featuredList = moderatorListEvents.find((e) => + e.tags.find((tag) => tag[0] === 'd' && tag[1] === 'soapbox-featured-apps') + ); + const approvedList = moderatorListEvents.find((e) => + e.tags.find((tag) => tag[0] === 'd' && tag[1] === 'soapbox-approved-apps') + ); + const homepageList = moderatorListEvents.find((e) => + e.tags.find((tag) => tag[0] === 'd' && tag[1] === 'soapbox-homepage-apps') + ); + + const dTag = appEvent.tags.find((tag) => tag[0] === 'd')?.[1]; + const appCoordinate = `${APP_SUBMISSION_KIND}:${appEvent.pubkey}:${dTag}`; + + const isFeatured = featuredList?.tags.some( + (tag) => tag[0] === 'a' && tag[1] === appCoordinate + ); + const isApproved = approvedList?.tags.some( + (tag) => tag[0] === 'a' && tag[1] === appCoordinate + ); + const isHomepage = homepageList?.tags.some( + (tag) => tag[0] === 'a' && tag[1] === appCoordinate + ); + + // Check if hidden + const appReports = new Map(); + for (const report of reportEvents) { + const aTag = report.tags.find((tag) => tag[0] === 'a'); + if (!aTag || !aTag[1]) continue; + + const reportCoord = aTag[1]; + if (reportCoord !== appCoordinate) continue; + + const reportType = aTag[2]; + const moderationLabel = report.tags.find( + (tag) => tag[0] === 'l' && tag[2] === 'soapbox.moderation' + )?.[1]; + + if (moderationLabel === 'hidden' && (reportType === 'spam' || reportType === 'other')) { + if (!appReports.has(reportCoord)) { + appReports.set(reportCoord, { hidden: 0, unhidden: 0 }); + } + appReports.get(reportCoord)!.hidden = Math.max( + appReports.get(reportCoord)!.hidden, + report.created_at + ); + } else if (moderationLabel === 'unhidden' && reportType === 'other') { + if (!appReports.has(reportCoord)) { + appReports.set(reportCoord, { hidden: 0, unhidden: 0 }); + } + appReports.get(reportCoord)!.unhidden = Math.max( + appReports.get(reportCoord)!.unhidden, + report.created_at + ); + } + } + + const reports = appReports.get(appCoordinate); + const isHidden = reports && reports.hidden > reports.unhidden; + + return { isFeatured, isApproved, isHomepage, isHidden }; + }, + enabled: !!appEvent, + }); + + // Parse app data from event + const app = React.useMemo(() => { + if (!appEvent) return null; + + const appName = appEvent.tags.find((tag) => tag[0] === 'title')?.[1] || ''; + const websiteUrl = appEvent.tags.find((tag) => tag[0] === 'website')?.[1] || ''; + const repositoryUrl = appEvent.tags.find((tag) => tag[0] === 'repository')?.[1] || ''; + const appIconUrl = appEvent.tags.find((tag) => tag[0] === 'icon')?.[1] || ''; + const screenshotUrl = appEvent.tags.find((tag) => tag[0] === 'screenshot')?.[1] || ''; + const authorNpub = appEvent.tags.find((tag) => tag[0] === 'author')?.[1] || appEvent.pubkey; + const appTags = appEvent.tags.filter((tag) => tag[0] === 'app-tag').map((tag) => tag[1]); + + return { + appName, + websiteUrl, + repositoryUrl, + description: appEvent.content, + appIconUrl, + screenshotUrl, + authorNpub, + appTags, + }; + }, [appEvent]); + + // Get author pubkey + const authorPubkey = React.useMemo(() => { + if (!app?.authorNpub) return appEvent?.pubkey || ''; + + try { + if (app.authorNpub.length === 64 && /^[0-9a-f]+$/i.test(app.authorNpub)) { + return app.authorNpub; + } + + const decoded = nip19.decode(app.authorNpub); + return decoded.type === 'npub' ? decoded.data : appEvent?.pubkey || ''; + } catch { + return appEvent?.pubkey || ''; + } + }, [app?.authorNpub, appEvent?.pubkey]); + + const { data: authorData } = useAuthor(authorPubkey); + + const isOwner = user?.pubkey === appEvent?.pubkey; + const isModerator = user?.pubkey === MODERATOR_HEX; + + // Moderation actions + const handleModerationAction = async ( + action: 'feature' | 'unfeature' | 'approve' | 'unapprove' | 'hide' | 'unhide' | 'homepage' | 'unhomepage' + ) => { + if (!isModerator || !user || !appEvent) return; + + const dTag = appEvent.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_SUBMISSION_KIND}:${appEvent.pubkey}:${dTag}`; + + try { + if (action === 'homepage' || action === 'unhomepage') { + const currentHomepageLists = await nostr.query( + [ + { + kinds: [30267], + authors: [MODERATOR_HEX], + '#d': ['soapbox-homepage-apps'], + limit: 1, + }, + ], + { signal: AbortSignal.timeout(3000) } + ); + + const currentHomepageList = currentHomepageLists.reduce( + (latest: NostrEvent | null, current: NostrEvent) => + !latest || current.created_at > latest.created_at ? current : latest, + null as NostrEvent | null + ); + + const currentHomepageApps = new Set(); + if (currentHomepageList) { + currentHomepageList.tags + .filter((tag) => tag[0] === 'a') + .forEach((tag) => currentHomepageApps.add(tag[1])); + } + + if (action === 'homepage') { + currentHomepageApps.add(appCoordinate); + } else { + currentHomepageApps.delete(appCoordinate); + } + + 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 (action === 'homepage') { + const currentApprovedLists = await nostr.query( + [ + { + kinds: [30267], + authors: [MODERATOR_HEX], + '#d': ['soapbox-approved-apps'], + limit: 1, + }, + ], + { signal: AbortSignal.timeout(3000) } + ); + + const currentApprovedList = currentApprovedLists.reduce( + (latest: NostrEvent | null, current: NostrEvent) => + !latest || current.created_at > latest.created_at ? current : latest, + null as NostrEvent | null + ); + + const currentApprovedApps = new Set(); + if (currentApprovedList) { + currentApprovedList.tags + .filter((tag) => tag[0] === 'a') + .forEach((tag) => currentApprovedApps.add(tag[1])); + } + + currentApprovedApps.add(appCoordinate); + + 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') { + const currentFeaturedLists = await nostr.query( + [ + { + kinds: [30267], + authors: [MODERATOR_HEX], + '#d': ['soapbox-featured-apps'], + limit: 1, + }, + ], + { signal: AbortSignal.timeout(3000) } + ); + + const currentFeaturedList = currentFeaturedLists.reduce( + (latest: NostrEvent | null, current: NostrEvent) => + !latest || current.created_at > latest.created_at ? current : latest, + null as NostrEvent | null + ); + + const currentFeaturedApps = new Set(); + if (currentFeaturedList) { + currentFeaturedList.tags + .filter((tag) => tag[0] === 'a') + .forEach((tag) => currentFeaturedApps.add(tag[1])); + } + + if (action === 'feature') { + currentFeaturedApps.add(appCoordinate); + } else { + currentFeaturedApps.delete(appCoordinate); + } + + 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 (action === 'feature') { + const currentApprovedLists = await nostr.query( + [ + { + kinds: [30267], + authors: [MODERATOR_HEX], + '#d': ['soapbox-approved-apps'], + limit: 1, + }, + ], + { signal: AbortSignal.timeout(3000) } + ); + + const currentApprovedList = currentApprovedLists.reduce( + (latest: NostrEvent | null, current: NostrEvent) => + !latest || current.created_at > latest.created_at ? current : latest, + null as NostrEvent | null + ); + + const currentApprovedApps = new Set(); + if (currentApprovedList) { + currentApprovedList.tags + .filter((tag) => tag[0] === 'a') + .forEach((tag) => currentApprovedApps.add(tag[1])); + } + + currentApprovedApps.add(appCoordinate); + + 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') { + const currentApprovedLists = await nostr.query( + [ + { + kinds: [30267], + authors: [MODERATOR_HEX], + '#d': ['soapbox-approved-apps'], + limit: 1, + }, + ], + { signal: AbortSignal.timeout(3000) } + ); + + const currentApprovedList = currentApprovedLists.reduce( + (latest: NostrEvent | null, current: NostrEvent) => + !latest || current.created_at > latest.created_at ? current : latest, + null as NostrEvent | null + ); + + const currentApprovedApps = new Set(); + if (currentApprovedList) { + currentApprovedList.tags + .filter((tag) => tag[0] === 'a') + .forEach((tag) => currentApprovedApps.add(tag[1])); + } + + if (action === 'approve') { + currentApprovedApps.add(appCoordinate); + } else { + currentApprovedApps.delete(appCoordinate); + } + + 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') { + publishEvent({ + kind: 1984, + content: `App ${action}den by moderator: "${app?.appName}"`, + tags: [ + ['a', appCoordinate, action === 'hide' ? 'spam' : 'other'], + ['p', appEvent.pubkey], + ['L', 'soapbox.moderation'], + ['l', action === 'hide' ? 'hidden' : 'unhidden', 'soapbox.moderation'], + ], + created_at: Math.floor(Date.now() / 1000), + }); + } + + queryClient.invalidateQueries({ queryKey: ['showcase-moderation', id] }); + queryClient.invalidateQueries({ queryKey: ['app-submissions'] }); + + 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', + }); + } + }; + + if (!id || !appData) { + return ( + +
+ + +

Invalid showcase app identifier

+ +
+
+
+
+ ); + } + + if (isLoading || !appEvent || !app) { + return ( + +
+
+ +
+ + + + +
+ +
+ + +
+
+
+ + + +
+
+ + +
+
+ + +
+
+
+ + +
+
+ ); + } + + return ( + +
+ {/* Back button */} +
+ +
+ + {/* App Details Card */} + +
+ {/* Screenshot */} +
+ {!imageError ? ( + {`${app.appName} setImageError(true)} + /> + ) : ( +
+
+
+ +
+

Screenshot unavailable

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

{app.appName}

+
+
+ {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'} + + +
+
+
+ + {/* Description */} +

{app.description}

+ + {/* Tags */} + {app.appTags.length > 0 && ( +
+ {app.appTags.map((tag, index) => ( + + {tag} + + ))} +
+ )} + + {/* Action Buttons */} +
+ + Edit with Shakespeare + + +
+
+
+ + {/* Ratings Section */} + + +

+ + Ratings +

+
+ + + +
+ + {/* Comments Section */} + +
+
+ ); +} + +// Ratings Section Component +function RatingsSection({ appEvent }: { appEvent: NostrEvent }) { + const { nostr } = useNostr(); + const { user } = useCurrentUser(); + const { mutate: publishEvent } = useNostrPublish(); + const { toast } = useToast(); + + // Fetch ratings (kind 7 reactions with rating content) + const { data: ratings = [], isLoading } = useQuery({ + queryKey: ['ratings', appEvent.id], + queryFn: async () => { + const events = await nostr.query( + [ + { + kinds: [7], + '#e': [appEvent.id], + limit: 500, + }, + ], + { signal: AbortSignal.timeout(3000) } + ); + + // Filter for numeric ratings (1-5) + return events.filter((e) => { + const rating = parseInt(e.content); + return !isNaN(rating) && rating >= 1 && rating <= 5; + }); + }, + }); + + // Calculate average rating + const averageRating = React.useMemo(() => { + if (ratings.length === 0) return 0; + const sum = ratings.reduce((acc, r) => acc + parseInt(r.content), 0); + return sum / ratings.length; + }, [ratings]); + + // Check if user has already rated + const userRating = React.useMemo(() => { + if (!user) return null; + const userRatingEvent = ratings.find((r) => r.pubkey === user.pubkey); + return userRatingEvent ? parseInt(userRatingEvent.content) : null; + }, [ratings, user]); + + const handleRate = (rating: number) => { + if (!user) { + toast({ + title: 'Login Required', + description: 'Please log in to rate this app', + variant: 'destructive', + }); + return; + } + + publishEvent({ + kind: 7, + content: rating.toString(), + tags: [ + ['e', appEvent.id], + ['p', appEvent.pubkey], + ], + created_at: Math.floor(Date.now() / 1000), + }); + + toast({ + title: 'Rating Submitted!', + description: `You rated this app ${rating} star${rating > 1 ? 's' : ''}`, + }); + }; + + if (isLoading) { + return ( +
+ + +
+ ); + } + + return ( +
+ {/* Average Rating Display */} +
+
+ {averageRating > 0 ? averageRating.toFixed(1) : 'No ratings yet'} +
+ {averageRating > 0 && ( +
+ {Array.from({ length: 5 }).map((_, i) => ( + + ))} +
+ )} +

+ {ratings.length} {ratings.length === 1 ? 'rating' : 'ratings'} +

+
+ + {/* User Rating Input */} +
+

+ {userRating ? 'Your rating:' : 'Rate this app:'} +

+
+ {Array.from({ length: 5 }).map((_, i) => { + const starValue = i + 1; + return ( + + ); + })} +
+ {!user && ( +

Log in to rate this app

+ )} +
+
+ ); +}