({ onAdd }: AddMCPDialogProps)
| 125 | } |
| 126 | |
| 127 | export function AddMCPDialog({ onAdd }: AddMCPDialogProps) { |
| 128 | const [open, setOpen] = useState(false); |
| 129 | const [pasteInput, setPasteInput] = useState(""); |
| 130 | const [name, setName] = useState(""); |
| 131 | const [configJson, setConfigJson] = useState(() => JSON.stringify(buildDefaultConfig(), null, 2)); |
| 132 | const [error, setError] = useState(""); |
| 133 | const [loading, setLoading] = useState(false); |
| 134 | |
| 135 | const resetForm = () => { |
| 136 | setPasteInput(""); |
| 137 | setName(""); |
| 138 | setConfigJson(JSON.stringify(buildDefaultConfig(), null, 2)); |
| 139 | setError(""); |
| 140 | }; |
| 141 | |
| 142 | useEffect(() => { |
| 143 | if (!pasteInput.trim()) return; |
| 144 | |
| 145 | const parsed = parseInput(pasteInput); |
| 146 | if (parsed) { |
| 147 | setName(parsed.name); |
| 148 | setConfigJson(JSON.stringify(parsed.config, null, 2)); |
| 149 | } |
| 150 | }, [pasteInput]); |
| 151 | |
| 152 | const handleSubmit = async (e: React.FormEvent) => { |
| 153 | e.preventDefault(); |
| 154 | setError(""); |
| 155 | |
| 156 | if (!name.trim()) { |
| 157 | setError("Please enter a server name"); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | if (name.includes(" ")) { |
| 162 | setError("Server name should not contain spaces"); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | let config: MCPConfig; |
| 167 | try { |
| 168 | config = JSON.parse(configJson); |
| 169 | if (typeof config !== "object" || Array.isArray(config)) { |
| 170 | setError("Config must be a JSON object"); |
| 171 | return; |
| 172 | } |
| 173 | } catch { |
| 174 | setError("Invalid JSON"); |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | try { |
| 179 | setLoading(true); |
| 180 | await onAdd(name, config); |
| 181 | resetForm(); |
| 182 | setOpen(false); |
| 183 | } catch (err: any) { |
| 184 | const msg = err.response?.data?.error || err.message || "Failed to add server"; |
nothing calls this directly
no test coverage detected