2022-12-18 14:51:47 +00:00
|
|
|
import "./Note.css";
|
2023-02-12 22:06:21 +00:00
|
|
|
import React, { useCallback, useMemo, useState, useLayoutEffect, ReactNode } from "react";
|
2023-01-25 18:08:53 +00:00
|
|
|
import { useNavigate, Link } from "react-router-dom";
|
2023-02-13 15:49:19 +00:00
|
|
|
import { useSelector, useDispatch } from "react-redux";
|
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";
|
2022-12-29 15:36:40 +00:00
|
|
|
|
2023-02-13 15:49:19 +00:00
|
|
|
import useEventPublisher from "Feed/EventPublisher";
|
|
|
|
import Bookmark from "Icons/Bookmark";
|
|
|
|
import Pin from "Icons/Pin";
|
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-12 12:31:48 +00:00
|
|
|
import { eventLink, getReactions, dedupeByPubkey, hexToBech32, normalizeReaction, Reaction } 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-01-27 21:38:41 +00:00
|
|
|
import { useUserProfiles } from "Feed/ProfileFeed";
|
2023-02-12 12:31:48 +00:00
|
|
|
import { TaggedRawEvent, u256, HexKey, Event as NEvent, EventKind } from "@snort/nostr";
|
2023-01-26 11:34:18 +00:00
|
|
|
import useModeration from "Hooks/useModeration";
|
2023-02-13 15:49:19 +00:00
|
|
|
import { setPinned, setBookmarked } from "State/Login";
|
|
|
|
import type { RootState } from "State/Store";
|
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-02-07 20:04:50 +00:00
|
|
|
data?: TaggedRawEvent;
|
|
|
|
className?: string;
|
|
|
|
related: TaggedRawEvent[];
|
|
|
|
highlight?: boolean;
|
|
|
|
ignoreModeration?: boolean;
|
2023-01-31 11:52:55 +00:00
|
|
|
options?: {
|
2023-02-07 20:04:50 +00:00
|
|
|
showHeader?: boolean;
|
|
|
|
showTime?: boolean;
|
2023-02-13 15:49:19 +00:00
|
|
|
showPinned?: boolean;
|
|
|
|
showBookmarked?: boolean;
|
2023-02-07 20:04:50 +00:00
|
|
|
showFooter?: boolean;
|
2023-02-12 12:31:48 +00:00
|
|
|
showReactionsLink?: boolean;
|
2023-02-13 15:49:19 +00:00
|
|
|
canUnpin?: boolean;
|
|
|
|
canUnbookmark?: boolean;
|
2023-02-07 20:04:50 +00:00
|
|
|
};
|
|
|
|
["data-ev"]?: NEvent;
|
2023-01-16 17:48:25 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 19:47:57 +00:00
|
|
|
const HiddenNote = ({ children }: { children: React.ReactNode }) => {
|
2023-02-07 20:04:50 +00:00
|
|
|
const [show, setShow] = useState(false);
|
|
|
|
return show ? (
|
2023-02-07 19:47:57 +00:00
|
|
|
<>{children}</>
|
2023-02-07 20:04:50 +00:00
|
|
|
) : (
|
2023-01-28 18:51:08 +00:00
|
|
|
<div className="card note hidden-note">
|
2023-01-27 21:10:14 +00:00
|
|
|
<div className="header">
|
2023-02-08 21:10:26 +00:00
|
|
|
<p>
|
|
|
|
<FormattedMessage {...messages.MutedAuthor} />
|
|
|
|
</p>
|
|
|
|
<button onClick={() => setShow(true)}>
|
|
|
|
<FormattedMessage {...messages.Show} />
|
|
|
|
</button>
|
2023-01-27 21:10:14 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-02-07 20:04:50 +00:00
|
|
|
);
|
|
|
|
};
|
2023-01-27 21:10:14 +00:00
|
|
|
|
2023-01-16 17:48:25 +00:00
|
|
|
export default function Note(props: NoteProps) {
|
2023-01-31 11:52:55 +00:00
|
|
|
const navigate = useNavigate();
|
2023-02-13 15:49:19 +00:00
|
|
|
const dispatch = useDispatch();
|
2023-02-09 12:26:54 +00:00
|
|
|
const { data, related, highlight, options: opt, ["data-ev"]: parsedEvent, ignoreModeration = false } = props;
|
2023-02-12 12:31:48 +00:00
|
|
|
const [showReactions, setShowReactions] = useState(false);
|
2023-01-31 11:52:55 +00:00
|
|
|
const ev = useMemo(() => parsedEvent ?? new NEvent(data), [data]);
|
|
|
|
const pubKeys = useMemo(() => ev.Thread?.PubKeys || [], [ev]);
|
|
|
|
const users = useUserProfiles(pubKeys);
|
2023-02-09 12:26:54 +00:00
|
|
|
const deletions = useMemo(() => getReactions(related, ev.Id, EventKind.Deletion), [related]);
|
2023-02-07 20:04:50 +00:00
|
|
|
const { isMuted } = useModeration();
|
|
|
|
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-02-13 15:49:19 +00:00
|
|
|
const { pinned, bookmarked } = useSelector((s: RootState) => s.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-02-12 12:31:48 +00:00
|
|
|
const reactions = useMemo(() => getReactions(related, ev.Id, EventKind.Reaction), [related, ev]);
|
|
|
|
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];
|
|
|
|
const reposts = useMemo(() => dedupeByPubkey(getReactions(related, ev.Id, EventKind.Repost)), [related, ev]);
|
|
|
|
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 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,
|
2023-02-13 15:49:19 +00:00
|
|
|
canUnpin: false,
|
|
|
|
canUnbookmark: false,
|
2023-02-07 20:04:50 +00:00
|
|
|
...opt,
|
2023-01-31 11:52:55 +00:00
|
|
|
};
|
|
|
|
|
2023-02-13 15:49:19 +00:00
|
|
|
async function unpin(id: HexKey) {
|
|
|
|
if (options.canUnpin) {
|
|
|
|
if (window.confirm(formatMessage(messages.ConfirmUnpin))) {
|
|
|
|
const es = pinned.filter(e => e !== id);
|
|
|
|
const ev = await publisher.pinned(es);
|
|
|
|
publisher.broadcast(ev);
|
|
|
|
dispatch(setPinned({ keys: es, createdAt: new Date().getTime() }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function unbookmark(id: HexKey) {
|
|
|
|
if (options.canUnbookmark) {
|
|
|
|
if (window.confirm(formatMessage(messages.ConfirmUnbookmark))) {
|
|
|
|
const es = bookmarked.filter(e => e !== id);
|
|
|
|
const ev = await publisher.bookmarked(es);
|
|
|
|
publisher.broadcast(ev);
|
|
|
|
dispatch(setBookmarked({ keys: es, createdAt: new Date().getTime() }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-31 11:52:55 +00:00
|
|
|
const transformBody = useCallback(() => {
|
2023-02-07 19:47:57 +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-02-09 12:26:54 +00:00
|
|
|
return <Text content={body} tags={ev.Tags} users={users || new Map()} creator={ev.PubKey} />;
|
2023-01-31 11:52:55 +00:00
|
|
|
}, [ev]);
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
if (entry && inView && extendable === false) {
|
2023-02-07 19:47:57 +00:00
|
|
|
const h = entry?.target.clientHeight ?? 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-02-07 19:47:57 +00:00
|
|
|
function goToEvent(e: React.MouseEvent, id: u256) {
|
2023-02-06 21:42:47 +00:00
|
|
|
e.stopPropagation();
|
|
|
|
navigate(eventLink(id));
|
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() {
|
|
|
|
if (ev.Thread === null) {
|
|
|
|
return null;
|
2023-01-17 14:00:59 +00:00
|
|
|
}
|
|
|
|
|
2023-01-31 11:52:55 +00:00
|
|
|
const maxMentions = 2;
|
2023-02-07 19:47:57 +00:00
|
|
|
const replyId = ev.Thread?.ReplyTo?.Event ?? ev.Thread?.Root?.Event;
|
|
|
|
const mentions: { pk: string; name: string; link: ReactNode }[] = [];
|
|
|
|
for (const pk of ev.Thread?.PubKeys ?? []) {
|
2023-01-31 11:52:55 +00:00
|
|
|
const u = users?.get(pk);
|
2023-02-07 20:04:50 +00:00
|
|
|
const npub = hexToBech32("npub", pk);
|
2023-01-31 11:52:55 +00:00
|
|
|
const shortNpub = npub.substring(0, 12);
|
|
|
|
if (u) {
|
|
|
|
mentions.push({
|
|
|
|
pk,
|
|
|
|
name: u.name ?? shortNpub,
|
2023-02-09 12:26:54 +00:00
|
|
|
link: <Link to={`/p/${npub}`}>{u.name ? `@${u.name}` : shortNpub}</Link>,
|
2023-01-31 11:52:55 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
mentions.push({
|
|
|
|
pk,
|
|
|
|
name: shortNpub,
|
2023-02-07 20:04:50 +00:00
|
|
|
link: <Link to={`/p/${npub}`}>{shortNpub}</Link>,
|
2023-01-31 11:52:55 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-02-09 12:26:54 +00:00
|
|
|
mentions.sort(a => (a.name.startsWith("npub") ? 1 : -1));
|
2023-02-07 19:47:57 +00:00
|
|
|
const othersLength = mentions.length - maxMentions;
|
2023-02-12 22:06:21 +00:00
|
|
|
const renderMention = (m: { link: React.ReactNode; pk: string; name: string }, idx: number) => {
|
2023-01-31 11:52:55 +00:00
|
|
|
return (
|
2023-02-12 22:06:21 +00:00
|
|
|
<React.Fragment key={m.pk}>
|
2023-01-31 11:52:55 +00:00
|
|
|
{idx > 0 && ", "}
|
|
|
|
{m.link}
|
2023-02-12 22:06:21 +00:00
|
|
|
</React.Fragment>
|
2023-02-07 20:04:50 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
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:
|
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-02-07 20:04:50 +00:00
|
|
|
) : (
|
2023-02-09 12:26:54 +00:00
|
|
|
replyId && <Link to={eventLink(replyId)}>{hexToBech32("note", replyId)?.substring(0, 12)}</Link>
|
2023-02-06 21:42:47 +00:00
|
|
|
)}
|
2023-01-27 21:10:14 +00:00
|
|
|
</div>
|
2023-02-07 20:04:50 +00:00
|
|
|
);
|
2023-01-31 11:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ev.Kind !== EventKind.TextNote) {
|
|
|
|
return (
|
|
|
|
<>
|
2023-02-08 21:10:26 +00:00
|
|
|
<h4>
|
2023-02-09 12:26:54 +00:00
|
|
|
<FormattedMessage {...messages.UnknownEventKind} values={{ kind: ev.Kind }} />
|
2023-02-08 21:10:26 +00:00
|
|
|
</h4>
|
2023-02-07 20:04:50 +00:00
|
|
|
<pre>{JSON.stringify(ev.ToObject(), undefined, " ")}</pre>
|
2023-01-31 11:52:55 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function translation() {
|
|
|
|
if (translated && translated.confidence > 0.5) {
|
2023-02-07 20:04:50 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<p className="highlight">
|
2023-02-09 12:26:54 +00:00
|
|
|
<FormattedMessage {...messages.TranslatedFrom} values={{ lang: translated.fromLanguage }} />
|
2023-02-07 20:04:50 +00:00
|
|
|
</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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function content() {
|
|
|
|
if (!inView) return null;
|
|
|
|
return (
|
2023-02-07 20:04:50 +00:00
|
|
|
<>
|
2023-02-08 21:10:26 +00:00
|
|
|
{options.showHeader && (
|
2023-02-07 20:04:50 +00:00
|
|
|
<div className="header flex">
|
2023-02-09 12:26:54 +00:00
|
|
|
<ProfileImage pubkey={ev.RootPubKey} subHeader={replyTag() ?? undefined} />
|
2023-02-13 15:49:19 +00:00
|
|
|
{(options.showTime || options.showBookmarked) && (
|
2023-02-07 20:04:50 +00:00
|
|
|
<div className="info">
|
2023-02-13 15:49:19 +00:00
|
|
|
{options.showBookmarked && (
|
|
|
|
<div className={`saved ${options.canUnbookmark ? "pointer" : ""}`} onClick={() => unbookmark(ev.Id)}>
|
|
|
|
<Bookmark /> <FormattedMessage {...messages.Bookmarked} />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{!options.showBookmarked && <NoteTime from={ev.CreatedAt * 1000} />}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{options.showPinned && (
|
|
|
|
<div className={`pinned ${options.canUnpin ? "pointer" : ""}`} onClick={() => unpin(ev.Id)}>
|
|
|
|
<Pin /> <FormattedMessage {...messages.Pinned} />
|
2023-02-07 20:04:50 +00:00
|
|
|
</div>
|
2023-02-08 21:10:26 +00:00
|
|
|
)}
|
2023-02-07 20:04:50 +00:00
|
|
|
</div>
|
2023-02-08 21:10:26 +00:00
|
|
|
)}
|
2023-02-09 12:26:54 +00:00
|
|
|
<div className="body" onClick={e => goToEvent(e, ev.Id)}>
|
2023-02-07 20:04:50 +00:00
|
|
|
{transformBody()}
|
|
|
|
{translation()}
|
2023-02-12 12:31:48 +00:00
|
|
|
{options.showReactionsLink && (
|
|
|
|
<div className="reactions-link" onClick={() => setShowReactions(true)}>
|
|
|
|
<FormattedMessage {...messages.ReactionsLink} values={{ n: totalReactions }} />
|
|
|
|
</div>
|
|
|
|
)}
|
2023-02-07 20:04:50 +00:00
|
|
|
</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} />
|
2023-02-07 20:04:50 +00:00
|
|
|
</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-02-07 20:04:50 +00:00
|
|
|
</>
|
|
|
|
);
|
2023-01-31 11:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const note = (
|
2023-02-07 20:04:50 +00:00
|
|
|
<div
|
2023-02-09 12:26:54 +00:00
|
|
|
className={`${baseClassName}${highlight ? " active " : " "}${extendable && !showMore ? " note-expand" : ""}`}
|
|
|
|
ref={ref}>
|
2023-01-31 11:52:55 +00:00
|
|
|
{content()}
|
|
|
|
</div>
|
2023-02-07 20:04:50 +00:00
|
|
|
);
|
2023-01-27 21:10:14 +00:00
|
|
|
|
2023-02-09 12:26:54 +00:00
|
|
|
return !ignoreModeration && isOpMuted ? <HiddenNote>{note}</HiddenNote> : note;
|
2023-01-14 01:39:20 +00:00
|
|
|
}
|