summaryrefslogtreecommitdiff
path: root/DomainDig/BatchResultsView.swift
blob: 51f6415f19e5244837cdbb2d3f1b88b288ed8f24 (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
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(.cyan)
                            .frame(width: 120)
                        Text(viewModel.batchProgressLabel)
                            .font(appDensity.font(.caption2))
                            .foregroundStyle(.secondary)
                    }
                } else if !viewModel.batchResults.isEmpty {
                    Text("\(viewModel.batchResults.count) domains")
                        .font(appDensity.font(.caption2))
                        .foregroundStyle(.secondary)
                }
            }

            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) {
            HStack(alignment: .firstTextBaseline, spacing: 8) {
                Text(result.domain)
                    .font(appDensity.font(.callout))
                    .foregroundStyle(.primary)
                    .lineLimit(1)
                Spacer(minLength: 8)
                Text(result.resultSource.label.lowercased())
                    .font(appDensity.font(.caption2))
                    .foregroundStyle(.secondary)
                AppStatusBadgeView(model: quickStatusBadge)
            }

            HStack(spacing: 10) {
                AppStatusBadgeView(model: AppStatusFactory.availability(result.availability))
                Text(result.primaryIP ?? "No IP")
                Text(result.timestamp.formatted(date: .abbreviated, time: .shortened))
            }
            .font(appDensity.font(.caption2))
            .foregroundStyle(.secondary)

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

            if let errorMessage = result.errorMessage {
                Text(errorMessage)
                    .font(appDensity.font(.caption2))
                    .foregroundStyle(result.status == .failed ? .red : .secondary)
            }
        }
        .frame(maxWidth: .infinity, alignment: .leading)
        .padding(.vertical, 4)
        .frame(minHeight: appDensity.metrics.rowMinHeight + 12, alignment: .topLeading)
    }

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

    private var quickStatusBadge: AppStatusBadgeModel {
        switch result.status {
        case .pending:
            return .init(title: "Pending", systemImage: "clock", foregroundColor: .secondary, backgroundColor: Color(.systemGray5).opacity(0.55))
        case .running:
            return .init(title: "Running", systemImage: "arrow.clockwise", foregroundColor: .cyan, backgroundColor: .cyan.opacity(0.16))
        case .completed:
            if result.changeSeverity == .high || result.certificateWarningLevel == .critical {
                return .init(title: "High", systemImage: "exclamationmark.octagon.fill", foregroundColor: .red, backgroundColor: .red.opacity(0.16))
            }
            if result.changeSeverity == .medium || result.certificateWarningLevel == .warning {
                return .init(title: "Warning", systemImage: "exclamationmark.triangle.fill", foregroundColor: .yellow, backgroundColor: .yellow.opacity(0.16))
            }
            if result.quickStatus == "Changed" {
                return .init(title: "Changed", systemImage: "arrow.triangle.2.circlepath", foregroundColor: .cyan, backgroundColor: .cyan.opacity(0.16))
            }
            return .init(title: "Stable", systemImage: "checkmark.circle.fill", foregroundColor: .green, backgroundColor: .green.opacity(0.16))
        case .failed:
            return .init(title: "Failed", systemImage: "xmark.circle.fill", foregroundColor: .red, backgroundColor: .red.opacity(0.16))
        }
    }
}