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
62 lines
1.8 KiB
TypeScript
62 lines
1.8 KiB
TypeScript
import { useEffect } from 'react';
|
|
import { useNostr } from '@nostrify/react';
|
|
import { useCurrentUser } from '@/hooks/useCurrentUser';
|
|
import { useAppContext } from '@/hooks/useAppContext';
|
|
|
|
/**
|
|
* NostrSync - Syncs user's Nostr data
|
|
*
|
|
* This component runs globally to sync various Nostr data when the user logs in.
|
|
* Currently syncs:
|
|
* - NIP-65 relay list (kind 10002)
|
|
*/
|
|
export function NostrSync() {
|
|
const { nostr } = useNostr();
|
|
const { user } = useCurrentUser();
|
|
const { config, updateConfig } = useAppContext();
|
|
|
|
useEffect(() => {
|
|
if (!user) return;
|
|
|
|
const syncRelaysFromNostr = async () => {
|
|
try {
|
|
const events = await nostr.query(
|
|
[{ kinds: [10002], authors: [user.pubkey], limit: 1 }],
|
|
{ signal: AbortSignal.timeout(5000) }
|
|
);
|
|
|
|
if (events.length > 0) {
|
|
const event = events[0];
|
|
|
|
// Only update if the event is newer than our stored data
|
|
if (event.created_at > config.relayMetadata.updatedAt) {
|
|
const fetchedRelays = event.tags
|
|
.filter(([name]) => name === 'r')
|
|
.map(([_, url, marker]) => ({
|
|
url,
|
|
read: !marker || marker === 'read',
|
|
write: !marker || marker === 'write',
|
|
}));
|
|
|
|
if (fetchedRelays.length > 0) {
|
|
console.log('Syncing relay list from Nostr:', fetchedRelays);
|
|
updateConfig((current) => ({
|
|
...current,
|
|
relayMetadata: {
|
|
relays: fetchedRelays,
|
|
updatedAt: event.created_at,
|
|
},
|
|
}));
|
|
}
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Failed to sync relays from Nostr:', error);
|
|
}
|
|
};
|
|
|
|
syncRelaysFromNostr();
|
|
}, [user, config.relayMetadata.updatedAt, nostr, updateConfig]);
|
|
|
|
return null;
|
|
} |