mkstack/src/components/NostrProvider.tsx

63 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-06-02 21:44:52 -05:00
import React, { useEffect, useRef } from 'react';
2025-04-16 21:43:54 -05:00
import { NostrEvent, NPool, NRelay1 } from '@nostrify/nostrify';
import { NostrContext } from '@nostrify/react';
2025-06-02 21:44:52 -05:00
import { useQueryClient } from '@tanstack/react-query';
import { useAppContext } from '@/hooks/useAppContext';
2025-04-16 21:43:54 -05:00
interface NostrProviderProps {
children: React.ReactNode;
}
const NostrProvider: React.FC<NostrProviderProps> = (props) => {
2025-06-02 21:44:52 -05:00
const { children } = props;
2025-06-04 11:32:12 -05:00
const { config, presetRelays } = useAppContext();
2025-06-02 21:44:52 -05:00
const queryClient = useQueryClient();
2025-04-16 21:43:54 -05:00
// Create NPool instance only once
const pool = useRef<NPool | undefined>(undefined);
2025-06-02 21:44:52 -05:00
// Use refs so the pool always has the latest data
const relayUrl = useRef<string>(config.relayUrl);
// Update refs when config changes
useEffect(() => {
relayUrl.current = config.relayUrl;
queryClient.resetQueries();
2025-06-04 10:04:39 -05:00
}, [config.relayUrl, queryClient]);
2025-06-02 21:44:52 -05:00
// Initialize NPool only once
2025-04-16 21:43:54 -05:00
if (!pool.current) {
pool.current = new NPool({
open(url: string) {
return new NRelay1(url);
},
reqRouter(filters) {
2025-06-02 21:44:52 -05:00
return new Map([[relayUrl.current, filters]]);
2025-04-16 21:43:54 -05:00
},
eventRouter(_event: NostrEvent) {
2025-06-04 11:32:12 -05:00
// Publish to the selected relay
const allRelays = new Set<string>([relayUrl.current]);
// Also publish to the preset relays, capped to 5
for (const { url } of (presetRelays ?? [])) {
allRelays.add(url);
if (allRelays.size >= 5) {
break;
}
}
return [...allRelays];
2025-04-16 21:43:54 -05:00
},
});
}
return (
<NostrContext.Provider value={{ nostr: pool.current }}>
{children}
</NostrContext.Provider>
);
};
export default NostrProvider;