snort/packages/app/src/Element/NoteFooter.tsx

253 lines
7.5 KiB
TypeScript
Raw Normal View History

2023-03-10 23:52:39 +00:00
import React, { useEffect, useState } from "react";
import { useSelector, useDispatch } from "react-redux";
2023-07-12 18:27:42 +00:00
import { useIntl } from "react-intl";
2023-02-25 21:18:36 +00:00
import { useLongPress } from "use-long-press";
2023-07-12 18:27:42 +00:00
import { TaggedNostrEvent, HexKey, u256, ParsedZap } from "@snort/system";
2023-06-15 11:03:05 +00:00
import { LNURL } from "@snort/shared";
2023-06-16 19:31:33 +00:00
import { useUserProfile } from "@snort/system-react";
2023-01-08 00:29:59 +00:00
2023-03-02 17:47:02 +00:00
import Icon from "Icons/Icon";
2023-03-02 15:23:53 +00:00
import Spinner from "Icons/Spinner";
2023-03-02 17:47:02 +00:00
2023-01-20 11:11:50 +00:00
import { formatShort } from "Number";
import useEventPublisher from "Feed/EventPublisher";
2023-05-24 10:12:23 +00:00
import { delay, normalizeReaction, unwrap } from "SnortUtils";
2023-01-20 11:11:50 +00:00
import { NoteCreator } from "Element/NoteCreator";
2023-02-07 13:32:32 +00:00
import SendSats from "Element/SendSats";
2023-06-21 15:08:48 +00:00
import { ZapsSummary } from "Element/Zap";
2023-01-20 11:11:50 +00:00
import { RootState } from "State/Store";
import { setReplyTo, setShow, reset } from "State/NoteCreator";
2023-07-12 18:27:42 +00:00
2023-03-02 15:23:53 +00:00
import { useWallet } from "Wallet";
2023-04-14 11:33:19 +00:00
import useLogin from "Hooks/useLogin";
2023-04-25 11:57:09 +00:00
import { useInteractionCache } from "Hooks/useInteractionCache";
2023-05-16 21:30:52 +00:00
import { ZapPoolController } from "ZapPoolController";
2023-06-15 11:03:05 +00:00
import { System } from "index";
2023-01-31 11:52:55 +00:00
2023-02-08 21:10:26 +00:00
import messages from "./messages";
2023-03-11 17:03:42 +00:00
let isZapperBusy = false;
const barrierZapper = async <T,>(then: () => Promise<T>): Promise<T> => {
while (isZapperBusy) {
await delay(100);
}
isZapperBusy = true;
try {
return await then();
} finally {
isZapperBusy = false;
}
};
2023-01-16 17:48:25 +00:00
export interface NoteFooterProps {
2023-08-17 18:54:14 +00:00
reposts: TaggedNostrEvent[];
2023-02-12 12:31:48 +00:00
zaps: ParsedZap[];
2023-08-17 18:54:14 +00:00
positive: TaggedNostrEvent[];
ev: TaggedNostrEvent;
2023-01-16 17:48:25 +00:00
}
export default function NoteFooter(props: NoteFooterProps) {
2023-07-12 18:27:42 +00:00
const { ev, positive, reposts, zaps } = props;
const dispatch = useDispatch();
2023-02-08 21:10:26 +00:00
const { formatMessage } = useIntl();
2023-04-14 11:33:19 +00:00
const login = useLogin();
2023-07-12 18:27:42 +00:00
const { publicKey, preferences: prefs, relays } = login;
2023-06-16 19:31:33 +00:00
const author = useUserProfile(System, ev.pubkey);
2023-04-25 11:57:09 +00:00
const interactionCache = useInteractionCache(publicKey, ev.id);
2023-01-20 17:07:14 +00:00
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;
2023-01-20 17:07:14 +00:00
const [tip, setTip] = useState(false);
2023-02-25 21:18:36 +00:00
const [zapping, setZapping] = useState(false);
2023-03-02 15:23:53 +00:00
const walletState = useWallet();
const wallet = walletState.wallet;
2023-04-14 11:33:19 +00:00
const isMine = ev.pubkey === publicKey;
const zapTotal = zaps.reduce((acc, z) => acc + z.amount, 0);
2023-04-25 11:57:09 +00:00
const didZap = interactionCache.data.zapped || zaps.some(a => a.sender === publicKey);
2023-02-25 21:18:36 +00:00
const longPress = useLongPress(
e => {
e.stopPropagation();
setTip(true);
},
{
captureEvent: true,
}
);
2023-01-08 00:29:59 +00:00
2023-01-20 17:07:14 +00:00
function hasReacted(emoji: string) {
2023-04-25 11:57:09 +00:00
return (
interactionCache.data.reacted ||
positive?.some(({ pubkey, content }) => normalizeReaction(content) === emoji && pubkey === publicKey)
);
2023-01-20 17:07:14 +00:00
}
2023-01-08 00:29:59 +00:00
2023-01-20 17:07:14 +00:00
function hasReposted() {
2023-04-25 11:57:09 +00:00
return interactionCache.data.reposted || reposts.some(a => a.pubkey === publicKey);
2023-01-20 17:07:14 +00:00
}
2023-01-08 00:29:59 +00:00
2023-01-20 17:07:14 +00:00
async function react(content: string) {
2023-04-14 15:02:15 +00:00
if (!hasReacted(content) && publisher) {
2023-02-07 19:47:57 +00:00
const evLike = await publisher.react(ev, content);
2023-06-15 11:03:05 +00:00
System.BroadcastEvent(evLike);
2023-04-25 11:57:09 +00:00
await interactionCache.react();
2023-01-08 17:33:54 +00:00
}
2023-01-20 17:07:14 +00:00
}
2023-01-08 17:33:54 +00:00
2023-01-20 17:07:14 +00:00
async function repost() {
2023-04-14 15:02:15 +00:00
if (!hasReposted() && publisher) {
2023-03-28 14:34:01 +00:00
if (!prefs.confirmReposts || window.confirm(formatMessage(messages.ConfirmRepost, { id: ev.id }))) {
2023-02-07 19:47:57 +00:00
const evRepost = await publisher.repost(ev);
2023-06-15 11:03:05 +00:00
System.BroadcastEvent(evRepost);
2023-04-25 11:57:09 +00:00
await interactionCache.repost();
2023-01-20 17:07:14 +00:00
}
2023-01-08 00:29:59 +00:00
}
2023-01-20 17:07:14 +00:00
}
2023-01-08 00:29:59 +00:00
2023-03-27 22:58:29 +00:00
function getLNURL() {
2023-04-05 10:58:26 +00:00
return ev.tags.find(a => a[0] === "zap")?.[1] || author?.lud16 || author?.lud06;
2023-03-27 22:58:29 +00:00
}
function getTargetName() {
2023-04-05 10:58:26 +00:00
const zapTarget = ev.tags.find(a => a[0] === "zap")?.[1];
if (zapTarget) {
return new LNURL(zapTarget).name;
} else {
return author?.display_name || author?.name;
}
2023-03-27 22:58:29 +00:00
}
2023-03-12 19:25:22 +00:00
async function fastZap(e?: React.MouseEvent) {
if (zapping || e?.isPropagationStopped()) return;
2023-02-25 21:18:36 +00:00
2023-03-27 22:58:29 +00:00
const lnurl = getLNURL();
2023-03-02 15:23:53 +00:00
if (wallet?.isReady() && lnurl) {
2023-02-25 21:18:36 +00:00
setZapping(true);
try {
2023-03-28 14:34:01 +00:00
await fastZapInner(lnurl, prefs.defaultZapAmount, ev.pubkey, ev.id);
2023-02-25 21:18:36 +00:00
} catch (e) {
2023-02-27 21:19:26 +00:00
console.warn("Fast zap failed", e);
if (!(e instanceof Error) || e.message !== "User rejected") {
setTip(true);
}
2023-02-25 21:18:36 +00:00
} finally {
setZapping(false);
}
} else {
setTip(true);
}
}
2023-03-03 22:40:49 +00:00
async function fastZapInner(lnurl: string, amount: number, key: HexKey, id?: u256) {
2023-03-12 19:25:22 +00:00
// only allow 1 invoice req/payment at a time to avoid hitting rate limits
await barrierZapper(async () => {
const handler = new LNURL(lnurl);
await handler.load();
2023-04-14 15:02:15 +00:00
const zr = Object.keys(relays.item);
const zap = handler.canZap && publisher ? await publisher.zap(amount * 1000, key, zr, id) : undefined;
2023-03-12 19:25:22 +00:00
const invoice = await handler.getInvoice(amount, undefined, zap);
await wallet?.payInvoice(unwrap(invoice.pr));
2023-05-16 21:30:52 +00:00
ZapPoolController.allocate(amount);
2023-04-25 11:57:09 +00:00
await interactionCache.zap();
2023-03-12 19:25:22 +00:00
});
}
2023-03-10 23:52:39 +00:00
useEffect(() => {
2023-04-25 11:57:09 +00:00
if (prefs.autoZap && !didZap && !isMine && !zapping) {
2023-03-27 22:58:29 +00:00
const lnurl = getLNURL();
2023-03-12 19:25:22 +00:00
if (wallet?.isReady() && lnurl) {
2023-03-11 17:03:42 +00:00
setZapping(true);
2023-03-10 23:52:39 +00:00
queueMicrotask(async () => {
try {
2023-03-28 14:34:01 +00:00
await fastZapInner(lnurl, prefs.defaultZapAmount, ev.pubkey, ev.id);
2023-03-12 19:25:22 +00:00
} catch {
// ignored
2023-03-10 23:52:39 +00:00
} finally {
setZapping(false);
}
});
}
}
2023-03-11 17:03:42 +00:00
}, [prefs.autoZap, author, zapping]);
2023-03-10 23:52:39 +00:00
2023-01-20 17:07:14 +00:00
function tipButton() {
2023-03-27 22:58:29 +00:00
const service = getLNURL();
2023-01-20 17:07:14 +00:00
if (service) {
return (
2023-01-20 17:07:14 +00:00
<>
2023-02-27 19:15:39 +00:00
<div className={`reaction-pill ${didZap ? "reacted" : ""}`} {...longPress()} onClick={e => fastZap(e)}>
2023-03-03 23:04:26 +00:00
{zapping ? <Spinner /> : wallet?.isReady() ? <Icon name="zapFast" /> : <Icon name="zap" />}
2023-02-09 12:26:54 +00:00
{zapTotal > 0 && <div className="reaction-pill-number">{formatShort(zapTotal)}</div>}
</div>
2023-01-20 17:07:14 +00:00
</>
);
}
2023-01-20 17:07:14 +00:00
return null;
}
2023-01-20 17:07:14 +00:00
function repostIcon() {
return (
2023-02-09 12:26:54 +00:00
<div className={`reaction-pill ${hasReposted() ? "reacted" : ""}`} onClick={() => repost()}>
2023-03-02 18:39:29 +00:00
<Icon name="repost" size={17} />
2023-02-09 12:26:54 +00:00
{reposts.length > 0 && <div className="reaction-pill-number">{formatShort(reposts.length)}</div>}
2023-01-20 17:07:14 +00:00
</div>
);
2023-01-20 17:07:14 +00:00
}
2023-01-20 17:07:14 +00:00
function reactionIcons() {
2023-04-25 09:15:15 +00:00
if (!prefs.enableReactions) {
2023-01-20 17:07:14 +00:00
return null;
}
2023-01-08 00:29:59 +00:00
return (
2023-01-20 17:07:14 +00:00
<>
2023-02-10 19:23:52 +00:00
<div
className={`reaction-pill ${hasReacted("+") ? "reacted" : ""} `}
onClick={() => react(prefs.reactionEmoji)}>
2023-03-02 18:39:29 +00:00
<Icon name="heart" />
2023-02-09 12:26:54 +00:00
<div className="reaction-pill-number">{formatShort(positive.length)}</div>
2023-01-20 17:07:14 +00:00
</div>
</>
);
2023-01-20 17:07:14 +00:00
}
const handleReplyButtonClick = () => {
if (replyTo?.id !== ev.id) {
dispatch(reset());
}
dispatch(setReplyTo(ev));
dispatch(setShow(!showNoteCreatorModal));
};
2023-01-20 17:07:14 +00:00
return (
2023-02-03 21:55:03 +00:00
<>
<div className="footer">
<div className="footer-reactions">
{tipButton()}
{reactionIcons()}
2023-04-20 21:05:11 +00:00
{repostIcon()}
<div className={`reaction-pill ${showNoteCreatorModal ? "reacted" : ""}`} onClick={handleReplyButtonClick}>
2023-03-02 18:39:29 +00:00
<Icon name="reply" size={17} />
2023-01-25 18:08:53 +00:00
</div>
</div>
{willRenderNoteCreator && <NoteCreator />}
<SendSats
2023-03-27 22:58:29 +00:00
lnurl={getLNURL()}
onClose={() => setTip(false)}
show={tip}
author={author?.pubkey}
2023-03-27 22:58:29 +00:00
target={getTargetName()}
2023-03-28 14:34:01 +00:00
note={ev.id}
2023-05-16 21:30:52 +00:00
allocatePool={true}
/>
</div>
<div className="zaps-container">
<ZapsSummary zaps={zaps} />
2023-01-20 17:07:14 +00:00
</div>
2023-02-03 21:55:03 +00:00
</>
);
}