|
| 1 | +import { useCallback, useRef, useState } from "react"; |
| 2 | +import Input from "components/utils/Input"; |
| 3 | +import { |
| 4 | + getContractByName, |
| 5 | + STELLAR_REGISTRY_URL, |
| 6 | + type RegistryContract, |
| 7 | +} from "@service/StellarRegistryService"; |
| 8 | + |
| 9 | +interface ContractNameSearchProps { |
| 10 | + /** Called with the resolved contract when the user picks a result. */ |
| 11 | + onSelect: (contract: RegistryContract) => void; |
| 12 | + /** Placeholder for the search input. */ |
| 13 | + placeholder?: string; |
| 14 | + disabled?: boolean; |
| 15 | +} |
| 16 | + |
| 17 | +type Status = "idle" | "loading" | "found" | "not-registered" | "error"; |
| 18 | + |
| 19 | +function shortAddress(address: string): string { |
| 20 | + return address.length > 12 |
| 21 | + ? `${address.slice(0, 6)}…${address.slice(-4)}` |
| 22 | + : address; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Exact-match contract name resolution backed by the Stellar Registry |
| 27 | + * smart contract (on-chain, no backend involved). |
| 28 | + * |
| 29 | + * The on-chain registry only supports exact name lookups, so there is no |
| 30 | + * suggestion list: the name is resolved on Enter or when leaving the field. |
| 31 | + * Unregistered names get an explicit "not on the registry" mark with a link |
| 32 | + * to the registry website so the author can double-check the name there. |
| 33 | + * |
| 34 | + * Deliberately decoupled from any feature: it only emits resolved contracts |
| 35 | + * through onSelect, so it can be embedded anywhere a contract address is |
| 36 | + * needed (e.g. tooling that must pick a deployed contract by name). |
| 37 | + */ |
| 38 | +export default function ContractNameSearch({ |
| 39 | + onSelect, |
| 40 | + placeholder = "Exact contract name (Stellar Registry)", |
| 41 | + disabled = false, |
| 42 | +}: ContractNameSearchProps) { |
| 43 | + const [query, setQuery] = useState(""); |
| 44 | + const [resolved, setResolved] = useState<RegistryContract | null>(null); |
| 45 | + const [status, setStatus] = useState<Status>("idle"); |
| 46 | + const resolveSeq = useRef(0); |
| 47 | + |
| 48 | + const resolve = useCallback(async (raw: string) => { |
| 49 | + const trimmed = raw.trim(); |
| 50 | + const seq = ++resolveSeq.current; |
| 51 | + |
| 52 | + if (!trimmed) { |
| 53 | + setResolved(null); |
| 54 | + setStatus("idle"); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + setStatus("loading"); |
| 59 | + try { |
| 60 | + const contract = await getContractByName(trimmed); |
| 61 | + if (resolveSeq.current !== seq) return; // a newer lookup superseded it |
| 62 | + if (contract) { |
| 63 | + setResolved(contract); |
| 64 | + setStatus("found"); |
| 65 | + } else { |
| 66 | + setResolved(null); |
| 67 | + setStatus("not-registered"); |
| 68 | + } |
| 69 | + } catch { |
| 70 | + if (resolveSeq.current !== seq) return; |
| 71 | + setResolved(null); |
| 72 | + setStatus("error"); |
| 73 | + } |
| 74 | + }, []); |
| 75 | + |
| 76 | + // Enter resolves immediately; leaving the field resolves what is typed. |
| 77 | + const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => { |
| 78 | + if (event.key === "Enter") { |
| 79 | + event.preventDefault(); |
| 80 | + void resolve(query); |
| 81 | + } |
| 82 | + }; |
| 83 | + |
| 84 | + const handleBlur = () => { |
| 85 | + void resolve(query); |
| 86 | + }; |
| 87 | + |
| 88 | + const handleUse = () => { |
| 89 | + if (resolved) { |
| 90 | + onSelect(resolved); |
| 91 | + setQuery(""); |
| 92 | + setResolved(null); |
| 93 | + setStatus("idle"); |
| 94 | + } |
| 95 | + }; |
| 96 | + |
| 97 | + const showNotRegistered = status === "not-registered"; |
| 98 | + const showFound = status === "found" && resolved !== null; |
| 99 | + |
| 100 | + return ( |
| 101 | + <div className="relative w-full"> |
| 102 | + <Input |
| 103 | + placeholder={placeholder} |
| 104 | + value={query} |
| 105 | + disabled={disabled} |
| 106 | + onChange={(e) => setQuery(e.target.value)} |
| 107 | + onKeyDown={handleKeyDown} |
| 108 | + onBlur={handleBlur} |
| 109 | + /> |
| 110 | + |
| 111 | + {status === "loading" && ( |
| 112 | + <p className="mt-1 text-xs text-secondary"> |
| 113 | + Resolving on the Stellar Registry… |
| 114 | + </p> |
| 115 | + )} |
| 116 | + |
| 117 | + {showNotRegistered && ( |
| 118 | + <p className="mt-1 text-xs text-red-500"> |
| 119 | + “{query.trim()}” is not on the Stellar Registry.{" "} |
| 120 | + <a |
| 121 | + href={STELLAR_REGISTRY_URL} |
| 122 | + target="_blank" |
| 123 | + rel="noopener noreferrer" |
| 124 | + className="underline" |
| 125 | + > |
| 126 | + Check the registry ↗ |
| 127 | + </a> |
| 128 | + </p> |
| 129 | + )} |
| 130 | + |
| 131 | + {status === "error" && ( |
| 132 | + <p className="mt-1 text-xs text-red-500"> |
| 133 | + Could not reach the Stellar Registry. Press Enter to retry, or paste |
| 134 | + the address manually. |
| 135 | + </p> |
| 136 | + )} |
| 137 | + |
| 138 | + {showFound && ( |
| 139 | + <div className="mt-2 flex flex-wrap justify-between items-center gap-2 p-2 border border-gray-300 rounded-md bg-white"> |
| 140 | + <div> |
| 141 | + <span className="font-medium text-primary"> |
| 142 | + {resolved!.contractName} |
| 143 | + </span> |
| 144 | + <span className="ml-2 text-xs text-secondary font-mono"> |
| 145 | + {shortAddress(resolved!.contractId)} |
| 146 | + </span> |
| 147 | + </div> |
| 148 | + <button |
| 149 | + type="button" |
| 150 | + onClick={handleUse} |
| 151 | + className="px-3 py-1 text-sm rounded-md bg-primary text-white cursor-pointer" |
| 152 | + > |
| 153 | + Use this address |
| 154 | + </button> |
| 155 | + </div> |
| 156 | + )} |
| 157 | + </div> |
| 158 | + ); |
| 159 | +} |
0 commit comments