summaryrefslogtreecommitdiff
path: root/DomainDig/DomainCompareView.swift
blob: 8219fa73a2ec6fbbcc51cf8c232ed3768f5711cb (plain) (blame)
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
import SwiftUI

/// Side-by-side comparison of two tracked domains' latest reports, reusing the
/// same section-diff rendering (`DomainDiffView`) that time-based history diffs
/// use, via `DiffService.compare(domainA:domainB:)`.
struct DomainCompareView: View {
    @Bindable var viewModel: DomainViewModel
    @State private var domainAID: UUID?
    @State private var domainBID: UUID?

    private var states: [PortfolioDomainStatus] {
        viewModel.portfolioDashboardData.domainStates
    }

    private var stateA: PortfolioDomainStatus? {
        states.first { $0.trackedDomain.id == domainAID }
    }

    private var stateB: PortfolioDomainStatus? {
        states.first { $0.trackedDomain.id == domainBID }
    }

    private var result: DomainComparisonResult? {
        guard let stateA, let stateB, stateA.trackedDomain.id != stateB.trackedDomain.id else {
            return nil
        }
        return DiffService.compare(domainA: stateA.report, domainB: stateB.report)
    }

    var body: some View {
        List {
            Section("Domains") {
                domainPicker("Domain A", selection: $domainAID)
                domainPicker("Domain B", selection: $domainBID)
            }

            if domainAID != nil, domainAID == domainBID {
                Section {
                    Text("Choose two different domains to compare.")
                        .font(.callout)
                        .foregroundStyle(Color(.appTextSecondary))
                }
            }

            if let result {
                Section {
                    DomainDiffView(
                        title: "\(result.domainA) vs \(result.domainB)",
                        sections: result.sections,
                        contextNote: result.contextNote,
                        showsUnchanged: true,
                        highlightedSectionID: nil
                    )
                }
                .listRowBackground(Color.clear)
            } else if domainAID == nil || domainBID == nil {
                Section {
                    EmptyStateCardView(
                        title: "Pick Two Domains",
                        message: "Select a domain in each slot to see a side-by-side comparison across DNS, TLS, ownership, and risk.",
                        suggestion: "Both domains must already be tracked.",
                        systemImage: "arrow.left.arrow.right",
                        showsCardBackground: false
                    )
                }
                .listRowBackground(Color(.appSurface))
            }
        }
        .scrollContentBackground(.hidden)
        .background(Color(.appBackground))
        .navigationTitle("Compare Domains")
    }

    private func domainPicker(_ label: String, selection: Binding<UUID?>) -> some View {
        Picker(label, selection: selection) {
            Text("Select a domain").tag(UUID?.none)
            ForEach(states) { state in
                Text(state.trackedDomain.domain).tag(Optional(state.trackedDomain.id))
            }
        }
    }
}