mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-05 16:33:36 +00:00
- Replace single relayUrl with NIP-65 compatible relayMetadata structure - Add RelayListManager component for managing multiple relays with read/write permissions - Add NostrSync component to automatically sync user's NIP-65 relay list on login - Update NostrProvider to use relay list instead of single relay - Remove RelaySelector component and update empty states - Update AGENTS.md documentation to reflect relay management changes - Automatically publish relay list changes as NIP-65 events when user is logged in
69 lines
1.9 KiB
TypeScript
69 lines
1.9 KiB
TypeScript
import React, { useEffect, useRef } from 'react';
|
|
import { NostrEvent, NostrFilter, NPool, NRelay1 } from '@nostrify/nostrify';
|
|
import { NostrContext } from '@nostrify/react';
|
|
import { useQueryClient } from '@tanstack/react-query';
|
|
import { useAppContext } from '@/hooks/useAppContext';
|
|
|
|
interface NostrProviderProps {
|
|
children: React.ReactNode;
|
|
}
|
|
|
|
const NostrProvider: React.FC<NostrProviderProps> = (props) => {
|
|
const { children } = props;
|
|
const { config } = useAppContext();
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
// Create NPool instance only once
|
|
const pool = useRef<NPool | undefined>(undefined);
|
|
|
|
// Use refs so the pool always has the latest data
|
|
const relayMetadata = useRef(config.relayMetadata);
|
|
|
|
// Invalidate Nostr queries when relay metadata changes
|
|
useEffect(() => {
|
|
relayMetadata.current = config.relayMetadata;
|
|
queryClient.invalidateQueries({ queryKey: ['nostr'] });
|
|
}, [config.relayMetadata, queryClient]);
|
|
|
|
// Initialize NPool only once
|
|
if (!pool.current) {
|
|
pool.current = new NPool({
|
|
open(url: string) {
|
|
return new NRelay1(url);
|
|
},
|
|
reqRouter(filters: NostrFilter[]) {
|
|
const routes = new Map<string, NostrFilter[]>();
|
|
|
|
// Route to all read relays
|
|
const readRelays = relayMetadata.current.relays
|
|
.filter(r => r.read)
|
|
.map(r => r.url);
|
|
|
|
for (const url of readRelays) {
|
|
routes.set(url, filters);
|
|
}
|
|
|
|
return routes;
|
|
},
|
|
eventRouter(_event: NostrEvent) {
|
|
// Get write relays from metadata
|
|
const writeRelays = relayMetadata.current.relays
|
|
.filter(r => r.write)
|
|
.map(r => r.url);
|
|
|
|
const allRelays = new Set<string>(writeRelays);
|
|
|
|
return [...allRelays];
|
|
},
|
|
});
|
|
}
|
|
|
|
return (
|
|
<NostrContext.Provider value={{ nostr: pool.current }}>
|
|
{children}
|
|
</NostrContext.Provider>
|
|
);
|
|
};
|
|
|
|
export default NostrProvider; |