mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 08:23:34 +00:00
award dates and progress bars
This commit is contained in:
1 parent
bc19d08a8a
commit
94dfe21bac
3 files changed
+121
-30
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { Dialog, DialogContent } from "@/components/ui/dialog";
|
||||
import { Dialog, DialogContent, DialogTitle, DialogDescription } from "@/components/ui/dialog";
|
||||
import { Award } from "lucide-react";
|
||||
import type { NostrEvent } from '@nostrify/nostrify';
|
||||
import { useNostr } from '@nostrify/react';
|
||||
@@ -33,8 +33,6 @@ export function BadgeAwardModal({ award, badgeDefinition: providedBadgeDefinitio
|
||||
}
|
||||
}
|
||||
|
||||
if (!dTag && !providedBadgeDefinition) return null;
|
||||
|
||||
// Fetch badge definition if we don't have it directly
|
||||
const { data: fetchedBadgeDefinition, isLoading } = useQuery({
|
||||
queryKey: ['badge-definition', BADGE_ISSUER_PUBKEY, dTag],
|
||||
@@ -55,6 +53,9 @@ export function BadgeAwardModal({ award, badgeDefinition: providedBadgeDefinitio
|
||||
const badgeDefinition = providedBadgeDefinition || fetchedBadgeDefinition;
|
||||
const isModalOpen = !!award || !!providedBadgeDefinition;
|
||||
|
||||
// Return null after all hooks if we don't have a badge definition
|
||||
if (!badgeDefinition) return null;
|
||||
|
||||
const nameTag = badgeDefinition?.tags?.find(([t]) => t === "name")?.[1];
|
||||
const descriptionTag = badgeDefinition?.tags?.find(([t]) => t === "description")?.[1];
|
||||
const imageTag = badgeDefinition?.tags?.find(([t]) => t === "image")?.[1];
|
||||
@@ -66,20 +67,26 @@ export function BadgeAwardModal({ award, badgeDefinition: providedBadgeDefinitio
|
||||
|
||||
return (
|
||||
<Dialog open={isModalOpen} onOpenChange={(open) => !open && onClose()}>
|
||||
<DialogContent className="w-[600px] h-[600px] aspect-square bg-transparent [&>button]:hidden border-0 ring-0 outline-none shadow-none flex items-center justify-center p-0">
|
||||
<div className="flex flex-col items-center justify-center space-y-8 w-full h-full px-8">
|
||||
<DialogContent className="w-[min(90vw,600px)] aspect-square bg-transparent [&>button]:hidden border-0 ring-0 outline-none shadow-none flex items-center justify-center p-[min(4vw,2rem)]">
|
||||
<DialogTitle className="sr-only">
|
||||
{award ? `Badge Awarded: ${badgeName}` : badgeName}
|
||||
</DialogTitle>
|
||||
<DialogDescription className="sr-only">
|
||||
{description || `Badge: ${badgeName}`}
|
||||
</DialogDescription>
|
||||
<div className="flex flex-col items-center justify-center space-y-[min(4vw,2rem)] w-full h-full">
|
||||
{isLoading ? (
|
||||
<>
|
||||
<Skeleton className="h-96 w-96 rounded-lg" />
|
||||
<div className="space-y-2 text-center">
|
||||
<Skeleton className="h-8 w-64 mx-auto" />
|
||||
<Skeleton className="h-5 w-80 mx-auto" />
|
||||
<Skeleton className="w-[min(60vw,384px)] aspect-square rounded-lg" />
|
||||
<div className="space-y-2 text-center w-full">
|
||||
<Skeleton className="h-[clamp(1.5rem,4vw,2rem)] w-[min(80%,16rem)] mx-auto" />
|
||||
<Skeleton className="h-[clamp(1rem,3vw,1.25rem)] w-[min(90%,20rem)] mx-auto" />
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
{/* Badge image */}
|
||||
<div className="relative aspect-square w-96 before:absolute before:inset-0 before:rounded-lg before:bg-white/30 before:blur-2xl before:animate-pulse before:-z-10">
|
||||
<div className="relative aspect-square w-[min(60vw,384px)] before:absolute before:inset-0 before:rounded-lg before:bg-white/30 before:blur-2xl before:animate-pulse before:-z-10">
|
||||
{imageUrl ? (
|
||||
<img
|
||||
src={imageUrl}
|
||||
@@ -92,11 +99,11 @@ export function BadgeAwardModal({ award, badgeDefinition: providedBadgeDefinitio
|
||||
</div>
|
||||
|
||||
{/* Title */}
|
||||
<div className="text-center space-y-3">
|
||||
{award && <h2 className="text-3xl font-bold">Badge Awarded!</h2>}
|
||||
<p className="text-2xl font-semibold">{badgeName}</p>
|
||||
<div className="text-center space-y-[min(1vw,0.75rem)] w-full px-4">
|
||||
{award && <h2 className="text-[clamp(1.5rem,6vw,1.875rem)] font-bold">Badge Awarded!</h2>}
|
||||
<p className="text-[clamp(1.25rem,5vw,1.5rem)] font-semibold">{badgeName}</p>
|
||||
{description && (
|
||||
<p className="text-base text-muted-foreground max-w-md">
|
||||
<p className="text-[clamp(0.875rem,3vw,1rem)] text-muted-foreground max-w-md mx-auto">
|
||||
{description}
|
||||
</p>
|
||||
)}
|
||||
|
||||
@@ -17,6 +17,8 @@ interface SettingsPageLayoutProps {
|
||||
children: ReactNode;
|
||||
/** Optional className for the content container */
|
||||
className?: string;
|
||||
/** Optional element to render next to the title */
|
||||
titleSuffix?: ReactNode;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -28,7 +30,8 @@ export function SettingsPageLayout({
|
||||
titleKey,
|
||||
descriptionKey,
|
||||
children,
|
||||
className,
|
||||
className = 'space-y-6',
|
||||
titleSuffix,
|
||||
}: SettingsPageLayoutProps) {
|
||||
const { t } = useTranslation();
|
||||
const isMobile = useIsMobile();
|
||||
@@ -48,9 +51,10 @@ export function SettingsPageLayout({
|
||||
{t('backToSettings')}
|
||||
</Button>
|
||||
<div className="space-y-2">
|
||||
<h1 className="text-2xl font-bold flex items-center gap-3">
|
||||
<h1 className="text-2xl font-bold flex flex-wrap items-center gap-3">
|
||||
<Icon className="h-6 w-6 text-primary" />
|
||||
{t(titleKey)}
|
||||
{titleSuffix}
|
||||
</h1>
|
||||
<p className="text-muted-foreground">
|
||||
{t(descriptionKey)}
|
||||
@@ -61,9 +65,10 @@ export function SettingsPageLayout({
|
||||
|
||||
{!isMobile && (
|
||||
<div className="space-y-2">
|
||||
<h1 className="text-2xl font-bold flex items-center gap-3">
|
||||
<h1 className="text-2xl font-bold flex flex-wrap items-center gap-3">
|
||||
<Icon className="h-6 w-6 text-primary" />
|
||||
{t(titleKey)}
|
||||
{titleSuffix}
|
||||
</h1>
|
||||
<p className="text-muted-foreground">
|
||||
{t(descriptionKey)}
|
||||
|
||||
@@ -2,6 +2,7 @@ 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 } from '@tanstack/react-query';
|
||||
import { Skeleton } from "@/components/ui/skeleton";
|
||||
@@ -16,10 +17,12 @@ const BADGE_ISSUER_PUBKEY = "804a5a94d972d2218d2cc8712881e6f00df09fe7a1a269ccbf9
|
||||
function BadgeCard({
|
||||
event,
|
||||
awarded,
|
||||
unlockDate,
|
||||
onClick
|
||||
}: {
|
||||
event: NostrEvent;
|
||||
awarded: boolean;
|
||||
unlockDate?: number | null;
|
||||
onClick?: () => void;
|
||||
}) {
|
||||
const dTag = event.tags.find(([t]) => t === "d")?.[1];
|
||||
@@ -32,6 +35,14 @@ function BadgeCard({
|
||||
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 (
|
||||
<Card
|
||||
className={cn(
|
||||
@@ -54,20 +65,25 @@ function BadgeCard({
|
||||
) : null}
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
{awarded ? (
|
||||
<CardTitle className="text-lg">
|
||||
{badgeName}
|
||||
</CardTitle>
|
||||
) : (
|
||||
<div className="flex items-center justify-center">
|
||||
<div className="h-7 flex items-center justify-center">
|
||||
{awarded ? (
|
||||
<CardTitle className="text-lg">
|
||||
{badgeName}
|
||||
</CardTitle>
|
||||
) : (
|
||||
<Lock className="h-5 w-5 text-muted-foreground" />
|
||||
</div>
|
||||
)}
|
||||
)}
|
||||
</div>
|
||||
{description && (
|
||||
<p className={cn("text-sm", 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>
|
||||
)}
|
||||
</div>
|
||||
</CardHeader>
|
||||
</Card>
|
||||
@@ -112,13 +128,17 @@ export function BadgesSettings() {
|
||||
enabled: !!userPubkey,
|
||||
});
|
||||
|
||||
// Create a map of awarded badges (a-tag -> true)
|
||||
const awardedBadges = new Set<string>();
|
||||
// Create a map of awarded badges (a-tag -> first award date)
|
||||
const awardedBadges = new Map<string, number>();
|
||||
if (badgeAwards) {
|
||||
for (const award of badgeAwards) {
|
||||
const aTag = award.tags.find(([t]) => t === "a")?.[1];
|
||||
if (aTag) {
|
||||
awardedBadges.add(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -132,13 +152,71 @@ export function BadgesSettings() {
|
||||
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 (
|
||||
<SettingsPageLayout
|
||||
icon={Award}
|
||||
titleKey="badges"
|
||||
descriptionKey="badgesDescription"
|
||||
titleSuffix={
|
||||
!isLoading && totalBadges > 0 ? (
|
||||
<div className="flex flex-wrap items-center gap-3 ml-3">
|
||||
<Card className="bg-muted/50 border">
|
||||
<CardContent className="py-1.5 px-4">
|
||||
<p className="text-sm font-medium">
|
||||
<span className="text-foreground">{unlockedBadges}</span>
|
||||
<span className="text-muted-foreground"> / </span>
|
||||
<span className="text-foreground">{totalBadges}</span>
|
||||
<span className="text-muted-foreground"> unlocked</span>
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<Card className="bg-muted/50 border">
|
||||
<CardContent className="py-1.5 px-4">
|
||||
<div className="flex items-center gap-2">
|
||||
<Progress value={progressPercentage} className="h-1.5 w-24" />
|
||||
<span className="text-sm font-medium">{Math.round(progressPercentage)}%</span>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
) : undefined
|
||||
}
|
||||
>
|
||||
<BadgeAwardModal
|
||||
badgeDefinition={selectedBadge}
|
||||
@@ -162,13 +240,14 @@ export function BadgesSettings() {
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
) : badgeDefinitions && badgeDefinitions.length > 0 ? (
|
||||
) : sortedBadgeDefinitions && sortedBadgeDefinitions.length > 0 ? (
|
||||
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
|
||||
{badgeDefinitions.map((event) => (
|
||||
{sortedBadgeDefinitions.map((event) => (
|
||||
<BadgeCard
|
||||
key={event.id}
|
||||
event={event}
|
||||
awarded={isBadgeAwarded(event)}
|
||||
unlockDate={getFirstAwardDate(event)}
|
||||
onClick={isBadgeAwarded(event) ? () => setSelectedBadge(event) : undefined}
|
||||
/>
|
||||
))}
|
||||
|
||||
Reference in new issue
Block a user