mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
award progress tracker
This commit is contained in:
1 parent
064fba16d0
commit
27d7096cc2
2 files changed
+170
-15
No files matched your search
+4
-1
@@ -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}}"
|
||||
}
|
||||
+166
-14
@@ -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 (
|
||||
<Card
|
||||
className={cn(
|
||||
"transition-all bg-transparent border-none shadow-none",
|
||||
"transition-all bg-transparent border-none shadow-none h-full",
|
||||
awarded ? "cursor-pointer hover:scale-105" : ""
|
||||
)}
|
||||
onClick={awarded ? onClick : undefined}
|
||||
>
|
||||
<CardHeader className="text-center space-y-4 p-0">
|
||||
<CardHeader className="text-center p-0 h-full flex flex-col">
|
||||
<div className="flex justify-center">
|
||||
{imageUrl ? (
|
||||
<img
|
||||
@@ -64,26 +114,34 @@ function BadgeCard({
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<div className="h-7 flex items-center justify-center">
|
||||
<div className="flex flex-col flex-1 mt-4">
|
||||
{/* Title area - fixed height for 2 lines */}
|
||||
<div className="min-h-[3.5rem] flex items-start justify-center">
|
||||
{awarded ? (
|
||||
<CardTitle className="text-lg">
|
||||
<CardTitle className="text-lg leading-tight line-clamp-2">
|
||||
{badgeName}
|
||||
</CardTitle>
|
||||
) : (
|
||||
<Lock className="h-5 w-5 text-muted-foreground" />
|
||||
<Lock className="h-5 w-5 text-muted-foreground mt-1" />
|
||||
)}
|
||||
</div>
|
||||
{/* Description area - fixed height for 2 lines */}
|
||||
{description && (
|
||||
<p className={cn("text-sm", awarded ? "text-muted-foreground" : "text-muted-foreground/70")}>
|
||||
<p className={cn(
|
||||
"text-sm min-h-[2.5rem] line-clamp-2 mt-1",
|
||||
awarded ? "text-muted-foreground" : "text-muted-foreground/70"
|
||||
)}>
|
||||
{description}
|
||||
</p>
|
||||
)}
|
||||
{awarded && formattedDate && (
|
||||
<p className="text-xs text-muted-foreground/70 mt-1">
|
||||
Unlocked {formattedDate}
|
||||
</p>
|
||||
)}
|
||||
{/* Unlock date - pushed to bottom */}
|
||||
<div className="mt-auto pt-2">
|
||||
{awarded && formattedDate && (
|
||||
<p className="text-xs text-muted-foreground/70">
|
||||
Unlocked {formattedDate}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</CardHeader>
|
||||
</Card>
|
||||
@@ -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<NostrEvent | null>(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<BadgeSyncResponse> => {
|
||||
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 (
|
||||
<Card
|
||||
className="mb-6 cursor-pointer hover:bg-muted/50 transition-colors"
|
||||
onClick={() => setProgressIndex(prev => prev + 1)}
|
||||
>
|
||||
<CardContent className="py-4">
|
||||
<div className="space-y-2">
|
||||
<div className="flex justify-between items-center">
|
||||
<span className="text-sm font-medium">
|
||||
{current.type === 'token' ? t('tokenProgress') : t('imageProgress')}
|
||||
</span>
|
||||
<span className="text-xs text-muted-foreground">
|
||||
{current.type === 'token'
|
||||
? `${formatNumber(current.progress.current)} / ${formatNumber(current.progress.next)}`
|
||||
: `${current.progress.current} / ${current.progress.next}`
|
||||
}
|
||||
</span>
|
||||
</div>
|
||||
<Progress value={current.progress.progress} className="h-2" />
|
||||
<div className="flex justify-between items-center">
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{t('nextBadgeAt', { milestone: formatNumber(current.progress.next) })}
|
||||
</p>
|
||||
{progressItems.length > 1 && (
|
||||
<div className="flex gap-1">
|
||||
{progressItems.map((_, i) => (
|
||||
<div
|
||||
key={i}
|
||||
className={cn(
|
||||
"w-1.5 h-1.5 rounded-full transition-colors",
|
||||
i === currentIndex ? "bg-primary" : "bg-muted-foreground/30"
|
||||
)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
})()}
|
||||
|
||||
<div className="space-y-4">
|
||||
{isLoading ? (
|
||||
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
|
||||
|
||||
Reference in new issue
Block a user