Repository Public
1import MCCCore2import SwiftUI34/// Alerts and incidents.5struct AlertsScreen: View {6 enum Scope: String, CaseIterable, Identifiable { case alerts, incidents; var id: String { rawValue } }78 @Environment(Navigator.self) private var nav9 @Environment(\.mccTheme) private var theme10 @Session private var session11 @State private var scope: Scope = .alerts12 @State private var filter: AlertListFilter = .open13 @State private var query = ""1415 var body: some View {16 ScreenStack {17 Picker(tr("alerts.scope"), selection: $scope) {18 Text(tr("section.alerts")).tag(Scope.alerts)19 Text(tr("alerts.incidents")).tag(Scope.incidents)20 }21 .pickerStyle(.segmented)22 .accessibilityIdentifier("alerts-scope")2324 switch scope {25 case .alerts: alertsList26 case .incidents: incidentsList27 }28 }29 .screen("alerts", title: tr("section.alerts"), load: load, reload: reload)30 .searchable(text: $query, prompt: tr("alerts.search.prompt"))31 .toolbar {32 ToolbarItem(placement: .topBarTrailing) {33 Button { nav.open(.silences) } label: { Label(tr("alerts.silences"), systemImage: "bell.slash") }34 .accessibilityIdentifier("alerts-silences")35 }36 }37 }3839 private func load() async {40 async let a: Void = session.alerts.alerts.loadIfNeeded()41 async let b: Void = session.alerts.incidents.loadIfNeeded()42 _ = await (a, b)43 }44 private func reload() async {45 async let a: Void = session.alerts.alerts.load()46 async let b: Void = session.alerts.incidents.load()47 _ = await (a, b)48 }4950 @ViewBuilder private var alertsList: some View {51 Picker(tr("alerts.filter"), selection: $filter) {52 ForEach(AlertListFilter.allCases) { Text($0.title).tag($0) }53 }54 .pickerStyle(.menu)55 .frame(minHeight: Tokens.Size.touchMin)56 .accessibilityIdentifier("alerts-filter")5758 ResourceSection(resource: session.alerts.alerts) { all in59 let items = AlertsLogic.sorted(AlertsLogic.filter(all, by: filter, query: query))60 if items.isEmpty {61 EmptyStateView(tr(filter == .open ? "alerts.empty.open" : "alerts.empty"), systemImage: "bell.slash")62 .accessibilityIdentifier("alerts-empty")63 } else {64 LazyVStack(spacing: Tokens.Space.s3) {65 ForEach(items, id: \.id) { a in66 RouteRow(route: .alert(a.id), identifier: "alert-row-" + a.id) { AlertRowLabel(alert: a) }67 }68 }69 }70 }71 }7273 @ViewBuilder private var incidentsList: some View {74 ResourceSection(resource: session.alerts.incidents) { all in75 let items = AlertsLogic.sorted(AlertsLogic.filter(all, query: query))76 if items.isEmpty {77 EmptyStateView(tr("alerts.incidents.empty"), systemImage: "checkmark.seal").accessibilityIdentifier("incidents-empty")78 } else {79 LazyVStack(spacing: Tokens.Space.s3) {80 ForEach(items, id: \.id) { i in81 RouteRow(route: .incident(i.id), identifier: "incident-row-" + i.id) {82 VStack(alignment: .leading, spacing: Tokens.Space.s1) {83 AdaptiveRow {84 Text(i.title).font(.body.weight(.medium)).foregroundStyle(theme.textPrimary)85 } trailing: {86 StatusBadge(i.state.label, tone: i.state.tone, compact: true)87 }88 SeverityLabel(severity: i.severity).font(.footnote)89 Text([tr("alerts.incident.alert_count", i.alertCount), i.ownerDisplayName, Format.relative(i.openedAt)].compactMap { $0 }.joined(separator: " · "))90 .font(.footnote).foregroundStyle(theme.textSecondary)91 }92 }93 }94 }95 }96 }97 }98}99100struct AlertRowLabel: View {101 @Environment(\.mccTheme) private var theme102 let alert: API.Alert103 var body: some View {104 VStack(alignment: .leading, spacing: Tokens.Space.s1) {105 AdaptiveRow {106 SeverityLabel(severity: alert.severity).font(.footnote.weight(.medium))107 } trailing: {108 StatusBadge(alert.state.label, tone: alert.tone, compact: true)109 }110 Text(alert.summary).font(.body).foregroundStyle(theme.textPrimary).multilineTextAlignment(.leading)111 Text([alert.nodeAlias, alert.ruleName, tr("alerts.since", Format.relative(alert.startedAt))].compactMap { $0 }.joined(separator: " · "))112 .font(.footnote).foregroundStyle(theme.textSecondary).multilineTextAlignment(.leading)113 if let s = alert.suppressedBy {114 Text(tr(s == .silence ? "alerts.suppressed.silence" : "alerts.suppressed.maintenance")).font(.footnote).foregroundStyle(theme.textTertiary)115 }116 }117 }118}119