import { useQueryClient } from '@tanstack/react-query'; import { useState } from 'react'; import { useStorage } from '@libs/storage/provider'; import { PlusIcon } from '@shared/icons'; const domainRegex = /^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/; export function RelayForm() { const { db } = useStorage(); const queryClient = useQueryClient(); const [url, setUrl] = useState(''); const [error, setError] = useState(''); const createRelay = async () => { if (url.length < 1) return setError('Please enter relay url'); try { const relay = new URL(url.replace(/\s/g, '')); if ( domainRegex.test(relay.host) && (relay.protocol === 'wss:' || relay.protocol === 'ws:') ) { const res = await db.createRelay(url); if (!res) return setError("You're already using this relay"); queryClient.invalidateQueries({ queryKey: ['user-relay'], }); setError(''); setUrl(''); } else { return setError( 'URL is invalid, a relay must use websocket protocol (start with wss:// or ws://). Please check again' ); } } catch { return setError('Relay URL is not valid. Please check again'); } }; return (
setUrl(e.target.value)} />
{error}
); }