snort/src/element/Thread.js

28 lines
938 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";
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);
if(root === undefined) {
return 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?.
filter(a => a.GetThread()?.Root?.Event === root.Id && a.Kind === EventKind.TextNote)
.sort((a, b) => b.CreatedAt - a.CreatedAt);
2022-12-20 12:08:41 +00:00
return (
<>
2022-12-20 23:14:13 +00:00
<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
</>
);
}