import { Award, Lock } from "lucide-react"; import { useTranslation } from 'react-i18next'; import { SettingsPageLayout } from '@/components/SettingsPageLayout'; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Progress } from "@/components/ui/progress"; import { useNostr } from '@nostrify/react'; import { useQuery, useQueryClient } from '@tanstack/react-query'; import { Skeleton } from "@/components/ui/skeleton"; import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useAISettings } from '@/hooks/useAISettings'; import { useAppContext } from '@/hooks/useAppContext'; import { BadgeAwardModal } from '@/components/BadgeAwardModal'; import { cn } from '@/lib/utils'; import { useState, useMemo } from 'react'; import type { NostrEvent } from '@nostrify/nostrify'; import { createAIClient } from '@/lib/ai-client'; import { nip19 } from 'nostr-tools'; const BADGE_ISSUER_NPUB = "npub1e0u5dfurw3dmd0n5lul873c5g3p89et6x372sa35spajcxjh4f6svcvahk"; const BADGE_ISSUER_PUBKEY = nip19.decode(BADGE_ISSUER_NPUB).data as string; // Badge sync response from the server interface BadgeSyncResponse { object: 'badge_sync'; synced: string[]; stats: { total_tokens: number; image_count: number; has_lightning_payment: boolean; giftcards_sent: number; max_giftcard_amount: number; }; } // Milestones for progress tracking const TOKEN_MILESTONES = [100_000, 1_000_000, 1_000_000_000, 10_000_000_000]; const IMAGE_MILESTONES = [1, 10, 100, 1_000]; function formatNumber(num: number): string { if (num >= 1_000_000_000) return `${(num / 1_000_000_000).toFixed(1)}B`; if (num >= 1_000_000) return `${(num / 1_000_000).toFixed(1)}M`; if (num >= 1_000) return `${(num / 1_000).toFixed(1)}K`; return num.toString(); } function getNextMilestone(current: number, milestones: number[]): number | null { for (const milestone of milestones) { if (current < milestone) return milestone; } return null; } function getMilestoneProgress(current: number, milestones: number[]): { progress: number; current: number; next: number | null } { const next = getNextMilestone(current, milestones); if (!next) { // All milestones achieved return { progress: 100, current, next: null }; } // Find the previous milestone (or 0 if none) const prevMilestone = milestones.filter(m => m <= current).pop() ?? 0; const range = next - prevMilestone; const progressInRange = current - prevMilestone; const progress = Math.min(100, (progressInRange / range) * 100); return { progress, current, next }; } function BadgeCard({ event, awarded, unlockDate, onClick }: { event: NostrEvent; awarded: boolean; unlockDate?: number | null; onClick?: () => void; }) { const dTag = event.tags.find(([t]) => t === "d")?.[1]; const nameTag = event.tags.find(([t]) => t === "name")?.[1]; const descriptionTag = event.tags.find(([t]) => t === "description")?.[1]; const imageTag = event.tags.find(([t]) => t === "image")?.[1]; const thumbTags = event.tags.filter(([t]) => t === "thumb"); const badgeName = nameTag || dTag || "Unnamed Badge"; const description = descriptionTag || ""; const imageUrl = imageTag || (thumbTags.length > 0 ? thumbTags[thumbTags.length - 1]?.[1] : null); const formattedDate = unlockDate ? new Date(unlockDate * 1000).toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric' }) : null; return (
{imageUrl ? ( {badgeName} ) : null}
{/* Title area - fixed height for 2 lines */}
{awarded ? ( {badgeName} ) : ( )}
{/* Description area - fixed height for 2 lines */} {description && (

{description}

)} {/* Unlock date - pushed to bottom */}
{awarded && formattedDate && (

Unlocked {formattedDate}

)}
); } export function BadgesSettings() { const { t } = useTranslation(); const { nostr } = useNostr(); const { user } = useCurrentUser(); const { settings } = useAISettings(); const { config } = useAppContext(); const queryClient = useQueryClient(); const userPubkey = user?.pubkey; const [selectedBadge, setSelectedBadge] = useState(null); const [progressIndex, setProgressIndex] = useState(0); // Find a Shakespeare provider (one with nostr auth) for badge sync const shakespeareProvider = useMemo( () => settings.providers.find(p => p.nostr), [settings.providers] ); // Badge sync query - calls the server to sync badges and get stats const { data: syncData } = useQuery({ queryKey: ['badge-sync', userPubkey, shakespeareProvider?.id], queryFn: async (): Promise => { if (!shakespeareProvider || !user) { throw new Error('No provider or user available'); } const ai = createAIClient(shakespeareProvider, user, config.corsProxy); const response = await ai.post('/badges/sync', {}) as BadgeSyncResponse; // If any badges were synced, invalidate the badge awards query if (response.synced.length > 0) { queryClient.invalidateQueries({ queryKey: ['badge-awards', BADGE_ISSUER_PUBKEY, userPubkey] }); } return response; }, enabled: !!user && !!shakespeareProvider, staleTime: 0, // Always refetch on mount for fresh progress gcTime: 5 * 60 * 1000, // Keep in cache for 5 min retry: 1, }); // Get stats from sync response const stats = syncData?.stats; const tokenProgress = stats ? getMilestoneProgress(stats.total_tokens, TOKEN_MILESTONES) : null; const imageProgress = stats ? getMilestoneProgress(stats.image_count, IMAGE_MILESTONES) : null; const { data: badgeDefinitions, isLoading: isLoadingDefinitions } = useQuery({ queryKey: ['badge-definitions', BADGE_ISSUER_PUBKEY], queryFn: async (c) => { const signal = AbortSignal.any([c.signal, AbortSignal.timeout(5000)]); const events = await nostr.query([{ kinds: [30009], authors: [BADGE_ISSUER_PUBKEY], limit: 100, }], { signal }); return events; }, }); // Query for badge awards if user is logged in const { data: badgeAwards, isLoading: isLoadingAwards } = useQuery({ queryKey: ['badge-awards', BADGE_ISSUER_PUBKEY, userPubkey], queryFn: async (c) => { if (!userPubkey) return []; const signal = AbortSignal.any([c.signal, AbortSignal.timeout(5000)]); const events = await nostr.query([{ kinds: [8], authors: [BADGE_ISSUER_PUBKEY], "#p": [userPubkey], limit: 100, }], { signal }); return events; }, enabled: !!userPubkey, }); // Create a map of awarded badges (a-tag -> first award date) const awardedBadges = new Map(); if (badgeAwards) { for (const award of badgeAwards) { const aTag = award.tags.find(([t]) => t === "a")?.[1]; if (aTag) { // Store the earliest award date for this badge const existingDate = awardedBadges.get(aTag); if (!existingDate || award.created_at < existingDate) { awardedBadges.set(aTag, award.created_at); } } } } // Helper to check if a badge is awarded const isBadgeAwarded = (badgeDefinition: NostrEvent): boolean => { if (!userPubkey) return false; const dTag = badgeDefinition.tags.find(([t]) => t === "d")?.[1]; if (!dTag) return false; const aTag = `30009:${BADGE_ISSUER_PUBKEY}:${dTag}`; return awardedBadges.has(aTag); }; // Helper to get first award date for a badge const getFirstAwardDate = (badgeDefinition: NostrEvent): number | null => { if (!userPubkey) return null; const dTag = badgeDefinition.tags.find(([t]) => t === "d")?.[1]; if (!dTag) return null; const aTag = `30009:${BADGE_ISSUER_PUBKEY}:${dTag}`; return awardedBadges.get(aTag) || null; }; // Sort badges: unlocked by first award date (earliest first), then locked badges const sortedBadgeDefinitions = badgeDefinitions ? [...badgeDefinitions].sort((a, b) => { const aAwarded = isBadgeAwarded(a); const bAwarded = isBadgeAwarded(b); // If both are awarded, sort by first award date (earliest first) if (aAwarded && bAwarded) { const aDate = getFirstAwardDate(a) || 0; const bDate = getFirstAwardDate(b) || 0; return aDate - bDate; } // If only one is awarded, awarded comes first if (aAwarded && !bAwarded) return -1; if (!aAwarded && bAwarded) return 1; // Both are locked, maintain original order return 0; }) : []; const isLoading = isLoadingDefinitions || (userPubkey && isLoadingAwards); // Calculate badge counts const totalBadges = sortedBadgeDefinitions.length; const unlockedBadges = sortedBadgeDefinitions.filter(badge => isBadgeAwarded(badge)).length; const progressPercentage = totalBadges > 0 ? (unlockedBadges / totalBadges) * 100 : 0; return ( 0 ? (

{unlockedBadges} / {totalBadges} unlocked

{Math.round(progressPercentage)}%
) : undefined } > setSelectedBadge(null)} /> {/* Progress Tracking Section */} {stats && (() => { const progressItems = [ tokenProgress?.next && { type: 'token', progress: tokenProgress }, imageProgress?.next && { type: 'image', progress: imageProgress }, ].filter(Boolean) as Array<{ type: 'token' | 'image'; progress: { progress: number; current: number; next: number } }>; if (progressItems.length === 0) return null; const currentIndex = progressIndex % progressItems.length; const current = progressItems[currentIndex]; return ( setProgressIndex(prev => prev + 1)} >
{current.type === 'token' ? t('tokenProgress') : t('imageProgress')} {current.type === 'token' ? `${formatNumber(current.progress.current)} / ${formatNumber(current.progress.next)}` : `${current.progress.current} / ${current.progress.next}` }

{t('nextBadgeAt', { milestone: formatNumber(current.progress.next) })}

{progressItems.length > 1 && (
{progressItems.map((_, i) => (
))}
)}
); })()}
{isLoading ? (
{[1, 2, 3].map((i) => (
))}
) : sortedBadgeDefinitions && sortedBadgeDefinitions.length > 0 ? (
{sortedBadgeDefinitions.map((event) => ( setSelectedBadge(event) : undefined} /> ))}
) : (

{t('noBadgesFound')}

)}
); } export default BadgesSettings;