snort/src/Element/Note.tsx

262 lines
6.6 KiB
TypeScript
Raw Normal View History

2022-12-18 14:51:47 +00:00
import "./Note.css";
import {
useCallback,
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";
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";
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-02-06 21:42:47 +00:00
import ShowMore from "Element/ShowMore";
2023-01-20 11:11:50 +00:00
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-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;
className?: string;
related: TaggedRawEvent[];
highlight?: boolean;
ignoreModeration?: boolean;
2023-01-31 11:52:55 +00:00
options?: {
showHeader?: boolean;
showTime?: boolean;
showFooter?: boolean;
};
["data-ev"]?: NEvent;
2023-01-16 17:48:25 +00:00
}
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 author has been muted</p>
<button onClick={() => setShow(true)}>Show</button>
</div>
</div>
);
};
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();
const {
data,
className,
related,
highlight,
options: opt,
["data-ev"]: parsedEvent,
ignoreModeration = false,
} = props;
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);
const deletions = useMemo(
() => getReactions(related, ev.Id, EventKind.Deletion),
[related]
);
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);
const baseClassname = `note card ${props.className ? props.className : ""}`;
2023-01-31 11:52:55 +00:00
const [translated, setTranslated] = useState<Translation>();
2023-02-06 21:42:47 +00:00
const replyId = ev.Thread?.ReplyTo?.Event ?? ev.Thread?.Root?.Event;
2023-01-31 11:52:55 +00:00
const options = {
showHeader: true,
showTime: true,
showFooter: true,
...opt,
2023-01-31 11:52:55 +00:00
};
const transformBody = useCallback(() => {
let body = ev?.Content ?? "";
if (deletions?.length > 0) {
return <b className="error">Deleted</b>;
2022-12-18 14:51:47 +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) {
let h = entry?.target.clientHeight ?? 0;
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-01-31 11:52:55 +00:00
function goToEvent(e: any, 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;
let replyId = ev.Thread?.ReplyTo?.Event ?? ev.Thread?.Root?.Event;
let mentions: { pk: string; name: string; link: ReactNode }[] = [];
2023-01-31 11:52:55 +00:00
for (let pk of ev.Thread?.PubKeys) {
const u = users?.get(pk);
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,
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,
link: <Link to={`/p/${npub}`}>{shortNpub}</Link>,
2023-01-31 11:52:55 +00:00
});
}
}
mentions.sort((a, b) => (a.name.startsWith("npub") ? 1 : -1));
let othersLength = mentions.length - maxMentions;
2023-01-31 11:52:55 +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" : ""}`
: "";
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 ? (
<>
{pubMentions}
{others}
</>
) : (
replyId && (
<Link to={eventLink(replyId)}>
{hexToBech32("note", replyId)?.substring(0, 12)}
</Link>
)
2023-02-06 21:42:47 +00:00
)}
</div>
);
2023-01-31 11:52:55 +00:00
}
if (ev.Kind !== EventKind.TextNote) {
return (
<>
<h4>Unknown event kind: {ev.Kind}</h4>
<pre>{JSON.stringify(ev.ToObject(), undefined, " ")}</pre>
2023-01-31 11:52:55 +00:00
</>
);
}
function translation() {
if (translated && translated.confidence > 0.5) {
return (
<>
<p className="highlight">
Translated from {translated.fromLanguage}:
</p>
{translated.text}
</>
);
2023-01-31 11:52:55 +00:00
} else if (translated) {
return <p className="highlight">Translation failed</p>;
2023-01-31 11:52:55 +00:00
}
}
function content() {
if (!inView) return null;
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()}
{translation()}
</div>
{extendable && !showMore && (
<span
className="expand-note mt10 flex f-center"
onClick={() => setShowMore(true)}
>
Show more
</span>
)}
{options.showFooter && (
<NoteFooter
ev={ev}
related={related}
onTranslated={(t) => setTranslated(t)}
/>
)}
</>
);
2023-01-31 11:52:55 +00:00
}
const note = (
<div
className={`${baseClassname}${highlight ? " active " : " "}${
extendable && !showMore ? " note-expand" : ""
}`}
ref={ref}
>
2023-01-31 11:52:55 +00:00
{content()}
</div>
);
return !ignoreModeration && isOpMuted ? (
<HiddenNote>{note}</HiddenNote>
) : (
note
);
2023-01-14 01:39:20 +00:00
}