summaryrefslogtreecommitdiff
path: root/Rune/Views/Domains/RecordListView.swift
blob: 74ab28682daa4e586e78a84da48a15cfaea41ede (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import SwiftUI

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

    @State private var showingAddRecord = false

    var body: some View {
        List {
            if let errorMessage = viewModel.recordsErrorMessage {
                Section {
                    InlineErrorView(message: errorMessage, retryTitle: "Retry Records") {
                        Task {
                            await viewModel.loadRecords(for: domainName, client: client)
                        }
                    }
                    .listRowInsets(EdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16))
                }
            }

            if viewModel.isLoadingRecords && viewModel.records.isEmpty {
                Section {
                    HStack {
                        Spacer()
                        ProgressView("Loading Records")
                        Spacer()
                    }
                }
            } else if viewModel.records.isEmpty {
                Section {
                    ContentUnavailableView(
                        "No Records",
                        systemImage: "list.bullet",
                        description: Text("No DNS records for this domain yet. Add a record to get started.")
                    )
                }
            } else {
                ForEach(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)
        }
        .onAppear {
            viewModel.startAutoRefreshRecords(for: domainName, client: client)
        }
        .onDisappear {
            viewModel.stopAutoRefreshRecords()
        }
        .refreshable {
            await viewModel.loadRecords(for: domainName, client: client)
        }
        .alert("Request Failed", isPresented: mutationErrorBinding) {
            Button("OK", role: .cancel) {}
        } message: {
            Text(viewModel.mutationErrorMessage ?? "")
        }
    }

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

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)
    }
}