mirror of
https://github.com/aljazceru/rabbit.git
synced 2025-12-18 14:34:25 +01:00
update
This commit is contained in:
51
src/clients/useReactions.ts
Normal file
51
src/clients/useReactions.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { type Accessor } from 'solid-js';
|
||||
import { type Event as NostrEvent } from 'nostr-tools/event';
|
||||
import { type CreateQueryResult } from '@tanstack/solid-query';
|
||||
|
||||
import useCachedEvents from '@/clients/useCachedEvents';
|
||||
|
||||
export type UseEventProps = {
|
||||
relayUrls: string[];
|
||||
eventId: string;
|
||||
};
|
||||
|
||||
export type UseEvent = {
|
||||
reactions: Accessor<NostrEvent[]>;
|
||||
reactionsGroupedByContent: Accessor<Map<string, NostrEvent[]>>;
|
||||
isReactedBy(pubkey: string): boolean;
|
||||
query: CreateQueryResult<NostrEvent[]>;
|
||||
};
|
||||
|
||||
const useReactions = (propsProvider: () => UseEventProps): UseEvent => {
|
||||
const query = useCachedEvents(() => {
|
||||
const { relayUrls, eventId } = propsProvider();
|
||||
return {
|
||||
relayUrls,
|
||||
filters: [
|
||||
{
|
||||
'#e': [eventId],
|
||||
kinds: [7],
|
||||
},
|
||||
],
|
||||
};
|
||||
});
|
||||
|
||||
const reactions = () => query.data ?? [];
|
||||
|
||||
const reactionsGroupedByContent = () => {
|
||||
const result = new Map<string, NostrEvent[]>();
|
||||
reactions().forEach((event) => {
|
||||
const events = result.get(event.content) ?? [];
|
||||
events.push(event);
|
||||
result.set(event.content, events);
|
||||
});
|
||||
return result;
|
||||
};
|
||||
|
||||
const isReactedBy = (pubkey: string): boolean =>
|
||||
reactions().findIndex((event) => event.pubkey === pubkey) !== -1;
|
||||
|
||||
return { reactions, reactionsGroupedByContent, isReactedBy, query };
|
||||
};
|
||||
|
||||
export default useReactions;
|
||||
Reference in New Issue
Block a user