krz/hutch
an ios client for sourcehut
clone: git clone https://gitbay.org/krz/hutch.git
v2.15.0: Hutch/Views/SystemStatus/SystemStatusSummaryRow.swift · raw
1import SwiftUI
2
3struct SystemStatusSummaryRow: View {
4 let title: String
5 let snapshot: SystemStatusSnapshot?
6 let isLoading: Bool
7 let errorMessage: String?
8 let isShowingStaleData: Bool
9
10 init(
11 title: String = "System Status",
12 snapshot: SystemStatusSnapshot?,
13 isLoading: Bool = false,
14 errorMessage: String? = nil,
15 isShowingStaleData: Bool = false
16 ) {
17 self.title = title
18 self.snapshot = snapshot
19 self.isLoading = isLoading
20 self.errorMessage = errorMessage
21 self.isShowingStaleData = isShowingStaleData
22 }
23
24 var body: some View {
25 HStack(spacing: 12) {
26 icon
27 .frame(width: 20)
28
29 VStack(alignment: .leading, spacing: 3) {
30 Text(title)
31 .font(.subheadline.weight(.semibold))
32 .foregroundStyle(.primary)
33
34 Text(primaryMessage)
35 .font(.caption)
36 .foregroundStyle(primaryMessageColor)
37 .lineLimit(2)
38
39 if let metadataMessage {
40 Text(metadataMessage)
41 .font(.caption2)
42 .foregroundStyle(.tertiary)
43 .lineLimit(1)
44 }
45 }
46
47 Spacer(minLength: 8)
48 }
49 .padding(.vertical, 4)
50 .contentShape(Rectangle())
51 }
52
53 @ViewBuilder
54 private var icon: some View {
55 if isLoading && snapshot == nil {
56 ProgressView()
57 .controlSize(.small)
58 } else {
59 Image(systemName: iconName)
60 .foregroundStyle(iconColor)
61 }
62 }
63
64 private var primaryMessage: String {
65 if let snapshot {
66 return snapshot.hasDisruption ? snapshot.bannerSummary : snapshot.overallStatusText
67 }
68 if let errorMessage, !errorMessage.isEmpty {
69 return errorMessage
70 }
71 if isLoading {
72 return "Loading system status…"
73 }
74 return "System status is unavailable right now."
75 }
76
77 private var metadataMessage: String? {
78 if let snapshot {
79 if isShowingStaleData {
80 return "Updated \(snapshot.lastUpdated.relativeDescription) • Showing saved data"
81 }
82 return "Updated \(snapshot.lastUpdated.relativeDescription)"
83 }
84 if errorMessage != nil {
85 return "Open System Status to retry."
86 }
87 return nil
88 }
89
90 private var iconName: String {
91 if let snapshot {
92 return snapshot.hasDisruption ? "exclamationmark.triangle.fill" : "checkmark.circle.fill"
93 }
94 if errorMessage != nil {
95 return "exclamationmark.triangle"
96 }
97 return "server.rack"
98 }
99
100 private var iconColor: Color {
101 if let snapshot {
102 return snapshot.hasDisruption ? .orange : .green
103 }
104 if errorMessage != nil {
105 return .secondary
106 }
107 return .secondary
108 }
109
110 private var primaryMessageColor: Color {
111 if snapshot != nil {
112 return .secondary
113 }
114 if errorMessage != nil {
115 return .secondary
116 }
117 return .secondary
118 }
119}