import { useState } from 'react'; import { Link } from 'react-router-dom'; import { useSeoMeta } from '@unhead/react'; import { nip19 } from 'nostr-tools'; import { Layout } from '@/components/Layout'; import { Card, CardContent } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { Skeleton } from '@/components/ui/skeleton'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { LoginArea } from '@/components/auth/LoginArea'; import { TradeReputationCard } from '@/components/TradeReputationCard'; import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useAuthor } from '@/hooks/useAuthor'; import { useToast } from '@/hooks/useToast'; import { useIncomingTradeRequests, usePendingTradeConfirmations, useTradeReputation, useTradeAttestation, parseListingCoord, } from '@/hooks/useTradeAttestations'; import { genUserName } from '@/lib/genUserName'; import { Handshake, CheckCircle, Loader2, Package, ArrowRight } from 'lucide-react'; function PeerName({ pubkey }: { pubkey: string }) { const author = useAuthor(pubkey); const name = author.data?.metadata?.name ?? genUserName(pubkey); return ( {name} ); } function listingUrl(pubkey: string, identifier: string): string { return `/listing/${pubkey}/${identifier}`; } /** * `/trades` — one place to manage marketplace trades: incoming confirmation * requests (as seller), pending confirmations (as buyer), and your own trade * attestation records. Mirrors the DM banners, in a dedicated surface. */ export default function TradesPage() { const { user } = useCurrentUser(); const { toast } = useToast(); const { publishTrade, isPending: publishing } = useTradeAttestation(); const { data: incoming, isLoading: loadingIncoming } = useIncomingTradeRequests(user?.pubkey); const { data: pending, isLoading: loadingPending } = usePendingTradeConfirmations(user?.pubkey); const { data: reputation, isLoading: loadingRep } = useTradeReputation(user?.pubkey); const [busy, setBusy] = useState(null); const [revokeTarget, setRevokeTarget] = useState<{ listing: { pubkey: string; identifier: string }; role: 'sold' | 'bought'; counterparty: string; } | null>(null); const [revoking, setRevoking] = useState(false); useSeoMeta({ title: 'My Trades — SkateSpots', description: 'Manage your marketplace trades, confirmations and reputation.', }); const handleConfirmSale = async ( listing: { pubkey: string; identifier: string }, requestor: string, requestCoord: string, ) => { setBusy(requestCoord); try { await publishTrade({ listing, role: 'sold', counterparty: requestor, requestCoord, }); toast({ title: 'Sale confirmed', description: 'They can now confirm their side.' }); } catch (e) { toast({ title: 'Could not confirm the sale', description: e instanceof Error ? e.message : 'Something went wrong.', variant: 'destructive', }); } finally { setBusy(null); } }; const handleConfirmPurchase = async ( listing: { pubkey: string; identifier: string }, seller: string, coord: string, ) => { setBusy(coord); try { await publishTrade({ listing, role: 'bought', counterparty: seller }); toast({ title: 'Trade confirmed', description: 'Reputation updated for both of you.' }); } catch (e) { toast({ title: 'Could not confirm the trade', description: e instanceof Error ? e.message : 'Something went wrong.', variant: 'destructive', }); } finally { setBusy(null); } }; const handleRevoke = async () => { if (!revokeTarget) return; setRevoking(true); try { await publishTrade({ listing: revokeTarget.listing, role: revokeTarget.role, counterparty: revokeTarget.counterparty, state: 'revoked', note: 'Retracted by the author.', }); toast({ title: 'Trade record revoked', description: 'It no longer counts toward reputation.', }); setRevokeTarget(null); } catch (e) { toast({ title: 'Could not revoke the trade', description: e instanceof Error ? e.message : 'Something went wrong.', variant: 'destructive', }); } finally { setRevoking(false); } }; if (!user) { return (

My Trades

Log in to manage your marketplace trades and reputation.

); } // Attestations this user authored (their own trade records). const authored = (reputation?.attestations ?? []).filter((e) => e.pubkey === user.pubkey); const actionNeeded = (incoming?.length ?? 0) + (pending?.length ?? 0); return (

My Trades

Confirmations, requests, and the reputation they build. Payment is arranged with the other skater — the app only records the deal.

{/* Action needed */}

Action needed {actionNeeded > 0 && `· ${actionNeeded}`}

{(loadingIncoming || loadingPending) && (
)} {/* Incoming: someone asked you (seller) to record a sale */} {incoming?.map((r) => (

asked you to confirm a sale

View listing
))} {/* Pending: a seller recorded a sale you haven't confirmed */} {pending?.map((t) => (

recorded a sale — confirm your side

View listing
))} {!loadingIncoming && !loadingPending && actionNeeded === 0 && (

Nothing waiting on you right now. 🛹

)}
{/* Your trade records */}

Your trade records

{loadingRep && } {!loadingRep && authored.length === 0 && (

No trades yet. Close a deal and confirm it to start building reputation.

)} {authored.map((ev) => { const coord = ev.tags.find(([t]) => t === 'a')?.[1]; const parsed = parseListingCoord(coord); const d = ev.tags.find(([t]) => t === 'd')?.[1] ?? ''; const roleTyped: 'sold' | 'bought' | null = d.includes(':trade:sold:') ? 'sold' : d.includes(':trade:bought:') ? 'bought' : null; const role = roleTyped === 'sold' ? 'Sold' : roleTyped === 'bought' ? 'Bought' : 'Trade'; const state = ev.tags.find(([t]) => t === 's')?.[1] ?? 'valid'; const counterparty = ev.tags.find(([t]) => t === 'p')?.[1]; return ( {role}

{counterparty ? <>with : 'trade'}

{new Date(ev.created_at * 1000).toLocaleDateString()} {state !== 'valid' && ` · ${state}`}

{parsed && ( View listing )} {roleTyped && counterparty && parsed && ( )}
); })}
!o && setRevokeTarget(null)}> Revoke this trade record? It will stop counting toward your reputation. The other party keeps their own attestation. Use this for a disputed or mistaken deal.
); }