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 { useState } from "react"; import { useAppContext } from "@/hooks/useAppContext"; interface RelaySelectorProps { className?: string; } export function RelaySelector(props: RelaySelectorProps) { const { className } = props; const { config, updateConfig, presetRelays = [] } = useAppContext(); const selectedRelay = config.relayUrl; const setSelectedRelay = (relay: string) => { updateConfig((current) => ({ ...current, relayUrl: relay })); }; const [open, setOpen] = useState(false); const [inputValue, setInputValue] = useState(""); const selectedOption = presetRelays.find((option) => option.url === selectedRelay); // 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) => { setSelectedRelay?.(normalizeRelayUrl(url)); 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."}
)}
{presetRelays .filter((option) => !inputValue || option.name.toLowerCase().includes(inputValue.toLowerCase()) || option.url.toLowerCase().includes(inputValue.toLowerCase()) ) .map((option) => ( { setSelectedRelay(normalizeRelayUrl(currentValue)); setOpen(false); setInputValue(""); }} >
{option.name} {option.url}
))} {inputValue && isValidRelayInput(inputValue) && ( handleAddCustomRelay(inputValue)} className="cursor-pointer border-t" >
Add custom relay {normalizeRelayUrl(inputValue)}
)}
); }