import { Check, ChevronsUpDown, Wifi, Plus } from "lucide-react"; import { cn } from "@/lib/utils"; import { Button } from "@/components/ui/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { useAppConfig } from "@/components/AppProvider"; import { useState } from "react"; interface RelaySelectorProps { className?: string; } export function RelaySelector({ className }: RelaySelectorProps) { const { config, updateConfig, availableRelays } = useAppConfig(); const [open, setOpen] = useState(false); const [inputValue, setInputValue] = useState(""); const selectedOption = availableRelays.find((option) => option.url === config.relayUrl); // Function to normalize relay URL by adding wss:// if no protocol is present const normalizeRelayUrl = (url: string): string => { const trimmed = url.trim(); if (!trimmed) return trimmed; // Check if it already has a protocol if (trimmed.includes('://')) { return trimmed; } // Add wss:// prefix return `wss://${trimmed}`; }; // Handle adding a custom relay const handleAddCustomRelay = (url: string) => { const normalizedUrl = normalizeRelayUrl(url); if (normalizedUrl) { updateConfig(config => ({ ...config, relayUrl: normalizedUrl })); setOpen(false); setInputValue(""); } }; // Check if input value looks like a valid relay URL const isValidRelayInput = (value: string): boolean => { const trimmed = value.trim(); if (!trimmed) return false; // Basic validation - should contain at least a domain-like structure const normalized = normalizeRelayUrl(trimmed); try { new URL(normalized); return true; } catch { return false; } }; return ( {inputValue && isValidRelayInput(inputValue) ? ( handleAddCustomRelay(inputValue)} className="cursor-pointer" >
Add custom relay {normalizeRelayUrl(inputValue)}
) : (
{inputValue ? "Invalid relay URL" : "No relay found."}
)}
{availableRelays .filter((option) => !inputValue || option.name.toLowerCase().includes(inputValue.toLowerCase()) || option.url.toLowerCase().includes(inputValue.toLowerCase()) ) .map((option) => ( { updateConfig(config => ({ ...config, relayUrl: currentValue })); setOpen(false); setInputValue(""); }} >
{option.name} {option.url}
))} {inputValue && isValidRelayInput(inputValue) && ( handleAddCustomRelay(inputValue)} className="cursor-pointer border-t" >
Add custom relay {normalizeRelayUrl(inputValue)}
)}
); }