snort/src/Element/Note.tsx

197 lines
6.5 KiB
TypeScript
Raw Normal View History

2022-12-18 14:51:47 +00:00
import "./Note.css";
2023-01-28 21:43:56 +00:00
import { useCallback, useMemo, useState, useLayoutEffect, ReactNode } from "react";
2023-01-25 18:08:53 +00:00
import { useNavigate, Link } 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";
2023-01-28 21:43:56 +00:00
2023-01-20 11:11:50 +00:00
import { eventLink, getReactions, hexToBech32 } from "Util";
import NoteFooter from "Element/NoteFooter";
import NoteTime from "Element/NoteTime";
import EventKind from "Nostr/EventKind";
import { useUserProfiles } from "Feed/ProfileFeed";
2023-01-20 11:11:50 +00:00
import { TaggedRawEvent, u256 } from "Nostr";
2023-01-17 14:00:59 +00:00
import { useInView } from "react-intersection-observer";
2023-01-26 11:34:18 +00:00
import useModeration from "Hooks/useModeration";
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,
2023-01-28 21:09:02 +00:00
ignoreModeration?: boolean,
2023-01-16 17:48:25 +00:00
options?: {
showHeader?: boolean,
showTime?: boolean,
showFooter?: boolean
},
["data-ev"]?: NEvent
}
const HiddenNote = ({ children }: any) => {
const [show, setShow] = useState(false)
return show ? children : (
2023-01-28 18:51:08 +00:00
<div className="card note hidden-note">
<div className="header">
<p>
This note was hidden because of your moderation settings
</p>
<button onClick={() => setShow(true)}>
Show
</button>
</div>
</div>
)
}
2023-01-16 17:48:25 +00:00
export default function Note(props: NoteProps) {
2022-12-18 14:51:47 +00:00
const navigate = useNavigate();
2023-01-28 21:09:02 +00:00
const { data, isThread, related, highlight, options: opt, ["data-ev"]: parsedEvent, ignoreModeration = false} = props
2023-01-16 17:48:25 +00:00
const ev = useMemo(() => parsedEvent ?? new NEvent(data), [data]);
const pubKeys = useMemo(() => ev.Thread?.PubKeys || [], [ev]);
const users = useUserProfiles(pubKeys);
2023-01-17 13:03:15 +00:00
const deletions = useMemo(() => getReactions(related, ev.Id, EventKind.Deletion), [related]);
2023-01-26 11:34:18 +00:00
const { isMuted } = useModeration()
const isOpMuted = isMuted(ev.PubKey)
2023-01-28 20:38:53 +00:00
const { ref, inView, entry } = useInView({ triggerOnce: true });
const [extendable, setExtendable] = useState<boolean>(false);
const [showMore, setShowMore] = useState<boolean>(false);
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-28 20:38:53 +00:00
useLayoutEffect(() => {
if (entry && inView && extendable === false) {
let h = entry?.target.clientHeight ?? 0;
if (h > 650) {
setExtendable(true);
}
}
}, [inView, entry, extendable]);
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-25 18:08:53 +00:00
let mentions: {pk: string, name: string, link: ReactNode}[] = [];
2023-01-16 17:48:25 +00:00
for (let pk of ev.Thread?.PubKeys) {
2023-01-25 18:08:53 +00:00
const u = users?.get(pk);
const npub = hexToBech32("npub", pk)
const shortNpub = npub.substring(0, 12);
2023-01-16 17:48:25 +00:00
if (u) {
2023-01-25 18:08:53 +00:00
mentions.push({
pk,
name: u.name ?? shortNpub,
link: (
<Link to={`/p/${npub}`}>
{u.name ? `@${u.name}` : shortNpub}
</Link>
)
});
2023-01-16 17:48:25 +00:00
} else {
2023-01-25 18:08:53 +00:00
mentions.push({
pk,
name: shortNpub,
link: (
<Link to={`/p/${npub}`}>
{shortNpub}
</Link>
)
});
2023-01-16 17:48:25 +00:00
}
}
2023-01-25 18:08:53 +00:00
mentions.sort((a, b) => a.name.startsWith("npub") ? 1 : -1);
2023-01-15 23:45:23 +00:00
let othersLength = mentions.length - maxMentions
2023-01-25 18:08:53 +00:00
const renderMention = (m: any, idx: number) => {
return (
<>
{idx > 0 && ", "}
{m.link}
</>
)
}
const pubMentions = mentions.length > maxMentions ? (
mentions?.slice(0, maxMentions).map(renderMention)
) : mentions?.map(renderMention);
const others = mentions.length > maxMentions ? ` & ${othersLength} other${othersLength > 1 ? 's' : ''}` : ''
2022-12-18 14:51:47 +00:00
return (
2023-01-09 16:18:34 +00:00
<div className="reply">
2023-01-28 21:43:56 +00:00
{(mentions?.length ?? 0) > 0 ? (
<>
{pubMentions}
{others}
</>
) : replyId ? (
hexToBech32("note", replyId)?.substring(0, 12) // todo: link
) : ""}
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>
2023-01-28 20:38:53 +00:00
{extendable && !showMore && (<div className="flex f-center">
2023-01-29 22:18:37 +00:00
<button className="show-more" onClick={() => setShowMore(true)}>Show more</button>
2023-01-28 20:38:53 +00:00
</div>)}
2023-01-17 14:00:59 +00:00
{options.showFooter ? <NoteFooter ev={ev} related={related} /> : null}
</>
)
}
const note = (
2023-01-28 21:43:56 +00:00
<div className={`note card${highlight ? " active" : ""}${isThread ? " thread" : ""}${extendable && !showMore ? " note-expand" : ""}`} ref={ref}>
{content()}
</div>
2022-12-18 14:51:47 +00:00
)
2023-01-28 21:09:02 +00:00
return !ignoreModeration && isOpMuted ? <HiddenNote>{note}</HiddenNote> : note
2023-01-14 01:39:20 +00:00
}