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
|
//
// SecurityEventListView.swift
// octosentry
//
import AppKit
import SwiftUI
struct SecurityEventListView: View {
var store: SecurityEventStore
var authStore: AuthStore
var updateStore: UpdateStore
var isStandaloneWindow: Bool = false
@State private var showingRepoManager = false
@Environment(\.openWindow) private var openWindow
var body: some View {
VStack(alignment: .leading, spacing: 0) {
header
if let release = updateStore.availableRelease {
UpdateBanner(release: release)
}
Divider()
if !authStore.isSignedIn {
SignInView(authStore: authStore)
} else if showingRepoManager {
RepoManagerView(store: store, authStore: authStore)
} else {
content
}
}
.task(id: authStore.isSignedIn) {
guard authStore.isSignedIn else { return }
await store.refresh()
store.startPolling()
}
.task {
await updateStore.checkForUpdate()
}
}
private var header: some View {
HStack {
Text("Security Events")
.font(.headline)
if store.isLoading {
ProgressView()
.controlSize(.small)
}
Spacer()
if authStore.isSignedIn && !showingRepoManager {
Picker("Minimum severity", selection: Binding(
get: { store.minimumSeverity },
set: { newValue in Task { await store.setMinimumSeverity(newValue) } }
)) {
ForEach(SecurityEventSeverity.allCases, id: \.self) { severity in
Text(severity.displayName).tag(severity)
}
}
.pickerStyle(.menu)
.labelsHidden()
.fixedSize()
Button {
Task { await store.refresh() }
} label: {
Image(systemName: "arrow.clockwise")
}
.buttonStyle(.plain)
.disabled(store.isLoading)
}
if authStore.isSignedIn {
if !isStandaloneWindow {
Button {
openWindow(id: SecurityEventWindow.id)
} label: {
Image(systemName: "macwindow")
}
.buttonStyle(.plain)
.help("Open in Window")
}
Button {
showingRepoManager.toggle()
} label: {
Image(systemName: showingRepoManager ? "xmark.circle" : "gearshape")
}
.buttonStyle(.plain)
}
Button("Quit") {
NSApplication.shared.terminate(nil)
}
.buttonStyle(.plain)
.foregroundStyle(.secondary)
}
.padding(12)
}
@ViewBuilder
private var content: some View {
if store.events.isEmpty && !store.errorMessages.isEmpty {
StatusView(systemImage: "exclamationmark.triangle", tint: .orange, message: store.errorMessages.joined(separator: "\n\n"))
} else if store.events.isEmpty && !store.isLoading {
VStack(spacing: 8) {
if store.totalFetchedCount > 0 {
StatusView(
systemImage: "line.3.horizontal.decrease.circle",
tint: .secondary,
message: "\(store.totalFetchedCount) alert(s) are below your minimum severity filter"
)
} else {
StatusView(systemImage: "checkmark.shield", tint: .green, message: "No open security alerts")
}
if !store.unavailableNotices.isEmpty {
NoticeBanner(messages: store.unavailableNotices)
.padding(.horizontal)
.padding(.bottom)
}
}
} else {
ScrollView {
LazyVStack(alignment: .leading, spacing: 0) {
if !store.errorMessages.isEmpty {
ErrorBanner(messages: store.errorMessages)
Divider()
}
if !store.unavailableNotices.isEmpty {
NoticeBanner(messages: store.unavailableNotices)
Divider()
}
ForEach(store.events) { event in
SecurityEventRow(event: event) {
Task { await store.markSeen(event.id) }
}
Divider()
}
}
}
}
}
}
private struct RepoManagerView: View {
var store: SecurityEventStore
var authStore: AuthStore
@State private var newRepoText = ""
@State private var isBrowsingRepos = false
@State private var availableRepos: [String] = []
@State private var isLoadingRepos = false
@State private var browseErrorMessage: String?
var body: some View {
VStack(alignment: .leading, spacing: 10) {
Text("Watched Repositories")
.font(.subheadline.weight(.semibold))
if store.watchedRepos.isEmpty {
Text("No repos watched yet.")
.font(.callout)
.foregroundStyle(.secondary)
} else {
ForEach(store.watchedRepos, id: \.self) { repo in
HStack {
Text(repo)
.font(.callout)
Spacer()
Button {
Task { await store.removeRepo(repo) }
} label: {
Image(systemName: "minus.circle.fill")
.foregroundStyle(.red)
}
.buttonStyle(.plain)
}
}
}
Divider()
if isBrowsingRepos {
browsingContent
} else {
HStack {
TextField("owner/repo", text: $newRepoText)
.textFieldStyle(.roundedBorder)
.onSubmit(addRepo)
Button("Add", action: addRepo)
.disabled(newRepoText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
}
Button(action: startBrowsing) {
Label("Browse your repos", systemImage: "list.bullet")
.font(.caption)
}
.buttonStyle(.plain)
.foregroundStyle(Color.accentColor)
}
if let errorMessage = store.watchListErrorMessage {
Text(errorMessage)
.font(.caption2)
.foregroundStyle(.red)
}
Spacer()
Divider()
Button("Sign Out") {
authStore.signOut()
}
.buttonStyle(.plain)
.foregroundStyle(.red)
}
.padding(12)
.frame(maxWidth: .infinity, alignment: .leading)
}
@ViewBuilder
private var browsingContent: some View {
VStack(alignment: .leading, spacing: 6) {
HStack {
Text("Your Repositories")
.font(.caption.weight(.semibold))
Spacer()
Button {
isBrowsingRepos = false
} label: {
Image(systemName: "xmark.circle")
}
.buttonStyle(.plain)
}
if isLoadingRepos {
ProgressView()
.controlSize(.small)
.frame(maxWidth: .infinity)
} else if let browseErrorMessage {
Text(browseErrorMessage)
.font(.caption2)
.foregroundStyle(.red)
} else {
let selectableRepos = availableRepos.filter { !store.watchedRepos.contains($0) }
if selectableRepos.isEmpty {
Text("All visible repos are already watched.")
.font(.caption2)
.foregroundStyle(.secondary)
} else {
ScrollView {
LazyVStack(alignment: .leading, spacing: 4) {
ForEach(selectableRepos, id: \.self) { repo in
Button {
Task { await store.addRepo(repo) }
isBrowsingRepos = false
} label: {
Text(repo)
.font(.callout)
.frame(maxWidth: .infinity, alignment: .leading)
}
.buttonStyle(.plain)
}
}
}
.frame(maxHeight: 160)
}
}
}
}
private func startBrowsing() {
guard authStore.hasRepoAccess else {
authStore.requestRepoAccess()
return
}
isBrowsingRepos = true
isLoadingRepos = true
browseErrorMessage = nil
Task {
do {
availableRepos = try await store.fetchAccessibleRepos()
} catch {
browseErrorMessage = (error as? LocalizedError)?.errorDescription ?? error.localizedDescription
}
isLoadingRepos = false
}
}
private func addRepo() {
let text = newRepoText
newRepoText = ""
Task { await store.addRepo(text) }
}
}
private struct UpdateBanner: View {
let release: UpdateChecker.LatestRelease
var body: some View {
Button {
NSWorkspace.shared.open(release.htmlURL)
} label: {
Label("Update available: \(release.version)", systemImage: "arrow.down.circle.fill")
.font(.caption)
.frame(maxWidth: .infinity, alignment: .leading)
}
.buttonStyle(.plain)
.foregroundStyle(.blue)
.padding(8)
.background(.blue.opacity(0.1))
}
}
private struct ErrorBanner: View {
let messages: [String]
var body: some View {
VStack(alignment: .leading, spacing: 4) {
ForEach(messages, id: \.self) { message in
Label(message, systemImage: "exclamationmark.triangle")
.font(.caption)
.foregroundStyle(.orange)
}
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding(10)
.background(.orange.opacity(0.1))
}
}
private struct NoticeBanner: View {
let messages: [String]
var body: some View {
VStack(alignment: .leading, spacing: 4) {
ForEach(messages, id: \.self) { message in
Label(message, systemImage: "info.circle")
.font(.caption2)
.foregroundStyle(.secondary)
}
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding(10)
.background(.secondary.opacity(0.08))
}
}
private struct StatusView: View {
let systemImage: String
let tint: Color
let message: String
var body: some View {
VStack(spacing: 8) {
Image(systemName: systemImage)
.font(.title2)
.foregroundStyle(tint)
Text(message)
.font(.callout)
.multilineTextAlignment(.center)
.foregroundStyle(.secondary)
}
.padding()
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
#Preview {
SecurityEventListView(store: SecurityEventStore(), authStore: AuthStore(), updateStore: UpdateStore())
.frame(width: 380, height: 420)
}
|