DomainDig/OwnershipSectionView.swift
103 lines · 3848 bytes
1import SwiftUI
2
3// One inspection section: the registration and ownership card. Pure
4// presentation over the view model's report data; the shared primitives it
5// builds on (CardView, SectionTitleView, LabeledValueRow, ResultColors,
6// appLoadingStyle) live in ResultPrimitives.swift.
7
8struct OwnershipSectionView: View {
9 @Environment(\.appDensity) private var appDensity
10 @Binding var isCollapsed: Bool
11 let rows: [InfoRowViewData]
12 let loading: Bool
13 let error: String?
14 let provenance: SectionProvenance?
15 let confidence: ConfidenceLevel?
16 let history: [DomainOwnershipHistoryEvent]
17 let historyLoading: Bool
18 let historyError: String?
19 let onLoadHistory: (() -> Void)?
20
21 init(
22 isCollapsed: Binding<Bool>,
23 rows: [InfoRowViewData],
24 loading: Bool,
25 error: String?,
26 provenance: SectionProvenance?,
27 confidence: ConfidenceLevel?,
28 history: [DomainOwnershipHistoryEvent] = [],
29 historyLoading: Bool = false,
30 historyError: String? = nil,
31 onLoadHistory: (() -> Void)? = nil
32 ) {
33 _isCollapsed = isCollapsed
34 self.rows = rows
35 self.loading = loading
36 self.error = error
37 self.provenance = provenance
38 self.confidence = confidence
39 self.history = history
40 self.historyLoading = historyLoading
41 self.historyError = historyError
42 self.onLoadHistory = onLoadHistory
43 }
44
45 var body: some View {
46 CollapsibleSectionView(title: "Ownership", isCollapsed: $isCollapsed) {
47 CardView(allowsHorizontalScroll: false) {
48 SectionTrustMetadataView(provenance: provenance, confidence: confidence)
49 if loading {
50 ProgressView("Fetching RDAP ownership…")
51 .appLoadingStyle()
52 } else {
53 ForEach(rows) { row in
54 LabeledValueRow(row: row)
55 }
56 if let error, rows.allSatisfy({ $0.value == "Unavailable" }) {
57 MessageRowView(text: error, isError: error != "Unavailable")
58 .padding(.top, 4)
59 }
60 historyBlock
61 }
62 }
63 }
64 }
65
66 @ViewBuilder
67 private var historyBlock: some View {
68 VStack(alignment: .leading, spacing: 8) {
69 HStack {
70 Text("History")
71 .font(appDensity.font(.caption))
72 .foregroundStyle(Color(.appTextSecondary))
73 Spacer()
74 if let onLoadHistory, history.isEmpty, !historyLoading {
75 Button("Load") {
76 onLoadHistory()
77 }
78 .buttonStyle(.bordered)
79 .font(appDensity.font(.caption2))
80 }
81 }
82 if historyLoading {
83 ProgressView("Loading history…")
84 .appLoadingStyle()
85 } else if !history.isEmpty {
86 ForEach(history) { event in
87 VStack(alignment: .leading, spacing: 3) {
88 Text(event.date.formatted(date: .abbreviated, time: .omitted))
89 .font(appDensity.font(.caption2))
90 .foregroundStyle(Color(.appTextSecondary))
91 Text(event.summary)
92 .font(appDensity.font(.caption))
93 Text(event.source)
94 .font(appDensity.font(.caption2))
95 .foregroundStyle(Color(.appTextSecondary))
96 }
97 }
98 } else if let historyError {
99 MessageRowView(text: historyError, isError: false)
100 }
101 }
102 }
103}