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
|
import SwiftUI
struct SystemStatusView: View {
@Environment(AppState.self) private var appState
@State private var viewModel: SystemStatusViewModel?
var body: some View {
Group {
if let viewModel {
content(viewModel)
} else {
SRHTLoadingStateView(message: "Loading system status…")
}
}
.navigationTitle("System Status")
.navigationBarTitleDisplayMode(.inline)
.task {
let vm: SystemStatusViewModel
if let viewModel {
vm = viewModel
} else {
let newViewModel = SystemStatusViewModel(repository: appState.systemStatusRepository)
viewModel = newViewModel
vm = newViewModel
}
await vm.load()
}
}
@ViewBuilder
private func content(_ viewModel: SystemStatusViewModel) -> some View {
List {
if viewModel.isShowingStaleData, let staleDataMessage = viewModel.staleDataMessage {
Section {
Label(staleDataMessage, systemImage: "clock.arrow.trianglehead.counterclockwise.rotate.90")
.font(.subheadline)
.foregroundStyle(.secondary)
.themedRow()
}
}
if let snapshot = viewModel.snapshot {
summarySection(snapshot)
servicesSection(snapshot)
activeIncidentsSection(snapshot.activeIncidents)
}
recentIncidentsSection(viewModel.recentIncidents)
}
.themedList()
.listStyle(.insetGrouped)
.refreshable {
await viewModel.load(forceRefresh: true)
}
.overlay {
if viewModel.isLoading && !viewModel.hasContent {
SRHTLoadingStateView(message: "Loading system status…")
} else if !viewModel.isLoading && !viewModel.hasContent, let errorMessage = viewModel.errorMessage {
SRHTErrorStateView(
title: "Couldn’t Load System Status",
message: errorMessage,
retryAction: { await viewModel.load(forceRefresh: true) }
)
} else if !viewModel.isLoading && !viewModel.hasContent {
ContentUnavailableView(
"No Status Data",
systemImage: "server.rack",
description: Text("System status information is not available right now.")
)
}
}
.connectivityOverlay(hasContent: viewModel.hasContent) {
await viewModel.load(forceRefresh: true)
}
.srhtErrorBanner(error: Binding(
get: { viewModel.errorMessage },
set: { viewModel.errorMessage = $0 }
))
}
@ViewBuilder
private func summarySection(_ snapshot: SystemStatusSnapshot) -> some View {
Section {
VStack(alignment: .leading, spacing: 12) {
HStack(spacing: 10) {
Image(systemName: snapshot.hasDisruption ? "exclamationmark.triangle.fill" : "checkmark.circle.fill")
.foregroundStyle(snapshot.hasDisruption ? .orange : .green)
VStack(alignment: .leading, spacing: 4) {
Text(snapshot.overallStatusText)
.font(.headline)
Text("Updated \(snapshot.lastUpdated.relativeDescription)")
.font(.subheadline)
.foregroundStyle(.secondary)
}
}
Link(destination: SRHTWebURL.status) {
Label("Open status.sr.ht", systemImage: "safari")
}
.font(.subheadline.weight(.medium))
}
.padding(.vertical, 4)
.themedRow()
}
}
@ViewBuilder
private func servicesSection(_ snapshot: SystemStatusSnapshot) -> some View {
Section("Services") {
ForEach(snapshot.services) { service in
HStack(spacing: 12) {
StatusLevelBadge(level: service.status)
VStack(alignment: .leading, spacing: 4) {
Text(service.name)
.font(.subheadline.weight(.medium))
Text(service.status.displayName)
.font(.caption)
.foregroundStyle(.secondary)
}
Spacer()
}
.padding(.vertical, 2)
}
.themedRow()
}
}
@ViewBuilder
private func activeIncidentsSection(_ incidents: [StatusIncident]) -> some View {
if !incidents.isEmpty {
Section("Active Incidents") {
ForEach(incidents) { incident in
incidentRow(incident)
}
.themedRow()
}
}
}
@ViewBuilder
private func recentIncidentsSection(_ incidents: [StatusIncident]) -> some View {
Section("Recent Incidents") {
if incidents.isEmpty {
ContentUnavailableView(
"No Recent Incidents",
systemImage: "clock.arrow.trianglehead.counterclockwise.rotate.90",
description: Text("The status feed didn’t return any recent incidents.")
)
.themedRow()
} else {
ForEach(incidents) { incident in
incidentRow(incident)
}
.themedRow()
}
}
}
@ViewBuilder
private func incidentRow(_ incident: StatusIncident) -> some View {
if let url = incident.url {
Link(destination: url) {
StatusIncidentRow(incident: incident)
}
} else {
StatusIncidentRow(incident: incident)
}
}
}
private struct StatusIncidentRow: View {
let incident: StatusIncident
var body: some View {
VStack(alignment: .leading, spacing: 6) {
HStack(alignment: .top, spacing: 8) {
Text(incident.title)
.font(.subheadline.weight(.medium))
.foregroundStyle(.primary)
Spacer(minLength: 8)
if incident.url != nil {
Image(systemName: "arrow.up.right.square")
.font(.caption)
.foregroundStyle(.secondary)
}
}
Text(timestampText)
.font(.caption)
.foregroundStyle(.secondary)
if let summary = incident.summary, !summary.isEmpty {
Text(summary)
.font(.caption)
.foregroundStyle(.secondary)
.lineLimit(3)
}
}
.padding(.vertical, 2)
}
private var timestampText: String {
if let updatedAt = incident.updatedAt {
return "Published \(incident.publishedAt.relativeDescription) • Updated \(updatedAt.relativeDescription)"
}
return "Published \(incident.publishedAt.relativeDescription)"
}
}
private struct StatusLevelBadge: View {
let level: StatusLevel
var body: some View {
HStack(spacing: 6) {
Circle()
.fill(color)
.frame(width: 8, height: 8)
Text(level.displayName)
.font(.caption.weight(.medium))
.foregroundStyle(.primary)
}
.padding(.horizontal, 10)
.padding(.vertical, 6)
.background(color.opacity(0.14), in: Capsule())
}
private var color: Color {
switch level {
case .operational:
.green
case .degraded:
.orange
case .majorOutage:
.red
case .maintenance:
.blue
case .unknown:
.gray
}
}
}
|