2025-07-12 19:23:50 +00:00
|
|
|
import { useState, useEffect, useRef } from 'react';
|
2025-07-13 06:47:45 +00:00
|
|
|
import { Zap, Copy, Sparkle, Sparkles, Star, Rocket, Wallet, Globe } from 'lucide-react';
|
2025-07-12 19:19:41 +00:00
|
|
|
import { Button } from '@/components/ui/button';
|
|
|
|
import {
|
|
|
|
Dialog,
|
|
|
|
DialogContent,
|
|
|
|
DialogDescription,
|
|
|
|
DialogFooter,
|
|
|
|
DialogHeader,
|
|
|
|
DialogTitle,
|
2025-07-12 19:23:50 +00:00
|
|
|
DialogTrigger,
|
2025-07-12 19:19:41 +00:00
|
|
|
} from '@/components/ui/dialog';
|
|
|
|
import { Input } from '@/components/ui/input';
|
|
|
|
import { Textarea } from '@/components/ui/textarea';
|
|
|
|
import { ToggleGroup, ToggleGroupItem } from '@/components/ui/toggle-group';
|
2025-07-12 19:23:50 +00:00
|
|
|
import { useCurrentUser } from '@/hooks/useCurrentUser';
|
|
|
|
import { useAuthor } from '@/hooks/useAuthor';
|
|
|
|
import { useToast } from '@/hooks/useToast';
|
|
|
|
import { useZaps } from '@/hooks/useZaps';
|
2025-07-13 06:47:45 +00:00
|
|
|
import { useWallet } from '@/hooks/useWallet';
|
2025-07-12 19:23:50 +00:00
|
|
|
import QRCode from 'qrcode';
|
2025-07-13 04:09:32 +00:00
|
|
|
import type { Event } from 'nostr-tools';
|
2025-07-12 19:19:41 +00:00
|
|
|
|
2025-07-12 19:23:50 +00:00
|
|
|
interface ZapDialogProps {
|
2025-07-13 04:09:32 +00:00
|
|
|
target: Event;
|
2025-07-12 19:23:50 +00:00
|
|
|
children?: React.ReactNode;
|
|
|
|
className?: string;
|
2025-07-12 19:19:41 +00:00
|
|
|
}
|
|
|
|
|
2025-07-13 05:59:56 +00:00
|
|
|
const presetAmounts = [
|
|
|
|
{ amount: 1, icon: Sparkle },
|
|
|
|
{ amount: 50, icon: Sparkles },
|
|
|
|
{ amount: 100, icon: Zap },
|
|
|
|
{ amount: 250, icon: Star },
|
|
|
|
{ amount: 1000, icon: Rocket },
|
|
|
|
];
|
2025-07-12 19:19:41 +00:00
|
|
|
|
2025-07-12 19:23:50 +00:00
|
|
|
export function ZapDialog({ target, children, className }: ZapDialogProps) {
|
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
const { user } = useCurrentUser();
|
|
|
|
const { data: author } = useAuthor(target.pubkey);
|
2025-07-12 19:19:41 +00:00
|
|
|
const { toast } = useToast();
|
2025-07-13 06:47:45 +00:00
|
|
|
const { webln, activeNWC, hasWebLN, hasNWC, detectWebLN } = useWallet();
|
2025-07-13 17:53:30 +00:00
|
|
|
|
|
|
|
// Debug logging
|
|
|
|
useEffect(() => {
|
|
|
|
console.debug('ZapDialog wallet status:', { hasWebLN, hasNWC, activeNWC: !!activeNWC });
|
|
|
|
}, [hasWebLN, hasNWC, activeNWC]);
|
|
|
|
|
|
|
|
// Additional debug logging when dialog opens
|
|
|
|
useEffect(() => {
|
|
|
|
if (open) {
|
|
|
|
console.debug('ZapDialog opened with wallet status:', {
|
|
|
|
hasWebLN,
|
|
|
|
hasNWC,
|
|
|
|
activeNWC: activeNWC ? { alias: activeNWC.alias, isConnected: activeNWC.isConnected } : null
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [open, hasWebLN, hasNWC, activeNWC]);
|
|
|
|
|
2025-07-13 06:47:45 +00:00
|
|
|
const { zap, isZapping, invoice, setInvoice } = useZaps(target, webln, activeNWC, () => setOpen(false));
|
2025-07-12 19:19:41 +00:00
|
|
|
const [amount, setAmount] = useState<number | string>(100);
|
|
|
|
const [comment, setComment] = useState<string>('');
|
|
|
|
const inputRef = useRef<HTMLInputElement>(null);
|
|
|
|
const qrCodeRef = useRef<HTMLCanvasElement>(null);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (target) {
|
|
|
|
setComment('Zapped with MKStack!');
|
|
|
|
}
|
|
|
|
}, [target]);
|
|
|
|
|
2025-07-13 05:59:56 +00:00
|
|
|
// Detect WebLN when dialog opens
|
|
|
|
useEffect(() => {
|
2025-07-13 06:47:45 +00:00
|
|
|
if (open && !hasWebLN) {
|
|
|
|
detectWebLN();
|
|
|
|
}
|
|
|
|
}, [open, hasWebLN, detectWebLN]);
|
2025-07-13 05:59:56 +00:00
|
|
|
|
2025-07-12 19:19:41 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (invoice && qrCodeRef.current) {
|
|
|
|
QRCode.toCanvas(qrCodeRef.current, invoice, { width: 256 });
|
|
|
|
}
|
|
|
|
}, [invoice]);
|
|
|
|
|
|
|
|
const handleCopy = () => {
|
|
|
|
if (invoice) {
|
|
|
|
navigator.clipboard.writeText(invoice);
|
|
|
|
toast({
|
|
|
|
title: 'Copied to clipboard!',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (open) {
|
|
|
|
setAmount(100);
|
|
|
|
setInvoice(null);
|
|
|
|
}
|
|
|
|
}, [open, setInvoice]);
|
|
|
|
|
|
|
|
const handleZap = () => {
|
|
|
|
const finalAmount = typeof amount === 'string' ? parseInt(amount, 10) : amount;
|
|
|
|
zap(finalAmount, comment);
|
|
|
|
};
|
|
|
|
|
2025-07-12 19:23:50 +00:00
|
|
|
if (!user || user.pubkey === target.pubkey || !author?.metadata?.lud16) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2025-07-12 19:19:41 +00:00
|
|
|
return (
|
2025-07-12 19:23:50 +00:00
|
|
|
<Dialog open={open} onOpenChange={setOpen}>
|
|
|
|
<DialogTrigger asChild>
|
2025-07-13 19:52:58 +00:00
|
|
|
<div className={`cursor-pointer ${className || ''}`}>
|
2025-07-12 19:23:50 +00:00
|
|
|
{children}
|
2025-07-13 19:52:58 +00:00
|
|
|
</div>
|
2025-07-12 19:23:50 +00:00
|
|
|
</DialogTrigger>
|
|
|
|
<DialogContent className="sm:max-w-[425px]" data-testid="zap-modal">
|
2025-07-12 19:19:41 +00:00
|
|
|
<DialogHeader>
|
|
|
|
<DialogTitle>{invoice ? 'Manual Zap' : 'Send a Zap'}</DialogTitle>
|
|
|
|
<DialogDescription asChild>
|
|
|
|
{invoice ? (
|
|
|
|
<div>Scan the QR code with a lightning-enabled wallet or copy the invoice below.</div>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<div>Zaps are small Bitcoin payments that support the creator of this item.</div>
|
|
|
|
<div className="mt-2">If you enjoyed this, consider sending a zap!</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</DialogDescription>
|
|
|
|
</DialogHeader>
|
|
|
|
{invoice ? (
|
|
|
|
<div className="flex flex-col items-center gap-4 py-4">
|
|
|
|
<canvas ref={qrCodeRef} />
|
|
|
|
<div className="flex w-full items-center gap-2">
|
|
|
|
<Input value={invoice} readOnly className="flex-1" />
|
|
|
|
<Button onClick={handleCopy} variant="outline" size="icon">
|
|
|
|
<Copy className="h-4 w-4" />
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<>
|
2025-07-13 06:47:45 +00:00
|
|
|
{/* Payment Method Indicator */}
|
|
|
|
<div className="flex items-center justify-center py-2 px-1">
|
|
|
|
<div className="flex items-center gap-2 text-sm text-muted-foreground">
|
|
|
|
{hasNWC ? (
|
|
|
|
<>
|
|
|
|
<Wallet className="h-4 w-4 text-green-600" />
|
|
|
|
<span>Wallet Connected</span>
|
|
|
|
</>
|
|
|
|
) : hasWebLN ? (
|
|
|
|
<>
|
|
|
|
<Globe className="h-4 w-4 text-blue-600" />
|
|
|
|
<span>WebLN Available</span>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<Copy className="h-4 w-4" />
|
|
|
|
<span>Manual Payment</span>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2025-07-12 19:19:41 +00:00
|
|
|
<div className="grid gap-4 py-4">
|
|
|
|
<ToggleGroup
|
|
|
|
type="single"
|
|
|
|
value={String(amount)}
|
|
|
|
onValueChange={(value) => {
|
|
|
|
if (value) {
|
|
|
|
setAmount(parseInt(value, 10));
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
className="grid grid-cols-5 gap-2"
|
|
|
|
>
|
2025-07-13 05:59:56 +00:00
|
|
|
{presetAmounts.map(({ amount: presetAmount, icon: Icon }) => (
|
2025-07-12 19:19:41 +00:00
|
|
|
<ToggleGroupItem
|
|
|
|
key={presetAmount}
|
|
|
|
value={String(presetAmount)}
|
|
|
|
className="flex flex-col h-auto"
|
|
|
|
>
|
2025-07-13 05:59:56 +00:00
|
|
|
<Icon className="h-5 w-5 mb-1.5" />
|
2025-07-12 19:19:41 +00:00
|
|
|
{presetAmount}
|
|
|
|
</ToggleGroupItem>
|
|
|
|
))}
|
|
|
|
</ToggleGroup>
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
<div className="h-px flex-1 bg-muted" />
|
|
|
|
<span className="text-xs text-muted-foreground">OR</span>
|
|
|
|
<div className="h-px flex-1 bg-muted" />
|
|
|
|
</div>
|
|
|
|
<Input
|
|
|
|
ref={inputRef}
|
|
|
|
id="custom-amount"
|
|
|
|
type="number"
|
|
|
|
placeholder="Custom amount"
|
|
|
|
value={amount}
|
|
|
|
onChange={(e) => setAmount(e.target.value)}
|
|
|
|
/>
|
|
|
|
<Textarea
|
|
|
|
id="custom-comment"
|
|
|
|
placeholder="Custom comment"
|
|
|
|
value={comment}
|
|
|
|
onChange={(e) => setComment(e.target.value)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<DialogFooter>
|
|
|
|
<Button onClick={handleZap} className="w-full" disabled={isZapping}>
|
|
|
|
{isZapping ? (
|
|
|
|
'Creating invoice...'
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<Zap className="h-4 w-4 mr-2" />
|
|
|
|
Zap {amount} sats
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Button>
|
|
|
|
</DialogFooter>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
}
|