DomainDig/BatchResultsView.swift

22ecca12b4fe0e0850401754c674632f322d919d
domain-dig/DomainDig/BatchResultsView.swift history · blame · raw

126 lines · 4595 bytes

  1import SwiftUI
  2
  3struct BatchResultsView: View {
  4    @Bindable var viewModel: DomainViewModel
  5    let title: String
  6
  7    var body: some View {
  8        VStack(alignment: .leading, spacing: 12) {
  9            HStack(alignment: .top) {
 10                SectionTitleView(title: title)
 11                Spacer()
 12                if viewModel.batchLookupRunning {
 13                    VStack(alignment: .trailing, spacing: 4) {
 14                        ProgressView(value: Double(viewModel.batchCompletedCount), total: Double(max(viewModel.batchTotalCount, 1)))
 15                            .tint(.cyan)
 16                            .frame(width: 120)
 17                        Text(viewModel.batchProgressLabel)
 18                            .font(.system(.caption2, design: .monospaced))
 19                            .foregroundStyle(.secondary)
 20                    }
 21                } else if !viewModel.batchResults.isEmpty {
 22                    Text("\(viewModel.batchResults.count) domains")
 23                        .font(.system(.caption2, design: .monospaced))
 24                        .foregroundStyle(.secondary)
 25                }
 26            }
 27
 28            if viewModel.batchResults.isEmpty {
 29                MessageCardView(text: "No batch results yet", isError: false)
 30            } else {
 31                CardView(allowsHorizontalScroll: false) {
 32                    ForEach(viewModel.batchResults) { result in
 33                        if let entry = viewModel.historyEntry(for: result) {
 34                            NavigationLink {
 35                                HistoryDetailView(viewModel: viewModel, entry: entry)
 36                            } label: {
 37                                BatchResultRowView(result: result)
 38                            }
 39                            .buttonStyle(.plain)
 40                        } else {
 41                            BatchResultRowView(result: result)
 42                        }
 43                    }
 44                }
 45            }
 46        }
 47    }
 48}
 49
 50struct BatchResultRowView: View {
 51    let result: BatchLookupResult
 52
 53    var body: some View {
 54        VStack(alignment: .leading, spacing: 6) {
 55            HStack(alignment: .firstTextBaseline, spacing: 8) {
 56                Text(result.domain)
 57                    .font(.system(.callout, design: .monospaced))
 58                    .foregroundStyle(.primary)
 59                    .lineLimit(1)
 60                Spacer(minLength: 8)
 61                Text(result.quickStatus)
 62                    .font(.system(.caption2, design: .monospaced))
 63                    .foregroundStyle(quickStatusColor)
 64                    .padding(.horizontal, 8)
 65                    .padding(.vertical, 4)
 66                    .background(quickStatusColor.opacity(0.16))
 67                    .clipShape(Capsule())
 68            }
 69
 70            HStack(spacing: 10) {
 71                Text(availabilityText)
 72                Text(result.primaryIP ?? "No IP")
 73                Text(result.timestamp.formatted(date: .abbreviated, time: .shortened))
 74            }
 75            .font(.system(.caption2, design: .monospaced))
 76            .foregroundStyle(.secondary)
 77
 78            if let summaryMessage = result.summaryMessage {
 79                Text(summaryMessage)
 80                    .font(.system(.caption2, design: .monospaced))
 81                    .foregroundStyle(.secondary)
 82            }
 83
 84            if let errorMessage = result.errorMessage {
 85                Text(errorMessage)
 86                    .font(.system(.caption2, design: .monospaced))
 87                    .foregroundStyle(.red)
 88            }
 89        }
 90        .frame(maxWidth: .infinity, alignment: .leading)
 91        .padding(.vertical, 4)
 92    }
 93
 94    private var availabilityText: String {
 95        switch result.availability {
 96        case .available:
 97            return "Available"
 98        case .registered:
 99            return "Registered"
100        case .unknown, .none:
101            return "Unknown"
102        }
103    }
104
105    private var quickStatusColor: Color {
106        switch result.status {
107        case .pending:
108            return .secondary
109        case .running:
110            return .cyan
111        case .completed:
112            if result.changeSeverity == .high || result.certificateWarningLevel == .critical {
113                return .red
114            }
115            if result.changeSeverity == .medium || result.certificateWarningLevel == .warning {
116                return .yellow
117            }
118            if result.quickStatus == "Changed" {
119                return .blue
120            }
121            return .green
122        case .failed:
123            return .red
124        }
125    }
126}