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

454 lines
14 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-02-08 21:10:26 +00:00
import { useIntl, FormattedMessage } from "react-intl";
import { Menu, MenuItem } from "@szhsin/react-menu";
2023-02-25 21:18:36 +00:00
import { useLongPress } from "use-long-press";
2023-06-21 15:08:48 +00:00
import { TaggedRawEvent, HexKey, u256, encodeTLV, NostrPrefix, Lists, 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-05-04 13:16:58 +00:00
import { ReBroadcaster } from "Element/ReBroadcaster";
2023-02-08 21:10:26 +00:00
import Reactions from "Element/Reactions";
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-05-04 13:16:58 +00:00
import {
setNote as setReBroadcastNote,
setShow as setReBroadcastShow,
reset as resetReBroadcast,
} from "State/ReBroadcast";
2023-01-26 11:34:18 +00:00
import useModeration from "Hooks/useModeration";
2023-05-16 21:30:52 +00:00
import { TranslateHost } from "Const";
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";
import { setBookmarked, setPinned } from "Login";
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-31 11:52:55 +00:00
export interface Translation {
text: string;
fromLanguage: string;
confidence: number;
2023-01-31 11:52:55 +00:00
}
2023-01-08 00:29:59 +00:00
2023-01-16 17:48:25 +00:00
export interface NoteFooterProps {
2023-02-12 12:31:48 +00:00
reposts: TaggedRawEvent[];
zaps: ParsedZap[];
positive: TaggedRawEvent[];
negative: TaggedRawEvent[];
showReactions: boolean;
setShowReactions(b: boolean): void;
2023-03-28 14:34:01 +00:00
ev: TaggedRawEvent;
onTranslated?: (content: Translation) => void;
2023-01-16 17:48:25 +00:00
}
export default function NoteFooter(props: NoteFooterProps) {
2023-02-12 12:31:48 +00:00
const { ev, showReactions, setShowReactions, positive, negative, 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-04-14 15:02:15 +00:00
const { pinned, bookmarked, publicKey, preferences: prefs, relays } = login;
const { mute, block } = useModeration();
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);
2023-05-04 13:16:58 +00:00
const showReBroadcastModal = useSelector((s: RootState) => s.reBroadcast.show);
const reBroadcastNote = useSelector((s: RootState) => s.reBroadcast.note);
const replyTo = useSelector((s: RootState) => s.noteCreator.replyTo);
const willRenderNoteCreator = showNoteCreatorModal && replyTo?.id === ev.id;
2023-05-04 13:16:58 +00:00
const willRenderReBroadcast = showReBroadcastModal && reBroadcastNote && reBroadcastNote?.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;
2023-01-31 11:52:55 +00:00
const lang = window.navigator.language;
const langNames = new Intl.DisplayNames([...window.navigator.languages], {
type: "language",
});
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 deleteEvent() {
2023-04-14 15:02:15 +00:00
if (window.confirm(formatMessage(messages.ConfirmDeletion, { id: ev.id.substring(0, 8) })) && publisher) {
2023-03-28 14:34:01 +00:00
const evDelete = await publisher.delete(ev.id);
2023-06-15 11:03:05 +00:00
System.BroadcastEvent(evDelete);
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-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
}
2023-01-20 22:59:26 +00:00
async function share() {
2023-05-15 12:52:29 +00:00
const link = encodeTLV(NostrPrefix.Event, ev.id, ev.relays);
2023-03-25 22:55:34 +00:00
const url = `${window.location.protocol}//${window.location.host}/e/${link}`;
2023-01-20 22:59:26 +00:00
if ("share" in window.navigator) {
await window.navigator.share({
title: "Snort",
url: url,
2023-01-20 22:59:26 +00:00
});
} else {
await navigator.clipboard.writeText(url);
}
}
2023-01-31 11:52:55 +00:00
async function translate() {
const res = await fetch(`${TranslateHost}/translate`, {
method: "POST",
body: JSON.stringify({
2023-03-28 14:34:01 +00:00
q: ev.content,
2023-01-31 11:52:55 +00:00
source: "auto",
2023-02-06 22:06:47 +00:00
target: lang.split("-")[0],
2023-01-31 11:52:55 +00:00
}),
headers: { "Content-Type": "application/json" },
2023-01-31 11:52:55 +00:00
});
if (res.ok) {
2023-02-07 19:47:57 +00:00
const result = await res.json();
2023-01-31 11:52:55 +00:00
if (typeof props.onTranslated === "function" && result) {
props.onTranslated({
text: result.translatedText,
fromLanguage: langNames.of(result.detectedLanguage.language),
confidence: result.detectedLanguage.confidence,
2023-01-31 11:52:55 +00:00
} as Translation);
}
}
}
2023-01-21 11:59:19 +00:00
async function copyId() {
2023-05-15 12:52:29 +00:00
const link = encodeTLV(NostrPrefix.Event, ev.id, ev.relays);
2023-03-25 22:55:34 +00:00
await navigator.clipboard.writeText(link);
2023-01-21 11:59:19 +00:00
}
async function pin(id: HexKey) {
2023-04-14 15:02:15 +00:00
if (publisher) {
const es = [...pinned.item, id];
const ev = await publisher.noteList(es, Lists.Pinned);
2023-06-15 11:03:05 +00:00
System.BroadcastEvent(ev);
2023-04-14 11:33:19 +00:00
setPinned(login, es, ev.created_at * 1000);
}
}
async function bookmark(id: HexKey) {
2023-04-14 15:02:15 +00:00
if (publisher) {
const es = [...bookmarked.item, id];
const ev = await publisher.noteList(es, Lists.Bookmarked);
2023-06-15 11:03:05 +00:00
System.BroadcastEvent(ev);
2023-04-14 11:33:19 +00:00
setBookmarked(login, es, ev.created_at * 1000);
}
}
2023-01-21 17:00:09 +00:00
async function copyEvent() {
2023-03-28 14:34:01 +00:00
await navigator.clipboard.writeText(JSON.stringify(ev, undefined, " "));
2023-01-21 17:00:09 +00:00
}
2023-01-20 22:59:26 +00:00
function menuItems() {
return (
<>
2023-02-14 22:47:28 +00:00
<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>
2023-02-28 19:50:00 +00:00
<MenuItem onClick={() => setShowReactions(true)}>
2023-03-02 17:47:02 +00:00
<Icon name="heart" />
2023-02-28 19:50:00 +00:00
<FormattedMessage {...messages.Reactions} />
</MenuItem>
2023-01-20 22:59:26 +00:00
<MenuItem onClick={() => share()}>
2023-03-02 17:47:02 +00:00
<Icon name="share" />
2023-02-08 21:10:26 +00:00
<FormattedMessage {...messages.Share} />
2023-01-20 22:59:26 +00:00
</MenuItem>
2023-04-14 11:33:19 +00:00
{!pinned.item.includes(ev.id) && (
2023-03-28 14:34:01 +00:00
<MenuItem onClick={() => pin(ev.id)}>
2023-03-02 17:47:02 +00:00
<Icon name="pin" />
<FormattedMessage {...messages.Pin} />
</MenuItem>
)}
2023-04-14 11:33:19 +00:00
{!bookmarked.item.includes(ev.id) && (
2023-03-28 14:34:01 +00:00
<MenuItem onClick={() => bookmark(ev.id)}>
2023-03-02 18:39:29 +00:00
<Icon name="bookmark" />
<FormattedMessage {...messages.Bookmark} />
</MenuItem>
)}
2023-01-21 11:59:19 +00:00
<MenuItem onClick={() => copyId()}>
2023-03-04 16:54:56 +00:00
<Icon name="copy" />
2023-02-08 21:10:26 +00:00
<FormattedMessage {...messages.CopyID} />
2023-01-21 11:59:19 +00:00
</MenuItem>
2023-03-28 14:34:01 +00:00
<MenuItem onClick={() => mute(ev.pubkey)}>
2023-03-02 17:47:02 +00:00
<Icon name="mute" />
2023-02-08 21:10:26 +00:00
<FormattedMessage {...messages.Mute} />
</MenuItem>
2023-02-08 21:10:26 +00:00
{prefs.enableReactions && (
<MenuItem onClick={() => react("-")}>
2023-03-04 16:54:56 +00:00
<Icon name="dislike" />
2023-02-09 11:24:15 +00:00
<FormattedMessage {...messages.DislikeAction} />
2023-02-08 21:10:26 +00:00
</MenuItem>
)}
2023-05-04 13:16:58 +00:00
{ev.pubkey === publicKey && (
<MenuItem onClick={handleReBroadcastButtonClick}>
<Icon name="relay" />
<FormattedMessage {...messages.ReBroadcast} />
</MenuItem>
)}
{ev.pubkey !== publicKey && (
<MenuItem onClick={() => block(ev.pubkey)}>
<Icon name="block" />
<FormattedMessage {...messages.Block} />
</MenuItem>
)}
2023-01-31 11:52:55 +00:00
<MenuItem onClick={() => translate()}>
2023-03-02 17:47:02 +00:00
<Icon name="translate" />
2023-02-09 12:26:54 +00:00
<FormattedMessage {...messages.TranslateTo} values={{ lang: langNames.of(lang.split("-")[0]) }} />
2023-01-31 11:52:55 +00:00
</MenuItem>
2023-01-21 17:00:09 +00:00
{prefs.showDebugMenus && (
<MenuItem onClick={() => copyEvent()}>
2023-03-02 17:47:02 +00:00
<Icon name="json" />
2023-02-08 21:10:26 +00:00
<FormattedMessage {...messages.CopyJSON} />
2023-01-21 17:00:09 +00:00
</MenuItem>
)}
2023-01-20 22:59:26 +00:00
{isMine && (
<MenuItem onClick={() => deleteEvent()}>
2023-03-02 17:47:02 +00:00
<Icon name="trash" className="red" />
2023-02-08 21:10:26 +00:00
<FormattedMessage {...messages.Delete} />
2023-01-20 22:59:26 +00:00
</MenuItem>
)}
</>
);
2023-01-20 22:59:26 +00:00
}
2023-01-21 11:59:19 +00:00
const handleReplyButtonClick = () => {
if (replyTo?.id !== ev.id) {
dispatch(reset());
}
dispatch(setReplyTo(ev));
dispatch(setShow(!showNoteCreatorModal));
};
2023-05-04 13:16:58 +00:00
const handleReBroadcastButtonClick = () => {
if (reBroadcastNote?.id !== ev.id) {
dispatch(resetReBroadcast());
}
dispatch(setReBroadcastNote(ev));
dispatch(setReBroadcastShow(!showReBroadcastModal));
};
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>
<Menu
menuButton={
<div className="reaction-pill">
2023-03-02 18:39:29 +00:00
<Icon name="dots" size={15} />
</div>
}
2023-02-09 12:26:54 +00:00
menuClassName="ctx-menu">
{menuItems()}
</Menu>
2023-01-25 18:08:53 +00:00
</div>
{willRenderNoteCreator && <NoteCreator />}
2023-05-04 13:16:58 +00:00
{willRenderReBroadcast && <ReBroadcaster />}
2023-02-08 21:10:26 +00:00
<Reactions
show={showReactions}
setShow={setShowReactions}
positive={positive}
negative={negative}
reposts={reposts}
zaps={zaps}
/>
<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
</>
);
}