summaryrefslogtreecommitdiff
path: root/DomainDig/BatchResultsView.swift
blob: 1636a169f904286220984091aee26d542c52b9f0 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import SwiftUI

struct BatchResultsView: View {
    @Environment(\.appDensity) private var appDensity
    @Bindable var viewModel: DomainViewModel
    let title: String

    var body: some View {
        VStack(alignment: .leading, spacing: appDensity.metrics.cardSpacing) {
            HStack(alignment: .top) {
                SectionTitleView(title: title)
                Spacer()
                if viewModel.batchLookupRunning {
                    VStack(alignment: .trailing, spacing: 4) {
                        ProgressView(value: Double(viewModel.batchCompletedCount), total: Double(max(viewModel.batchTotalCount, 1)))
                            .tint(Color(.statusInfo))
                            .frame(width: 120)
                        Text(viewModel.batchProgressLabel)
                            .font(appDensity.font(.caption2))
                            .foregroundStyle(Color(.appTextSecondary))
                    }
                } else if !viewModel.batchResults.isEmpty {
                    Text("\(viewModel.batchResults.count) domains")
                        .font(appDensity.font(.caption2))
                        .foregroundStyle(Color(.appTextSecondary))
                }
            }

            if viewModel.batchResults.isEmpty {
                EmptyStateCardView(
                    title: "No Batch Results Yet",
                    message: "Batch runs collect availability, IP, and change status for multiple domains in one pass.",
                    suggestion: "Switch to Bulk mode, paste a list of domains, then run a batch lookup.",
                    systemImage: "square.stack.3d.up"
                )
            } else {
                CardView(allowsHorizontalScroll: false) {
                    ForEach(viewModel.batchResults) { result in
                        if let entry = viewModel.historyEntry(for: result) {
                            NavigationLink {
                                HistoryDetailView(viewModel: viewModel, entry: entry)
                            } label: {
                                BatchResultRowView(result: result)
                            }
                            .buttonStyle(.plain)
                        } else {
                            BatchResultRowView(result: result)
                        }
                    }
                }
            }
        }
    }
}

struct BatchResultRowView: View {
    @Environment(\.appDensity) private var appDensity
    let result: BatchLookupResult

    var body: some View {
        VStack(alignment: .leading, spacing: appDensity.metrics.rowSpacing + 1) {
            // Same reflow as WatchlistRowView: wide while it fits, stacked at
            // accessibility sizes so the badge cannot letter-wrap vertically.
            ViewThatFits(in: .horizontal) {
                HStack(alignment: .firstTextBaseline, spacing: 8) {
                    domainTitle
                    Spacer(minLength: 8)
                    sourceLabel
                    AppStatusBadgeView(model: quickStatusBadge)
                }
                VStack(alignment: .leading, spacing: 6) {
                    domainTitle
                    HStack(spacing: 8) {
                        AppStatusBadgeView(model: quickStatusBadge)
                        sourceLabel
                    }
                }
            }

            HStack(spacing: 10) {
                AppStatusBadgeView(model: availabilityBadgeModel)
                if let riskScore = result.riskScore, let riskLevel = result.riskLevel {
                    Text("Risk \(riskScore) \(riskLevel.title)")
                        .lineLimit(1)
                        .minimumScaleFactor(0.85)
                }
            }
            .font(appDensity.font(.caption2))
            .foregroundStyle(Color(.appTextSecondary))

            HStack(spacing: 10) {
                Text(result.primaryIP ?? "No IP")
                    .lineLimit(1)
                    .truncationMode(.middle)

                Spacer(minLength: 8)

                Text(result.timestamp.formatted(date: .abbreviated, time: .shortened))
                    .lineLimit(1)
            }
            .font(appDensity.font(.caption2))
            .foregroundStyle(Color(.appTextSecondary))

            if let summaryMessage = result.summaryMessage {
                Text(summaryMessage)
                    .font(appDensity.font(.caption2))
                    .foregroundStyle(Color(.appTextSecondary))
            }

            if let changeClassification = result.changeClassification {
                Text("Impact: \(changeClassification.title)")
                    .font(appDensity.font(.caption2))
                    .foregroundStyle(changeClassification.tone.foreground)
            }

            if let errorMessage = result.errorMessage {
                Text(errorMessage)
                    .font(appDensity.font(.caption2))
                    .foregroundStyle(result.status == .failed ? Color(.statusCritical) : Color(.appTextSecondary))
            }
        }
        .frame(maxWidth: .infinity, alignment: .leading)
        .padding(.vertical, 4)
        .frame(minHeight: appDensity.metrics.rowMinHeight + 12, alignment: .topLeading)
        // One VoiceOver stop per row: domain as the label, status as the value,
        // everything else on the More Content rotor. Reading all eight text
        // elements inline would make a 200-domain sweep unnavigable. `.high`
        // importance is spoken without the rotor; the rest waits for a swipe.
        // Extracted to a modifier — inlined, the chain broke the type-checker.
        .modifier(BatchRowAccessibility(
            domain: result.domain,
            status: "\(quickStatusBadge.title), \(availabilityText)",
            risk: riskDescription,
            ip: result.primaryIP ?? "none",
            checked: result.timestamp.formatted(date: .abbreviated, time: .shortened),
            source: result.resultSource.label,
            changeLabel: changeContentLabel,
            changeValue: changeContentValue
        ))
    }

