fix: add connection test feedback in server form

- Show error message when connection test fails
- Disable test button while testing
- Provide clear feedback when API key or hostname are invalid
- Reset error message on successful connection
This commit is contained in:
Micha
2026-01-03 14:16:11 +01:00
parent ae83ea7dab
commit 8cf974118b

View File

@@ -20,6 +20,8 @@ struct ServerFormView: View {
@State private var hostname: String
@State private var apiKey: String
@State private var connectionOK: Bool = false
@State private var testingConnection: Bool = false
@State private var connectionError: String = ""
@Environment(\.dismiss) private var dismiss
@@ -55,6 +57,12 @@ struct ServerFormView: View {
SecureField("API Key", text: $apiKey)
.textFieldStyle(RoundedBorderTextFieldStyle())
if !connectionError.isEmpty {
Text(connectionError)
.foregroundColor(.red)
.font(.caption)
}
HStack {
Button("Cancel") {
dismiss()
@@ -65,6 +73,8 @@ struct ServerFormView: View {
await testConnection()
}
}
.disabled(hostname.isEmpty || apiKey.isEmpty || testingConnection)
Button("Save") {
saveServer()
updateServer()
@@ -102,9 +112,21 @@ struct ServerFormView: View {
let host = hostname.trimmingCharacters(in: .whitespacesAndNewlines)
let key = apiKey.trimmingCharacters(in: .whitespacesAndNewlines)
await MainActor.run {
testingConnection = true
connectionError = ""
}
let reachable = await PingService.ping(hostname: host, apiKey: key)
await MainActor.run {
connectionOK = reachable
testingConnection = false
if reachable {
connectionOK = true
connectionError = ""
} else {
connectionOK = false
connectionError = "Connection failed. Check hostname and API key."
}
}
//
// guard let url = URL(string: "https://\(host)/api/v2/ping") else {