aboutsummaryrefslogtreecommitdiff
path: root/DomainDig/TimelineView.swift
blob: 16e2bf8d0c9a7498d44621a6e193d8508d33da90 (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
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
import SwiftUI

struct TimelineView: View {
    @Environment(\.appDensity) private var appDensity
    @Bindable var viewModel: DomainViewModel
    let domain: String

    @State private var presentedDiff: DomainDiff?
    @State private var focusedSectionID: String?

    private var timelineSections: [TimelineSection] {
        viewModel.timelineSections(for: domain)
    }

    private var compareButtonDisabled: Bool {
        viewModel.selectedSnapshots.count != 2 && viewModel.historyEntries(for: domain).count < 2
    }

    var body: some View {
        List {
            ForEach(timelineSections) { section in
                Section(section.title) {
                    ForEach(section.entries) { summary in
                        if let entry = viewModel.historyEntry(withID: summary.historyEntryID) {
                            NavigationLink {
                                HistoryDetailView(viewModel: viewModel, entry: entry)
                            } label: {
                                TimelineRow(summary: summary, entry: entry)
                            }
                            .swipeActions(edge: .trailing, allowsFullSwipe: false) {
                                Button {
                                    viewModel.toggleSnapshotSelection(entry)
                                } label: {
                                    Label(
                                        viewModel.selectedSnapshotIDs.contains(entry.id) ? "Selected" : "Compare",
                                        systemImage: viewModel.selectedSnapshotIDs.contains(entry.id) ? "checkmark.circle.fill" : "arrow.left.arrow.right"
                                    )
                                }

                                Button(role: .destructive) {
                                    viewModel.removeHistoryEntries(withIDs: [entry.id])
                                } label: {
                                    Label("Delete", systemImage: "trash")
                                }
                            }
                        }
                    }
                    .listRowBackground(Color(.appSurface))
                }
            }
        }
        .scrollContentBackground(.hidden)
        .background(Color(.appBackground))
        .navigationTitle(domain)
        .toolbar {
            ToolbarItemGroup(placement: .topBarTrailing) {
                Menu {
                    Picker("Grouping", selection: $viewModel.timelineGrouping) {
                        ForEach(TimelineGroupingOption.allCases) { option in
                            Text(option.title).tag(option)
                        }
                    }
                } label: {
                    Image(systemName: "line.3.horizontal.decrease.circle")
                }

                Button("Compare") {
                    if viewModel.selectedSnapshots.count == 2 {
                        presentedDiff = viewModel.generateDiffForSelectedSnapshots()
                    } else {
                        let entries = viewModel.historyEntries(for: domain)
                        guard entries.count >= 2 else { return }
                        presentedDiff = viewModel.generateDiff(from: entries[1], to: entries[0])
                    }
                    focusedSectionID = viewModel.currentDiffTargetSectionID
                }
                .disabled(compareButtonDisabled)

                Menu("Export") {
                    Button("Export TXT") {
                        ExportPresenter.share(
                            filename: "\(domain)-timeline.txt",
                            contents: viewModel.exportTimelineText(domain: domain, includeDiffSummary: true)
                        )
                    }

                    Button("Export JSON") {
                        guard let data = viewModel.exportTimelineJSONData(domain: domain, includeDiffSummary: true) else { return }
                        ExportPresenter.share(filename: "\(domain)-timeline.json", data: data)
                    }
                }
            }
        }
        .sheet(item: $presentedDiff) { diff in
            NavigationStack {
                TimelineDiffView(viewModel: viewModel, diff: diff, focusedSectionID: $focusedSectionID)
            }
        }
    }
}

private struct TimelineRow: View {
    @Environment(\.appDensity) private var appDensity
    let summary: SnapshotSummary
    let entry: HistoryEntry

    var body: some View {
        VStack(alignment: .leading, spacing: appDensity.metrics.rowSpacing + 1) {
            HStack(alignment: .center, spacing: 8) {
                Text(summary.timestamp.formatted(date: .abbreviated, time: .shortened))
                    .font(appDensity.font(.callout))
                    .foregroundStyle(.primary)
                Spacer()
                if let severity = summary.severitySummary {
                    AppStatusBadgeView(
                        model: .init(
                            title: severity.title,
                            systemImage: "arrow.triangle.2.circlepath",
                            foregroundColor: severity == .high ? Color(.statusCritical) : Color(.statusWarning),
                            backgroundColor: (severity == .high ? Color(.statusCritical) : Color(.statusWarning)).opacity(0.16)
                        )
                    )
                }
            }

            Text(summary.changeSummaryMessage ?? "No change summary")
                .font(appDensity.font(.caption))
                .foregroundStyle(Color(.appTextSecondary))
                .lineLimit(2)

            HStack(spacing: 8) {
                AppStatusBadgeView(model: AppStatusFactory.availability(summary.availability))
                if let riskScore = summary.riskScore {
                    Text("Risk \(riskScore)")
                        .lineLimit(1)
                        .minimumScaleFactor(0.85)
                }
            }
            .font(appDensity.font(.caption2))
            .foregroundStyle(Color(.appTextSecondary))

            if !entry.intelligenceTimeline.isEmpty {
                VStack(alignment: .leading, spacing: 4) {
                    ForEach(Array(entry.intelligenceTimeline.prefix(2))) { event in
                        Text("\(event.title): \(event.detail)")
                            .font(appDensity.font(.caption2))
                            .foregroundStyle(Color(.appTextSecondary))
                            .lineLimit(1)
                    }
                }
            }

            HStack(spacing: 8) {
                if let primaryIP = summary.primaryIP {
                    Text(primaryIP)
                        .lineLimit(1)
                        .truncationMode(.middle)
                }
                Spacer(minLength: 8)
                Text(summary.timestamp.formatted(date: .abbreviated, time: .shortened))
                    .lineLimit(1)
            }
            .font(appDensity.font(.caption2))
            .foregroundStyle(Color(.appTextSecondary))
        }
    }
}

struct TimelineDiffView: View {
    @Bindable var viewModel: DomainViewModel
    let diff: DomainDiff
    @Binding var focusedSectionID: String?

    var body: some View {
        ScrollViewReader { proxy in
            ScrollView {
                VStack(alignment: .leading, spacing: 12) {
                    HStack {
                        Button("Previous Change") {
                            viewModel.moveToPreviousDiffChange()
                            focusedSectionID = viewModel.currentDiffTargetSectionID
                            scroll(proxy: proxy)
                        }
                        .disabled(viewModel.activeDiffChangeIndex == 0)

                        Button("Next Change") {
                            viewModel.moveToNextDiffChange()
                            focusedSectionID = viewModel.currentDiffTargetSectionID
                            scroll(proxy: proxy)
                        }
                        .disabled(viewModel.activeDomainDiff?.changedSectionIDs.isEmpty != false || viewModel.currentDiffTargetSectionID == viewModel.activeDomainDiff?.changedSectionIDs.last)

                        Spacer()
                    }

                    DomainDiffView(
                        title: "Snapshot Diff",
                        sections: diff.sections,
                        contextNote: diff.contextNote,
                        showsUnchanged: false,
                        highlightedSectionID: focusedSectionID
                    )
                }
                .padding()
            }
            .background(Color(.appBackground))
            .navigationTitle("Compare Snapshots")
            .navigationBarTitleDisplayMode(.inline)
            .onAppear {
                scroll(proxy: proxy)
            }
            .onChange(of: focusedSectionID) { _, _ in
                scroll(proxy: proxy)
            }
        }
    }

    private func scroll(proxy: ScrollViewProxy) {
        guard let focusedSectionID else { return }
        withAnimation {
            proxy.scrollTo(focusedSectionID, anchor: .top)
        }
    }
}