From 94dfe21bac23eab4ec4aed2f6511b26fadfed61a Mon Sep 17 00:00:00 2001 From: lemonknowsall Date: Tue, 30 Dec 2025 22:56:31 -0800 Subject: [PATCH] award dates and progress bars --- src/components/BadgeAwardModal.tsx | 35 +++++---- src/components/SettingsPageLayout.tsx | 11 ++- src/pages/BadgesSettings.tsx | 105 ++++++++++++++++++++++---- 3 files changed, 121 insertions(+), 30 deletions(-) diff --git a/src/components/BadgeAwardModal.tsx b/src/components/BadgeAwardModal.tsx index 0721cb64..67538acc 100644 --- a/src/components/BadgeAwardModal.tsx +++ b/src/components/BadgeAwardModal.tsx @@ -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 ( !open && onClose()}> - -
+ + + {award ? `Badge Awarded: ${badgeName}` : badgeName} + + + {description || `Badge: ${badgeName}`} + +
{isLoading ? ( <> - -
- - + +
+ +
) : ( <> {/* Badge image */} -
+
{imageUrl ? ( {/* Title */} -
- {award &&

Badge Awarded!

} -

{badgeName}

+
+ {award &&

Badge Awarded!

} +

{badgeName}

{description && ( -

+

{description}

)} diff --git a/src/components/SettingsPageLayout.tsx b/src/components/SettingsPageLayout.tsx index 683a2be6..a0c22405 100644 --- a/src/components/SettingsPageLayout.tsx +++ b/src/components/SettingsPageLayout.tsx @@ -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')}
-

+

{t(titleKey)} + {titleSuffix}

{t(descriptionKey)} @@ -61,9 +65,10 @@ export function SettingsPageLayout({ {!isMobile && (

-

+

{t(titleKey)} + {titleSuffix}

{t(descriptionKey)} diff --git a/src/pages/BadgesSettings.tsx b/src/pages/BadgesSettings.tsx index 42d26464..2c468896 100644 --- a/src/pages/BadgesSettings.tsx +++ b/src/pages/BadgesSettings.tsx @@ -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 (

- {awarded ? ( - - {badgeName} - - ) : ( -
+
+ {awarded ? ( + + {badgeName} + + ) : ( -
- )} + )} +
{description && (

{description}

)} + {awarded && formattedDate && ( +

+ Unlocked {formattedDate} +

+ )}
@@ -112,13 +128,17 @@ export function BadgesSettings() { enabled: !!userPubkey, }); - // Create a map of awarded badges (a-tag -> true) - const awardedBadges = new Set(); + // 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) { - 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 ( 0 ? ( +
+ + +

+ {unlockedBadges} + / + {totalBadges} + unlocked +

+
+
+ + +
+ + {Math.round(progressPercentage)}% +
+
+
+
+ ) : undefined + } > ))}
- ) : badgeDefinitions && badgeDefinitions.length > 0 ? ( + ) : sortedBadgeDefinitions && sortedBadgeDefinitions.length > 0 ? (
- {badgeDefinitions.map((event) => ( + {sortedBadgeDefinitions.map((event) => ( setSelectedBadge(event) : undefined} /> ))}