further refinement for new app builds

This commit is contained in:
Chad Curtis committed 2025-07-13 19:52:58 +00:00
1 parent 45ff37e752
commit 8c34dbc8d5
4 files changed
+175 -63

No files matched your search

+32 -18
View File
@@ -3,45 +3,59 @@ import { useZaps } from '@/hooks/useZaps';
import { useWallet } from '@/hooks/useWallet';
import { useCurrentUser } from '@/hooks/useCurrentUser';
import { useAuthor } from '@/hooks/useAuthor';
import { Zap } from 'lucide-react';
import type { Event } from 'nostr-tools';
interface ZapButtonProps {
target: Event;
className?: string;
showCount?: boolean;
// New: option to pass pre-fetched zap data (for batch mode)
zapData?: { count: number; totalSats: number; isLoading?: boolean };
}
export function ZapButton({ target, className = "text-xs ml-1", showCount = true }: ZapButtonProps) {
export function ZapButton({
target,
className = "text-xs ml-1",
showCount = true,
zapData: externalZapData
}: ZapButtonProps) {
const { user } = useCurrentUser();
const { data: author } = useAuthor(target.pubkey);
const { webln, activeNWC } = useWallet();
const { zaps } = useZaps(target, webln, activeNWC);
// Only fetch data if not provided externally
const { getZapData, isLoading } = useZaps(
externalZapData ? [] : target, // Empty array prevents fetching if external data provided
webln,
activeNWC
);
// Don't show zap button if user is not logged in, is the author, or author has no lightning address
if (!user || user.pubkey === target.pubkey || (!author?.metadata?.lud16 && !author?.metadata?.lud06)) {
return null;
}
const zapCount = zaps?.length || 0;
const totalSats = zaps?.reduce((total, zap) => {
// Extract amount from amount tag
const amountTag = zap.tags.find(([name]) => name === 'amount')?.[1];
if (amountTag) {
return total + Math.floor(parseInt(amountTag) / 1000); // Convert millisats to sats
}
// If no amount tag, don't count towards total
return total;
}, 0) || 0;
// Use external data if provided, otherwise use fetched data
const zapInfo = externalZapData || getZapData(target.id);
const { count: zapCount, totalSats } = zapInfo;
const dataLoading = 'isLoading' in zapInfo ? zapInfo.isLoading : false;
const showLoading = externalZapData?.isLoading || dataLoading || isLoading;
return (
<ZapDialog target={target}>
{showCount && zapCount > 0 && (
<span className={className}>
{totalSats > 0 ? `${totalSats.toLocaleString()}` : zapCount}
<div className={className}>
<Zap className="h-4 w-4 mr-1" />
<span className="text-xs">
{showLoading ? (
'...'
) : showCount && zapCount > 0 ? (
totalSats > 0 ? `${totalSats.toLocaleString()}` : zapCount
) : (
'Zap'
)}
</span>
)}
</div>
</ZapDialog>
);
}
+2 -3
View File
@@ -111,10 +111,9 @@ export function ZapDialog({ target, children, className }: ZapDialogProps) {
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button variant="ghost" size="sm" className={`text-muted-foreground hover:text-yellow-600 ${className || ''}`}>
<Zap className={`h-4 w-4 ${children ? 'mr-1' : ''}`} />
<div className={`cursor-pointer ${className || ''}`}>
{children}
</Button>
</div>
</DialogTrigger>
<DialogContent className="sm:max-w-[425px]" data-testid="zap-modal">
<DialogHeader>