diff --git a/src/locales/en.json b/src/locales/en.json index 8157e801..fdd1b5e7 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -564,5 +564,8 @@ "resume": "Resume", "badges": "Badges", "badgesDescription": "View and manage your earned badges and achievements", - "noBadgesFound": "No badges found" + "noBadgesFound": "No badges found", + "tokenProgress": "Tokens", + "imageProgress": "Images", + "nextBadgeAt": "Next badge at {{milestone}}" } diff --git a/src/pages/BadgesSettings.tsx b/src/pages/BadgesSettings.tsx index f5d22dca..e03a3b4e 100644 --- a/src/pages/BadgesSettings.tsx +++ b/src/pages/BadgesSettings.tsx @@ -4,16 +4,66 @@ 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 } from '@tanstack/react-query'; +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 } from 'react'; +import { useState, useMemo } from 'react'; import type { NostrEvent } from '@nostrify/nostrify'; +import { createAIClient } from '@/lib/ai-client'; const BADGE_ISSUER_PUBKEY = "804a5a94d972d2218d2cc8712881e6f00df09fe7a1a269ccbf916e5c8c17efcc"; +// 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, @@ -46,12 +96,12 @@ function BadgeCard({ return ( - +
{imageUrl ? ( ) : null}
-
-
+
+ {/* Title area - fixed height for 2 lines */} +
{awarded ? ( - + {badgeName} ) : ( - + )}
+ {/* Description area - fixed height for 2 lines */} {description && ( -

+

{description}

)} - {awarded && formattedDate && ( -

- Unlocked {formattedDate} -

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

+ Unlocked {formattedDate} +

+ )} +
@@ -94,8 +152,46 @@ 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], @@ -222,6 +318,62 @@ export function BadgesSettings() { badgeDefinition={selectedBadge} onClose={() => 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 ? (