snort/packages/app/src/Components/Event/Note/NoteFooter.tsx

321 lines
9.8 KiB
TypeScript
Raw Normal View History

2024-01-09 10:30:45 +00:00
import { barrierQueue, normalizeReaction, processWorkQueue, WorkQueueItem } from "@snort/shared";
2024-01-08 09:24:14 +00:00
import { countLeadingZeros, NostrLink, TaggedNostrEvent } from "@snort/system";
2024-01-08 09:01:26 +00:00
import { useEventReactions, useReactions, useUserProfile } from "@snort/system-react";
2023-10-13 11:40:39 +00:00
import { Menu, MenuItem } from "@szhsin/react-menu";
2023-10-16 15:54:55 +00:00
import classNames from "classnames";
import React, { forwardRef, useEffect, useMemo, useState } from "react";
2024-01-04 17:01:18 +00:00
import { FormattedMessage, useIntl } from "react-intl";
import { useLongPress } from "use-long-press";
2023-01-08 00:29:59 +00:00
import { AsyncIcon, AsyncIconProps } from "@/Components/Button/AsyncIcon";
2024-01-10 14:02:36 +00:00
import { ZapsSummary } from "@/Components/Event/ZapsSummary";
2024-01-04 17:01:18 +00:00
import Icon from "@/Components/Icons/Icon";
import SendSats from "@/Components/SendSats/SendSats";
import useEventPublisher from "@/Hooks/useEventPublisher";
2023-11-17 11:52:10 +00:00
import { useInteractionCache } from "@/Hooks/useInteractionCache";
2024-01-04 17:01:18 +00:00
import useLogin from "@/Hooks/useLogin";
2023-11-17 11:52:10 +00:00
import { useNoteCreator } from "@/State/NoteCreator";
2024-01-09 10:30:45 +00:00
import { findTag, getDisplayName } from "@/Utils";
2024-01-04 17:01:18 +00:00
import { formatShort } from "@/Utils/Number";
import { Zapper, ZapTarget } from "@/Utils/Zapper";
import { ZapPoolController } from "@/Utils/ZapPoolController";
import { useWallet } from "@/Wallet";
2023-01-31 11:52:55 +00:00
import messages from "../../messages";
2023-02-08 21:10:26 +00:00
2024-01-09 10:30:45 +00:00
const ZapperQueue: Array<WorkQueueItem> = [];
processWorkQueue(ZapperQueue);
2023-03-11 17:03:42 +00:00
2023-01-16 17:48:25 +00:00
export interface NoteFooterProps {
2023-10-13 10:22:58 +00:00
replies?: number;
2023-08-17 18:54:14 +00:00
ev: TaggedNostrEvent;
2023-01-16 17:48:25 +00:00
}
export default function NoteFooter(props: NoteFooterProps) {
2024-01-08 09:01:26 +00:00
const { ev } = props;
const link = useMemo(() => NostrLink.fromEvent(ev), [ev.id]);
const ids = useMemo(() => [link], [link]);
2024-01-08 09:01:26 +00:00
const related = useReactions("note:reactions", ids, undefined, false);
const { reactions, zaps, reposts } = useEventReactions(link, related);
2024-01-08 09:01:26 +00:00
const { positive } = reactions;
2023-02-08 21:10:26 +00:00
const { formatMessage } = useIntl();
2023-09-23 21:21:37 +00:00
const {
publicKey,
preferences: prefs,
readonly,
2023-11-13 16:51:29 +00:00
} = useLogin(s => ({ preferences: s.appData.item.preferences, publicKey: s.publicKey, readonly: s.readonly }));
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-10-13 15:34:31 +00:00
const { publisher, system } = useEventPublisher();
2023-10-13 11:40:39 +00:00
const note = useNoteCreator(n => ({ show: n.show, replyTo: n.replyTo, update: n.update, quote: n.quote }));
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-09-23 21:21:37 +00:00
const canFastZap = wallet?.isReady() && !readonly;
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-10-13 15:34:31 +00:00
system.BroadcastEvent(evLike);
2023-12-18 11:17:06 +00:00
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-10-13 15:34:31 +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-09-14 11:31:17 +00:00
function getZapTarget(): Array<ZapTarget> | undefined {
if (ev.tags.some(v => v[0] === "zap")) {
return Zapper.fromEvent(ev);
}
2023-03-27 22:58:29 +00:00
2023-09-14 11:31:17 +00:00
const authorTarget = author?.lud16 || author?.lud06;
if (authorTarget) {
return [
{
type: "lnurl",
value: authorTarget,
weight: 1,
name: getDisplayName(author, ev.pubkey),
zap: {
pubkey: ev.pubkey,
2024-01-08 09:01:26 +00:00
event: link,
2023-09-14 11:31:17 +00:00
},
} as ZapTarget,
];
2023-04-05 10:58:26 +00:00
}
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-09-14 11:31:17 +00:00
const lnurl = getZapTarget();
2023-09-23 21:21:37 +00:00
if (canFastZap && lnurl) {
2023-02-25 21:18:36 +00:00
setZapping(true);
try {
2023-09-14 11:31:17 +00:00
await fastZapInner(lnurl, prefs.defaultZapAmount);
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-09-14 11:31:17 +00:00
async function fastZapInner(targets: Array<ZapTarget>, amount: number) {
if (wallet) {
// only allow 1 invoice req/payment at a time to avoid hitting rate limits
2024-01-09 10:30:45 +00:00
await barrierQueue(ZapperQueue, async () => {
2023-09-14 11:31:17 +00:00
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) {
2023-10-17 09:54:34 +00:00
if (CONFIG.features.zapPool) {
ZapPoolController?.allocate(totalSent);
}
2023-09-14 11:31:17 +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-09-14 11:31:17 +00:00
const lnurl = getZapTarget();
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-09-14 11:31:17 +00:00
await fastZapInner(lnurl, prefs.defaultZapAmount);
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 (
2023-11-20 19:16:47 +00:00
<AsyncFooterIcon
title={formatMessage({ defaultMessage: "Proof of Work", id: "grQ+mI" })}
iconName="diamond"
value={pow}
/>
2023-08-18 18:01:34 +00:00
);
}
}
2023-01-20 17:07:14 +00:00
function tipButton() {
2023-09-14 11:31:17 +00:00
const targets = getZapTarget();
if (targets) {
return (
<div className="flex flex-row gap-4 items-center">
<AsyncFooterIcon
className={didZap ? "reacted text-nostr-orange" : "hover:text-nostr-orange"}
{...longPress()}
title={formatMessage({ defaultMessage: "Zap", id: "fBI91o" })}
iconName={canFastZap ? "zapFast" : "zap"}
value={zapTotal}
onClick={e => fastZap(e)}
/>
<ZapsSummary zaps={zaps} />
</div>
);
}
return <div className="w-[18px]"></div>;
2023-01-20 17:07:14 +00:00
}
2023-01-20 17:07:14 +00:00
function repostIcon() {
2023-09-23 21:21:37 +00:00
if (readonly) return;
2023-01-20 17:07:14 +00:00
return (
2023-10-13 11:40:39 +00:00
<Menu
menuButton={
<AsyncFooterIcon
2023-10-18 08:19:50 +00:00
className={hasReposted() ? "reacted text-nostr-blue" : "hover:text-nostr-blue"}
2023-10-13 11:40:39 +00:00
iconName="repeat"
2023-11-20 19:16:47 +00:00
title={formatMessage({ defaultMessage: "Repost", id: "JeoS4y" })}
2023-10-13 11:40:39 +00:00
value={reposts.length}
/>
}
menuClassName="ctx-menu"
align="start">
<div className="close-menu-container">
{/* This menu item serves as a "close menu" button;
it allows the user to click anywhere nearby the menu to close it. */}
<MenuItem>
<div className="close-menu" />
</MenuItem>
</div>
<MenuItem onClick={() => repost()} disabled={hasReposted()}>
<Icon name="repeat" />
<FormattedMessage defaultMessage="Repost" id="JeoS4y" />
2023-10-13 11:40:39 +00:00
</MenuItem>
<MenuItem
onClick={() =>
note.update(n => {
n.reset();
n.quote = ev;
n.show = true;
})
}>
<Icon name="edit" />
<FormattedMessage defaultMessage="Quote Repost" id="C7642/" />
2023-10-13 11:40:39 +00:00
</MenuItem>
</Menu>
);
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
2023-10-18 08:19:50 +00:00
className={reacted ? "reacted text-nostr-red" : "hover:text-nostr-red"}
2023-08-18 18:01:34 +00:00
iconName={reacted ? "heart-solid" : "heart"}
2023-11-20 19:16:47 +00:00
title={formatMessage({ defaultMessage: "Like", id: "qtWLmt" })}
2023-08-18 18:01:34 +00:00
value={positive.length}
2023-09-23 21:21:37 +00:00
onClick={async () => {
if (readonly) return;
await react(prefs.reactionEmoji);
}}
2023-08-18 18:01:34 +00:00
/>
);
2023-01-20 17:07:14 +00:00
}
2023-08-18 18:11:05 +00:00
function replyIcon() {
2023-09-23 21:21:37 +00:00
if (readonly) return;
2023-08-18 18:11:05 +00:00
return (
<AsyncFooterIcon
2023-10-18 08:19:50 +00:00
className={note.show ? "reacted text-nostr-purple" : "hover:text-nostr-purple"}
2023-08-18 18:11:05 +00:00
iconName="reply"
2023-11-20 19:16:47 +00:00
title={formatMessage({ defaultMessage: "Reply", id: "9HU8vw" })}
2023-10-13 10:22:58 +00:00
value={props.replies ?? 0}
2023-08-18 18:11:05 +00:00
onClick={async () => handleReplyButtonClick()}
/>
);
}
const handleReplyButtonClick = () => {
2023-09-21 20:02:59 +00:00
note.update(v => {
if (v.replyTo?.id !== ev.id) {
v.reset();
}
v.show = true;
v.replyTo = ev;
});
};
2023-01-20 17:07:14 +00:00
return (
<div className="flex flex-row justify-between gap-2 overflow-hidden w-[360px] flex-grow max-w-full h-6">
{replyIcon()}
{repostIcon()}
{reactionIcon()}
{powIcon()}
{tipButton()}
<SendSats targets={getZapTarget()} onClose={() => setTip(false)} show={tip} note={ev.id} allocatePool={true} />
</div>
);
}
2023-08-18 18:01:34 +00:00
2023-11-17 11:52:10 +00:00
const AsyncFooterIcon = forwardRef((props: AsyncIconProps & { value: number }, ref) => {
2023-08-23 12:19:48 +00:00
const mergedProps = {
...props,
iconSize: 18,
2023-11-27 13:31:45 +00:00
className: classNames("transition duration-200 ease-in-out reaction-pill cursor-pointer", props.className),
2023-08-23 12:19:48 +00:00
};
2023-11-17 11:52:10 +00:00
2023-08-18 18:01:34 +00:00
return (
2023-11-17 11:52:10 +00:00
<AsyncIcon ref={ref} {...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
);
});
2024-01-04 11:13:28 +00:00
2024-01-04 12:05:13 +00:00
AsyncFooterIcon.displayName = "AsyncFooterIcon";