2025-02-27 20:08:53 +01:00
|
|
|
import { NDKSubscriptionCacheUsage } from '@nostr-dev-kit/ndk'
|
2025-01-16 11:15:17 +01:00
|
|
|
import { Dots } from 'components/Spinner'
|
2024-09-30 21:20:30 +05:00
|
|
|
import { ZapSplit } from 'components/Zap'
|
2024-10-21 18:15:53 +05:00
|
|
|
import {
|
|
|
|
useAppSelector,
|
|
|
|
useBodyScrollDisable,
|
|
|
|
useDidMount,
|
|
|
|
useNDKContext
|
|
|
|
} from 'hooks'
|
2024-09-30 21:20:30 +05:00
|
|
|
import { useState } from 'react'
|
2024-09-10 11:41:54 +05:00
|
|
|
import { toast } from 'react-toastify'
|
2024-11-05 13:57:39 +01:00
|
|
|
import { Addressable } from 'types'
|
2025-01-16 11:15:17 +01:00
|
|
|
import { abbreviateNumber, log, LogType } from 'utils'
|
2024-09-10 11:41:54 +05:00
|
|
|
|
|
|
|
type ZapProps = {
|
2024-11-05 13:57:39 +01:00
|
|
|
addressable: Addressable
|
2024-09-10 11:41:54 +05:00
|
|
|
}
|
|
|
|
|
2024-11-05 13:57:39 +01:00
|
|
|
export const Zap = ({ addressable }: ZapProps) => {
|
2024-09-10 11:41:54 +05:00
|
|
|
const [isOpen, setIsOpen] = useState(false)
|
2025-01-16 11:15:17 +01:00
|
|
|
const [isLoading, setIsLoading] = useState(true)
|
|
|
|
const [isAvailable, setIsAvailable] = useState(false)
|
2024-10-21 14:33:40 +05:00
|
|
|
const [totalZappedAmount, setTotalZappedAmount] = useState(0)
|
2024-09-10 11:41:54 +05:00
|
|
|
const [hasZapped, setHasZapped] = useState(false)
|
|
|
|
|
|
|
|
const userState = useAppSelector((state) => state.user)
|
2025-01-16 11:15:17 +01:00
|
|
|
const { getTotalZapAmount, findMetadata } = useNDKContext()
|
2024-09-10 11:41:54 +05:00
|
|
|
|
2024-10-21 11:44:15 +02:00
|
|
|
useBodyScrollDisable(isOpen)
|
|
|
|
|
2024-09-10 11:41:54 +05:00
|
|
|
useDidMount(() => {
|
2025-02-27 20:08:53 +01:00
|
|
|
findMetadata(addressable.author, {
|
|
|
|
cacheUsage: NDKSubscriptionCacheUsage.ONLY_RELAY
|
|
|
|
})
|
2025-01-16 11:15:17 +01:00
|
|
|
.then((res) => {
|
|
|
|
setIsAvailable(typeof res?.lud16 !== 'undefined' && res.lud16 !== '')
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
log(true, LogType.Error, err.message || err)
|
|
|
|
})
|
|
|
|
|
2024-10-21 14:33:40 +05:00
|
|
|
getTotalZapAmount(
|
2024-11-05 13:57:39 +01:00
|
|
|
addressable.author,
|
|
|
|
addressable.id,
|
|
|
|
addressable.aTag,
|
2024-10-21 14:33:40 +05:00
|
|
|
userState.user?.pubkey as string
|
|
|
|
)
|
2024-09-10 11:41:54 +05:00
|
|
|
.then((res) => {
|
|
|
|
setTotalZappedAmount(res.accumulatedZapAmount)
|
|
|
|
setHasZapped(res.hasZapped)
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
toast.error(err.message || err)
|
|
|
|
})
|
2025-01-16 11:15:17 +01:00
|
|
|
.finally(() => {
|
|
|
|
setIsLoading(false)
|
|
|
|
})
|
2024-09-10 11:41:54 +05:00
|
|
|
})
|
|
|
|
|
2025-01-16 11:15:17 +01:00
|
|
|
// Hide button if the author hasn't set lud16
|
|
|
|
if (!isAvailable) return null
|
|
|
|
|
2024-09-10 11:41:54 +05:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div
|
|
|
|
id='reactBolt'
|
|
|
|
className={`IBMSMSMBSS_Details_Card IBMSMSMBSS_D_CBolt ${
|
|
|
|
hasZapped ? 'IBMSMSMBSS_D_CBActive' : ''
|
|
|
|
}`}
|
2025-01-16 11:15:17 +01:00
|
|
|
onClick={isLoading ? undefined : () => setIsOpen(true)}
|
2024-09-10 11:41:54 +05:00
|
|
|
>
|
|
|
|
<div className='IBMSMSMBSS_Details_CardVisual'>
|
|
|
|
<svg
|
|
|
|
xmlns='http://www.w3.org/2000/svg'
|
|
|
|
viewBox='-64 0 512 512'
|
|
|
|
width='1em'
|
|
|
|
height='1em'
|
|
|
|
fill='currentColor'
|
|
|
|
className='IBMSMSMBSS_Details_CardVisualIcon'
|
|
|
|
>
|
|
|
|
<path d='M240.5 224H352C365.3 224 377.3 232.3 381.1 244.7C386.6 257.2 383.1 271.3 373.1 280.1L117.1 504.1C105.8 513.9 89.27 514.7 77.19 505.9C65.1 497.1 60.7 481.1 66.59 467.4L143.5 288H31.1C18.67 288 6.733 279.7 2.044 267.3C-2.645 254.8 .8944 240.7 10.93 231.9L266.9 7.918C278.2-1.92 294.7-2.669 306.8 6.114C318.9 14.9 323.3 30.87 317.4 44.61L240.5 224z'></path>
|
|
|
|
</svg>
|
|
|
|
</div>
|
|
|
|
<p className='IBMSMSMBSS_Details_CardText'>
|
2025-01-16 11:15:17 +01:00
|
|
|
{isLoading ? <Dots /> : abbreviateNumber(totalZappedAmount)}
|
2024-09-10 11:41:54 +05:00
|
|
|
</p>
|
|
|
|
<div className='IBMSMSMBSSCL_CAElementLoadWrapper'>
|
|
|
|
<div className='IBMSMSMBSSCL_CAElementLoad'></div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{isOpen && (
|
2024-09-30 21:20:30 +05:00
|
|
|
<ZapSplit
|
2024-11-05 13:57:39 +01:00
|
|
|
pubkey={addressable.author}
|
|
|
|
eventId={addressable.id}
|
|
|
|
aTag={addressable.aTag}
|
2024-09-10 11:41:54 +05:00
|
|
|
setTotalZapAmount={setTotalZappedAmount}
|
2024-09-11 22:25:03 +05:00
|
|
|
setHasZapped={setHasZapped}
|
2024-09-30 21:20:30 +05:00
|
|
|
handleClose={() => setIsOpen(false)}
|
2024-09-10 11:41:54 +05:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|