snort/src/element/Thread.js

28 lines
993 B
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) {
/** @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) {
return notes?.filter(a => a.Kind === EventKind.Reaction && a.GetThread()?.Root?.Event === id);
}
const repliesToRoot = notes?.
2022-12-28 17:05:20 +00:00
filter(a => a.GetThread()?.Root?.Event === root?.Id && a.Kind === EventKind.TextNote)
2022-12-20 23:14:13 +00:00
.sort((a, b) => b.CreatedAt - a.CreatedAt);
2022-12-20 12:08:41 +00:00
return (
<>
2022-12-28 17:05:20 +00:00
{root === undefined ?
<NoteGhost />
: <Note data={root?.ToObject()} reactions={reactions(root?.Id)} />}
{repliesToRoot?.map(a => <Note key={a.Id} data={a.ToObject()} reactions={reactions(a.Id)} />)}
2022-12-20 12:08:41 +00:00
</>
);
}