import { webln } from '@getalby/sdk'; import { SendPaymentResponse } from '@getalby/sdk/dist/types'; import * as Dialog from '@radix-ui/react-dialog'; import { invoke } from '@tauri-apps/api'; import { message } from '@tauri-apps/plugin-dialog'; import { QRCodeSVG } from 'qrcode.react'; import { useEffect, useRef, useState } from 'react'; import CurrencyInput from 'react-currency-input-field'; import { CancelIcon, ZapIcon } from '@shared/icons'; import { useEvent } from '@utils/hooks/useEvent'; import { useNostr } from '@utils/hooks/useNostr'; import { useProfile } from '@utils/hooks/useProfile'; import { sendNativeNotification } from '@utils/notification'; import { compactNumber } from '@utils/number'; export function NoteZap({ id, pubkey }: { id: string; pubkey: string }) { const { createZap } = useNostr(); const { user } = useProfile(pubkey); const { data: event } = useEvent(id); const [walletConnectURL, setWalletConnectURL] = useState(null); const [amount, setAmount] = useState('21'); const [zapMessage, setZapMessage] = useState(''); const [invoice, setInvoice] = useState(null); const [isOpen, setIsOpen] = useState(false); const [isCompleted, setIsCompleted] = useState(false); const [isLoading, setIsLoading] = useState(false); const nwc = useRef(null); const createZapRequest = async () => { try { const zapAmount = parseInt(amount) * 1000; const res = await createZap(event, zapAmount, zapMessage); if (!res) return await message('Cannot create zap request', { title: 'Zap', type: 'error', }); // user don't connect nwc, create QR Code for invoice if (!walletConnectURL) return setInvoice(res); // user connect nwc nwc.current = new webln.NostrWebLNProvider({ nostrWalletConnectUrl: walletConnectURL, }); await nwc.current.enable(); // start loading setIsLoading(true); // send payment via nwc const send: SendPaymentResponse = await nwc.current.sendPayment(res); if (send) { await sendNativeNotification( `You've tipped ${compactNumber.format(send.amount)} sats to ${ user?.name || user?.display_name || user?.displayName }` ); // eose nwc.current.close(); setIsCompleted(true); setIsLoading(false); // reset after 3 secs const timeout = setTimeout(() => setIsCompleted(false), 3000); clearTimeout(timeout); } } catch (e) { nwc.current.close(); setIsLoading(false); await message(JSON.stringify(e), { title: 'Zap', type: 'error' }); } }; useEffect(() => { async function getWalletConnectURL() { const uri: string = await invoke('secure_load', { key: 'nwc' }); if (uri) setWalletConnectURL(uri); } if (isOpen) { getWalletConnectURL(); } return () => { setAmount('21'); setZapMessage(''); setIsCompleted(false); setIsLoading(false); }; }, [isOpen]); return (
Send tip to {user?.name || user?.display_name || user?.displayName}
{!invoice ? ( <>
setAmount(value)} className="w-full flex-1 bg-transparent text-right text-4xl font-semibold text-white placeholder:text-neutral-600 focus:outline-none dark:text-neutral-400" /> sats
setZapMessage(e.target.value)} spellCheck={false} autoComplete="off" autoCorrect="off" autoCapitalize="off" placeholder="Enter message (optional)" className="relative min-h-[56px] w-full resize-none rounded-lg bg-white/10 px-3 py-2 !outline-none backdrop-blur-xl placeholder:text-neutral-600 dark:text-neutral-400" />
{walletConnectURL ? ( ) : ( )}
) : (

Scan to pay

You must use Bitcoin wallet which support Lightning
such as: Blue Wallet, Bitkit, Phoenix,...
)}
); }