Show total zap amount (sats) instead of zap count on notes
- useEventStats now sums the `amount` tag (millisatoshis) from kind 9735 zap receipts and returns `zapAmount` in sats instead of a zap count - NoteCard displays the total with compact formatting (e.g. 1.2K, 3.5M) Co-authored-by: shakespeare.diy <assistant@shakespeare.diy>
This commit is contained in:
@@ -17,6 +17,13 @@ interface NoteCardProps {
|
||||
className?: string;
|
||||
}
|
||||
|
||||
/** Formats a sats amount into a compact human-readable string. */
|
||||
function formatSats(sats: number): string {
|
||||
if (sats >= 1_000_000) return `${(sats / 1_000_000).toFixed(1).replace(/\.0$/, '')}M`;
|
||||
if (sats >= 1_000) return `${(sats / 1_000).toFixed(1).replace(/\.0$/, '')}K`;
|
||||
return sats.toString();
|
||||
}
|
||||
|
||||
/** Extracts image URLs from note content. */
|
||||
function extractImages(content: string): string[] {
|
||||
const urlRegex = /https?:\/\/[^\s]+\.(jpg|jpeg|png|gif|webp|svg)(\?[^\s]*)?/gi;
|
||||
@@ -164,7 +171,7 @@ export function NoteCard({ event, className }: NoteCardProps) {
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
<Zap className="size-[18px]" />
|
||||
{stats?.zaps ? <span className="text-xs">{stats.zaps}</span> : null}
|
||||
{stats?.zapAmount ? <span className="text-xs">{formatSats(stats.zapAmount)}</span> : null}
|
||||
</button>
|
||||
|
||||
{/* More */}
|
||||
|
||||
@@ -77,18 +77,27 @@ export function useEventStats(eventId: string | undefined) {
|
||||
let replies = 0;
|
||||
let reposts = 0;
|
||||
let reactions = 0;
|
||||
let zaps = 0;
|
||||
let zapAmount = 0;
|
||||
|
||||
for (const e of events) {
|
||||
switch (e.kind) {
|
||||
case 1: replies++; break;
|
||||
case 6: reposts++; break;
|
||||
case 7: reactions++; break;
|
||||
case 9735: zaps++; break;
|
||||
case 9735: {
|
||||
const amountTag = e.tags.find(([name]) => name === 'amount');
|
||||
if (amountTag?.[1]) {
|
||||
const msats = parseInt(amountTag[1], 10);
|
||||
if (!isNaN(msats)) {
|
||||
zapAmount += Math.floor(msats / 1000);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { replies, reposts, reactions, zaps };
|
||||
return { replies, reposts, reactions, zapAmount };
|
||||
},
|
||||
enabled: !!eventId,
|
||||
staleTime: 60 * 1000,
|
||||
|
||||
Reference in New Issue
Block a user