relays: refactor

This commit is contained in:
William Casarin
2023-02-07 09:56:46 -08:00
parent ae6608cf7d
commit f71b67f036
2 changed files with 27 additions and 19 deletions

View File

@@ -24,14 +24,14 @@ enum RelayFlags: Int {
case broken = 1 case broken = 1
} }
struct RelayNIP11: Codable { struct RelayMetadata: Codable {
var name = "No data available" let name: String?
var description = "No data available" let description: String?
var pubkey = "No data available" let pubkey: String?
var contact = "No data available" let contact: String?
var supported_nips: [Int] = [] let supported_nips: [Int]?
var software = "No data available" let software: String?
var version = "No data available" let version: String?
} }
class Relay: Identifiable { class Relay: Identifiable {

View File

@@ -12,16 +12,22 @@ struct RelayDetailView: View {
let relay: String let relay: String
@State private var errorString: String? @State private var errorString: String?
@State private var nip11: RelayNIP11? @State private var nip11: RelayMetadata?
@State var conn_color: Color @State var conn_color: Color
func FieldText(_ str: String?) -> some View {
Text(str ?? "No data available")
}
var body: some View { var body: some View {
Group { Group {
if let nip11 { if let nip11 {
Form { Form {
Section(NSLocalizedString("Admin", comment: "Label to display relay contact user.")) { if let pubkey = nip11.pubkey {
UserView(damus_state: state, pubkey: nip11.pubkey) Section(NSLocalizedString("Admin", comment: "Label to display relay contact user.")) {
UserView(damus_state: state, pubkey: pubkey)
}
} }
Section(NSLocalizedString("Relay", comment: "Label to display relay address.")) { Section(NSLocalizedString("Relay", comment: "Label to display relay address.")) {
HStack { HStack {
@@ -33,19 +39,21 @@ struct RelayDetailView: View {
} }
} }
Section(NSLocalizedString("Description", comment: "Label to display relay description.")) { Section(NSLocalizedString("Description", comment: "Label to display relay description.")) {
Text(nip11.description) FieldText(nip11.description)
} }
Section(NSLocalizedString("Contact", comment: "Label to display relay contact information.")) { Section(NSLocalizedString("Contact", comment: "Label to display relay contact information.")) {
Text(nip11.contact) FieldText(nip11.contact)
} }
Section(NSLocalizedString("Software", comment: "Label to display relay software.")) { Section(NSLocalizedString("Software", comment: "Label to display relay software.")) {
Text(nip11.software) FieldText(nip11.software)
} }
Section(NSLocalizedString("Version", comment: "Label to display relay software version.")) { Section(NSLocalizedString("Version", comment: "Label to display relay software version.")) {
Text(nip11.version) FieldText(nip11.version)
} }
Section(NSLocalizedString("Supported NIPs", comment: "Label to display relay's supported NIPs.")) { if let nips = nip11.supported_nips, nips.count > 0 {
Text(nipsList(nips: nip11.supported_nips)) Section(NSLocalizedString("Supported NIPs", comment: "Label to display relay's supported NIPs.")) {
Text(nipsList(nips: nips))
}
} }
} }
} else if let errorString { } else if let errorString {
@@ -83,7 +91,7 @@ struct RelayDetailView: View {
} }
do { do {
let nip11 = try JSONDecoder().decode(RelayNIP11.self, from: data) let nip11 = try JSONDecoder().decode(RelayMetadata.self, from: data)
self.nip11 = nip11 self.nip11 = nip11
} catch { } catch {
errorString = error.localizedDescription errorString = error.localizedDescription