Add Relay Detail View

Changelog-Added: Relay Detail View
Closes: #479
This commit is contained in:
Joel Klabo
2023-02-01 13:49:32 -08:00
committed by William Casarin
parent 852609ee30
commit 552402f2b5
5 changed files with 165 additions and 1 deletions

View File

@@ -24,6 +24,16 @@ enum RelayFlags: Int {
case broken = 1
}
struct RelayNIP11: Codable {
var name = "No data available"
var description = "No data available"
var pubkey = "No data available"
var contact = "No data available"
var supported_nips: [Int] = []
var software = "No data available"
var version = "No data available"
}
class Relay: Identifiable {
let descriptor: RelayDescriptor
let connection: RelayConnection

View File

@@ -0,0 +1,26 @@
//
// NIPURLBuilder.swift
// damus
//
// Created by Honk on 2/1/23.
//
import Foundation
struct NIPURLBuilder {
static private let baseURL = "https://github.com/nostr-protocol/nips/blob/master/"
static func url(forNIP nip: Int) -> URL? {
let urlString = baseURL + "\(formatNipNumber(nip: nip)).md"
return URL(string: urlString)
}
static func formatNipNumber(nip: Int) -> String {
let formatted: String
if nip < 10 {
formatted = "0\(nip)"
} else {
formatted = "\(nip)"
}
return formatted
}
}

View File

@@ -0,0 +1,116 @@
//
// RelayDetailView.swift
// damus
//
// Created by Joel Klabo on 2/1/23.
//
import SwiftUI
struct RelayDetailView: View {
let state: DamusState
let relay: String
@State private var errorString: String?
@State private var nip11: RelayNIP11?
@State var conn_color: Color
var body: some View {
Group {
if let nip11 {
Form {
Section(NSLocalizedString("Admin", comment: "Label to display relay contact user.")) {
UserView(damus_state: state, pubkey: nip11.pubkey)
}
Section(NSLocalizedString("Relay", comment: "Label to display relay address.")) {
HStack {
Text(relay)
Spacer()
Circle()
.frame(width: 8.0, height: 8.0)
.foregroundColor(conn_color)
}
}
Section(NSLocalizedString("Description", comment: "Label to display relay description.")) {
Text(nip11.description)
}
Section(NSLocalizedString("Contact", comment: "Label to display relay contact information.")) {
Text(nip11.contact)
}
Section(NSLocalizedString("Software", comment: "Label to display relay software.")) {
Text(nip11.software)
}
Section(NSLocalizedString("Version", comment: "Label to display relay software version.")) {
Text(nip11.version)
}
Section(NSLocalizedString("Supported NIPs", comment: "Label to display relay's supported NIPs.")) {
Text(nipsList(nips: nip11.supported_nips))
}
}
} else if let errorString {
Text(errorString)
.foregroundColor(.red)
.font(.caption)
} else {
ProgressView()
}
}
.navigationTitle(nip11?.name ?? "")
.task {
var urlString = relay.replacingOccurrences(of: "wss://", with: "https://")
urlString = urlString.replacingOccurrences(of: "ws://", with: "http://")
guard let url = URL(string: urlString) else {
return
}
var request = URLRequest(url: url)
request.setValue("application/nostr+json", forHTTPHeaderField: "Accept")
var res: (Data, URLResponse)? = nil
do {
res = try await URLSession.shared.data(for: request)
} catch {
errorString = error.localizedDescription
return
}
guard let data = res?.0 else {
errorString = "Relay not responding to metadata request"
return
}
do {
let nip11 = try JSONDecoder().decode(RelayNIP11.self, from: data)
self.nip11 = nip11
} catch {
errorString = error.localizedDescription
}
}
}
private func nipsList(nips: [Int]) -> AttributedString {
var attrString = AttributedString()
let lastNipIndex = nips.count - 1
for (index, nip) in nips.enumerated() {
if let link = NIPURLBuilder.url(forNIP: nip) {
let nipString = NIPURLBuilder.formatNipNumber(nip: nip)
var nipAttrString = AttributedString(stringLiteral: nipString)
nipAttrString.link = link
attrString = attrString + nipAttrString
if index < lastNipIndex {
attrString = attrString + AttributedString(stringLiteral: ", ")
}
}
}
return attrString
}
}
struct RelayDetailView_Previews: PreviewProvider {
static var previews: some View {
RelayDetailView(state: test_damus_state(), relay: "wss://nostr.klabo.blog", conn_color: .green)
}
}

View File

@@ -34,7 +34,11 @@ struct RelayView: View {
Circle()
.frame(width: 8.0, height: 8.0)
.foregroundColor(conn_color)
Text(relay)
NavigationLink {
RelayDetailView(state: state, relay: relay, conn_color: conn_color)
} label: {
Text(relay)
}
}
.onReceive(timer) { _ in
update_connection_color()