2022-12-18 14:51:47 +00:00
|
|
|
import "./Note.css";
|
2023-01-17 17:13:22 +00:00
|
|
|
import { useCallback, useMemo } from "react";
|
2023-01-07 20:54:12 +00:00
|
|
|
import { useNavigate } from "react-router-dom";
|
2022-12-29 15:36:40 +00:00
|
|
|
|
2023-01-20 11:11:50 +00:00
|
|
|
import { default as NEvent } from "Nostr/Event";
|
|
|
|
import ProfileImage from "Element/ProfileImage";
|
|
|
|
import Text from "Element/Text";
|
|
|
|
import { eventLink, getReactions, hexToBech32 } from "Util";
|
|
|
|
import NoteFooter from "Element/NoteFooter";
|
|
|
|
import NoteTime from "Element/NoteTime";
|
|
|
|
import EventKind from "Nostr/EventKind";
|
|
|
|
import useProfile from "Feed/ProfileFeed";
|
|
|
|
import { TaggedRawEvent, u256 } from "Nostr";
|
2023-01-17 14:00:59 +00:00
|
|
|
import { useInView } from "react-intersection-observer";
|
2022-12-18 14:51:47 +00:00
|
|
|
|
2023-01-16 17:48:25 +00:00
|
|
|
export interface NoteProps {
|
|
|
|
data?: TaggedRawEvent,
|
|
|
|
isThread?: boolean,
|
2023-01-17 13:03:15 +00:00
|
|
|
related: TaggedRawEvent[],
|
2023-01-16 17:48:25 +00:00
|
|
|
highlight?: boolean,
|
|
|
|
options?: {
|
|
|
|
showHeader?: boolean,
|
|
|
|
showTime?: boolean,
|
|
|
|
showFooter?: boolean
|
|
|
|
},
|
|
|
|
["data-ev"]?: NEvent
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function Note(props: NoteProps) {
|
2022-12-18 14:51:47 +00:00
|
|
|
const navigate = useNavigate();
|
2023-01-17 13:03:15 +00:00
|
|
|
const { data, isThread, related, highlight, options: opt, ["data-ev"]: parsedEvent } = props
|
2023-01-16 17:48:25 +00:00
|
|
|
const ev = useMemo(() => parsedEvent ?? new NEvent(data), [data]);
|
2023-01-16 13:17:29 +00:00
|
|
|
const pubKeys = useMemo(() => ev.Thread?.PubKeys || [], [ev]);
|
|
|
|
const users = useProfile(pubKeys);
|
2023-01-17 13:03:15 +00:00
|
|
|
const deletions = useMemo(() => getReactions(related, ev.Id, EventKind.Deletion), [related]);
|
2023-01-18 23:39:50 +00:00
|
|
|
const { ref, inView } = useInView({ triggerOnce: true });
|
2022-12-18 14:51:47 +00:00
|
|
|
|
2023-01-02 11:15:13 +00:00
|
|
|
const options = {
|
|
|
|
showHeader: true,
|
|
|
|
showTime: true,
|
|
|
|
showFooter: true,
|
|
|
|
...opt
|
|
|
|
};
|
|
|
|
|
2023-01-02 13:40:42 +00:00
|
|
|
const transformBody = useCallback(() => {
|
|
|
|
let body = ev?.Content ?? "";
|
2023-01-17 13:03:15 +00:00
|
|
|
if (deletions?.length > 0) {
|
2023-01-12 09:48:39 +00:00
|
|
|
return (<b className="error">Deleted</b>);
|
|
|
|
}
|
2023-01-16 17:48:25 +00:00
|
|
|
return <Text content={body} tags={ev.Tags} users={users || new Map()} />;
|
2023-01-17 14:00:59 +00:00
|
|
|
}, [ev]);
|
|
|
|
|
2023-01-16 17:48:25 +00:00
|
|
|
function goToEvent(e: any, id: u256) {
|
2022-12-20 12:08:41 +00:00
|
|
|
if (!window.location.pathname.startsWith("/e/")) {
|
|
|
|
e.stopPropagation();
|
2023-01-06 14:36:13 +00:00
|
|
|
navigate(eventLink(id));
|
2022-12-20 12:08:41 +00:00
|
|
|
}
|
2022-12-18 14:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function replyTag() {
|
2023-01-06 19:29:12 +00:00
|
|
|
if (ev.Thread === null) {
|
2022-12-18 14:51:47 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-01-09 16:18:34 +00:00
|
|
|
const maxMentions = 2;
|
2023-01-09 16:48:03 +00:00
|
|
|
let replyId = ev.Thread?.ReplyTo?.Event ?? ev.Thread?.Root?.Event;
|
2023-01-16 17:48:25 +00:00
|
|
|
let mentions: string[] = [];
|
|
|
|
for (let pk of ev.Thread?.PubKeys) {
|
|
|
|
let u = users?.get(pk);
|
|
|
|
if (u) {
|
|
|
|
mentions.push(u.name ?? hexToBech32("npub", pk).substring(0, 12));
|
|
|
|
} else {
|
|
|
|
mentions.push(hexToBech32("npub", pk).substring(0, 12));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mentions.sort((a, b) => a.startsWith("npub") ? 1 : -1);
|
2023-01-15 23:45:23 +00:00
|
|
|
let othersLength = mentions.length - maxMentions
|
|
|
|
let pubMentions = mentions.length > maxMentions ? `${mentions?.slice(0, maxMentions).join(", ")} & ${othersLength} other${othersLength > 1 ? 's' : ''}` : mentions?.join(", ");
|
2022-12-18 14:51:47 +00:00
|
|
|
return (
|
2023-01-09 16:18:34 +00:00
|
|
|
<div className="reply">
|
2023-01-18 23:31:34 +00:00
|
|
|
{(pubMentions?.length ?? 0) > 0 ? pubMentions : replyId ? hexToBech32("note", replyId)?.substring(0, 12) : ""}
|
2022-12-18 14:51:47 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-01-15 19:40:47 +00:00
|
|
|
if (ev.Kind !== EventKind.TextNote) {
|
2022-12-18 22:23:52 +00:00
|
|
|
return (
|
|
|
|
<>
|
2023-01-08 18:35:36 +00:00
|
|
|
<h4>Unknown event kind: {ev.Kind}</h4>
|
|
|
|
<pre>
|
|
|
|
{JSON.stringify(ev.ToObject(), undefined, ' ')}
|
|
|
|
</pre>
|
2022-12-18 22:23:52 +00:00
|
|
|
</>
|
|
|
|
);
|
2022-12-18 14:51:47 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 14:00:59 +00:00
|
|
|
function content() {
|
2023-01-17 17:13:22 +00:00
|
|
|
if (!inView) return null;
|
2023-01-17 14:00:59 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{options.showHeader ?
|
|
|
|
<div className="header flex">
|
|
|
|
<ProfileImage pubkey={ev.RootPubKey} subHeader={replyTag() ?? undefined} />
|
|
|
|
{options.showTime ?
|
|
|
|
<div className="info">
|
|
|
|
<NoteTime from={ev.CreatedAt * 1000} />
|
|
|
|
</div> : null}
|
|
|
|
</div> : null}
|
|
|
|
<div className="body" onClick={(e) => goToEvent(e, ev.Id)}>
|
|
|
|
{transformBody()}
|
|
|
|
</div>
|
|
|
|
{options.showFooter ? <NoteFooter ev={ev} related={related} /> : null}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-12-18 14:51:47 +00:00
|
|
|
return (
|
2023-01-17 14:00:59 +00:00
|
|
|
<div className={`note${highlight ? " active" : ""}${isThread ? " thread" : ""}`} ref={ref}>
|
|
|
|
{content()}
|
2022-12-18 14:51:47 +00:00
|
|
|
</div>
|
|
|
|
)
|
2023-01-14 01:39:20 +00:00
|
|
|
}
|