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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
import SwiftUI
struct HistoryView: View {
@Environment(\.appDensity) private var appDensity
@Bindable var viewModel: DomainViewModel
@Environment(\.dismiss) private var dismiss
@State private var showClearAllConfirmation = false
private var groupedHistory: [HistoryGroup] {
HistoryGroup.groups(for: viewModel.filteredHistory)
}
var body: some View {
List {
if viewModel.filteredHistory.isEmpty {
EmptyStateCardView(
title: "No History Yet",
message: "History stores local snapshots of previous inspections so you can revisit and compare them later.",
suggestion: "Run a lookup to create your first saved snapshot.",
systemImage: "clock.arrow.trianglehead.counterclockwise.rotate.90"
)
.listRowBackground(Color(.systemGray6).opacity(0.5))
} else {
ForEach(groupedHistory) { group in
Section(group.title) {
ForEach(group.entries) { entry in
NavigationLink {
HistoryDetailView(viewModel: viewModel, entry: entry)
} label: {
VStack(alignment: .leading, spacing: appDensity.metrics.rowSpacing + 1) {
HStack(alignment: .center, spacing: 8) {
Text(entry.domain)
.font(appDensity.font(.callout))
.foregroundStyle(.primary)
Spacer()
AppStatusBadgeView(model: AppStatusFactory.change(entry.changeSummary))
}
HStack(spacing: 8) {
AppStatusBadgeView(model: AppStatusFactory.availability(entry.availabilityResult?.status))
AppStatusBadgeView(model: AppStatusFactory.tls(sslInfo: entry.sslInfo, error: entry.sslError))
if entry.isPartialSnapshot {
AppStatusBadgeView(model: .init(title: "Partial", systemImage: "exclamationmark.triangle.fill", foregroundColor: .yellow, backgroundColor: .yellow.opacity(0.16)))
}
}
HStack(spacing: 8) {
Text(entry.timestamp.formatted(date: .abbreviated, time: .shortened))
Text(entry.timestamp.formatted(.relative(presentation: .named)))
Text(entry.resolverDisplayName)
if let totalLookupDurationMs = entry.totalLookupDurationMs {
Text("\(totalLookupDurationMs) ms")
}
}
.font(appDensity.font(.caption2))
.foregroundStyle(.secondary)
if let note = entry.note, !note.isEmpty {
Text(note)
.font(appDensity.font(.caption2))
.foregroundStyle(.secondary)
.lineLimit(1)
}
}
}
.swipeActions(edge: .trailing, allowsFullSwipe: true) {
Button(role: .destructive) {
viewModel.removeHistoryEntries(withIDs: [entry.id])
} label: {
Label("Delete", systemImage: "trash")
}
}
}
.listRowBackground(Color(.systemGray6).opacity(0.5))
}
}
}
}
.scrollContentBackground(.hidden)
.background(Color.black)
.navigationTitle("History")
.searchable(text: $viewModel.historySearchText, prompt: "Search domains")
.toolbar {
if !viewModel.history.isEmpty {
ToolbarItemGroup(placement: .topBarTrailing) {
Menu {
Picker("Date Range", selection: $viewModel.historyDateFilter) {
ForEach(HistoryDateFilter.allCases) { option in
Text(option.title).tag(option)
}
}
Picker("Change Filter", selection: $viewModel.historyChangeFilter) {
ForEach(ChangeFilterOption.allCases) { option in
Text(option.title).tag(option)
}
}
Picker("Sort", selection: $viewModel.historySortOption) {
ForEach(HistorySortOption.allCases) { option in
Text(option.title).tag(option)
}
}
Button("Clear All", role: .destructive) {
showClearAllConfirmation = true
}
} label: {
Image(systemName: "line.3.horizontal.decrease.circle")
}
EditButton()
}
}
}
.alert("Clear history?", isPresented: $showClearAllConfirmation) {
Button("Clear All", role: .destructive) {
viewModel.clearHistory()
}
Button("Cancel", role: .cancel) {}
} message: {
Text("This will delete all saved history entries. This cannot be undone.")
}
.onChange(of: viewModel.rerunNavigationToken) { _, _ in
dismiss()
}
.preferredColorScheme(.dark)
}
private func deleteFilteredHistoryEntries(at offsets: IndexSet) {
let ids = offsets.map { viewModel.filteredHistory[$0].id }
viewModel.removeHistoryEntries(withIDs: ids)
}
}
struct HistoryDetailView: View {
@Environment(\.appDensity) private var appDensity
@Bindable var viewModel: DomainViewModel
let entry: HistoryEntry
@Environment(\.dismiss) private var dismiss
@State private var noteDraft = ""
@State private var isEditingNote = false
@State private var showRerunOptions = false
private let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .short
return formatter
}()
private var snapshot: LookupSnapshot {
entry.snapshot
}
var body: some View {
ScrollView(.vertical) {
VStack(alignment: .leading, spacing: 0) {
snapshotBanner
SummaryView(fields: DomainViewModel.summaryFields(from: snapshot))
.padding(.top, 8)
DomainSectionView(
isCollapsed: .constant(false),
rows: DomainViewModel.domainRows(from: snapshot),
suggestions: DomainViewModel.suggestionRows(from: snapshot),
showSuggestions: entry.availabilityResult?.status == .registered && !entry.suggestions.isEmpty,
availabilityLoading: false,
suggestionsLoading: false,
provenance: snapshot.provenanceBySection[.availability],
confidence: snapshot.availabilityConfidence,
snapshotNote: entry.note,
trackedDomain: viewModel.trackedDomains.first(where: { $0.domain.lowercased() == entry.domain.lowercased() }),
trackingLimitMessage: nil,
onTrack: {
_ = viewModel.trackDomain(domain: entry.domain, availabilityStatus: entry.availabilityResult?.status)
},
onTogglePinned: {
guard let trackedDomain = viewModel.trackedDomains.first(where: { $0.domain.lowercased() == entry.domain.lowercased() }) else { return }
viewModel.togglePinned(for: trackedDomain)
},
onEditNote: nil
)
.padding(.top, appDensity.metrics.sectionSpacing)
OwnershipSectionView(
isCollapsed: .constant(false),
rows: DomainViewModel.ownershipRows(from: snapshot),
loading: false,
error: snapshot.ownershipError,
provenance: snapshot.provenanceBySection[.ownership],
confidence: snapshot.ownershipConfidence,
showsHistoryPlaceholder: !DataAccessService.hasAccess(to: .ownershipHistory)
)
.padding(.top, appDensity.metrics.sectionSpacing)
SubdomainsSectionView(
isCollapsed: .constant(false),
rows: DomainViewModel.subdomainRows(from: snapshot),
loading: false,
error: snapshot.subdomainsError,
provenance: snapshot.provenanceBySection[.subdomains],
confidence: snapshot.subdomainConfidence,
showsExtendedPlaceholder: !DataAccessService.hasAccess(to: .extendedSubdomains)
)
.padding(.top, appDensity.metrics.sectionSpacing)
if let comparisonSnapshot = viewModel.comparisonSnapshot(for: entry) {
if let changeSummary = entry.changeSummary {
DomainChangeSummaryView(summary: changeSummary)
.padding(.top, appDensity.metrics.sectionSpacing)
}
DomainDiffView(
title: "Compared With Previous Snapshot",
sections: DomainDiffService.diff(from: comparisonSnapshot, to: snapshot),
contextNote: DomainDiffService.comparisonContextNote(from: comparisonSnapshot, to: snapshot),
showsUnchanged: false
)
.padding(.top, appDensity.metrics.sectionSpacing)
}
DNSSectionView(
isCollapsed: .constant(false),
dnssecLabel: DomainViewModel.dnssecLabel(from: snapshot),
sections: DomainViewModel.dnsRows(from: snapshot),
ptrMessage: DomainViewModel.ptrMessage(from: snapshot),
loading: false,
dnsProvenance: snapshot.provenanceBySection[.dns],
ptrProvenance: snapshot.provenanceBySection[.ptr],
sectionError: snapshot.dnsError
)
.padding(.top, appDensity.metrics.sectionSpacing)
WebSectionView(
isCollapsed: .constant(false),
certificateRows: DomainViewModel.webCertificateRows(from: snapshot),
sslInfo: snapshot.sslInfo,
sslLoading: false,
sslError: snapshot.sslError,
tlsProvenance: snapshot.provenanceBySection[.ssl],
responseRows: DomainViewModel.webResponseRows(from: snapshot),
headers: snapshot.httpHeaders,
headersLoading: false,
headersError: snapshot.httpHeadersError,
httpProvenance: snapshot.provenanceBySection[.httpHeaders],
redirects: DomainViewModel.redirectRows(from: snapshot),
redirectLoading: false,
redirectError: snapshot.redirectChainError,
redirectProvenance: snapshot.provenanceBySection[.redirectChain],
finalURL: snapshot.redirectChain.last?.url
)
.padding(.top, appDensity.metrics.sectionSpacing)
EmailSectionView(
isCollapsed: .constant(false),
rows: DomainViewModel.emailRows(from: snapshot),
loading: false,
provenance: snapshot.provenanceBySection[.emailSecurity],
confidence: snapshot.emailSecurityConfidence,
error: snapshot.emailSecurityError
)
.padding(.top, appDensity.metrics.sectionSpacing)
NetworkSectionView(
isCollapsed: .constant(false),
reachabilityRows: DomainViewModel.reachabilityRows(from: snapshot),
reachabilityLoading: false,
reachabilityError: snapshot.reachabilityError,
reachabilityProvenance: snapshot.provenanceBySection[.reachability],
locationRows: DomainViewModel.locationRows(from: snapshot),
geolocation: snapshot.ipGeolocation,
geolocationLoading: false,
geolocationError: snapshot.ipGeolocationError,
geolocationProvenance: snapshot.provenanceBySection[.ipGeolocation],
geolocationConfidence: snapshot.geolocationConfidence,
standardPortRows: DomainViewModel.portRows(from: snapshot, kind: .standard),
customPortRows: DomainViewModel.portRows(from: snapshot, kind: .custom),
portScanLoading: false,
portScanError: snapshot.portScanError,
portScanProvenance: snapshot.provenanceBySection[.portScan],
customPortScanLoading: false,
customPortScanError: nil,
isCloudflareProxied: snapshot.httpHeaders.contains(where: { $0.name.lowercased() == "cf-ray" }),
customPortsExpanded: .constant(false),
customPortInput: .constant(""),
onScanCustomPorts: {}
)
.padding(.top, appDensity.metrics.sectionSpacing)
}
.padding(.horizontal)
.padding(.bottom, 32)
}
.background(Color.black)
.navigationTitle(entry.domain)
.toolbar {
ToolbarItemGroup(placement: .topBarTrailing) {
Button("Note") {
noteDraft = entry.note ?? ""
isEditingNote = true
}
Button("Re-run") {
showRerunOptions = true
}
}
}
.onChange(of: viewModel.rerunNavigationToken) { _, _ in
dismiss()
}
.confirmationDialog("Re-run lookup", isPresented: $showRerunOptions) {
Button("Run with Current Settings") {
viewModel.rerunLookup(from: entry, useSnapshotResolver: false)
}
Button("Run with Snapshot Resolver") {
viewModel.rerunLookup(from: entry, useSnapshotResolver: true)
}
Button("Cancel", role: .cancel) {}
} message: {
Text(viewModel.resolverMismatchNote(for: entry) ?? "Choose how to reproduce this snapshot.")
}
.sheet(isPresented: $isEditingNote) {
NavigationStack {
Form {
Section("Audit Note") {
TextField("Optional note", text: $noteDraft, axis: .vertical)
.lineLimit(3...6)
}
}
.navigationTitle(entry.domain)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
isEditingNote = false
}
}
ToolbarItem(placement: .confirmationAction) {
Button("Save") {
viewModel.updateHistoryNote(noteDraft, for: entry)
isEditingNote = false
}
}
}
}
}
.preferredColorScheme(.dark)
}
private var snapshotBanner: some View {
VStack(alignment: .leading, spacing: 8) {
HStack(spacing: 8) {
Image(systemName: "archivebox")
.font(.caption)
Text("Snapshot from \(dateFormatter.string(from: entry.timestamp))")
.font(appDensity.font(.caption))
Spacer()
Text("Live re-run available")
.font(appDensity.font(.caption2))
.foregroundStyle(.secondary)
}
if let mismatchNote = viewModel.resolverMismatchNote(for: entry) {
Text(mismatchNote)
.font(appDensity.font(.caption2))
.foregroundStyle(.orange)
}
if entry.isPartialSnapshot {
Text("Partial snapshot: \(entry.validationIssues.joined(separator: " | "))")
.font(appDensity.font(.caption2))
.foregroundStyle(.yellow)
}
if let note = entry.note, !note.isEmpty {
Text(note)
.font(appDensity.font(.caption2))
.foregroundStyle(.secondary)
}
}
.foregroundStyle(.secondary)
.padding(8)
.frame(maxWidth: .infinity, alignment: .leading)
.background(Color(.systemGray6).opacity(0.3))
.clipShape(RoundedRectangle(cornerRadius: appDensity.metrics.cardCornerRadius))
.padding(.vertical, 12)
}
}
private struct HistoryGroup: Identifiable {
let title: String
let entries: [HistoryEntry]
var id: String { title }
static func groups(for entries: [HistoryEntry]) -> [HistoryGroup] {
let calendar = Calendar.current
let today = Date()
let yesterday = calendar.date(byAdding: .day, value: -1, to: today) ?? today
let grouped = Dictionary(grouping: entries) { entry -> String in
if calendar.isDate(entry.timestamp, inSameDayAs: today) {
return "Today"
}
if calendar.isDate(entry.timestamp, inSameDayAs: yesterday) {
return "Yesterday"
}
return "Older"
}
return ["Today", "Yesterday", "Older"].compactMap { title in
guard let entries = grouped[title], !entries.isEmpty else { return nil }
return HistoryGroup(title: title, entries: entries)
}
}
}
|