snort/src/element/Thread.js

33 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-12-20 12:08:41 +00:00
import Event from "../nostr/Event";
2022-12-20 23:14:13 +00:00
import EventKind from "../nostr/EventKind";
2022-12-20 12:08:41 +00:00
import Note from "./Note";
2022-12-28 17:05:20 +00:00
import NoteGhost from "./NoteGhost";
2022-12-20 12:08:41 +00:00
export default function Thread(props) {
2022-12-28 22:09:39 +00:00
const thisEvent = props.this;
2022-12-20 12:08:41 +00:00
/** @type {Array<Event>} */
const notes = props.notes?.map(a => Event.FromObject(a));
// root note has no thread info
const root = notes.find(a => a.GetThread() === null);
2022-12-20 23:14:13 +00:00
function reactions(id) {
2022-12-28 22:09:39 +00:00
return notes?.filter(a => a.Kind === EventKind.Reaction && a.Tags.find(a => a.Key === "e").Event === id);
2022-12-20 23:14:13 +00:00
}
const repliesToRoot = notes?.
2022-12-28 22:09:39 +00:00
filter(a => a.GetThread()?.Root !== null && a.Kind === EventKind.TextNote && a.Id !== thisEvent)
.sort((a, b) => a.CreatedAt - b.CreatedAt);
const thisNote = notes?.find(a => a.Id === thisEvent);
2022-12-20 12:08:41 +00:00
return (
<>
2022-12-28 17:05:20 +00:00
{root === undefined ?
2022-12-28 22:09:39 +00:00
<NoteGhost text={`Loading... (${notes.length} events loaded)`}/>
: <Note data-ev={root} reactions={reactions(root?.Id)} />}
{thisNote ? <Note data-ev={thisNote} reactions={reactions(thisNote.Id)}/> : null}
<h4>Other Replies</h4>
{repliesToRoot?.map(a => <Note key={a.Id} data-ev={a} reactions={reactions(a.Id)} />)}
2022-12-20 12:08:41 +00:00
</>
);
}