DomainDig/BatchResultsView.swift
114 lines · 4091 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 errorMessage = result.errorMessage {
79 Text(errorMessage)
80 .font(.system(.caption2, design: .monospaced))
81 .foregroundStyle(.red)
82 }
83 }
84 .frame(maxWidth: .infinity, alignment: .leading)
85 .padding(.vertical, 4)
86 }
87
88 private var availabilityText: String {
89 switch result.availability {
90 case .available:
91 return "Available"
92 case .registered:
93 return "Registered"
94 case .unknown, .none:
95 return "Unknown"
96 }
97 }
98
99 private var quickStatusColor: Color {
100 switch result.status {
101 case .pending:
102 return .secondary
103 case .running:
104 return .cyan
105 case .completed:
106 if result.quickStatus == "Changed" {
107 return .yellow
108 }
109 return .green
110 case .failed:
111 return .red
112 }
113 }
114}