fix: cleanup

This commit is contained in:
2023-10-16 21:33:01 +01:00
parent 5e42c5e70c
commit f34ccf72cb
6 changed files with 35 additions and 25 deletions

View File

@ -6,33 +6,39 @@ import { SnortContext } from "./context";
/**
* Parse reactions to a given event from a set of related events
* @param ev
* @param related
* @returns
* @param ev
* @param related
* @returns
*/
export function useEventReactions(ev: TaggedNostrEvent, related: ReadonlyArray<TaggedNostrEvent>) {
const system = useContext(SnortContext);
return useMemo(() => {
const link = NostrLink.fromEvent(ev);
const reactionKinds = related.reduce((acc, v) => {
if (link.isReplyToThis(v)) {
acc[v.kind.toString()] ??= [];
acc[v.kind.toString()].push(v);
}
return acc;
}, {} as Record<string, Array<TaggedNostrEvent>>);
const reactionKinds = related.reduce(
(acc, v) => {
if (link.isReplyToThis(v)) {
acc[v.kind.toString()] ??= [];
acc[v.kind.toString()].push(v);
}
return acc;
},
{} as Record<string, Array<TaggedNostrEvent>>,
);
const deletions = reactionKinds[EventKind.Deletion.toString()] ?? [];
const reactions = reactionKinds[EventKind.Reaction.toString()] ?? [];
const reposts = reactionKinds[EventKind.Repost.toString()] ?? [];
const groupReactions = reactions?.reduce((acc, reaction) => {
const kind = normalizeReaction(reaction.content);
acc[kind] ??= [];
acc[kind].push(reaction);
return acc;
}, {} as Record<Reaction, Array<TaggedNostrEvent>>);
const groupReactions = reactions?.reduce(
(acc, reaction) => {
const kind = normalizeReaction(reaction.content);
acc[kind] ??= [];
acc[kind].push(reaction);
return acc;
},
{} as Record<Reaction, Array<TaggedNostrEvent>>,
);
const zaps = (reactionKinds[EventKind.ZapReceipt] ?? [])
.map(a => parseZap(a, system.ProfileLoader.Cache, ev))
@ -42,14 +48,17 @@ export function useEventReactions(ev: TaggedNostrEvent, related: ReadonlyArray<T
return {
deletions,
reactions: {
positive: groupReactions[Reaction.Positive],
negative: groupReactions[Reaction.Negative],
positive: groupReactions[Reaction.Positive] ?? [],
negative: groupReactions[Reaction.Negative] ?? [],
},
reposts,
zaps,
others: Object.fromEntries(
Object.entries(reactionKinds)
.filter(([k]) => ![EventKind.Deletion, EventKind.Reaction, EventKind.Repost, EventKind.ZapReceipt].includes(Number(k))))
Object.entries(reactionKinds).filter(
([k]) =>
![EventKind.Deletion, EventKind.Reaction, EventKind.Repost, EventKind.ZapReceipt].includes(Number(k)),
),
),
};
}, [ev, related]);
}