summaryrefslogtreecommitdiff
path: root/HutchWidgetExtension/SystemStatusWidget.swift
blob: 4a3c1e5b9ca5566d3c3a044e380c22ec759e2802 (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
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
import SwiftUI
import WidgetKit

struct SystemStatusEntry: TimelineEntry {
    let date: Date
    let snapshot: SystemStatusWidgetSnapshot?
}

struct SystemStatusTimelineProvider: TimelineProvider {
    func placeholder(in _: Context) -> SystemStatusEntry {
        SystemStatusEntry(
            date: .now,
            snapshot: SystemStatusWidgetSnapshot(
                services: [
                    .init(id: "git", name: "git.sr.ht", status: "Operational", requiresAttention: false),
                    .init(id: "builds", name: "builds.sr.ht", status: "Operational", requiresAttention: false),
                    .init(id: "lists", name: "lists.sr.ht", status: "Operational", requiresAttention: false),
                    .init(id: "todo", name: "todo.sr.ht", status: "Operational", requiresAttention: false),
                    .init(id: "meta", name: "meta.sr.ht", status: "Operational", requiresAttention: false),
                    .init(id: "hg", name: "hg.sr.ht", status: "Operational", requiresAttention: false),
                ],
                hasDisruption: false,
                overallStatusText: "All monitored services operational",
                bannerSummary: "",
                updatedAt: .now
            )
        )
    }

    func getSnapshot(in _: Context, completion: @escaping (SystemStatusEntry) -> Void) {
        completion(
            SystemStatusEntry(
                date: .now,
                snapshot: SystemStatusWidgetSnapshotStore.load()
            )
        )
    }

    func getTimeline(in _: Context, completion: @escaping (Timeline<SystemStatusEntry>) -> Void) {
        let refreshDate = Calendar.current.date(byAdding: .minute, value: 15, to: .now) ?? .now.addingTimeInterval(900)
        let entry = SystemStatusEntry(date: .now, snapshot: SystemStatusWidgetSnapshotStore.load())
        completion(Timeline(entries: [entry], policy: .after(refreshDate)))
    }
}

struct SystemStatusWidget: Widget {
    static let kind = SystemStatusWidgetConfiguration.kind

    var body: some WidgetConfiguration {
        StaticConfiguration(kind: Self.kind, provider: SystemStatusTimelineProvider()) { entry in
            SystemStatusWidgetView(entry: entry)
        }
        .configurationDisplayName("SourceHut Status")
        .description("Current status of SourceHut services at a glance.")
        .supportedFamilies([.systemSmall, .systemMedium])
    }
}

private struct SystemStatusWidgetView: View {
    let entry: SystemStatusEntry
    @Environment(\.widgetFamily) private var family

    var body: some View {
        Group {
            if let snapshot = entry.snapshot, !snapshot.services.isEmpty {
                switch family {
                case .systemMedium:
                    mediumView(snapshot: snapshot)
                default:
                    smallView(snapshot: snapshot)
                }
            } else {
                fallbackView
            }
        }
        .widgetURL(URL(string: "hutch://status"))
        .containerBackground(for: .widget) {
            Color(.systemBackground)
        }
    }

    private func smallView(snapshot: SystemStatusWidgetSnapshot) -> some View {
        VStack(alignment: .leading, spacing: 8) {
            HStack(spacing: 6) {
                Image(systemName: snapshot.hasDisruption ? "exclamationmark.triangle.fill" : "checkmark.circle.fill")
                    .font(.title3.weight(.semibold))
                    .foregroundStyle(snapshot.hasDisruption ? .orange : .green)
                Spacer()
            }

            Text(snapshot.hasDisruption ? snapshot.bannerSummary : "All Clear")
                .font(.subheadline.weight(.semibold))
                .lineLimit(2)

            Spacer(minLength: 0)

            if snapshot.hasDisruption {
                let disrupted = snapshot.services.filter(\.requiresAttention)
                Text(disrupted.map(\.name).joined(separator: ", "))
                    .font(.caption2)
                    .foregroundStyle(.secondary)
                    .lineLimit(2)
            } else {
                Text("\(snapshot.services.count) services monitored")
                    .font(.caption2)
                    .foregroundStyle(.secondary)
            }
        }
        .padding()
    }

    private func mediumView(snapshot: SystemStatusWidgetSnapshot) -> some View {
        VStack(alignment: .leading, spacing: 6) {
            HStack(spacing: 8) {
                Image(systemName: snapshot.hasDisruption ? "exclamationmark.triangle.fill" : "checkmark.circle.fill")
                    .font(.title3.weight(.semibold))
                    .foregroundStyle(snapshot.hasDisruption ? .orange : .green)

                Text(snapshot.hasDisruption ? snapshot.bannerSummary : "All Services Operational")
                    .font(.subheadline.weight(.semibold))
                    .lineLimit(1)

                Spacer()
            }

            Divider()

            let columns = Array(snapshot.services.prefix(8))
            let half = (columns.count + 1) / 2
            let leftColumn = Array(columns.prefix(half))
            let rightColumn = Array(columns.suffix(from: half))

            HStack(alignment: .top, spacing: 16) {
                VStack(alignment: .leading, spacing: 4) {
                    ForEach(leftColumn) { service in
                        serviceRow(service)
                    }
                }
                VStack(alignment: .leading, spacing: 4) {
                    ForEach(rightColumn) { service in
                        serviceRow(service)
                    }
                }
                Spacer(minLength: 0)
            }

            Spacer(minLength: 0)
        }
        .padding(.horizontal, 16)
        .padding(.top, 14)
        .padding(.bottom, 12)
    }

    private func serviceRow(_ service: SystemStatusWidgetSnapshot.ServiceEntry) -> some View {
        HStack(spacing: 6) {
            Circle()
                .fill(service.requiresAttention ? Color.orange : Color.green)
                .frame(width: 6, height: 6)
            Text(service.name)
                .font(.caption2)
                .foregroundStyle(service.requiresAttention ? .primary : .secondary)
                .lineLimit(1)
        }
    }

    private var fallbackView: some View {
        VStack(alignment: .leading, spacing: 8) {
            Spacer(minLength: 0)
            Text("Open Hutch")
                .font(.title3.weight(.semibold))
            Text("Sign in to load system status.")
                .font(.caption)
                .foregroundStyle(.secondary)
        }
        .padding()
    }
}