mkstack/src/components/ZapButton.tsx

58 lines
1.8 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 || '');
2025-07-13 17:53:30 +00:00
const { webln, activeNWC } = useWallet();
2025-07-13 19:52:58 +00:00
// Only fetch data if not provided externally
2025-07-14 01:03:04 +00:00
const { totalSats: fetchedTotalSats, isLoading } = useZaps(
externalZapData ? [] : target ?? [], // Empty array prevents fetching if external data provided
2025-07-13 19:52:58 +00:00
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 || !target || user.pubkey === target.pubkey || (!author?.metadata?.lud16 && !author?.metadata?.lud06)) {
2025-07-13 17:53:30 +00:00
return null;
}
2025-07-13 19:52:58 +00:00
// Use external data if provided, otherwise use fetched data
2025-07-14 01:03:04 +00:00
const totalSats = externalZapData?.totalSats ?? fetchedTotalSats;
const showLoading = externalZapData?.isLoading || isLoading;
2025-07-13 17:53:30 +00:00
return (
<ZapDialog target={target}>
2025-07-14 01:03:04 +00:00
<div className={`flex items-center gap-1 ${className}`}>
<Zap className="h-4 w-4" />
2025-07-13 19:52:58 +00:00
<span className="text-xs">
{showLoading ? (
'...'
2025-07-14 01:03:04 +00:00
) : showCount && totalSats > 0 ? (
`${totalSats.toLocaleString()}`
2025-07-13 19:52:58 +00:00
) : (
'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>
);
}