-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNip07Context.tsx
48 lines (38 loc) · 1.1 KB
/
Nip07Context.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Nip07Context.tsx
import React, { createContext, useContext, useEffect, useState } from 'react';
import NDK, { NDKNip07Signer } from '@nostr-dev-kit/ndk';
export const Nip07Context = createContext(null);
export const Nip07Provider = ({ children }) => {
const [ndk, setNdk] = useState(null);
useEffect(() => {
let isActive = true;
const initNdk = async () => {
const ndkInstance = new NDK({
signer: new NDKNip07Signer(),
explicitRelayUrls: [
'wss://nostr.cercatrova.me',
'wss://relay.damus.io',
]
});
ndkInstance.on('disconnect', () => {
if (isActive) {
console.log('Disconnected, attempting to reconnect...');
initNdk(); // Recursive reinitialization on disconnect
}
});
await ndkInstance.connect();
setNdk(ndkInstance);
};
initNdk();
return () => {
isActive = false;
ndk?.disconnect();
};
}, []);
return (
<Nip07Context.Provider value={ndk}>
{children}
</Nip07Context.Provider>
);
};
export const useNip07 = () => useContext(Nip07Context);