DomainDig/BatchResultsView.swift

36ea668d7fc9070770973b0bc329f4319bf43cab
domain-dig/DomainDig/BatchResultsView.swift history · blame · raw

131 lines · 5888 bytes

  1import SwiftUI
  2
  3struct BatchResultsView: View {
  4    @Environment(\.appDensity) private var appDensity
  5    @Bindable var viewModel: DomainViewModel
  6    let title: String
  7
  8    var body: some View {
  9        VStack(alignment: .leading, spacing: appDensity.metrics.cardSpacing) {
 10            HStack(alignment: .top) {
 11                SectionTitleView(title: title)
 12                Spacer()
 13                if viewModel.batchLookupRunning {
 14                    VStack(alignment: .trailing, spacing: 4) {
 15                        ProgressView(value: Double(viewModel.batchCompletedCount), total: Double(max(viewModel.batchTotalCount, 1)))
 16                            .tint(.cyan)
 17                            .frame(width: 120)
 18                        Text(viewModel.batchProgressLabel)
 19                            .font(appDensity.font(.caption2))
 20                            .foregroundStyle(.secondary)
 21                    }
 22                } else if !viewModel.batchResults.isEmpty {
 23                    Text("\(viewModel.batchResults.count) domains")
 24                        .font(appDensity.font(.caption2))
 25                        .foregroundStyle(.secondary)
 26                }
 27            }
 28
 29            if viewModel.batchResults.isEmpty {
 30                EmptyStateCardView(
 31                    title: "No Batch Results Yet",
 32                    message: "Batch runs collect availability, IP, and change status for multiple domains in one pass.",
 33                    suggestion: "Switch to Bulk mode, paste a list of domains, then run a batch lookup.",
 34                    systemImage: "square.stack.3d.up"
 35                )
 36            } else {
 37                CardView(allowsHorizontalScroll: false) {
 38                    ForEach(viewModel.batchResults) { result in
 39                        if let entry = viewModel.historyEntry(for: result) {
 40                            NavigationLink {
 41                                HistoryDetailView(viewModel: viewModel, entry: entry)
 42                            } label: {
 43                                BatchResultRowView(result: result)
 44                            }
 45                            .buttonStyle(.plain)
 46                        } else {
 47                            BatchResultRowView(result: result)
 48                        }
 49                    }
 50                }
 51            }
 52        }
 53    }
 54}
 55
 56struct BatchResultRowView: View {
 57    @Environment(\.appDensity) private var appDensity
 58    let result: BatchLookupResult
 59
 60    var body: some View {
 61        VStack(alignment: .leading, spacing: appDensity.metrics.rowSpacing + 1) {
 62            HStack(alignment: .firstTextBaseline, spacing: 8) {
 63                Text(result.domain)
 64                    .font(appDensity.font(.callout))
 65                    .foregroundStyle(.primary)
 66                    .lineLimit(1)
 67                Spacer(minLength: 8)
 68                Text(result.resultSource.label.lowercased())
 69                    .font(appDensity.font(.caption2))
 70                    .foregroundStyle(.secondary)
 71                AppStatusBadgeView(model: quickStatusBadge)
 72            }
 73
 74            HStack(spacing: 10) {
 75                AppStatusBadgeView(model: AppStatusFactory.availability(result.availability))
 76                Text(result.primaryIP ?? "No IP")
 77                Text(result.timestamp.formatted(date: .abbreviated, time: .shortened))
 78            }
 79            .font(appDensity.font(.caption2))
 80            .foregroundStyle(.secondary)
 81
 82            if let summaryMessage = result.summaryMessage {
 83                Text(summaryMessage)
 84                    .font(appDensity.font(.caption2))
 85                    .foregroundStyle(.secondary)
 86            }
 87
 88            if let errorMessage = result.errorMessage {
 89                Text(errorMessage)
 90                    .font(appDensity.font(.caption2))
 91                    .foregroundStyle(result.status == .failed ? .red : .secondary)
 92            }
 93        }
 94        .frame(maxWidth: .infinity, alignment: .leading)
 95        .padding(.vertical, 4)
 96        .frame(minHeight: appDensity.metrics.rowMinHeight + 12, alignment: .topLeading)
 97    }
 98
 99    private var availabilityText: String {
100        switch result.availability {
101        case .available:
102            return "Available"
103        case .registered:
104            return "Registered"
105        case .unknown, .none:
106            return "Unknown"
107        }
108    }
109
110    private var quickStatusBadge: AppStatusBadgeModel {
111        switch result.status {
112        case .pending:
113            return .init(title: "Pending", systemImage: "clock", foregroundColor: .secondary, backgroundColor: Color(.systemGray5).opacity(0.55))
114        case .running:
115            return .init(title: "Running", systemImage: "arrow.clockwise", foregroundColor: .cyan, backgroundColor: .cyan.opacity(0.16))
116        case .completed:
117            if result.changeSeverity == .high || result.certificateWarningLevel == .critical {
118                return .init(title: "High", systemImage: "exclamationmark.octagon.fill", foregroundColor: .red, backgroundColor: .red.opacity(0.16))
119            }
120            if result.changeSeverity == .medium || result.certificateWarningLevel == .warning {
121                return .init(title: "Warning", systemImage: "exclamationmark.triangle.fill", foregroundColor: .yellow, backgroundColor: .yellow.opacity(0.16))
122            }
123            if result.quickStatus == "Changed" {
124                return .init(title: "Changed", systemImage: "arrow.triangle.2.circlepath", foregroundColor: .cyan, backgroundColor: .cyan.opacity(0.16))
125            }
126            return .init(title: "Stable", systemImage: "checkmark.circle.fill", foregroundColor: .green, backgroundColor: .green.opacity(0.16))
127        case .failed:
128            return .init(title: "Failed", systemImage: "xmark.circle.fill", foregroundColor: .red, backgroundColor: .red.opacity(0.16))
129        }
130    }
131}