2023-01-08 00:29:59 +00:00
|
|
|
import { useMemo, useState } from "react";
|
|
|
|
import { useSelector } from "react-redux";
|
2023-02-08 21:10:26 +00:00
|
|
|
import { useIntl, FormattedMessage } from "react-intl";
|
2023-02-07 20:04:50 +00:00
|
|
|
import {
|
|
|
|
faTrash,
|
2023-02-08 21:10:26 +00:00
|
|
|
faHeart,
|
2023-02-07 20:04:50 +00:00
|
|
|
faRepeat,
|
|
|
|
faShareNodes,
|
|
|
|
faCopy,
|
|
|
|
faCommentSlash,
|
|
|
|
faBan,
|
|
|
|
faLanguage,
|
|
|
|
} from "@fortawesome/free-solid-svg-icons";
|
2023-01-08 00:29:59 +00:00
|
|
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
2023-02-07 20:04:50 +00:00
|
|
|
import { Menu, MenuItem } from "@szhsin/react-menu";
|
2023-01-08 00:29:59 +00:00
|
|
|
|
2023-01-25 18:08:53 +00:00
|
|
|
import Dislike from "Icons/Dislike";
|
|
|
|
import Heart from "Icons/Heart";
|
|
|
|
import Dots from "Icons/Dots";
|
|
|
|
import Zap from "Icons/Zap";
|
|
|
|
import Reply from "Icons/Reply";
|
2023-01-20 11:11:50 +00:00
|
|
|
import { formatShort } from "Number";
|
|
|
|
import useEventPublisher from "Feed/EventPublisher";
|
2023-02-09 12:26:54 +00:00
|
|
|
import { getReactions, dedupeByPubkey, hexToBech32, normalizeReaction, Reaction } from "Util";
|
2023-01-20 11:11:50 +00:00
|
|
|
import { NoteCreator } from "Element/NoteCreator";
|
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-02-08 22:04:55 +00:00
|
|
|
import { parseZap, ZapsSummary } from "Element/Zap";
|
2023-01-27 21:38:41 +00:00
|
|
|
import { useUserProfile } from "Feed/ProfileFeed";
|
2023-01-20 11:11:50 +00:00
|
|
|
import { default as NEvent } from "Nostr/Event";
|
|
|
|
import { RootState } from "State/Store";
|
|
|
|
import { HexKey, TaggedRawEvent } from "Nostr";
|
|
|
|
import EventKind from "Nostr/EventKind";
|
2023-01-20 17:07:14 +00:00
|
|
|
import { UserPreferences } from "State/Login";
|
2023-01-26 11:34:18 +00:00
|
|
|
import useModeration from "Hooks/useModeration";
|
2023-01-31 11:52:55 +00:00
|
|
|
import { TranslateHost } from "Const";
|
|
|
|
|
2023-02-08 21:10:26 +00:00
|
|
|
import messages from "./messages";
|
|
|
|
|
2023-01-31 11:52:55 +00:00
|
|
|
export interface Translation {
|
2023-02-07 20:04:50 +00:00
|
|
|
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-07 20:04:50 +00:00
|
|
|
related: TaggedRawEvent[];
|
|
|
|
ev: NEvent;
|
|
|
|
onTranslated?: (content: Translation) => void;
|
2023-01-16 17:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function NoteFooter(props: NoteFooterProps) {
|
2023-01-20 17:07:14 +00:00
|
|
|
const { related, ev } = props;
|
2023-02-08 21:10:26 +00:00
|
|
|
const { formatMessage } = useIntl();
|
2023-02-09 12:26:54 +00:00
|
|
|
const login = useSelector<RootState, HexKey | undefined>(s => s.login.publicKey);
|
2023-01-27 21:10:14 +00:00
|
|
|
const { mute, block } = useModeration();
|
2023-02-09 12:26:54 +00:00
|
|
|
const prefs = useSelector<RootState, UserPreferences>(s => s.login.preferences);
|
2023-01-27 21:38:41 +00:00
|
|
|
const author = useUserProfile(ev.RootPubKey);
|
2023-01-20 17:07:14 +00:00
|
|
|
const publisher = useEventPublisher();
|
|
|
|
const [reply, setReply] = useState(false);
|
2023-02-08 21:10:26 +00:00
|
|
|
const [showReactions, setShowReactions] = useState(false);
|
2023-01-20 17:07:14 +00:00
|
|
|
const [tip, setTip] = useState(false);
|
|
|
|
const isMine = ev.RootPubKey === login;
|
2023-01-31 11:52:55 +00:00
|
|
|
const lang = window.navigator.language;
|
2023-02-07 20:04:50 +00:00
|
|
|
const langNames = new Intl.DisplayNames([...window.navigator.languages], {
|
|
|
|
type: "language",
|
|
|
|
});
|
2023-02-09 12:26:54 +00:00
|
|
|
const reactions = useMemo(() => getReactions(related, ev.Id, EventKind.Reaction), [related, ev]);
|
|
|
|
const reposts = useMemo(() => dedupeByPubkey(getReactions(related, ev.Id, EventKind.Repost)), [related, ev]);
|
2023-02-08 21:10:26 +00:00
|
|
|
const zaps = useMemo(() => {
|
|
|
|
const sortedZaps = getReactions(related, ev.Id, EventKind.ZapReceipt)
|
|
|
|
.map(parseZap)
|
2023-02-09 12:26:54 +00:00
|
|
|
.filter(z => z.valid && z.zapper !== ev.PubKey);
|
2023-02-08 21:10:26 +00:00
|
|
|
sortedZaps.sort((a, b) => b.amount - a.amount);
|
|
|
|
return sortedZaps;
|
|
|
|
}, [related]);
|
2023-02-07 20:04:50 +00:00
|
|
|
const zapTotal = zaps.reduce((acc, z) => acc + z.amount, 0);
|
2023-02-09 12:26:54 +00:00
|
|
|
const didZap = zaps.some(a => a.zapper === login);
|
2023-01-20 17:07:14 +00:00
|
|
|
const groupReactions = useMemo(() => {
|
2023-02-08 21:10:26 +00:00
|
|
|
const result = reactions?.reduce(
|
|
|
|
(acc, reaction) => {
|
2023-02-07 19:47:57 +00:00
|
|
|
const kind = normalizeReaction(reaction.content);
|
2023-02-08 21:10:26 +00:00
|
|
|
const rs = acc[kind] || [];
|
2023-02-09 12:26:54 +00:00
|
|
|
if (rs.map(e => e.pubkey).includes(reaction.pubkey)) {
|
2023-02-08 21:10:26 +00:00
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
return { ...acc, [kind]: [...rs, reaction] };
|
2023-02-07 20:04:50 +00:00
|
|
|
},
|
|
|
|
{
|
2023-02-08 21:10:26 +00:00
|
|
|
[Reaction.Positive]: [] as TaggedRawEvent[],
|
|
|
|
[Reaction.Negative]: [] as TaggedRawEvent[],
|
2023-02-07 20:04:50 +00:00
|
|
|
}
|
|
|
|
);
|
2023-02-08 21:10:26 +00:00
|
|
|
return {
|
|
|
|
[Reaction.Positive]: dedupeByPubkey(result[Reaction.Positive]),
|
|
|
|
[Reaction.Negative]: dedupeByPubkey(result[Reaction.Negative]),
|
|
|
|
};
|
2023-01-20 17:07:14 +00:00
|
|
|
}, [reactions]);
|
2023-02-08 21:10:26 +00:00
|
|
|
const positive = groupReactions[Reaction.Positive];
|
|
|
|
const negative = groupReactions[Reaction.Negative];
|
2023-01-08 00:29:59 +00:00
|
|
|
|
2023-01-20 17:07:14 +00:00
|
|
|
function hasReacted(emoji: string) {
|
2023-02-09 12:26:54 +00:00
|
|
|
return reactions?.some(({ pubkey, content }) => normalizeReaction(content) === emoji && pubkey === login);
|
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-02-09 12:26:54 +00:00
|
|
|
return reposts.some(a => a.pubkey === login);
|
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) {
|
|
|
|
if (!hasReacted(content)) {
|
2023-02-07 19:47:57 +00:00
|
|
|
const evLike = await publisher.react(ev, content);
|
2023-01-20 17:07:14 +00:00
|
|
|
publisher.broadcast(evLike);
|
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-02-09 12:26:54 +00:00
|
|
|
if (window.confirm(formatMessage(messages.ConfirmDeletion, { id: ev.Id.substring(0, 8) }))) {
|
2023-02-07 19:47:57 +00:00
|
|
|
const evDelete = await publisher.delete(ev.Id);
|
2023-01-20 17:07:14 +00:00
|
|
|
publisher.broadcast(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() {
|
|
|
|
if (!hasReposted()) {
|
2023-02-09 12:26:54 +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-01-20 17:07:14 +00:00
|
|
|
publisher.broadcast(evRepost);
|
|
|
|
}
|
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
|
|
|
function tipButton() {
|
2023-02-07 19:47:57 +00:00
|
|
|
const service = author?.lud16 || author?.lud06;
|
2023-01-20 17:07:14 +00:00
|
|
|
if (service) {
|
2023-01-19 17:56:39 +00:00
|
|
|
return (
|
2023-01-20 17:07:14 +00:00
|
|
|
<>
|
2023-02-09 12:26:54 +00:00
|
|
|
<div className={`reaction-pill ${didZap ? "reacted" : ""}`} onClick={() => setTip(true)}>
|
2023-01-19 17:56:39 +00:00
|
|
|
<div className="reaction-pill-icon">
|
2023-01-25 18:08:53 +00:00
|
|
|
<Zap />
|
2023-01-19 17:56:39 +00:00
|
|
|
</div>
|
2023-02-09 12:26:54 +00:00
|
|
|
{zapTotal > 0 && <div className="reaction-pill-number">{formatShort(zapTotal)}</div>}
|
2023-01-19 17:56:39 +00:00
|
|
|
</div>
|
2023-01-20 17:07:14 +00:00
|
|
|
</>
|
2023-02-07 20:04:50 +00:00
|
|
|
);
|
2023-01-19 17:56:39 +00:00
|
|
|
}
|
2023-01-20 17:07:14 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-01-19 17:56:39 +00:00
|
|
|
|
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-01-20 17:07:14 +00:00
|
|
|
<div className="reaction-pill-icon">
|
|
|
|
<FontAwesomeIcon icon={faRepeat} />
|
|
|
|
</div>
|
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-02-07 20:04:50 +00:00
|
|
|
);
|
2023-01-20 17:07:14 +00:00
|
|
|
}
|
2023-01-19 17:56:39 +00:00
|
|
|
|
2023-01-20 17:07:14 +00:00
|
|
|
function reactionIcons() {
|
|
|
|
if (!prefs.enableReactions) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-01-08 00:29:59 +00:00
|
|
|
return (
|
2023-01-20 17:07:14 +00:00
|
|
|
<>
|
2023-02-09 12:26:54 +00:00
|
|
|
<div className={`reaction-pill ${hasReacted("+") ? "reacted" : ""} `} onClick={() => react("+")}>
|
2023-01-20 17:07:14 +00:00
|
|
|
<div className="reaction-pill-icon">
|
2023-01-25 18:08:53 +00:00
|
|
|
<Heart />
|
2023-01-20 17:07:14 +00:00
|
|
|
</div>
|
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>
|
|
|
|
{repostIcon()}
|
|
|
|
</>
|
2023-02-07 20:04:50 +00:00
|
|
|
);
|
2023-01-20 17:07:14 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 22:59:26 +00:00
|
|
|
async function share() {
|
2023-02-09 12:26:54 +00:00
|
|
|
const url = `${window.location.protocol}//${window.location.host}/e/${hexToBech32("note", ev.Id)}`;
|
2023-01-20 22:59:26 +00:00
|
|
|
if ("share" in window.navigator) {
|
|
|
|
await window.navigator.share({
|
|
|
|
title: "Snort",
|
2023-02-07 20:04:50 +00:00
|
|
|
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({
|
|
|
|
q: ev.Content,
|
|
|
|
source: "auto",
|
2023-02-06 22:06:47 +00:00
|
|
|
target: lang.split("-")[0],
|
2023-01-31 11:52:55 +00:00
|
|
|
}),
|
2023-02-07 20:04:50 +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),
|
2023-02-07 20:04:50 +00:00
|
|
|
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() {
|
|
|
|
await navigator.clipboard.writeText(hexToBech32("note", ev.Id));
|
|
|
|
}
|
|
|
|
|
2023-01-21 17:00:09 +00:00
|
|
|
async function copyEvent() {
|
2023-02-09 12:26:54 +00:00
|
|
|
await navigator.clipboard.writeText(JSON.stringify(ev.Original, undefined, " "));
|
2023-01-21 17:00:09 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 22:59:26 +00:00
|
|
|
function menuItems() {
|
|
|
|
return (
|
|
|
|
<>
|
2023-01-25 18:08:53 +00:00
|
|
|
{prefs.enableReactions && (
|
2023-02-08 21:10:26 +00:00
|
|
|
<MenuItem onClick={() => setShowReactions(true)}>
|
|
|
|
<FontAwesomeIcon icon={faHeart} />
|
|
|
|
<FormattedMessage {...messages.Reactions} />
|
2023-01-25 18:08:53 +00:00
|
|
|
</MenuItem>
|
|
|
|
)}
|
2023-01-20 22:59:26 +00:00
|
|
|
<MenuItem onClick={() => share()}>
|
2023-01-21 11:59:19 +00:00
|
|
|
<FontAwesomeIcon icon={faShareNodes} />
|
2023-02-08 21:10:26 +00:00
|
|
|
<FormattedMessage {...messages.Share} />
|
2023-01-20 22:59:26 +00:00
|
|
|
</MenuItem>
|
2023-01-21 11:59:19 +00:00
|
|
|
<MenuItem onClick={() => copyId()}>
|
|
|
|
<FontAwesomeIcon icon={faCopy} />
|
2023-02-08 21:10:26 +00:00
|
|
|
<FormattedMessage {...messages.CopyID} />
|
2023-01-21 11:59:19 +00:00
|
|
|
</MenuItem>
|
2023-01-26 11:34:18 +00:00
|
|
|
<MenuItem onClick={() => mute(ev.PubKey)}>
|
|
|
|
<FontAwesomeIcon icon={faCommentSlash} />
|
2023-02-08 21:10:26 +00:00
|
|
|
<FormattedMessage {...messages.Mute} />
|
2023-01-27 21:10:14 +00:00
|
|
|
</MenuItem>
|
2023-02-08 21:10:26 +00:00
|
|
|
{prefs.enableReactions && (
|
|
|
|
<MenuItem onClick={() => react("-")}>
|
|
|
|
<Dislike />
|
2023-02-09 12:26:54 +00:00
|
|
|
<FormattedMessage {...messages.Dislike} values={{ n: negative.length }} />
|
2023-02-08 21:10:26 +00:00
|
|
|
</MenuItem>
|
|
|
|
)}
|
2023-01-27 21:10:14 +00:00
|
|
|
<MenuItem onClick={() => block(ev.PubKey)}>
|
|
|
|
<FontAwesomeIcon icon={faBan} />
|
2023-02-08 21:10:26 +00:00
|
|
|
<FormattedMessage {...messages.Block} />
|
2023-01-26 11:34:18 +00:00
|
|
|
</MenuItem>
|
2023-01-31 11:52:55 +00:00
|
|
|
<MenuItem onClick={() => translate()}>
|
|
|
|
<FontAwesomeIcon icon={faLanguage} />
|
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()}>
|
|
|
|
<FontAwesomeIcon icon={faCopy} />
|
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-01-21 11:59:19 +00:00
|
|
|
<FontAwesomeIcon icon={faTrash} className="red" />
|
2023-02-08 21:10:26 +00:00
|
|
|
<FormattedMessage {...messages.Delete} />
|
2023-01-20 22:59:26 +00:00
|
|
|
</MenuItem>
|
|
|
|
)}
|
|
|
|
</>
|
2023-02-07 20:04:50 +00:00
|
|
|
);
|
2023-01-20 22:59:26 +00:00
|
|
|
}
|
2023-01-21 11:59:19 +00:00
|
|
|
|
2023-01-20 17:07:14 +00:00
|
|
|
return (
|
2023-02-03 21:55:03 +00:00
|
|
|
<>
|
2023-02-07 20:04:50 +00:00
|
|
|
<div className="footer">
|
|
|
|
<div className="footer-reactions">
|
|
|
|
{tipButton()}
|
|
|
|
{reactionIcons()}
|
2023-02-09 12:26:54 +00:00
|
|
|
<div className={`reaction-pill ${reply ? "reacted" : ""}`} onClick={() => setReply(s => !s)}>
|
2023-02-07 20:04:50 +00:00
|
|
|
<div className="reaction-pill-icon">
|
|
|
|
<Reply />
|
|
|
|
</div>
|
2023-01-25 18:08:53 +00:00
|
|
|
</div>
|
2023-02-07 20:04:50 +00:00
|
|
|
<Menu
|
|
|
|
menuButton={
|
|
|
|
<div className="reaction-pill">
|
|
|
|
<div className="reaction-pill-icon">
|
|
|
|
<Dots />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
}
|
2023-02-09 12:26:54 +00:00
|
|
|
menuClassName="ctx-menu">
|
2023-02-07 20:04:50 +00:00
|
|
|
{menuItems()}
|
|
|
|
</Menu>
|
2023-01-25 18:08:53 +00:00
|
|
|
</div>
|
2023-02-09 12:26:54 +00:00
|
|
|
<NoteCreator autoFocus={true} replyTo={ev} onSend={() => setReply(false)} show={reply} setShow={setReply} />
|
2023-02-08 21:10:26 +00:00
|
|
|
<Reactions
|
|
|
|
show={showReactions}
|
|
|
|
setShow={setShowReactions}
|
|
|
|
positive={positive}
|
|
|
|
negative={negative}
|
|
|
|
reposts={reposts}
|
|
|
|
zaps={zaps}
|
|
|
|
/>
|
2023-02-07 20:04:50 +00:00
|
|
|
<SendSats
|
|
|
|
svc={author?.lud16 || author?.lud06}
|
|
|
|
onClose={() => setTip(false)}
|
|
|
|
show={tip}
|
|
|
|
author={author?.pubkey}
|
|
|
|
target={author?.display_name || author?.name}
|
|
|
|
note={ev.Id}
|
|
|
|
/>
|
|
|
|
</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
|
|
|
</>
|
2023-02-07 20:04:50 +00:00
|
|
|
);
|
2023-01-15 23:44:50 +00:00
|
|
|
}
|