import { useState } from 'react'; import { Link } from 'react-router-dom'; import { nip19 } from 'nostr-tools'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Avatar, AvatarImage, AvatarFallback } from '@/components/ui/avatar'; import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useAuthor } from '@/hooks/useAuthor'; import { useToast } from '@/hooks/useToast'; import { useTraderRecommendations, useMyRecommendation, useRecommendationActions, } from '@/hooks/useTraderRecommendations'; import { genUserName } from '@/lib/genUserName'; import { ShieldCheck, Loader2 } from 'lucide-react'; function Recommender({ pubkey }: { pubkey: string }) { const author = useAuthor(pubkey); const name = author.data?.metadata?.name ?? genUserName(pubkey); return ( {name.slice(0, 2).toUpperCase()} {name} ); } /** * Trusted-trader recommendations for a profile (kind 31873). Shows who vouches * for this trader, and lets a logged-in viewer add their own recommendation. */ export function TradeRecommendationsCard({ pubkey, className }: { pubkey: string; className?: string }) { const { user } = useCurrentUser(); const { data: recs, isLoading } = useTraderRecommendations(pubkey); const { data: mine } = useMyRecommendation(pubkey); const { recommend } = useRecommendationActions(); const { toast } = useToast(); const [busy, setBusy] = useState(false); const isSelf = user?.pubkey === pubkey; const count = recs?.length ?? 0; // Nothing to show: no recommendations and the viewer can't add one. if (!isLoading && count === 0 && (!user || isSelf)) return null; const handleRecommend = async () => { setBusy(true); try { await recommend(pubkey); toast({ title: 'Recommendation published', description: 'Thanks for vouching.' }); } catch (e) { toast({ title: 'Could not recommend', description: e instanceof Error ? e.message : 'Something went wrong.', variant: 'destructive', }); } finally { setBusy(false); } }; return ( Trusted trader {count > 0 ? ( <> Recommended by {count} skater {count === 1 ? '' : 's'}. > ) : ( 'No recommendations yet.' )} {count > 0 && ( {recs!.slice(0, 8).map((r) => ( ))} )} {user && !isSelf && ( mine ? ( You recommend this trader ✓ ) : ( {busy ? : } Recommend as trusted trader ) )} ); }
{count > 0 ? ( <> Recommended by {count} skater {count === 1 ? '' : 's'}. > ) : ( 'No recommendations yet.' )}
You recommend this trader ✓