Skip to content

Commit

Permalink
add useNDKSessionEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
pablof7z committed Nov 25, 2024
1 parent bed2f8d commit ed29a9d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
16 changes: 14 additions & 2 deletions ndk-mobile/src/hooks/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useContext } from 'react';
import NDKSessionContext from '../context/session';
import { NDKEvent, NDKKind } from '@nostr-dev-kit/ndk';
import { useNDK } from './ndk';
import { NDKEventWithFrom } from './subscribe';
import { NDKEventWithFrom, useSubscribe } from './subscribe';

const useNDKSession = (): NDKSessionContext => {
const context = useContext(NDKSessionContext);
Expand Down Expand Up @@ -38,4 +38,16 @@ const useNDKSessionEventKind = <T extends NDKEvent>(
return firstEvent ? EventClass.from(firstEvent) : undefined;
};

export { useNDKSession, useNDKSessionEventKind };
const useNDKSessionEvents = <T extends NDKEvent>(
kinds: NDKKind[],
eventClass?: NDKEventWithFrom<any>,
): T[] => {
const { events } = useNDKSession();
let allEvents = kinds.flatMap((kind) => events.get(kind) || []);

if (kinds.length > 1) allEvents = allEvents.sort((a, b) => a.created_at - b.created_at);

return allEvents.map((e) => eventClass ? eventClass.from(e) : e as T);
};

export { useNDKSession, useNDKSessionEventKind, useNDKSessionEvents };
1 change: 1 addition & 0 deletions ndk/src/events/kinds/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export enum NDKKind {
CurationSet = 30004, // Deprecated but left for backwards compatibility
ArticleCurationSet = 30004,
VideoCurationSet = 30005,
ImageCurationSet = 30006,
InterestSet = 30015,
InterestsList = NDKKind.InterestSet, // Deprecated but left for backwards compatibility
EmojiSet = 30030,
Expand Down

0 comments on commit ed29a9d

Please sign in to comment.