DomainDig/DNSSectionView.swift
168 lines · 6840 bytes
1import SwiftUI
2
3// One inspection section: the DNS records card. Pure presentation over the
4// view model's report data; the shared primitives it builds on (CardView,
5// SectionTitleView, LabeledValueRow, ResultColors, appLoadingStyle) live in
6// ResultPrimitives.swift.
7
8struct DNSSectionView: View {
9 @Environment(\.appDensity) private var appDensity
10 @Binding var isCollapsed: Bool
11 let dnssecLabel: String?
12 let patternSummary: DNSPatternSummary?
13 let sections: [DNSRecordSectionViewData]
14 let ptrMessage: SectionMessageViewData?
15 let loading: Bool
16 let dnsProvenance: SectionProvenance?
17 let ptrProvenance: SectionProvenance?
18 let sectionError: String?
19 let history: [DNSHistoryEvent]
20 let historyLoading: Bool
21 let historyError: String?
22 let onLoadHistory: (() -> Void)?
23
24 init(
25 isCollapsed: Binding<Bool>,
26 dnssecLabel: String?,
27 patternSummary: DNSPatternSummary?,
28 sections: [DNSRecordSectionViewData],
29 ptrMessage: SectionMessageViewData?,
30 loading: Bool,
31 dnsProvenance: SectionProvenance?,
32 ptrProvenance: SectionProvenance?,
33 sectionError: String?,
34 history: [DNSHistoryEvent] = [],
35 historyLoading: Bool = false,
36 historyError: String? = nil,
37 onLoadHistory: (() -> Void)? = nil
38 ) {
39 _isCollapsed = isCollapsed
40 self.dnssecLabel = dnssecLabel
41 self.patternSummary = patternSummary
42 self.sections = sections
43 self.ptrMessage = ptrMessage
44 self.loading = loading
45 self.dnsProvenance = dnsProvenance
46 self.ptrProvenance = ptrProvenance
47 self.sectionError = sectionError
48 self.history = history
49 self.historyLoading = historyLoading
50 self.historyError = historyError
51 self.onLoadHistory = onLoadHistory
52 }
53
54 var body: some View {
55 CollapsibleSectionView(title: "DNS", isCollapsed: $isCollapsed, subtitle: dnssecLabel) {
56 if loading {
57 LoadingCardView(text: "Querying DNS…")
58 } else if let sectionError, sections.isEmpty {
59 MessageCardView(text: sectionError, isError: true)
60 } else {
61 provenanceCard
62 ForEach(sections) { section in
63 CardView {
64 Text(section.title)
65 .font(.system(.subheadline, design: .monospaced))
66 .fontWeight(.semibold)
67 .foregroundStyle(Color(.statusInfo))
68
69 if let message = section.message {
70 MessageRowView(text: message.text, isError: message.isError)
71 }
72
73 ForEach(section.rows) { row in
74 LabeledValueRow(row: row)
75 }
76
77 if let wildcardTitle = section.wildcardTitle {
78 Text(wildcardTitle)
79 .font(.system(.caption, design: .monospaced))
80 .foregroundStyle(Color(.appTextSecondary))
81 .padding(.top, 4)
82 ForEach(section.wildcardRows) { row in
83 LabeledValueRow(row: row)
84 }
85 }
86 }
87 }
88
89 if let ptrMessage {
90 CardView {
91 Text("PTR")
92 .font(.system(.subheadline, design: .monospaced))
93 .fontWeight(.semibold)
94 .foregroundStyle(Color(.statusInfo))
95 SectionTrustMetadataView(provenance: ptrProvenance, confidence: nil)
96 MessageRowView(text: ptrMessage.text, isError: ptrMessage.isError)
97 }
98 }
99
100 CardView(allowsHorizontalScroll: false) {
101 HStack {
102 Text("History")
103 .font(appDensity.font(.subheadline, weight: .semibold))
104 .foregroundStyle(Color(.statusInfo))
105 Spacer()
106 if let onLoadHistory, history.isEmpty, !historyLoading {
107 Button("Load") {
108 onLoadHistory()
109 }
110 .buttonStyle(.bordered)
111 .font(appDensity.font(.caption2))
112 }
113 }
114 historyContent
115 }
116 }
117 }
118 }
119
120 @ViewBuilder
121 private var historyContent: some View {
122 if historyLoading {
123 ProgressView("Loading DNS history…")
124 .appLoadingStyle()
125 } else if !history.isEmpty {
126 ForEach(history) { event in
127 VStack(alignment: .leading, spacing: 3) {
128 Text(event.date.formatted(date: .abbreviated, time: .omitted))
129 .font(appDensity.font(.caption2))
130 .foregroundStyle(Color(.appTextSecondary))
131 Text(event.summary)
132 .font(appDensity.font(.caption))
133 if !event.aRecords.isEmpty {
134 Text("A: \(event.aRecords.joined(separator: ", "))")
135 .font(appDensity.font(.caption2))
136 .foregroundStyle(Color(.appTextSecondary))
137 }
138 if !event.nameservers.isEmpty {
139 Text("NS: \(event.nameservers.joined(separator: ", "))")
140 .font(appDensity.font(.caption2))
141 .foregroundStyle(Color(.appTextSecondary))
142 }
143 }
144 }
145 } else if let historyError {
146 MessageRowView(text: historyError, isError: false)
147 }
148 }
149
150 @ViewBuilder
151 private var provenanceCard: some View {
152 if dnsProvenance != nil {
153 CardView(allowsHorizontalScroll: false) {
154 SectionTrustMetadataView(provenance: dnsProvenance, confidence: nil)
155 if let patternSummary {
156 if !patternSummary.providers.isEmpty {
157 MessageRowView(text: "Providers: \(patternSummary.providers.joined(separator: ", "))", isError: false)
158 }
159 if !patternSummary.patterns.isEmpty {
160 ForEach(Array(patternSummary.patterns.enumerated()), id: \.offset) { _, pattern in
161 MessageRowView(text: pattern, isError: false)
162 }
163 }
164 }
165 }
166 }
167 }
168}