DomainDig/HistoryView.swift

397c7c420c22fc1c235893bd85b2029da9145730
domain-dig/DomainDig/HistoryView.swift history · blame · raw

174 lines · 7330 bytes

  1import SwiftUI
  2
  3struct HistoryView: View {
  4    @Bindable var viewModel: DomainViewModel
  5
  6    private let dateFormatter: DateFormatter = {
  7        let formatter = DateFormatter()
  8        formatter.dateStyle = .medium
  9        formatter.timeStyle = .short
 10        return formatter
 11    }()
 12
 13    var body: some View {
 14        List {
 15            if viewModel.history.isEmpty {
 16                Text("No lookup history")
 17                    .font(.system(.callout, design: .monospaced))
 18                    .foregroundStyle(.secondary)
 19                    .listRowBackground(Color(.systemGray6).opacity(0.5))
 20            } else {
 21                ForEach(viewModel.history) { entry in
 22                    NavigationLink {
 23                        HistoryDetailView(viewModel: viewModel, entry: entry)
 24                    } label: {
 25                        VStack(alignment: .leading, spacing: 4) {
 26                            Text(entry.domain)
 27                                .font(.system(.callout, design: .monospaced))
 28                                .foregroundStyle(.primary)
 29                            HStack(spacing: 8) {
 30                                Text(dateFormatter.string(from: entry.timestamp))
 31                                Text("Snapshot")
 32                                Text(entry.resolverDisplayName)
 33                                if let totalLookupDurationMs = entry.totalLookupDurationMs {
 34                                    Text("\(totalLookupDurationMs) ms")
 35                                }
 36                            }
 37                            .font(.system(.caption2, design: .monospaced))
 38                            .foregroundStyle(.secondary)
 39                        }
 40                    }
 41                    .listRowBackground(Color(.systemGray6).opacity(0.5))
 42                }
 43                .onDelete { offsets in
 44                    viewModel.removeHistoryEntries(at: offsets)
 45                }
 46            }
 47        }
 48        .scrollContentBackground(.hidden)
 49        .background(Color.black)
 50        .navigationTitle("History")
 51        .toolbar {
 52            if !viewModel.history.isEmpty {
 53                EditButton()
 54            }
 55        }
 56        .preferredColorScheme(.dark)
 57    }
 58}
 59
 60struct HistoryDetailView: View {
 61    @Bindable var viewModel: DomainViewModel
 62    let entry: HistoryEntry
 63
 64    private let dateFormatter: DateFormatter = {
 65        let formatter = DateFormatter()
 66        formatter.dateStyle = .medium
 67        formatter.timeStyle = .short
 68        return formatter
 69    }()
 70
 71    private var snapshot: LookupSnapshot {
 72        entry.snapshot
 73    }
 74
 75    var body: some View {
 76        ScrollView(.vertical) {
 77            VStack(alignment: .leading, spacing: 0) {
 78                snapshotBanner
 79                SummaryView(fields: DomainViewModel.summaryFields(from: snapshot))
 80                    .padding(.top, 8)
 81                DomainSectionView(
 82                    rows: DomainViewModel.domainRows(from: snapshot),
 83                    suggestions: DomainViewModel.suggestionRows(from: snapshot),
 84                    showSuggestions: entry.availabilityResult?.status == .registered && !entry.suggestions.isEmpty,
 85                    availabilityLoading: false,
 86                    suggestionsLoading: false,
 87                    isWatched: viewModel.watchedDomains.contains(where: { $0.domain.lowercased() == entry.domain.lowercased() }),
 88                    onToggleWatch: {
 89                        viewModel.toggleWatchedDomain(domain: entry.domain, availabilityStatus: entry.availabilityResult?.status)
 90                    }
 91                )
 92                    .padding(.top, 16)
 93                DNSSectionView(
 94                    dnssecLabel: DomainViewModel.dnssecLabel(from: snapshot),
 95                    sections: DomainViewModel.dnsRows(from: snapshot),
 96                    ptrMessage: DomainViewModel.ptrMessage(from: snapshot),
 97                    loading: false,
 98                    sectionError: snapshot.dnsError
 99                )
100                .padding(.top, 16)
101                WebSectionView(
102                    certificateRows: DomainViewModel.webCertificateRows(from: snapshot),
103                    sslInfo: snapshot.sslInfo,
104                    sslLoading: false,
105                    sslError: snapshot.sslError,
106                    responseRows: DomainViewModel.webResponseRows(from: snapshot),
107                    headers: snapshot.httpHeaders,
108                    headersLoading: false,
109                    headersError: snapshot.httpHeadersError,
110                    redirects: DomainViewModel.redirectRows(from: snapshot),
111                    redirectLoading: false,
112                    redirectError: snapshot.redirectChainError,
113                    finalURL: snapshot.redirectChain.last?.url
114                )
115                .padding(.top, 16)
116                EmailSectionView(
117                    rows: DomainViewModel.emailRows(from: snapshot),
118                    loading: false,
119                    error: snapshot.emailSecurityError
120                )
121                .padding(.top, 16)
122                NetworkSectionView(
123                    reachabilityRows: DomainViewModel.reachabilityRows(from: snapshot),
124                    reachabilityLoading: false,
125                    reachabilityError: snapshot.reachabilityError,
126                    locationRows: DomainViewModel.locationRows(from: snapshot),
127                    geolocation: snapshot.ipGeolocation,
128                    geolocationLoading: false,
129                    geolocationError: snapshot.ipGeolocationError,
130                    standardPortRows: DomainViewModel.portRows(from: snapshot, kind: .standard),
131                    customPortRows: DomainViewModel.portRows(from: snapshot, kind: .custom),
132                    portScanLoading: false,
133                    portScanError: snapshot.portScanError,
134                    customPortScanLoading: false,
135                    customPortScanError: nil,
136                    isCloudflareProxied: snapshot.httpHeaders.contains(where: { $0.name.lowercased() == "cf-ray" }),
137                    customPortsExpanded: .constant(false),
138                    customPortInput: .constant(""),
139                    onScanCustomPorts: {}
140                )
141                .padding(.top, 16)
142            }
143            .padding(.horizontal)
144            .padding(.bottom, 32)
145        }
146        .background(Color.black)
147        .navigationTitle(entry.domain)
148        .toolbar {
149            Button("Re-run") {
150                viewModel.rerunLookup(from: entry)
151            }
152        }
153        .preferredColorScheme(.dark)
154    }
155
156    private var snapshotBanner: some View {
157        HStack(spacing: 8) {
158            Image(systemName: "archivebox")
159                .font(.caption)
160            Text("Snapshot from \(dateFormatter.string(from: entry.timestamp))")
161                .font(.system(.caption, design: .monospaced))
162            Spacer()
163            Text("Live re-run available")
164                .font(.system(.caption2, design: .monospaced))
165                .foregroundStyle(.secondary)
166        }
167        .foregroundStyle(.secondary)
168        .padding(8)
169        .frame(maxWidth: .infinity, alignment: .leading)
170        .background(Color(.systemGray6).opacity(0.3))
171        .cornerRadius(6)
172        .padding(.vertical, 12)
173    }
174}