Skip to content

Commit

Permalink
export useNDKSessionInit independently
Browse files Browse the repository at this point in the history
  • Loading branch information
pablof7z committed Jan 23, 2025
1 parent f96b062 commit 0c77da0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
25 changes: 12 additions & 13 deletions ndk-mobile/src/hooks/session.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import NDK, { NDKEvent, NDKKind, NDKUser } from '@nostr-dev-kit/ndk';
import { useNDK } from './ndk.js';
import { NDKEventWithFrom } from './subscribe.js';
import { useNDKSessionStore } from '../stores/session/index.js';
import { useNDKSession } from '../stores/session/index.js';
import { useNDKWallet } from './wallet.js';
import { walletFromLoadingString } from '@nostr-dev-kit/ndk-wallet';
import { SessionInitOpts, SessionInitCallbacks } from '../stores/session/types.js';
import { SettingsStore } from '../types.js';

const useNDKSession = () => {
const init = useNDKSessionStore(s => s.init);
const mutePubkey = useNDKSessionStore(s => s.mutePubkey);
const useNDKSessionInit = () => {
const init = useNDKSession(s => s.init);

const { setActiveWallet } = useNDKWallet();

Expand All @@ -26,13 +25,13 @@ const useNDKSession = () => {
}
}

return { init: wrappedInit, mutePubkey };
return wrappedInit;
}

const useFollows = () => useNDKSessionStore(s => s.follows);
const useMuteList = () => useNDKSessionStore(s => s.muteList);
const useSessionEvents = () => useNDKSessionStore(s => s.events);
const useWOT = () => useNDKSessionStore(s => s.wot);
const useFollows = () => useNDKSession(s => s.follows);
const useMuteList = () => useNDKSession(s => s.muteList);
const useSessionEvents = () => useNDKSession(s => s.events);
const useWOT = () => useNDKSession(s => s.wot);

/**
* This hook allows you to get a specific kind, wrapped in the event class you provide.
Expand All @@ -47,7 +46,7 @@ const useNDKSessionEventKind = <T extends NDKEvent>(
{ create }: { create: boolean } = { create: false }
): T | undefined => {
const { ndk } = useNDK();
const events = useNDKSessionStore(s => s.events);
const events = useNDKSession(s => s.events);
const kindEvents = events.get(kind) || [];
const firstEvent = kindEvents[0];

Expand All @@ -65,7 +64,7 @@ const useNDKSessionEvents = <T extends NDKEvent>(
kinds: NDKKind[],
eventClass?: NDKEventWithFrom<any>,
): T[] => {
const events = useNDKSessionStore(s => s.events);
const events = useNDKSession(s => s.events);
let allEvents = kinds.flatMap((kind) => events.get(kind) || []);

if (kinds.length > 1) allEvents = allEvents.sort((a, b) => a.created_at - b.created_at);
Expand All @@ -83,5 +82,5 @@ export {
useWOT,
useNDKSessionEventKind,
useNDKSessionEvents,
useNDKSession,
};
useNDKSessionInit,
};
4 changes: 2 additions & 2 deletions ndk-mobile/src/hooks/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useStore } from 'zustand';
import NDK, { NDKEvent, NDKFilter, NDKKind, NDKRelaySet, NDKSubscription, NDKSubscriptionOptions, wrapEvent } from '@nostr-dev-kit/ndk';
import { useCallback, useEffect, useMemo, useRef } from 'react';
import { useNDK } from './ndk.js';
import { useNDKSessionStore } from '../stores/session/index.js';
import { useNDKSession } from '../stores/session/index.js';

/**
* Extends NDKEvent with a 'from' method to wrap events with a kind-specific handler
Expand Down Expand Up @@ -181,7 +181,7 @@ export const useSubscribe = <T extends NDKEvent>(
dependencies.push(!!filters);

const { ndk } = useNDK();
const muteList = useNDKSessionStore(s => s.muteList);
const muteList = useNDKSession(s => s.muteList);
const store = useMemo(() => createSubscribeStore<T>(opts?.bufferMs), dependencies);
const storeInstance = useStore(store);

Expand Down
2 changes: 1 addition & 1 deletion ndk-mobile/src/stores/session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { setMuteList } from './actions/setMuteList.js';
import { setEvents } from './actions/setEvents.js';
import { addEvent } from './actions/addEvent.js';

export const useNDKSessionStore = create<SessionState>()((set, get) => ({
export const useNDKSession = create<SessionState>()((set, get) => ({
follows: undefined,
ndk: undefined,
muteList: new Set(),
Expand Down

0 comments on commit 0c77da0

Please sign in to comment.