SPB Git

Repository Public

IncidentDetailScreen.swift 4.2 KB 83 lines swift

1import MCCCore2import SwiftUI34/// One incident: state, linked alerts, timeline, state change and comments.5struct IncidentDetailScreen: View {6    @Environment(Navigator.self) private var nav7    @Environment(\.mccTheme) private var theme8    @Session private var session9    let incidentID: String10    @State private var runner = MutationRunner()11    @State private var commenting = false1213    private var resource: Resource<API.IncidentDetail> { session.alerts.incidentDetail[incidentID] }1415    var body: some View {16        ScreenStack {17            ResourceSection(resource: resource) { d in18                VStack(alignment: .leading, spacing: Tokens.Space.s5) {19                    header(d.incident)20                    controls(d.incident)21                    SectionCard(tr("alerts.incident.alerts")) {22                        if d.alerts.isEmpty {23                            Text(tr("alerts.incident.no_alerts")).font(.callout).foregroundStyle(theme.textSecondary)24                        } else {25                            ForEach(AlertsLogic.sorted(d.alerts), id: \.id) { a in26                                Button { nav.go(to: .alert(a.id)) } label: {27                                    AlertRowLabel(alert: a).frame(maxWidth: .infinity, minHeight: Tokens.Size.touchMin, alignment: .leading).contentShape(Rectangle())28                                }29                                .buttonStyle(.plain)30                                .accessibilityIdentifier("incident-alert-" + a.id)31                            }32                        }33                    }34                    SectionCard(tr("alerts.timeline")) { TimelineList(events: d.timeline) }35                }36            }37        }38        .screen("incident-detail", title: tr("alerts.incident.title"), load: { await resource.loadIfNeeded(maxAge: 5) }, reload: { await resource.load() })39        .navigationBarTitleDisplayMode(.inline)40        .loads(resource, maxAge: 5)41        .mutationError(runner)42        .sheet(isPresented: $commenting) {43            TextPromptSheet(title: tr("alerts.comment.title"), fieldLabel: tr("alerts.comment"), required: true,44                            fieldID: "incident-comment", confirmTitle: tr("alerts.comment.send"), confirmID: "incident-comment-confirm") { text in45                await runner.run { try await session.alerts.comment(incidentID: incidentID, body: text) } != nil46            }47        }48    }4950    private func header(_ i: API.Incident) -> some View {51        Card {52            VStack(alignment: .leading, spacing: Tokens.Space.s3) {53                AdaptiveRow {54                    SeverityLabel(severity: i.severity).font(.callout.weight(.medium))55                } trailing: {56                    StatusBadge(i.state.label, tone: i.state.tone)57                        .accessibilityElement(children: .ignore)58                        .accessibilityLabel(tr("alerts.state"))59                        .accessibilityValue(i.state.rawValue)60                        .accessibilityIdentifier("incident-state")61                }62                Text(i.title).font(.title3.weight(.semibold)).foregroundStyle(theme.textPrimary).textSelection(.enabled)63                if !i.summary.isEmpty { Text(i.summary).font(.callout).foregroundStyle(theme.textSecondary).textSelection(.enabled) }64                InfoRow(tr("alerts.incident.owner"), i.ownerDisplayName)65                InfoRow(tr("alerts.incident.opened"), Format.dateTime(i.openedAt))66                InfoRow(tr("alerts.resolved"), i.resolvedAt.map { Format.dateTime($0) })67            }68        }69    }7071    private func controls(_ i: API.Incident) -> some View {72        TileGrid(minimum: 150) {73            ForEach(API.IncidentState.allCases.filter { $0 != i.state }, id: \.self) { s in74                MutationButton(title: tr("alerts.incident.mark", s.label), permission: "alerts.manage", identifier: "incident-set-" + s.rawValue) {75                    Task { await runner.run { try await session.alerts.updateIncident(id: incidentID, API.UpdateIncidentRequest(state: s)) } }76                }77            }78            MutationButton(title: tr("alerts.comment.add"), systemImage: "text.bubble", permission: "alerts.manage", identifier: "incident-add-comment") { commenting = true }79        }80        .disabled(runner.isRunning)81    }82}83
Files main
Menu

Theme