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);
|
|
|
|
|
2023-01-04 13:23:05 +00:00
|
|
|
function reactions(id, kind = EventKind.Reaction) {
|
|
|
|
return notes?.filter(a => a.Kind === kind && a.Tags.find(a => a.Key === "e" && a.Event === id));
|
2022-12-20 23:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const repliesToRoot = notes?.
|
2022-12-28 23:28:28 +00:00
|
|
|
filter(a => a.GetThread()?.Root !== null && a.Kind === EventKind.TextNote && a.Id !== thisEvent && a.Id !== root?.Id)
|
2022-12-28 22:09:39 +00:00
|
|
|
.sort((a, b) => a.CreatedAt - b.CreatedAt);
|
|
|
|
const thisNote = notes?.find(a => a.Id === thisEvent);
|
2022-12-28 23:28:28 +00:00
|
|
|
const thisIsRootNote = thisNote?.Id === root?.Id;
|
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)} />}
|
2023-01-04 13:23:05 +00:00
|
|
|
{thisNote && !thisIsRootNote ? <Note data-ev={thisNote} reactions={reactions(thisNote.Id)} deletion={reactions(thisNote.Id, EventKind.Deletion)}/> : null}
|
2022-12-28 22:09:39 +00:00
|
|
|
<h4>Other Replies</h4>
|
2023-01-04 13:23:05 +00:00
|
|
|
{repliesToRoot?.map(a => <Note key={a.Id} data-ev={a} reactions={reactions(a.Id)} deletion={reactions(a.Id, EventKind.Deletion)}/>)}
|
2022-12-20 12:08:41 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|