import { NDKSubscriptionCacheUsage } from '@nostr-dev-kit/ndk' import { Dots } from 'components/Spinner' import { ZapSplit } from 'components/Zap' import { useAppSelector, useBodyScrollDisable, useDidMount, useNDKContext } from 'hooks' import { useState } from 'react' import { toast } from 'react-toastify' import { Addressable } from 'types' import { abbreviateNumber, log, LogType } from 'utils' type ZapProps = { addressable: Addressable } export const Zap = ({ addressable }: ZapProps) => { const [isOpen, setIsOpen] = useState(false) const [isLoading, setIsLoading] = useState(true) const [isAvailable, setIsAvailable] = useState(false) const [totalZappedAmount, setTotalZappedAmount] = useState(0) const [hasZapped, setHasZapped] = useState(false) const userState = useAppSelector((state) => state.user) const { getTotalZapAmount, findMetadata } = useNDKContext() useBodyScrollDisable(isOpen) useDidMount(() => { findMetadata(addressable.author, { cacheUsage: NDKSubscriptionCacheUsage.ONLY_RELAY }) .then((res) => { setIsAvailable(typeof res?.lud16 !== 'undefined' && res.lud16 !== '') }) .catch((err) => { log(true, LogType.Error, err.message || err) }) getTotalZapAmount( addressable.author, addressable.id, addressable.aTag, userState.user?.pubkey as string ) .then((res) => { setTotalZappedAmount(res.accumulatedZapAmount) setHasZapped(res.hasZapped) }) .catch((err) => { toast.error(err.message || err) }) .finally(() => { setIsLoading(false) }) }) // Hide button if the author hasn't set lud16 if (!isAvailable) return null return ( <>
setIsOpen(true)} >

{isLoading ? : abbreviateNumber(totalZappedAmount)}

{isOpen && ( setIsOpen(false)} /> )} ) }