import React, { HTMLProps, useContext, useEffect, useState } from "react"; import { useSelector, useDispatch } from "react-redux"; import { useIntl } from "react-intl"; import { useLongPress } from "use-long-press"; import { TaggedNostrEvent, ParsedZap, countLeadingZeros, createNostrLinkToEvent } from "@snort/system"; import { SnortContext, useUserProfile } from "@snort/system-react"; import { formatShort } from "Number"; import useEventPublisher from "Feed/EventPublisher"; import { delay, findTag, normalizeReaction } from "SnortUtils"; import { NoteCreator } from "Element/NoteCreator"; import SendSats from "Element/SendSats"; import { ZapsSummary } from "Element/Zap"; import { RootState } from "State/Store"; import { setReplyTo, setShow, reset } from "State/NoteCreator"; import { AsyncIcon } from "Element/AsyncIcon"; import { useWallet } from "Wallet"; import useLogin from "Hooks/useLogin"; import { useInteractionCache } from "Hooks/useInteractionCache"; import { ZapPoolController } from "ZapPoolController"; import { System } from "index"; import { Zapper, ZapTarget } from "Zapper"; import { getDisplayName } from "./ProfileImage"; import messages from "./messages"; let isZapperBusy = false; const barrierZapper = async (then: () => Promise): Promise => { while (isZapperBusy) { await delay(100); } isZapperBusy = true; try { return await then(); } finally { isZapperBusy = false; } }; export interface NoteFooterProps { reposts: TaggedNostrEvent[]; zaps: ParsedZap[]; positive: TaggedNostrEvent[]; ev: TaggedNostrEvent; } export default function NoteFooter(props: NoteFooterProps) { const { ev, positive, reposts, zaps } = props; const dispatch = useDispatch(); const system = useContext(SnortContext); const { formatMessage } = useIntl(); const login = useLogin(); const { publicKey, preferences: prefs } = login; const author = useUserProfile(ev.pubkey); const interactionCache = useInteractionCache(publicKey, ev.id); const publisher = useEventPublisher(); const showNoteCreatorModal = useSelector((s: RootState) => s.noteCreator.show); const replyTo = useSelector((s: RootState) => s.noteCreator.replyTo); const willRenderNoteCreator = showNoteCreatorModal && replyTo?.id === ev.id; const [tip, setTip] = useState(false); const [zapping, setZapping] = useState(false); const walletState = useWallet(); const wallet = walletState.wallet; const isMine = ev.pubkey === publicKey; const zapTotal = zaps.reduce((acc, z) => acc + z.amount, 0); const didZap = interactionCache.data.zapped || zaps.some(a => a.sender === publicKey); const longPress = useLongPress( e => { e.stopPropagation(); setTip(true); }, { captureEvent: true, }, ); function hasReacted(emoji: string) { return ( interactionCache.data.reacted || positive?.some(({ pubkey, content }) => normalizeReaction(content) === emoji && pubkey === publicKey) ); } function hasReposted() { return interactionCache.data.reposted || reposts.some(a => a.pubkey === publicKey); } async function react(content: string) { if (!hasReacted(content) && publisher) { const evLike = await publisher.react(ev, content); System.BroadcastEvent(evLike); await interactionCache.react(); } } async function repost() { if (!hasReposted() && publisher) { if (!prefs.confirmReposts || window.confirm(formatMessage(messages.ConfirmRepost, { id: ev.id }))) { const evRepost = await publisher.repost(ev); System.BroadcastEvent(evRepost); await interactionCache.repost(); } } } function getZapTarget(): Array | undefined { if (ev.tags.some(v => v[0] === "zap")) { return Zapper.fromEvent(ev); } const authorTarget = author?.lud16 || author?.lud06; if (authorTarget) { return [ { type: "lnurl", value: authorTarget, weight: 1, name: getDisplayName(author, ev.pubkey), zap: { pubkey: ev.pubkey, event: createNostrLinkToEvent(ev), }, } as ZapTarget, ]; } } async function fastZap(e?: React.MouseEvent) { if (zapping || e?.isPropagationStopped()) return; const lnurl = getZapTarget(); if (wallet?.isReady() && lnurl) { setZapping(true); try { await fastZapInner(lnurl, prefs.defaultZapAmount); } catch (e) { console.warn("Fast zap failed", e); if (!(e instanceof Error) || e.message !== "User rejected") { setTip(true); } } finally { setZapping(false); } } else { setTip(true); } } async function fastZapInner(targets: Array, amount: number) { if (wallet) { // only allow 1 invoice req/payment at a time to avoid hitting rate limits await barrierZapper(async () => { const zapper = new Zapper(system, publisher); const result = await zapper.send(wallet, targets, amount); const totalSent = result.reduce((acc, v) => (acc += v.sent), 0); if (totalSent > 0) { ZapPoolController.allocate(totalSent); await interactionCache.zap(); } }); } } useEffect(() => { if (prefs.autoZap && !didZap && !isMine && !zapping) { const lnurl = getZapTarget(); if (wallet?.isReady() && lnurl) { setZapping(true); queueMicrotask(async () => { try { await fastZapInner(lnurl, prefs.defaultZapAmount); } catch { // ignored } finally { setZapping(false); } }); } } }, [prefs.autoZap, author, zapping]); function powIcon() { const pow = findTag(ev, "nonce") ? countLeadingZeros(ev.id) : undefined; if (pow) { return ( ); } } function tipButton() { const targets = getZapTarget(); if (targets) { return ( fastZap(e)} /> ); } return null; } function repostIcon() { return ( repost()} /> ); } function reactionIcon() { if (!prefs.enableReactions) { return null; } const reacted = hasReacted("+"); return ( react(prefs.reactionEmoji)} /> ); } function replyIcon() { return ( handleReplyButtonClick()} /> ); } const handleReplyButtonClick = () => { if (replyTo?.id !== ev.id) { dispatch(reset()); } dispatch(setReplyTo(ev)); dispatch(setShow(!showNoteCreatorModal)); }; return ( <>
{tipButton()} {reactionIcon()} {repostIcon()} {replyIcon()} {powIcon()}
{willRenderNoteCreator && } setTip(false)} show={tip} note={ev.id} allocatePool={true} />
); } interface AsyncFooterIconProps extends HTMLProps { iconName: string; value: number; loading?: boolean; onClick?: (e: React.MouseEvent) => Promise; } function AsyncFooterIcon(props: AsyncFooterIconProps) { const mergedProps = { ...props, iconSize: 18, className: `reaction-pill${props.className ? ` ${props.className}` : ""}`, }; return ( {props.value > 0 &&
{formatShort(props.value)}
}
); }