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

298 lines
8.4 KiB
TypeScript
Raw Normal View History

2023-08-18 18:01:34 +00:00
import React, { HTMLProps, 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-08-18 18:01:34 +00:00
import { TaggedNostrEvent, HexKey, u256, ParsedZap, countLeadingZeros } 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-01-20 11:11:50 +00:00
import { formatShort } from "Number";
import useEventPublisher from "Feed/EventPublisher";
2023-08-18 18:01:34 +00:00
import { delay, findTag, 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-08-23 12:19:48 +00:00
import { AsyncIcon } from "Element/AsyncIcon";
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-08-24 14:25:54 +00:00
const author = useUserProfile(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-09-12 21:58:37 +00:00
},
2023-02-25 21:18:36 +00:00
);
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) {
2023-09-13 11:18:13 +00:00
try {
return new LNURL(zapTarget).name;
} catch {
// ignore
}
2023-04-05 10:58:26 +00:00
} 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-08-18 18:01:34 +00:00
function powIcon() {
const pow = findTag(ev, "nonce") ? countLeadingZeros(ev.id) : undefined;
if (pow) {
return (
<AsyncFooterIcon title={formatMessage({ defaultMessage: "Proof of Work" })} iconName="diamond" value={pow} />
);
}
}
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-08-18 18:01:34 +00:00
<AsyncFooterIcon
className={didZap ? "reacted" : ""}
{...longPress()}
2023-08-18 18:11:05 +00:00
title={formatMessage({ defaultMessage: "Zap" })}
2023-08-18 18:01:34 +00:00
iconName={wallet?.isReady() ? "zapFast" : "zap"}
value={zapTotal}
onClick={e => fastZap(e)}
/>
);
}
2023-01-20 17:07:14 +00:00
return null;
}
2023-01-20 17:07:14 +00:00
function repostIcon() {
return (
2023-08-18 18:01:34 +00:00
<AsyncFooterIcon
className={hasReposted() ? "reacted" : ""}
iconName="repeat"
2023-08-18 18:11:05 +00:00
title={formatMessage({ defaultMessage: "Repost" })}
2023-08-18 18:01:34 +00:00
value={reposts.length}
onClick={() => repost()}
/>
);
2023-01-20 17:07:14 +00:00
}
2023-08-18 18:11:05 +00:00
function reactionIcon() {
2023-04-25 09:15:15 +00:00
if (!prefs.enableReactions) {
2023-01-20 17:07:14 +00:00
return null;
}
2023-07-24 14:30:21 +00:00
const reacted = hasReacted("+");
2023-01-08 00:29:59 +00:00
return (
2023-08-18 18:01:34 +00:00
<AsyncFooterIcon
className={reacted ? "reacted" : ""}
iconName={reacted ? "heart-solid" : "heart"}
2023-08-18 18:11:05 +00:00
title={formatMessage({ defaultMessage: "Like" })}
2023-08-18 18:01:34 +00:00
value={positive.length}
onClick={() => react(prefs.reactionEmoji)}
/>
);
2023-01-20 17:07:14 +00:00
}
2023-08-18 18:11:05 +00:00
function replyIcon() {
return (
<AsyncFooterIcon
className={showNoteCreatorModal ? "reacted" : ""}
iconName="reply"
title={formatMessage({ defaultMessage: "Reply" })}
value={0}
onClick={async () => handleReplyButtonClick()}
/>
);
}
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()}
2023-08-18 18:11:05 +00:00
{reactionIcon()}
2023-04-20 21:05:11 +00:00
{repostIcon()}
2023-08-18 18:11:05 +00:00
{replyIcon()}
2023-08-18 18:01:34 +00:00
{powIcon()}
2023-01-25 18:08:53 +00:00
</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>
2023-07-18 15:14:38 +00:00
<ZapsSummary zaps={zaps} />
2023-02-03 21:55:03 +00:00
</>
);
}
2023-08-18 18:01:34 +00:00
interface AsyncFooterIconProps extends HTMLProps<HTMLDivElement> {
iconName: string;
2023-08-18 18:11:05 +00:00
value: number;
2023-08-18 18:01:34 +00:00
loading?: boolean;
onClick?: (e: React.MouseEvent<HTMLDivElement>) => Promise<void>;
}
function AsyncFooterIcon(props: AsyncFooterIconProps) {
2023-08-23 12:19:48 +00:00
const mergedProps = {
...props,
iconSize: 18,
className: `reaction-pill${props.className ? ` ${props.className}` : ""}`,
};
2023-08-18 18:01:34 +00:00
return (
2023-08-23 12:19:48 +00:00
<AsyncIcon {...mergedProps}>
2023-08-18 18:11:05 +00:00
{props.value > 0 && <div className="reaction-pill-number">{formatShort(props.value)}</div>}
2023-08-23 12:19:48 +00:00
</AsyncIcon>
2023-08-18 18:01:34 +00:00
);
}