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

82 lines
2.4 KiB
TypeScript
Raw Normal View History

2023-01-02 11:15:13 +00:00
import "./NoteReaction.css";
2023-01-08 18:35:36 +00:00
import { Link } from "react-router-dom";
import { useMemo } from "react";
2023-03-28 14:34:01 +00:00
import { EventKind, RawEvent, TaggedRawEvent, NostrPrefix } from "@snort/nostr";
2023-01-20 11:11:50 +00:00
import Note from "Element/Note";
import ProfileImage from "Element/ProfileImage";
import { eventLink, hexToBech32 } from "Util";
import NoteTime from "Element/NoteTime";
2023-01-26 11:34:18 +00:00
import useModeration from "Hooks/useModeration";
2023-03-28 14:34:01 +00:00
import { EventExt } from "System/EventExt";
2023-01-02 11:15:13 +00:00
2023-01-16 17:48:25 +00:00
export interface NoteReactionProps {
2023-03-28 14:34:01 +00:00
data: TaggedRawEvent;
root?: TaggedRawEvent;
2023-01-16 17:48:25 +00:00
}
export default function NoteReaction(props: NoteReactionProps) {
2023-03-28 14:34:01 +00:00
const { data: ev } = props;
const { isMuted } = useModeration();
2023-01-02 11:15:13 +00:00
const refEvent = useMemo(() => {
if (ev) {
2023-03-28 14:34:01 +00:00
const eTags = ev.tags.filter(a => a[0] === "e");
if (eTags.length > 0) {
2023-03-25 22:55:34 +00:00
return eTags[0];
}
2023-01-02 11:15:13 +00:00
}
return null;
}, [ev]);
2023-01-02 11:15:13 +00:00
2023-02-15 22:26:26 +00:00
if (
2023-03-28 14:34:01 +00:00
ev.kind !== EventKind.Reaction &&
ev.kind !== EventKind.Repost &&
(ev.kind !== EventKind.TextNote ||
ev.tags.every((a, i) => a[1] !== refEvent?.[1] || a[3] !== "mention" || ev.content !== `#[${i}]`))
2023-02-15 22:26:26 +00:00
) {
return null;
}
2023-01-02 11:15:13 +00:00
/**
* Some clients embed the reposted note in the content
*/
function extractRoot() {
2023-03-28 14:34:01 +00:00
if (ev?.kind === EventKind.Repost && ev.content.length > 0 && ev.content !== "#[0]") {
try {
2023-03-28 14:34:01 +00:00
const r: RawEvent = JSON.parse(ev.content);
return r as TaggedRawEvent;
} catch (e) {
console.error("Could not load reposted content", e);
}
}
return props.root;
}
const root = extractRoot();
const isOpMuted = root && isMuted(root.pubkey);
const shouldNotBeRendered = isOpMuted || root?.kind !== EventKind.TextNote;
const opt = {
2023-03-28 14:34:01 +00:00
showHeader: ev?.kind === EventKind.Repost || ev?.kind === EventKind.TextNote,
showFooter: false,
};
2023-01-02 11:15:13 +00:00
return shouldNotBeRendered ? null : (
<div className="reaction">
<div className="header flex">
2023-03-28 14:34:01 +00:00
<ProfileImage pubkey={EventExt.getRootPubKey(ev)} />
<div className="info">
2023-03-28 14:34:01 +00:00
<NoteTime from={ev.created_at * 1000} />
2023-01-02 11:15:13 +00:00
</div>
</div>
{root ? <Note data={root} options={opt} related={[]} /> : null}
{!root && refEvent ? (
<p>
2023-03-28 14:34:01 +00:00
<Link to={eventLink(refEvent[1] ?? "", refEvent[2])}>
#{hexToBech32(NostrPrefix.Event, refEvent[1]).substring(0, 12)}
2023-03-25 22:55:34 +00:00
</Link>
</p>
) : null}
</div>
);
2023-01-15 23:14:52 +00:00
}