import { QRCodeSVG } from 'qrcode.react' import React, { Dispatch, SetStateAction, useMemo } from 'react' import Countdown, { CountdownRenderProps } from 'react-countdown' import { toast } from 'react-toastify' import { ZapController } from '../controllers' import { useDidMount } from '../hooks' import '../styles/popup.css' import { PaymentRequest } from '../types' import { copyTextToClipboard } from '../utils' type PresetAmountProps = { label: string value: number setAmount: Dispatch> } export const PresetAmount = React.memo( ({ label, value, setAmount }: PresetAmountProps) => { return ( ) } ) type ZapPresetsProps = { setAmount: Dispatch> } export const ZapPresets = React.memo(({ setAmount }: ZapPresetsProps) => { return ( <> ) }) type ZapButtonsProps = { disabled: boolean handleGenerateQRCode: () => void handleSend: () => void } export const ZapButtons = ({ disabled, handleGenerateQRCode, handleSend }: ZapButtonsProps) => { return (
) } type ZapQRProps = { paymentRequest: PaymentRequest handleClose: () => void handleQRExpiry: () => void } export const ZapQR = React.memo( ({ paymentRequest, handleClose, handleQRExpiry }: ZapQRProps) => { useDidMount(() => { ZapController.getInstance() .pollZapReceipt(paymentRequest) .then(() => { toast.success(`Successfully sent sats!`) }) .catch((err) => { toast.error(err.message || err) }) .finally(() => { handleClose() }) }) const onQrCodeClicked = async () => { if (!paymentRequest) return const zapController = ZapController.getInstance() if (await zapController.isWeblnProviderExists()) { zapController.sendPayment(paymentRequest.pr) } else { console.warn('Webln provider not present') const href = `lightning:${paymentRequest.pr}` const a = document.createElement('a') a.href = href a.click() } } return (
) } ) const MAX_POLLING_TIME = 2 * 60 * 1000 // 2 minutes in milliseconds const renderer = ({ minutes, seconds }: CountdownRenderProps) => ( {minutes}:{seconds} ) type TimerProps = { onTimerExpired: () => void } const Timer = React.memo(({ onTimerExpired }: TimerProps) => { const expiryTime = useMemo(() => { return Date.now() + MAX_POLLING_TIME }, []) return (
) })