summaryrefslogtreecommitdiff
path: root/Rune/Views/Domains/RecordListView.swift
blob: 67a4140d604b680e6695c71bdcd9a63029b4ab0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import SwiftUI

struct RecordListView: View {
    let domainName: String
    @ObservedObject var viewModel: DomainViewModel
    let client: NjallaClient

    @State private var showingAddRecord = false

    var body: some View {
        Group {
            if viewModel.isLoadingRecords && viewModel.records.isEmpty {
                ProgressView()
            } else if viewModel.records.isEmpty {
                ContentUnavailableView("No Records", systemImage: "list.bullet", description: Text("No DNS records for this domain."))
            } else {
                List(viewModel.records) { record in
                    NavigationLink {
                        RecordEditView(domainName: domainName, record: record, viewModel: viewModel, client: client)
                    } label: {
                        RecordRow(record: record)
                    }
                }
                .listStyle(.insetGrouped)
            }
        }
        .navigationTitle("DNS Records")
        .navigationBarTitleDisplayMode(.inline)
        .toolbar {
            Button {
                showingAddRecord = true
            } label: {
                Label("Add Record", systemImage: "plus")
            }
        }
        .sheet(isPresented: $showingAddRecord) {
            NavigationStack {
                RecordAddView(domainName: domainName, viewModel: viewModel, client: client)
            }
        }
        .task {
            await viewModel.loadRecords(for: domainName, client: client)
        }
        .refreshable {
            await viewModel.loadRecords(for: domainName, client: client)
        }
        .alert("API Error", isPresented: errorBinding) {
            Button("OK", role: .cancel) {}
        } message: {
            Text(viewModel.errorMessage ?? "")
        }
    }

    private var errorBinding: Binding<Bool> {
        Binding(
            get: { viewModel.errorMessage != nil },
            set: { newValue in
                if !newValue {
                    viewModel.errorMessage = nil
                }
            }
        )
    }
}

private struct RecordRow: View {
    let record: DNSRecord

    var body: some View {
        VStack(alignment: .leading, spacing: 4) {
            Text("\(record.type) \(record.name)")
                .font(.headline)
            if let detail = [record.content, record.target].compactMap({ $0 }).first, !detail.isEmpty {
                Text(detail)
                    .font(.subheadline)
                    .foregroundStyle(.secondary)
            }
        }
        .padding(.vertical, 4)
    }
}