krz/hutch

an ios client for sourcehut

clone: git clone https://gitbay.org/krz/hutch.git

v3.4.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            let summary = snapshot.hasDisruption ? snapshot.bannerSummary : snapshot.overallStatusText
 67            return "\(summary) • Updated \(snapshot.lastUpdated.relativeDescription)"
 68        }
 69        if let errorMessage, !errorMessage.isEmpty {
 70            return errorMessage
 71        }
 72        if isLoading {
 73            return "Loading system status…"
 74        }
 75        return "System status is unavailable right now."
 76    }
 77
 78    private var metadataMessage: String? {
 79        if snapshot != nil, isShowingStaleData {
 80            return "Showing saved data"
 81        }
 82        if errorMessage != nil {
 83            return "Open System Status to retry."
 84        }
 85        return nil
 86    }
 87
 88    private var iconName: String {
 89        if let snapshot {
 90            return snapshot.hasDisruption ? "exclamationmark.triangle.fill" : "checkmark.circle.fill"
 91        }
 92        if errorMessage != nil {
 93            return "exclamationmark.triangle"
 94        }
 95        return "server.rack"
 96    }
 97
 98    private var iconColor: Color {
 99        if let snapshot {
100            return snapshot.hasDisruption ? .orange : .green
101        }
102        if errorMessage != nil {
103            return .secondary
104        }
105        return .secondary
106    }
107
108    private var primaryMessageColor: Color {
109        if snapshot != nil {
110            return .secondary
111        }
112        if errorMessage != nil {
113            return .secondary
114        }
115        return .secondary
116    }
117}