summaryrefslogtreecommitdiff
path: root/Hutch/Views/SystemStatus/SystemStatusSummaryRow.swift
blob: 5d94dad82102eb55b71f31b9ddcfa7a887f0bcdf (plain) (blame)
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
import SwiftUI

struct SystemStatusSummaryRow: View {
    let title: String
    let snapshot: SystemStatusSnapshot?
    let isLoading: Bool
    let errorMessage: String?
    let isShowingStaleData: Bool

    init(
        title: String = "System Status",
        snapshot: SystemStatusSnapshot?,
        isLoading: Bool = false,
        errorMessage: String? = nil,
        isShowingStaleData: Bool = false
    ) {
        self.title = title
        self.snapshot = snapshot
        self.isLoading = isLoading
        self.errorMessage = errorMessage
        self.isShowingStaleData = isShowingStaleData
    }

    var body: some View {
        HStack(spacing: 12) {
            icon
                .frame(width: 20)

            VStack(alignment: .leading, spacing: 3) {
                Text(title)
                    .font(.subheadline.weight(.semibold))
                    .foregroundStyle(.primary)

                Text(primaryMessage)
                    .font(.caption)
                    .foregroundStyle(primaryMessageColor)
                    .lineLimit(2)

                if let metadataMessage {
                    Text(metadataMessage)
                        .font(.caption2)
                        .foregroundStyle(.tertiary)
                        .lineLimit(1)
                }
            }

            Spacer(minLength: 8)
        }
        .padding(.vertical, 4)
        .contentShape(Rectangle())
    }

    @ViewBuilder
    private var icon: some View {
        if isLoading && snapshot == nil {
            ProgressView()
                .controlSize(.small)
        } else {
            Image(systemName: iconName)
                .foregroundStyle(iconColor)
        }
    }

    private var primaryMessage: String {
        if let snapshot {
            return snapshot.hasDisruption ? snapshot.bannerSummary : snapshot.overallStatusText
        }
        if let errorMessage, !errorMessage.isEmpty {
            return errorMessage
        }
        if isLoading {
            return "Loading system status…"
        }
        return "System status is unavailable right now."
    }

    private var metadataMessage: String? {
        if let snapshot {
            if isShowingStaleData {
                return "Updated \(snapshot.lastUpdated.relativeDescription) • Showing saved data"
            }
            return "Updated \(snapshot.lastUpdated.relativeDescription)"
        }
        if errorMessage != nil {
            return "Open System Status to retry."
        }
        return nil
    }

    private var iconName: String {
        if let snapshot {
            return snapshot.hasDisruption ? "exclamationmark.triangle.fill" : "checkmark.circle.fill"
        }
        if errorMessage != nil {
            return "exclamationmark.triangle"
        }
        return "server.rack"
    }

    private var iconColor: Color {
        if let snapshot {
            return snapshot.hasDisruption ? .orange : .green
        }
        if errorMessage != nil {
            return .secondary
        }
        return .secondary
    }

    private var primaryMessageColor: Color {
        if snapshot != nil {
            return .secondary
        }
        if errorMessage != nil {
            return .secondary
        }
        return .secondary
    }
}