snort/src/Element/NoteFooter.tsx

384 lines
11 KiB
TypeScript
Raw Normal View History

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";
import {
faTrash,
2023-02-08 21:10:26 +00:00
faHeart,
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";
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-08 21:10:26 +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";
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 {
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 {
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();
const login = useSelector<RootState, HexKey | undefined>(
(s) => s.login.publicKey
);
const { mute, block } = useModeration();
const prefs = useSelector<RootState, UserPreferences>(
(s) => s.login.preferences
);
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;
const langNames = new Intl.DisplayNames([...window.navigator.languages], {
type: "language",
});
const reactions = useMemo(
() => getReactions(related, ev.Id, EventKind.Reaction),
[related, ev]
);
const reposts = useMemo(
2023-02-08 21:10:26 +00:00
() => 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)
.filter((z) => z.valid && z.zapper !== ev.PubKey);
sortedZaps.sort((a, b) => b.amount - a.amount);
return sortedZaps;
}, [related]);
const zapTotal = zaps.reduce((acc, z) => acc + z.amount, 0);
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] || [];
if (rs.map((e) => e.pubkey).includes(reaction.pubkey)) {
return acc;
}
return { ...acc, [kind]: [...rs, reaction] };
},
{
2023-02-08 21:10:26 +00:00
[Reaction.Positive]: [] as TaggedRawEvent[],
[Reaction.Negative]: [] as TaggedRawEvent[],
}
);
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) {
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() {
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() {
if (
window.confirm(
2023-02-08 21:10:26 +00:00
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()) {
if (
!prefs.confirmReposts ||
2023-02-08 21:10:26 +00:00
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) {
return (
2023-01-20 17:07:14 +00:00
<>
<div
className={`reaction-pill ${didZap ? "reacted" : ""}`}
onClick={() => setTip(true)}
>
<div className="reaction-pill-icon">
2023-01-25 18:08:53 +00:00
<Zap />
</div>
{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 (
<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>
{reposts.length > 0 && (
<div className="reaction-pill-number">
{formatShort(reposts.length)}
</div>
)}
</div>
);
2023-01-20 17:07:14 +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
<>
<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>
<div className="reaction-pill-number">
2023-02-08 21:10:26 +00:00
{formatShort(positive.length)}
2023-01-20 17:07:14 +00:00
</div>
</div>
{repostIcon()}
</>
);
2023-01-20 17:07:14 +00:00
}
2023-01-20 22:59:26 +00:00
async function share() {
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",
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
}),
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() {
await navigator.clipboard.writeText(hexToBech32("note", ev.Id));
}
2023-01-21 17:00:09 +00:00
async function copyEvent() {
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} />
</MenuItem>
2023-02-08 21:10:26 +00:00
{prefs.enableReactions && (
<MenuItem onClick={() => react("-")}>
<Dislike />
<FormattedMessage
{...messages.Dislike}
values={{ n: negative.length }}
/>
</MenuItem>
)}
<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-08 21:10:26 +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-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
<>
<div className="footer">
<div className="footer-reactions">
{tipButton()}
{reactionIcons()}
<div
className={`reaction-pill ${reply ? "reacted" : ""}`}
2023-02-07 19:47:57 +00:00
onClick={() => setReply((s) => !s)}
>
<div className="reaction-pill-icon">
<Reply />
</div>
2023-01-25 18:08:53 +00:00
</div>
<Menu
menuButton={
<div className="reaction-pill">
<div className="reaction-pill-icon">
<Dots />
</div>
</div>
}
menuClassName="ctx-menu"
>
{menuItems()}
</Menu>
2023-01-25 18:08:53 +00:00
</div>
<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}
/>
<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
</>
);
}