    private var domainTitle: some View {
        Text(result.domain)
            .font(appDensity.font(.callout))
            .foregroundStyle(.primary)
            .lineLimit(3)
            .multilineTextAlignment(.leading)
            .fixedSize(horizontal: false, vertical: true)
    }

    private var sourceLabel: some View {
        Text(result.resultSource.label.lowercased())
            .font(appDensity.font(.caption2))
            .foregroundStyle(Color(.appTextSecondary))
    }

    private var changeContentLabel: String {
        result.changeClassification != nil ? "Impact" : "Status"
    }

    private var changeContentValue: String {
        if let change = result.changeClassification {
            return change.title
        }
        return result.errorMessage ?? result.summaryMessage ?? quickStatusBadge.title
    }

    private var riskDescription: String {
        if let score = result.riskScore, let level = result.riskLevel {
            return "\(score), \(level.title)"
        }
        return "not scored"
    }

    private var availabilityText: String {
        switch result.availability {
        case .available:
            return "Available"
        case .registered:
            return "Registered"
        case .unknown, .none:
            return "Unknown"
        }
    }

    private var availabilityBadgeModel: AppStatusBadgeModel {
        guard result.status == .failed else {
            return AppStatusFactory.availability(result.availability)
        }

        return .init(
            title: availabilityText,
            systemImage: "exclamationmark.circle",
            foregroundColor: Color(.appTextSecondary),
            backgroundColor: Color(.appSurfaceElevated)
        )
    }

    private var quickStatusBadge: AppStatusBadgeModel {
        switch result.status {
        case .pending:
            return .init(title: "Pending", systemImage: "clock", foregroundColor: Color(.appTextSecondary), backgroundColor: Color(.appSurfaceElevated))
        case .running:
            return .init(title: "Running", systemImage: "arrow.clockwise", foregroundColor: Color(.statusInfo), backgroundColor: Color(.statusInfoSurface))
        case .completed:
            if result.changeClassification == .critical || result.certificateWarningLevel == .critical || result.riskLevel == .high {
                return .init(title: "Critical", systemImage: "exclamationmark.octagon.fill", foregroundColor: Color(.statusCritical), backgroundColor: Color(.statusCriticalSurface))
            }
            if result.changeClassification == .warning || result.changeSeverity == .medium || result.certificateWarningLevel == .warning {
                return .init(title: "Warning", systemImage: "exclamationmark.triangle.fill", foregroundColor: Color(.statusWarning), backgroundColor: Color(.statusWarningSurface))
            }
            if result.quickStatus == "Changed" {
                return .init(title: "Changed", systemImage: "arrow.triangle.2.circlepath", foregroundColor: Color(.statusInfo), backgroundColor: Color(.statusInfoSurface))
            }
            return .init(title: "Stable", systemImage: "checkmark.circle.fill", foregroundColor: Color(.statusPositive), backgroundColor: Color(.statusPositiveSurface))
        case .failed:
            return .init(title: "Failed", systemImage: "xmark.circle.fill", foregroundColor: Color(.statusCritical), backgroundColor: Color(.statusCriticalSurface))
        }
    }
}

/// Row-level VoiceOver treatment for a batch result: a single element whose
/// label is the domain and whose value is the status, with the remaining fields
/// on the More Content rotor. Extracted from the row body because inlining the
/// full modifier chain broke Swift's type-checker.
private struct BatchRowAccessibility: ViewModifier {
    let domain: String
    let status: String
    let risk: String
    let ip: String
    let checked: String
    let source: String
    let changeLabel: String
    let changeValue: String

    func body(content: Content) -> some View {
        content
            .accessibilityElement(children: .ignore)
            .accessibilityLabel(domain)
            .accessibilityValue(status)
            .accessibilityCustomContent("Risk", risk, importance: .high)
            .accessibilityCustomContent("IP address", ip)
            .accessibilityCustomContent("Checked", checked)
            .accessibilityCustomContent("Source", source)
            .accessibilityCustomContent(LocalizedStringResource(stringLiteral: changeLabel), changeValue)
    }
}