2025-04-04 15:46:31 -04:00
|
|
|
import { useContext } from 'react';
|
|
|
|
import { NDKContext } from '@/lib/auth/ReactQueryAuthProvider';
|
2025-03-03 22:40:11 -05:00
|
|
|
import { useNDKStore } from '@/lib/stores/ndk';
|
2025-03-06 16:34:50 -05:00
|
|
|
import type { NDKUser, NDKEvent, NDKFilter } from '@nostr-dev-kit/ndk-mobile';
|
2025-02-22 01:16:33 -05:00
|
|
|
|
2025-04-04 15:46:31 -04:00
|
|
|
// Core hook for NDK access
|
|
|
|
// Uses the context from ReactQueryAuthProvider rather than Zustand store
|
2025-02-22 01:16:33 -05:00
|
|
|
export function useNDK() {
|
2025-04-04 15:46:31 -04:00
|
|
|
const { ndk, isInitialized } = useContext(NDKContext);
|
2025-04-01 00:03:41 -04:00
|
|
|
|
2025-04-04 15:46:31 -04:00
|
|
|
return {
|
|
|
|
ndk,
|
|
|
|
isLoading: !isInitialized,
|
|
|
|
error: !ndk && isInitialized ? new Error('NDK initialization failed') : undefined
|
|
|
|
};
|
2025-02-22 01:16:33 -05:00
|
|
|
}
|
|
|
|
|
2025-03-06 16:34:50 -05:00
|
|
|
// Hook for current user info
|
|
|
|
export function useNDKCurrentUser() {
|
2025-03-03 22:40:11 -05:00
|
|
|
const { currentUser, isAuthenticated, isLoading } = useNDKStore(state => ({
|
|
|
|
currentUser: state.currentUser,
|
|
|
|
isAuthenticated: state.isAuthenticated,
|
|
|
|
isLoading: state.isLoading
|
|
|
|
}));
|
2025-04-01 00:03:41 -04:00
|
|
|
|
|
|
|
return {
|
|
|
|
currentUser,
|
2025-02-22 01:16:33 -05:00
|
|
|
isAuthenticated,
|
2025-04-01 00:03:41 -04:00
|
|
|
isLoading
|
2025-02-22 01:16:33 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2025-03-06 16:34:50 -05:00
|
|
|
// Hook for authentication actions
|
2025-02-22 01:16:33 -05:00
|
|
|
export function useNDKAuth() {
|
2025-04-01 00:03:41 -04:00
|
|
|
const {
|
|
|
|
login,
|
|
|
|
loginWithExternalSigner,
|
|
|
|
logout,
|
|
|
|
generateKeys,
|
|
|
|
isAuthenticated,
|
|
|
|
isLoading
|
2025-03-29 17:47:10 -07:00
|
|
|
} = useNDKStore(state => ({
|
2025-03-03 22:40:11 -05:00
|
|
|
login: state.login,
|
2025-03-29 17:47:10 -07:00
|
|
|
loginWithExternalSigner: state.loginWithExternalSigner,
|
2025-03-03 22:40:11 -05:00
|
|
|
logout: state.logout,
|
2025-03-06 16:34:50 -05:00
|
|
|
generateKeys: state.generateKeys,
|
2025-03-03 22:40:11 -05:00
|
|
|
isAuthenticated: state.isAuthenticated,
|
2025-03-06 16:34:50 -05:00
|
|
|
isLoading: state.isLoading
|
2025-03-03 22:40:11 -05:00
|
|
|
}));
|
2025-04-01 00:03:41 -04:00
|
|
|
|
2025-02-22 01:16:33 -05:00
|
|
|
return {
|
|
|
|
login,
|
2025-03-29 17:47:10 -07:00
|
|
|
loginWithExternalSigner,
|
2025-02-22 01:16:33 -05:00
|
|
|
logout,
|
2025-03-03 22:40:11 -05:00
|
|
|
generateKeys,
|
2025-02-22 01:16:33 -05:00
|
|
|
isAuthenticated,
|
|
|
|
isLoading
|
|
|
|
};
|
2025-03-03 22:40:11 -05:00
|
|
|
}
|
|
|
|
|
2025-03-06 16:34:50 -05:00
|
|
|
// New hook for event operations
|
2025-03-03 22:40:11 -05:00
|
|
|
export function useNDKEvents() {
|
|
|
|
const { publishEvent, fetchEventsByFilter } = useNDKStore(state => ({
|
|
|
|
publishEvent: state.publishEvent,
|
|
|
|
fetchEventsByFilter: state.fetchEventsByFilter
|
|
|
|
}));
|
2025-04-01 00:03:41 -04:00
|
|
|
|
2025-03-03 22:40:11 -05:00
|
|
|
return {
|
|
|
|
publishEvent,
|
|
|
|
fetchEventsByFilter
|
|
|
|
};
|
2025-03-29 17:47:10 -07:00
|
|
|
}
|