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

393 lines
12 KiB
TypeScript
Raw Normal View History

2022-12-18 14:51:47 +00:00
import "./Note.css";
2023-04-08 21:30:00 +00:00
import React, { useMemo, useState, useLayoutEffect, ReactNode } from "react";
2023-01-25 18:08:53 +00:00
import { useNavigate, Link } from "react-router-dom";
2023-02-06 21:42:47 +00:00
import { useInView } from "react-intersection-observer";
2023-02-08 21:10:26 +00:00
import { useIntl, FormattedMessage } from "react-intl";
2023-04-14 15:02:15 +00:00
import { TaggedRawEvent, HexKey, EventKind, NostrPrefix, Lists } from "@snort/nostr";
2022-12-29 15:36:40 +00:00
import useEventPublisher from "Feed/EventPublisher";
2023-03-02 17:47:02 +00:00
import Icon from "Icons/Icon";
2023-02-12 12:31:48 +00:00
import { parseZap } from "Element/Zap";
2023-01-20 11:11:50 +00:00
import ProfileImage from "Element/ProfileImage";
import Text from "Element/Text";
2023-02-18 01:10:18 +00:00
import {
eventLink,
getReactions,
dedupeByPubkey,
tagFilterOfTextRepost,
hexToBech32,
normalizeReaction,
Reaction,
2023-03-03 14:30:31 +00:00
profileLink,
2023-02-18 01:10:18 +00:00
} from "Util";
2023-01-31 11:52:55 +00:00
import NoteFooter, { Translation } from "Element/NoteFooter";
2023-01-20 11:11:50 +00:00
import NoteTime from "Element/NoteTime";
2023-04-08 21:29:38 +00:00
import Reveal from "Element/Reveal";
2023-01-26 11:34:18 +00:00
import useModeration from "Hooks/useModeration";
2023-03-29 12:10:22 +00:00
import { UserCache } from "Cache/UserCache";
2023-04-05 15:10:14 +00:00
import Poll from "Element/Poll";
2023-04-14 11:33:19 +00:00
import { EventExt } from "System/EventExt";
import useLogin from "Hooks/useLogin";
import { setBookmarked, setPinned } from "Login";
import { NostrFileElement } from "Element/NostrFileHeader";
2022-12-18 14:51:47 +00:00
2023-02-08 21:10:26 +00:00
import messages from "./messages";
2023-01-16 17:48:25 +00:00
export interface NoteProps {
2023-03-28 14:34:01 +00:00
data: TaggedRawEvent;
className?: string;
2023-03-28 14:34:01 +00:00
related: readonly TaggedRawEvent[];
highlight?: boolean;
ignoreModeration?: boolean;
2023-04-18 12:45:26 +00:00
onClick?: (e: TaggedRawEvent) => void;
2023-04-18 21:20:13 +00:00
depth?: number;
2023-01-31 11:52:55 +00:00
options?: {
showHeader?: boolean;
showTime?: boolean;
showPinned?: boolean;
showBookmarked?: boolean;
showFooter?: boolean;
2023-02-12 12:31:48 +00:00
showReactionsLink?: boolean;
canUnpin?: boolean;
canUnbookmark?: boolean;
2023-03-31 22:43:07 +00:00
canClick?: boolean;
};
2023-01-16 17:48:25 +00:00
}
2023-02-07 19:47:57 +00:00
const HiddenNote = ({ children }: { children: React.ReactNode }) => {
const [show, setShow] = useState(false);
return show ? (
2023-02-07 19:47:57 +00:00
<>{children}</>
) : (
2023-01-28 18:51:08 +00:00
<div className="card note hidden-note">
<div className="header">
2023-02-08 21:10:26 +00:00
<p>
<FormattedMessage {...messages.MutedAuthor} />
</p>
<button onClick={() => setShow(true)}>
<FormattedMessage {...messages.Show} />
</button>
</div>
</div>
);
};
2023-01-16 17:48:25 +00:00
export default function Note(props: NoteProps) {
2023-03-28 14:34:01 +00:00
const { data: ev, related, highlight, options: opt, ignoreModeration = false } = props;
if (ev.kind === EventKind.FileHeader) {
return <NostrFileElement ev={ev} />;
}
const navigate = useNavigate();
2023-02-12 12:31:48 +00:00
const [showReactions, setShowReactions] = useState(false);
2023-03-28 14:34:01 +00:00
const deletions = useMemo(() => getReactions(related, ev.id, EventKind.Deletion), [related]);
const { isMuted } = useModeration();
2023-03-28 14:34:01 +00:00
const isOpMuted = isMuted(ev?.pubkey);
2023-01-31 11:52:55 +00:00
const { ref, inView, entry } = useInView({ triggerOnce: true });
const [extendable, setExtendable] = useState<boolean>(false);
const [showMore, setShowMore] = useState<boolean>(false);
2023-02-07 19:47:57 +00:00
const baseClassName = `note card ${props.className ? props.className : ""}`;
2023-04-14 11:33:19 +00:00
const login = useLogin();
const { pinned, bookmarked } = login;
const publisher = useEventPublisher();
2023-01-31 11:52:55 +00:00
const [translated, setTranslated] = useState<Translation>();
2023-02-08 21:10:26 +00:00
const { formatMessage } = useIntl();
2023-03-28 14:34:01 +00:00
const reactions = useMemo(() => getReactions(related, ev.id, EventKind.Reaction), [related, ev]);
2023-02-12 12:31:48 +00:00
const groupReactions = useMemo(() => {
const result = reactions?.reduce(
(acc, reaction) => {
const kind = normalizeReaction(reaction.content);
const rs = acc[kind] || [];
return { ...acc, [kind]: [...rs, reaction] };
},
{
[Reaction.Positive]: [] as TaggedRawEvent[],
[Reaction.Negative]: [] as TaggedRawEvent[],
}
);
return {
[Reaction.Positive]: dedupeByPubkey(result[Reaction.Positive]),
[Reaction.Negative]: dedupeByPubkey(result[Reaction.Negative]),
};
}, [reactions]);
const positive = groupReactions[Reaction.Positive];
const negative = groupReactions[Reaction.Negative];
2023-02-15 22:26:26 +00:00
const reposts = useMemo(
() =>
dedupeByPubkey([
2023-03-28 14:34:01 +00:00
...getReactions(related, ev.id, EventKind.TextNote).filter(e => e.tags.some(tagFilterOfTextRepost(e, ev.id))),
...getReactions(related, ev.id, EventKind.Repost),
2023-02-15 22:26:26 +00:00
]),
[related, ev]
);
2023-02-12 12:31:48 +00:00
const zaps = useMemo(() => {
2023-03-28 14:34:01 +00:00
const sortedZaps = getReactions(related, ev.id, EventKind.ZapReceipt)
.map(a => parseZap(a, ev))
.filter(z => z.valid);
2023-02-12 12:31:48 +00:00
sortedZaps.sort((a, b) => b.amount - a.amount);
return sortedZaps;
}, [related]);
const totalReactions = positive.length + negative.length + reposts.length + zaps.length;
2023-01-31 11:52:55 +00:00
const options = {
showHeader: true,
showTime: true,
showFooter: true,
canUnpin: false,
canUnbookmark: false,
...opt,
2023-01-31 11:52:55 +00:00
};
async function unpin(id: HexKey) {
2023-04-14 15:02:15 +00:00
if (options.canUnpin && publisher) {
if (window.confirm(formatMessage(messages.ConfirmUnpin))) {
2023-04-14 11:33:19 +00:00
const es = pinned.item.filter(e => e !== id);
2023-04-14 15:02:15 +00:00
const ev = await publisher.noteList(es, Lists.Pinned);
publisher.broadcast(ev);
setPinned(login, es, ev.created_at * 1000);
}
}
}
async function unbookmark(id: HexKey) {
2023-04-14 15:02:15 +00:00
if (options.canUnbookmark && publisher) {
if (window.confirm(formatMessage(messages.ConfirmUnbookmark))) {
2023-04-14 11:33:19 +00:00
const es = bookmarked.item.filter(e => e !== id);
2023-04-14 15:02:15 +00:00
const ev = await publisher.noteList(es, Lists.Bookmarked);
publisher.broadcast(ev);
setBookmarked(login, es, ev.created_at * 1000);
}
}
}
2023-04-08 21:29:38 +00:00
const transformBody = () => {
2023-03-28 14:34:01 +00:00
const body = ev?.content ?? "";
2023-01-31 11:52:55 +00:00
if (deletions?.length > 0) {
2023-02-08 21:10:26 +00:00
return (
<b className="error">
<FormattedMessage {...messages.Deleted} />
</b>
);
2022-12-18 14:51:47 +00:00
}
2023-04-06 22:12:51 +00:00
const contentWarning = ev.tags.find(a => a[0] === "content-warning");
2023-04-08 21:29:38 +00:00
if (contentWarning) {
2023-04-06 22:12:51 +00:00
return (
2023-04-08 21:29:38 +00:00
<Reveal
message={
2023-04-06 22:12:51 +00:00
<>
2023-04-08 21:29:38 +00:00
<FormattedMessage defaultMessage="This note has been marked as sensitive, click here to reveal" />
{contentWarning[1] && (
<>
<br />
<FormattedMessage
defaultMessage="Reason: {reason}"
values={{
reason: contentWarning[1],
}}
/>
</>
)}
2023-04-06 22:12:51 +00:00
</>
2023-04-08 21:29:38 +00:00
}>
<Text content={body} tags={ev.tags} creator={ev.pubkey} />
</Reveal>
2023-04-06 22:12:51 +00:00
);
}
2023-04-18 21:20:13 +00:00
return <Text content={body} tags={ev.tags} creator={ev.pubkey} depth={props.depth} />;
2023-04-08 21:29:38 +00:00
};
2023-01-31 11:52:55 +00:00
useLayoutEffect(() => {
if (entry && inView && extendable === false) {
2023-02-21 14:35:53 +00:00
const h = (entry?.target as HTMLDivElement)?.offsetHeight ?? 0;
2023-01-31 11:52:55 +00:00
if (h > 650) {
setExtendable(true);
}
2022-12-18 14:51:47 +00:00
}
2023-01-31 11:52:55 +00:00
}, [inView, entry, extendable]);
2022-12-18 14:51:47 +00:00
2023-03-25 22:55:34 +00:00
function goToEvent(
e: React.MouseEvent,
eTarget: TaggedRawEvent,
isTargetAllowed: boolean = e.target === e.currentTarget
) {
2023-04-18 13:16:08 +00:00
if (!isTargetAllowed || opt?.canClick === false) {
2023-04-18 12:45:26 +00:00
return;
}
2023-04-25 10:20:33 +00:00
e.stopPropagation();
2023-04-18 13:16:08 +00:00
if (props.onClick) {
props.onClick(eTarget);
2023-02-28 01:16:43 +00:00
return;
}
2023-03-07 14:59:38 +00:00
2023-03-25 22:55:34 +00:00
const link = eventLink(eTarget.id, eTarget.relays);
// detect cmd key and open in new tab
if (e.metaKey) {
2023-03-25 22:55:34 +00:00
window.open(link, "_blank");
} else {
2023-03-28 14:34:01 +00:00
navigate(link, {
state: ev,
});
}
2023-01-31 11:52:55 +00:00
}
2022-12-18 14:51:47 +00:00
2023-01-31 11:52:55 +00:00
function replyTag() {
2023-03-28 14:34:01 +00:00
const thread = EventExt.extractThread(ev);
if (thread === undefined) {
return undefined;
2023-01-17 14:00:59 +00:00
}
2023-01-31 11:52:55 +00:00
const maxMentions = 2;
2023-03-28 14:34:01 +00:00
const replyId = thread?.replyTo?.Event ?? thread?.root?.Event;
const replyRelayHints = thread?.replyTo?.Relay ?? thread.root?.Relay;
2023-02-07 19:47:57 +00:00
const mentions: { pk: string; name: string; link: ReactNode }[] = [];
2023-03-28 14:34:01 +00:00
for (const pk of thread?.pubKeys ?? []) {
2023-03-29 12:10:22 +00:00
const u = UserCache.getFromCache(pk);
2023-03-25 22:55:34 +00:00
const npub = hexToBech32(NostrPrefix.PublicKey, pk);
2023-01-31 11:52:55 +00:00
const shortNpub = npub.substring(0, 12);
2023-03-03 14:30:31 +00:00
mentions.push({
pk,
name: u?.name ?? shortNpub,
link: <Link to={profileLink(pk)}>{u?.name ? `@${u.name}` : shortNpub}</Link>,
});
2023-01-31 11:52:55 +00:00
}
2023-03-25 22:55:34 +00:00
mentions.sort(a => (a.name.startsWith(NostrPrefix.PublicKey) ? 1 : -1));
2023-02-07 19:47:57 +00:00
const othersLength = mentions.length - maxMentions;
const renderMention = (m: { link: React.ReactNode; pk: string; name: string }, idx: number) => {
2023-01-31 11:52:55 +00:00
return (
<React.Fragment key={m.pk}>
2023-01-31 11:52:55 +00:00
{idx > 0 && ", "}
{m.link}
</React.Fragment>
);
};
const pubMentions =
2023-02-09 12:26:54 +00:00
mentions.length > maxMentions ? mentions?.slice(0, maxMentions).map(renderMention) : mentions?.map(renderMention);
const others = mentions.length > maxMentions ? formatMessage(messages.Others, { n: othersLength }) : "";
2023-01-31 11:52:55 +00:00
return (
<div className="reply">
2023-02-06 21:42:47 +00:00
re:&nbsp;
2023-01-31 11:52:55 +00:00
{(mentions?.length ?? 0) > 0 ? (
<>
2023-02-12 20:23:25 +00:00
{pubMentions} {others}
2023-01-31 11:52:55 +00:00
</>
) : (
2023-03-25 22:55:34 +00:00
replyId && (
<Link to={eventLink(replyId, replyRelayHints)}>
{hexToBech32(NostrPrefix.Event, replyId)?.substring(0, 12)}
</Link>
)
2023-02-06 21:42:47 +00:00
)}
</div>
);
2023-01-31 11:52:55 +00:00
}
2023-04-05 15:10:14 +00:00
const canRenderAsTextNote = [EventKind.TextNote, EventKind.Polls];
if (!canRenderAsTextNote.includes(ev.kind)) {
2023-01-31 11:52:55 +00:00
return (
<>
2023-02-08 21:10:26 +00:00
<h4>
2023-03-28 14:34:01 +00:00
<FormattedMessage {...messages.UnknownEventKind} values={{ kind: ev.kind }} />
2023-02-08 21:10:26 +00:00
</h4>
2023-03-28 14:34:01 +00:00
<pre>{JSON.stringify(ev, undefined, " ")}</pre>
2023-01-31 11:52:55 +00:00
</>
);
}
function translation() {
if (translated && translated.confidence > 0.5) {
return (
<>
<p className="highlight">
2023-02-09 12:26:54 +00:00
<FormattedMessage {...messages.TranslatedFrom} values={{ lang: translated.fromLanguage }} />
</p>
{translated.text}
</>
);
2023-01-31 11:52:55 +00:00
} else if (translated) {
2023-02-08 21:10:26 +00:00
return (
<p className="highlight">
<FormattedMessage {...messages.TranslationFailed} />
</p>
);
2023-01-31 11:52:55 +00:00
}
}
2023-04-05 15:10:14 +00:00
function pollOptions() {
if (ev.kind !== EventKind.Polls) return;
return <Poll ev={ev} zaps={zaps} />;
}
2023-01-31 11:52:55 +00:00
function content() {
2023-03-28 14:34:01 +00:00
if (!inView) return undefined;
2023-01-31 11:52:55 +00:00
return (
<>
2023-02-08 21:10:26 +00:00
{options.showHeader && (
<div className="header flex">
2023-03-31 22:43:07 +00:00
<ProfileImage
pubkey={ev.pubkey}
subHeader={replyTag() ?? undefined}
2023-05-10 12:29:07 +00:00
link={opt?.canClick === undefined ? undefined : ""}
2023-03-31 22:43:07 +00:00
/>
{(options.showTime || options.showBookmarked) && (
<div className="info">
{options.showBookmarked && (
2023-03-28 14:34:01 +00:00
<div className={`saved ${options.canUnbookmark ? "pointer" : ""}`} onClick={() => unbookmark(ev.id)}>
2023-03-02 17:47:02 +00:00
<Icon name="bookmark" /> <FormattedMessage {...messages.Bookmarked} />
</div>
)}
2023-03-28 14:34:01 +00:00
{!options.showBookmarked && <NoteTime from={ev.created_at * 1000} />}
</div>
)}
{options.showPinned && (
2023-03-28 14:34:01 +00:00
<div className={`pinned ${options.canUnpin ? "pointer" : ""}`} onClick={() => unpin(ev.id)}>
2023-03-02 17:47:02 +00:00
<Icon name="pin" /> <FormattedMessage {...messages.Pinned} />
</div>
2023-02-08 21:10:26 +00:00
)}
</div>
2023-02-08 21:10:26 +00:00
)}
2023-03-28 14:34:01 +00:00
<div className="body" onClick={e => goToEvent(e, ev, true)}>
{transformBody()}
{translation()}
2023-04-05 15:10:14 +00:00
{pollOptions()}
2023-02-12 12:31:48 +00:00
{options.showReactionsLink && (
<div className="reactions-link" onClick={() => setShowReactions(true)}>
<FormattedMessage {...messages.ReactionsLink} values={{ n: totalReactions }} />
</div>
)}
</div>
{extendable && !showMore && (
2023-02-09 12:26:54 +00:00
<span className="expand-note mt10 flex f-center" onClick={() => setShowMore(true)}>
2023-02-08 21:10:26 +00:00
<FormattedMessage {...messages.ShowMore} />
</span>
)}
2023-02-12 12:31:48 +00:00
{options.showFooter && (
<NoteFooter
ev={ev}
positive={positive}
negative={negative}
reposts={reposts}
zaps={zaps}
onTranslated={t => setTranslated(t)}
showReactions={showReactions}
setShowReactions={setShowReactions}
/>
)}
</>
);
2023-01-31 11:52:55 +00:00
}
const note = (
<div
2023-02-09 12:26:54 +00:00
className={`${baseClassName}${highlight ? " active " : " "}${extendable && !showMore ? " note-expand" : ""}`}
2023-03-28 14:34:01 +00:00
onClick={e => goToEvent(e, ev)}
2023-02-09 12:26:54 +00:00
ref={ref}>
2023-01-31 11:52:55 +00:00
{content()}
</div>
);
2023-02-09 12:26:54 +00:00
return !ignoreModeration && isOpMuted ? <HiddenNote>{note}</HiddenNote> : note;
2023-01-14 01:39:20 +00:00
}