Repository Public
1import MCCCore2import SwiftUI34/// Live window of one action: state, people, timings, streamed output, result — and an honest "outcome unknown".5struct ActionWindowView: View {6 @Environment(AppModel.self) private var app7 @Environment(\.mccTheme) private var theme8 let actionID: String9 @State private var run: ActionRunModel?10 @State private var approvalComment = ""11 @State private var rejectReason = ""12 @State private var showsReject = false1314 var body: some View {15 Group {16 if let run {17 ResourceView(run.detail) { detail in content(detail.action, run: run) }18 } else {19 ProgressView()20 }21 }22 .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)23 .background(theme.canvas)24 .navigationTitle(run?.action.map { "\($0._type) — \($0.targetLabel ?? $0.nodeAlias ?? "")" } ?? tr("actions.window.title"))25 .task(id: actionID) {26 let r = ActionRunModel(actionID: actionID, session: app.session)27 run = r28 r.start()29 }30 .onDisappear { run?.stop() }31 }3233 private func content(_ a: API.Action, run: ActionRunModel) -> some View {34 VStack(alignment: .leading, spacing: 0) {35 OfflineBanner()36 ScrollView {37 VStack(alignment: .leading, spacing: Tokens.Space.s5) {38 header(a)39 if a.outcomeUnknown {40 InfoBanner(tone: .warning, title: tr("actions.outcome_unknown.title"), message: tr("actions.outcome_unknown.message"))41 .accessibilityIdentifier("outcome-unknown")42 }43 if a.state == .pendingApproval { approval(a) }44 DetailSection(title: tr("actions.window.details")) {45 KeyValueRow(tr("actions.target"), "\(a.target.kind.label) · \(a.targetLabel ?? a.target.id)")46 if let n = a.nodeAlias { KeyValueRow(tr("actions.window.node"), n) }47 KeyValueRow(tr("actions.window.requested_by"), [a.requestedByDisplayName ?? "—", a.requestedVia].joined(separator: " · "))48 if !a.justification.isEmpty { KeyValueRow(tr("actions.justification"), a.justification) }49 if a.approvalRequired {50 KeyValueRow(tr("actions.window.approval"), a.approvedByDisplayName.map { tr("actions.window.approved_by", $0) } ?? tr("actions.window.approval.pending"))51 }52 if let r = a.rejectionReason { KeyValueRow(tr("actions.window.rejected"), r) }53 KeyValueRow(tr("actions.window.created"), Format.dateTime(a.createdAt))54 if let t = a.startedAt { KeyValueRow(tr("actions.window.started"), Format.dateTime(t)) }55 if let t = a.finishedAt { KeyValueRow(tr("actions.window.finished"), Format.dateTime(t)) }56 if let s = a.startedAt, let f = a.finishedAt { KeyValueRow(tr("actions.window.duration"), Format.duration(seconds: f.timeIntervalSince(s))) }57 if let d = a.deadlineAt, !a.state.isTerminal { KeyValueRow(tr("actions.window.deadline"), Format.dateTime(d)) }58 KeyValueRow(tr("error.correlation_id"), a.correlationId, monospaced: true)59 }60 let params = JSONValue(a.params.additionalProperties)61 if params.objectValue?.isEmpty == false {62 DetailSection(title: tr("actions.parameters")) { JSONBlock(value: params) }63 }64 if let code = a.errorCode {65 DetailSection(title: tr("actions.window.error")) {66 InlineErrorView(APIError(code: code, message: a.errorMessage ?? code, correlationID: a.correlationId))67 }68 }69 if let result = a.result {70 DetailSection(title: tr("actions.window.result")) { JSONBlock(value: JSONValue(result.additionalProperties)) }71 }72 output(run)73 }74 .padding(Tokens.Space.s6)75 }76 }77 }7879 private func header(_ a: API.Action) -> some View {80 HStack(alignment: .firstTextBaseline, spacing: Tokens.Space.s4) {81 VStack(alignment: .leading, spacing: 2) {82 Text(a._type).font(.title2.weight(.semibold).monospaced()).foregroundStyle(theme.textPrimary)83 Text(a.targetLabel ?? a.nodeAlias ?? a.target.id).font(.callout).foregroundStyle(theme.textSecondary)84 }85 Spacer()86 StatusBadge(tr("actions.criticality", a.criticality.label), tone: a.criticality.tone)87 StatusBadge(a.state.label, tone: a.state.tone).accessibilityIdentifier("action-state")88 if !a.state.isTerminal {89 if a.state == .running || a.state == .dispatched || a.state == .queued { ProgressView().controlSize(.small) }90 Button(tr("actions.cancel_action")) {91 Task { await app.perform { _ = try await app.session.actions.cancel(id: a.id) } }92 }93 .mutation(app, permission: "actions.read")94 .disabled(a.cancelRequestedAt != nil)95 }96 }97 }9899 private func approval(_ a: API.Action) -> some View {100 DetailSection(title: tr("actions.approval.title")) {101 Text(tr("actions.approval.message")).font(.callout).foregroundStyle(theme.textSecondary)102 if app.session.can("actions.approve") {103 TextField(tr("actions.approval.comment"), text: $approvalComment).textFieldStyle(.roundedBorder)104 HStack {105 Button(tr("actions.approval.approve")) {106 Task { await app.perform { _ = try await app.session.actions.approve(id: a.id, comment: approvalComment.isEmpty ? nil : approvalComment) } }107 }108 .buttonStyle(.borderedProminent)109 .mutation(app, permission: "actions.approve")110 Button(tr("actions.approval.reject")) { showsReject = true }.mutation(app, permission: "actions.approve")111 Text(tr("actions.approval.note")).font(.caption).foregroundStyle(theme.textTertiary)112 }113 .alert(tr("actions.approval.reject"), isPresented: $showsReject) {114 TextField(tr("actions.approval.reason"), text: $rejectReason)115 Button(tr("actions.approval.reject"), role: .destructive) {116 let reason = rejectReason117 Task { await app.perform { _ = try await app.session.actions.reject(id: a.id, reason: reason) } }118 }119 Button(tr("action.cancel"), role: .cancel) {}120 }121 }122 }123 }124125 private func output(_ run: ActionRunModel) -> some View {126 let buffer = run.output127 return DetailSection(title: tr("actions.window.output")) {128 if buffer.events.isEmpty {129 if let e = buffer.error {130 if let reason = Loadable<Int>.failureReason(e) { UnavailableInline(reason: reason, error: e) } else { InlineErrorView(e) }131 } else {132 Text(run.isFinished ? tr("actions.window.output.none") : tr("actions.window.output.waiting")).font(.callout).foregroundStyle(theme.textSecondary)133 }134 } else {135 ScrollView {136 Text(buffer.text)137 .font(.callout.monospaced())138 .foregroundStyle(theme.textPrimary)139 .textSelection(.enabled)140 .frame(maxWidth: .infinity, alignment: .leading)141 .padding(Tokens.Space.s3)142 }143 .frame(minHeight: 120, maxHeight: 360)144 .background(theme.inset, in: RoundedRectangle(cornerRadius: Tokens.Radius.md))145 .accessibilityLabel(tr("actions.window.output"))146 .accessibilityIdentifier("action-output")147 HStack {148 if buffer.truncated { StatusLabel(tr("actions.window.output.truncated"), tone: .warning).font(.caption) }149 if buffer.complete { Text(tr("actions.window.output.complete")).font(.caption).foregroundStyle(theme.textTertiary) }150 Spacer()151 CopyButton(value: buffer.text, label: tr("actions.window.output.copy"))152 }153 }154 }155 }156}157158extension Loadable {159 /// The "cannot be shown" reason of an error, if it is one (501 / 503 / 403 / offline).160 static func failureReason(_ e: APIError) -> UnavailableReason? {161 if e.isNotImplemented { return .notImplemented }162 if e.isServiceUnavailable { return .serviceUnavailable(code: e.code) }163 if e.isForbidden { return .forbidden }164 if e.isTransport || e.code == APIError.codeOffline { return .offline }165 return nil166 }167}168