import { NDKRelayUrl } from '@nostr-dev-kit/ndk'; import { normalizeRelayUrl } from 'nostr-fetch'; import { useState } from 'react'; import { toast } from 'sonner'; import { PlusIcon } from '@shared/icons'; import { useRelay } from '@utils/hooks/useRelay'; const domainRegex = /^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/; export function RelayForm() { const { connectRelay } = useRelay(); const [relay, setRelay] = useState<{ url: NDKRelayUrl; purpose: 'read' | 'write' | undefined; }>({ url: '', purpose: undefined }); const create = () => { if (relay.url.length < 1) return toast.info('Please enter relay url'); try { const relayUrl = new URL(relay.url.replace(/\s/g, '')); if ( domainRegex.test(relayUrl.host) && (relayUrl.protocol === 'wss:' || relayUrl.protocol === 'ws:') ) { connectRelay.mutate(normalizeRelayUrl(relay.url)); setRelay({ url: '', purpose: undefined }); } else { return toast.error( 'URL is invalid, a relay must use websocket protocol (start with wss:// or ws://). Please check again' ); } } catch { return toast.error('Relay URL is not valid. Please check again'); } }; return (
setRelay((prev) => ({ ...prev, url: e.target.value }))} />
); }