feat: add useEventReactions to system-react
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@snort/system-react",
|
||||
"version": "1.0.16",
|
||||
"version": "1.0.17",
|
||||
"description": "React hooks for @snort/system",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
@ -15,7 +15,7 @@
|
||||
"dist"
|
||||
],
|
||||
"dependencies": {
|
||||
"@snort/shared": "^1.0.7",
|
||||
"@snort/shared": "^1.0.8",
|
||||
"@snort/system": "^1.0.22",
|
||||
"react": "^18.2.0"
|
||||
},
|
||||
|
@ -1,5 +1,7 @@
|
||||
export * from "./context";
|
||||
|
||||
export * from "./useRequestBuilder";
|
||||
export * from "./useSystemState";
|
||||
export * from "./useUserProfile";
|
||||
export * from "./context";
|
||||
export * from "./useUserSearch";
|
||||
export * from "./useEventReactions";
|
55
packages/system-react/src/useEventReactions.tsx
Normal file
55
packages/system-react/src/useEventReactions.tsx
Normal file
@ -0,0 +1,55 @@
|
||||
import { useContext, useMemo } from "react";
|
||||
import { normalizeReaction, Reaction } from "@snort/shared";
|
||||
import { EventKind, NostrLink, parseZap, TaggedNostrEvent } from "@snort/system";
|
||||
|
||||
import { SnortContext } from "./context";
|
||||
|
||||
/**
|
||||
* Parse reactions to a given event from a set of related events
|
||||
* @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 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 zaps = (reactionKinds[EventKind.ZapReceipt] ?? [])
|
||||
.map(a => parseZap(a, system.ProfileLoader.Cache, ev))
|
||||
.filter(a => a.valid)
|
||||
.sort((a, b) => b.amount - a.amount);
|
||||
|
||||
return {
|
||||
deletions,
|
||||
reactions: {
|
||||
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))))
|
||||
};
|
||||
}, [ev, related]);
|
||||
}
|
Reference in New Issue
Block a user