mkstack/src/components/ZapButton.tsx

60 lines
1.9 KiB
TypeScript
Raw Normal View History

2025-07-13 17:53:30 +00:00
import { ZapDialog } from '@/components/ZapDialog';
import { useZaps } from '@/hooks/useZaps';
import { useWallet } from '@/hooks/useWallet';
import { useCurrentUser } from '@/hooks/useCurrentUser';
import { useAuthor } from '@/hooks/useAuthor';
2025-07-13 19:52:58 +00:00
import { Zap } from 'lucide-react';
2025-07-13 17:53:30 +00:00
import type { Event } from 'nostr-tools';
interface ZapButtonProps {
target: Event;
className?: string;
showCount?: boolean;
2025-07-13 19:52:58 +00:00
zapData?: { count: number; totalSats: number; isLoading?: boolean };
2025-07-13 17:53:30 +00:00
}
2025-07-13 19:52:58 +00:00
export function ZapButton({
target,
className = "text-xs ml-1",
showCount = true,
zapData: externalZapData
}: ZapButtonProps) {
2025-07-13 17:53:30 +00:00
const { user } = useCurrentUser();
const { data: author } = useAuthor(target.pubkey);
const { webln, activeNWC } = useWallet();
2025-07-13 19:52:58 +00:00
// Only fetch data if not provided externally
const { getZapData, isLoading } = useZaps(
externalZapData ? [] : target, // Empty array prevents fetching if external data provided
webln,
activeNWC
);
2025-07-13 17:53:30 +00:00
// 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;
}
2025-07-13 19:52:58 +00:00
// 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;
2025-07-13 17:53:30 +00:00
return (
<ZapDialog target={target}>
2025-07-13 19:52:58 +00:00
<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'
)}
2025-07-13 17:53:30 +00:00
</span>
2025-07-13 19:52:58 +00:00
</div>
2025-07-13 17:53:30 +00:00
</ZapDialog>
);
}