import { forwardRef, useState } from "react"; import { Button, Flex, IconButton, Input, InputGroup, InputLeftElement, InputProps, InputRightElement, Modal, ModalBody, ModalCloseButton, ModalContent, ModalHeader, ModalOverlay, ModalProps, useDisclosure, } from "@chakra-ui/react"; import { useAsync } from "react-use"; import { unique } from "../helpers/array"; import { RelayIcon, SearchIcon } from "./icons"; function RelayPickerModal({ onSelect, onClose, ...props }: { onSelect: (relay: string) => void } & Omit) { const [search, setSearch] = useState(""); const { value: onlineRelays } = useAsync(async () => fetch("https://api.nostr.watch/v1/online").then((res) => res.json() as Promise) ); const relayList = unique(onlineRelays ?? []); const filteredRelays = search ? relayList.filter((url) => url.includes(search)) : relayList; return ( Pick Relay } /> setSearch(e.target.value)} /> {filteredRelays.map((url) => ( ))} ); } export type RelayUrlInputProps = Omit; export const RelayUrlInput = forwardRef( ({ onChange, ...props }: Omit & { onChange: (url: string) => void }, ref) => { const { isOpen, onClose, onOpen } = useDisclosure(); const { value: relaysJson } = useAsync(async () => fetch("https://api.nostr.watch/v1/online").then((res) => res.json() as Promise) ); const relaySuggestions = unique(relaysJson ?? []); return ( <> onChange(e.target.value)} {...props} /> {relaySuggestions.map((url) => ( ))} } aria-label="Pick from list" size="sm" onClick={onOpen} /> onChange(url)} size="2xl" /> ); } );