File tree Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,8 @@ export * from "./session/hooks/use-available-sessions";
20
20
export * from "./subscribe/store" ;
21
21
export * from "./wallet/hooks" ;
22
22
23
+ export * from "./ndk/headless/index.js" ;
24
+
23
25
export * from "@nostr-dev-kit/ndk" ;
24
26
import NDK from "@nostr-dev-kit/ndk" ;
25
27
export default NDK ;
Original file line number Diff line number Diff line change
1
+ import NDK , { NDKConstructorParams } from "@nostr-dev-kit/ndk" ;
2
+ import { NDKSessionStorageAdapter } from "../../session/storage" ;
3
+ import { useNDKInit } from "../hooks" ;
4
+ import { useEffect } from "react" ;
5
+ import { useNDKSessionMonitor } from "../../session/hooks/use-ndk-session-monitor" ;
6
+ import { SessionStartOptions } from "../../session/store/types" ;
7
+
8
+ interface NDKHeadlessProps {
9
+ ndk : NDKConstructorParams ;
10
+ session : {
11
+ storage : NDKSessionStorageAdapter ;
12
+ opts : SessionStartOptions ;
13
+ } | false ;
14
+ } ;
15
+
16
+ /**
17
+ * Add a headless component to make it simpler to instantiate NDK in React apps.
18
+ *
19
+ * @example
20
+ * ```tsx
21
+ * import { NDKHeadless } from "@nostr-dev-kit/ndk-hooks";
22
+ *
23
+ * function App() {
24
+ * return (
25
+ * <>
26
+ * <NDKHeadless
27
+ * ndk={{ explicitRelayUrls: ["wss://relay.damus.io"] }}
28
+ * session={{ storage: new NDKSessionLocalStorage(), opts: { follows: true, profile: true } }}
29
+ * />
30
+ * <YourApp />
31
+ * </>
32
+ * );
33
+ * }
34
+ */
35
+ export function NDKHeadless ( {
36
+ ndk,
37
+ session = false ,
38
+ } : NDKHeadlessProps ) {
39
+ const initNDK = useNDKInit ( ) ;
40
+
41
+ useEffect ( ( ) => {
42
+ const _ndk = new NDK ( ndk ) ;
43
+ initNDK ( _ndk ) ;
44
+ } , [ ] ) ;
45
+
46
+ useNDKSessionMonitor ( session ? session . storage : false , session ? session . opts : undefined ) ;
47
+
48
+ return null ;
49
+ }
You can’t perform that action at this time.
0 commit comments