Repository Public
1import MCCCore2import SwiftUI34/// One action, followed live: state, facts, output, approval.5struct ActionRunScreen: View {6 private enum Prompt: String, Identifiable { case approve, reject; var id: String { rawValue } }78 @Environment(Navigator.self) private var nav9 @Environment(\.mccTheme) private var theme10 @Session private var session11 let actionID: String12 @State private var model: ActionRunModel?13 @State private var runner = MutationRunner()14 @State private var prompt: Prompt?15 @State private var confirmingCancel = false1617 private var resource: Resource<API.ActionDetail> { session.actions.detail[actionID] }1819 var body: some View {20 ScreenStack {21 ResourceSection(resource: resource) { d in22 VStack(alignment: .leading, spacing: Tokens.Space.s5) {23 header(d.action)24 controls(d.action)25 facts(d.action)26 output27 if let r = d.action.result { result(r) }28 events(d.events)29 }30 }31 }32 .screen("action-run", title: tr("actions.run.title"), load: {}, reload: {33 await resource.load()34 await session.actions.refreshOutput(for: actionID)35 })36 .navigationBarTitleDisplayMode(.inline)37 .onAppear {38 if model == nil { model = ActionRunModel(actionID: actionID, session: session) }39 model?.start()40 }41 .onDisappear { model?.stop() }42 .mutationError(runner)43 .confirmationDialog(tr("actions.cancel.title"), isPresented: $confirmingCancel, titleVisibility: .visible) {44 Button(tr("actions.cancel.confirm"), role: .destructive) {45 Task { await runner.run { try await session.actions.cancel(id: actionID) } }46 }47 Button(tr("actions.cancel.keep"), role: .cancel) {}48 } message: { Text(tr("actions.cancel.message")) }49 .sheet(item: $prompt) { p in50 switch p {51 case .approve:52 TextPromptSheet(title: tr("actions.approve.title"), message: tr("actions.approve.message"), fieldLabel: tr("alerts.comment.optional"),53 fieldID: "action-approve-comment", confirmTitle: tr("actions.approve"), confirmID: "action-approve-confirm") { text in54 await runner.run { try await session.actions.approve(id: actionID, comment: text.isEmpty ? nil : text) } != nil55 }56 case .reject:57 TextPromptSheet(title: tr("actions.reject.title"), fieldLabel: tr("actions.reject.reason"), required: true,58 fieldID: "action-reject-reason", confirmTitle: tr("actions.reject"), confirmID: "action-reject-confirm", destructive: true) { text in59 await runner.run { try await session.actions.reject(id: actionID, reason: text) } != nil60 }61 }62 }63 }6465 private func header(_ a: API.Action) -> some View {66 Card {67 VStack(alignment: .leading, spacing: Tokens.Space.s3) {68 AdaptiveRow {69 Text(a._type).font(.title3.monospaced().weight(.semibold)).foregroundStyle(theme.textPrimary).textSelection(.enabled)70 } trailing: {71 HStack(spacing: Tokens.Space.s2) {72 if !a.state.isTerminal { ProgressView().accessibilityHidden(true) }73 StatusBadge(a.state.label, tone: a.state.tone)74 .accessibilityElement(children: .ignore)75 .accessibilityLabel(tr("actions.state"))76 .accessibilityValue(a.state.rawValue)77 .accessibilityIdentifier("action-state")78 }79 }80 Text(ActionsLogic.targetLabel(a)).font(.callout).foregroundStyle(theme.textSecondary)81 if a.outcomeUnknown {82 InfoBanner(tone: .warning, title: tr("actions.outcome_unknown"), message: tr("actions.outcome_unknown.message"))83 .accessibilityIdentifier("action-outcome-unknown")84 }85 if a.errorCode != nil || a.errorMessage != nil {86 InfoBanner(tone: .critical, title: a.errorCode ?? tr("error.title"), message: a.errorMessage).accessibilityIdentifier("action-failure")87 }88 if let r = a.rejectionReason, !r.isEmpty {89 InfoBanner(tone: .warning, title: tr("actions.rejected.reason"), message: r)90 }91 }92 }93 }9495 @ViewBuilder private func controls(_ a: API.Action) -> some View {96 if !a.state.isTerminal {97 TileGrid(minimum: 150) {98 if a.state == .pendingApproval, session.can("actions.approve") {99 MutationButton(title: tr("actions.approve"), systemImage: "checkmark.seal", identifier: "action-approve", prominent: true) { prompt = .approve }100 MutationButton(title: tr("actions.reject"), systemImage: "xmark.seal", role: .destructive, identifier: "action-reject") { prompt = .reject }101 }102 MutationButton(title: tr(a.cancelRequestedAt == nil ? "actions.cancel" : "actions.cancel.requested"), systemImage: "stop.circle", role: .destructive, identifier: "action-cancel") {103 confirmingCancel = true104 }105 .disabled(a.cancelRequestedAt != nil)106 }107 .disabled(runner.isRunning)108 if a.state == .pendingApproval {109 Text(tr("actions.approval.note")).font(.footnote).foregroundStyle(theme.textTertiary)110 }111 }112 }113114 private func facts(_ a: API.Action) -> some View {115 SectionCard(tr("actions.facts")) {116 VStack(alignment: .leading, spacing: Tokens.Space.s3) {117 InfoRow(tr("actions.target"), ActionsLogic.targetLabel(a))118 if let id = a.nodeId {119 Button { nav.go(to: .node(id)) } label: {120 Label(tr("actions.node.open", a.nodeAlias ?? ""), systemImage: "desktopcomputer").frame(minHeight: Tokens.Size.touchMin)121 }122 .accessibilityIdentifier("action-node")123 }124 InfoRow(tr("actions.criticality"), a.criticality.label)125 InfoRow(tr("actions.requested_by"), [a.requestedByDisplayName, a.requestedVia].compactMap { $0 }.joined(separator: " · "))126 if !a.justification.isEmpty { InfoRow(tr("actions.justification"), a.justification) }127 if a.approvalRequired {128 InfoRow(tr("actions.approved_by"), a.approvedAt.map { [a.approvedByDisplayName, Format.dateTime($0)].compactMap { $0 }.joined(separator: " · ") } ?? tr("actions.approval.pending"))129 }130 let lines = ActionsLogic.parameterLines(JSONValue(a.params.additionalProperties).objectValue ?? [:])131 if !lines.isEmpty { InfoRow(tr("actions.parameters"), lines.joined(separator: "\n"), monospaced: true) }132 InfoRow(tr("actions.created"), Format.dateTime(a.createdAt))133 InfoRow(tr("actions.queued"), a.queuedAt.map { Format.dateTime($0) })134 InfoRow(tr("actions.started"), a.startedAt.map { Format.dateTime($0) })135 InfoRow(tr("actions.finished"), a.finishedAt.map { Format.dateTime($0) })136 InfoRow(tr("actions.timeout"), Format.duration(seconds: Double(a.timeoutS)))137 InfoRow(tr("actions.deadline"), a.deadlineAt.map { Format.dateTime($0) })138 if let c = a.cancelRequestedAt { InfoRow(tr("actions.cancel.requested"), Format.dateTime(c)) }139 if let job = a.jobId {140 Button { nav.go(to: .job(job)) } label: { Label(tr("actions.job.open"), systemImage: "list.bullet.rectangle").frame(minHeight: Tokens.Size.touchMin) }141 }142 CorrelationIDView(a.correlationId)143 }144 }145 }146147 @ViewBuilder private var output: some View {148 let buffer = session.actions.output(for: actionID)149 SectionCard(tr("actions.output")) {150 if let e = buffer.error { InlineErrorView(e) }151 if buffer.text.isEmpty {152 Text(tr(buffer.complete ? "actions.output.none" : "actions.output.waiting")).font(.callout).foregroundStyle(theme.textSecondary)153 .accessibilityIdentifier("action-output-empty")154 } else {155 ScrollView(.horizontal) {156 Text(buffer.text)157 .font(.footnote.monospaced())158 .foregroundStyle(theme.textPrimary)159 .textSelection(.enabled)160 .frame(maxWidth: .infinity, alignment: .leading)161 .padding(Tokens.Space.s3)162 }163 .background(theme.inset, in: RoundedRectangle(cornerRadius: Tokens.Radius.md))164 .accessibilityElement(children: .ignore)165 .accessibilityLabel(tr("actions.output"))166 .accessibilityValue(buffer.text)167 .accessibilityIdentifier("action-output")168 }169 if buffer.truncated { StatusLabel(tr("actions.output.truncated"), tone: .warning).font(.footnote) }170 }171 }172173 private func result(_ r: API.Action.ResultPayload) -> some View {174 SectionCard(tr("actions.result")) {175 Text(JSONValue(r.additionalProperties).prettyPrinted())176 .font(.footnote.monospaced()).foregroundStyle(theme.textPrimary).textSelection(.enabled)177 .frame(maxWidth: .infinity, alignment: .leading)178 .accessibilityIdentifier("action-result")179 }180 }181182 @ViewBuilder private func events(_ events: [API.ActionEvent]) -> some View {183 let items = events.filter { $0.stream == nil }184 if !items.isEmpty {185 SectionCard(tr("actions.events")) {186 ForEach(items, id: \.id) { e in187 VStack(alignment: .leading, spacing: Tokens.Space.s1) {188 Text(e.kind).font(.callout.weight(.medium)).foregroundStyle(theme.textPrimary)189 if !e.data.isEmpty { Text(e.data).font(.footnote.monospaced()).foregroundStyle(theme.textSecondary).textSelection(.enabled) }190 Text(Format.time(e.ts, seconds: true)).font(.footnote).foregroundStyle(theme.textTertiary)191 }192 .frame(maxWidth: .infinity, alignment: .leading)193 .accessibilityElement(children: .combine)194 }195 }196 }197 }198}199