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
|
//
// SecurityEventListView.swift
// octosentry
//
import AppKit
import SwiftUI
struct SecurityEventListView: View {
var store: SecurityEventStore
@State private var showingRepoManager = false
var body: some View {
VStack(alignment: .leading, spacing: 0) {
header
Divider()
if showingRepoManager {
RepoManagerView(store: store)
} else {
content
}
}
.frame(width: 380, height: 420)
.task {
await store.refresh()
store.startPolling()
}
}
private var header: some View {
HStack {
Text("Security Events")
.font(.headline)
if store.isLoading {
ProgressView()
.controlSize(.small)
}
Spacer()
if !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)
}
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)
Divider()
}
}
}
}
}
}
private struct RepoManagerView: View {
var store: SecurityEventStore
@State private var newRepoText = ""
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()
HStack {
TextField("owner/repo", text: $newRepoText)
.textFieldStyle(.roundedBorder)
.onSubmit(addRepo)
Button("Add", action: addRepo)
.disabled(newRepoText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
}
if let errorMessage = store.watchListErrorMessage {
Text(errorMessage)
.font(.caption2)
.foregroundStyle(.red)
}
Spacer()
}
.padding(12)
.frame(maxWidth: .infinity, alignment: .leading)
}
private func addRepo() {
let text = newRepoText
newRepoText = ""
Task { await store.addRepo(text) }
}
}
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())
}
|