2025-07-12 19:23:50 +00:00
|
|
|
import { useState, useEffect, useRef } from 'react';
|
2025-07-14 01:16:31 +00:00
|
|
|
import { Zap, Copy, Check, ExternalLink, Sparkle, Sparkles, Star, Rocket } 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';
|
2025-07-14 01:16:31 +00:00
|
|
|
import { Label } from '@/components/ui/label';
|
2025-07-12 19:19:41 +00:00
|
|
|
import { Textarea } from '@/components/ui/textarea';
|
2025-07-14 01:16:31 +00:00
|
|
|
import { Card, CardContent } from '@/components/ui/card';
|
|
|
|
import { Separator } from '@/components/ui/separator';
|
2025-07-12 19:19:41 +00:00
|
|
|
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-13 04:09:32 +00:00
|
|
|
import type { Event } from 'nostr-tools';
|
2025-07-14 01:16:31 +00:00
|
|
|
import QRCode from 'qrcode';
|
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 20:12:42 +00:00
|
|
|
const { webln, activeNWC, hasWebLN, detectWebLN } = useWallet();
|
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>('');
|
2025-07-14 01:16:31 +00:00
|
|
|
const [copied, setCopied] = useState(false);
|
|
|
|
const [qrCodeUrl, setQrCodeUrl] = useState<string>('');
|
2025-07-12 19:19:41 +00:00
|
|
|
const inputRef = useRef<HTMLInputElement>(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-14 01:16:31 +00:00
|
|
|
// Generate QR code
|
2025-07-12 19:19:41 +00:00
|
|
|
useEffect(() => {
|
2025-07-14 01:16:31 +00:00
|
|
|
const generateQR = async () => {
|
|
|
|
if (!invoice) return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const url = await QRCode.toDataURL(invoice.toUpperCase(), {
|
|
|
|
width: 256,
|
|
|
|
margin: 2,
|
|
|
|
color: {
|
|
|
|
dark: '#000000',
|
|
|
|
light: '#FFFFFF',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
setQrCodeUrl(url);
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Failed to generate QR code:', err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
generateQR();
|
2025-07-12 19:19:41 +00:00
|
|
|
}, [invoice]);
|
|
|
|
|
2025-07-14 01:16:31 +00:00
|
|
|
const handleCopy = async () => {
|
2025-07-12 19:19:41 +00:00
|
|
|
if (invoice) {
|
2025-07-14 01:16:31 +00:00
|
|
|
await navigator.clipboard.writeText(invoice);
|
|
|
|
setCopied(true);
|
2025-07-12 19:19:41 +00:00
|
|
|
toast({
|
2025-07-14 01:16:31 +00:00
|
|
|
title: 'Invoice copied',
|
|
|
|
description: 'Lightning invoice copied to clipboard',
|
2025-07-12 19:19:41 +00:00
|
|
|
});
|
2025-07-14 01:16:31 +00:00
|
|
|
setTimeout(() => setCopied(false), 2000);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const openInWallet = () => {
|
|
|
|
if (invoice) {
|
|
|
|
const lightningUrl = `lightning:${invoice}`;
|
|
|
|
window.open(lightningUrl, '_blank');
|
2025-07-12 19:19:41 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (open) {
|
|
|
|
setAmount(100);
|
|
|
|
setInvoice(null);
|
2025-07-14 01:16:31 +00:00
|
|
|
setCopied(false);
|
|
|
|
setQrCodeUrl('');
|
2025-07-12 19:19:41 +00:00
|
|
|
}
|
|
|
|
}, [open, setInvoice]);
|
|
|
|
|
|
|
|
const handleZap = () => {
|
|
|
|
const finalAmount = typeof amount === 'string' ? parseInt(amount, 10) : amount;
|
|
|
|
zap(finalAmount, comment);
|
|
|
|
};
|
|
|
|
|
2025-07-13 20:12:42 +00:00
|
|
|
if (!user || user.pubkey === target.pubkey || !author?.metadata?.lud06 && !author?.metadata?.lud16) {
|
2025-07-12 19:23:50 +00:00
|
|
|
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>
|
2025-07-14 01:16:31 +00:00
|
|
|
<DialogTitle>{invoice ? 'Lightning Payment' : 'Send a Zap'}</DialogTitle>
|
|
|
|
<DialogDescription>
|
2025-07-12 19:19:41 +00:00
|
|
|
{invoice ? (
|
2025-07-14 01:16:31 +00:00
|
|
|
'Scan the QR code or copy the invoice to pay with any Lightning wallet'
|
2025-07-12 19:19:41 +00:00
|
|
|
) : (
|
|
|
|
<>
|
2025-07-14 01:16:31 +00:00
|
|
|
Zaps are small Bitcoin payments that support the creator of this item.
|
|
|
|
{' '}If you enjoyed this, consider sending a zap!
|
2025-07-12 19:19:41 +00:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</DialogDescription>
|
|
|
|
</DialogHeader>
|
|
|
|
{invoice ? (
|
2025-07-14 01:51:48 +00:00
|
|
|
<div className="space-y-6 overflow-y-auto max-h-[calc(100vh-200px)] p-1">
|
2025-07-14 01:16:31 +00:00
|
|
|
{/* Payment amount display */}
|
|
|
|
<div className="text-center">
|
|
|
|
<div className="text-2xl font-bold">{amount} sats</div>
|
|
|
|
<div className="text-sm text-muted-foreground">Lightning Network Payment</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Separator />
|
|
|
|
|
|
|
|
{/* QR Code */}
|
|
|
|
<div className="flex justify-center">
|
|
|
|
<Card className="p-4">
|
|
|
|
<CardContent className="p-0">
|
|
|
|
{qrCodeUrl ? (
|
|
|
|
<img
|
|
|
|
src={qrCodeUrl}
|
|
|
|
alt="Lightning Invoice QR Code"
|
|
|
|
className="w-64 h-64"
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div className="w-64 h-64 bg-muted animate-pulse rounded" />
|
|
|
|
)}
|
|
|
|
</CardContent>
|
|
|
|
</Card>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{/* Invoice input */}
|
|
|
|
<div className="space-y-2">
|
|
|
|
<Label htmlFor="invoice">Lightning Invoice</Label>
|
|
|
|
<div className="flex gap-2">
|
|
|
|
<Input
|
|
|
|
id="invoice"
|
|
|
|
value={invoice}
|
|
|
|
readOnly
|
|
|
|
className="font-mono text-xs"
|
|
|
|
onClick={(e) => e.currentTarget.select()}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
variant="outline"
|
|
|
|
size="icon"
|
|
|
|
onClick={handleCopy}
|
|
|
|
className="shrink-0"
|
|
|
|
>
|
|
|
|
{copied ? (
|
|
|
|
<Check className="h-4 w-4 text-green-600" />
|
|
|
|
) : (
|
|
|
|
<Copy className="h-4 w-4" />
|
|
|
|
)}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{/* Payment buttons */}
|
|
|
|
<div className="space-y-3">
|
|
|
|
{hasWebLN && (
|
|
|
|
<Button
|
|
|
|
onClick={() => {
|
|
|
|
const finalAmount = typeof amount === 'string' ? parseInt(amount, 10) : amount;
|
|
|
|
zap(finalAmount, comment);
|
|
|
|
}}
|
|
|
|
disabled={isZapping}
|
|
|
|
className="w-full"
|
|
|
|
size="lg"
|
|
|
|
>
|
|
|
|
<Zap className="h-4 w-4 mr-2" />
|
|
|
|
{isZapping ? "Processing..." : "Pay with WebLN"}
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<Button
|
|
|
|
variant="outline"
|
|
|
|
onClick={openInWallet}
|
|
|
|
className="w-full"
|
|
|
|
size="lg"
|
|
|
|
>
|
|
|
|
<ExternalLink className="h-4 w-4 mr-2" />
|
|
|
|
Open in Lightning Wallet
|
2025-07-12 19:19:41 +00:00
|
|
|
</Button>
|
2025-07-14 01:16:31 +00:00
|
|
|
|
|
|
|
<div className="text-xs text-muted-foreground text-center">
|
|
|
|
Scan the QR code or copy the invoice to pay with any Lightning wallet
|
|
|
|
</div>
|
2025-07-12 19:19:41 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<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>
|
|
|
|
);
|
|
|
|
}
|