Repository Public
1import MCCCore2import SwiftUI34/// Request form of one action on one target, driven by the catalog. The app checks only that what the server will5/// ask for is present; permission, step-up, justification, the typed confirmation and approval are verified by the server.6struct ActionRequestSheet: View {7 @Environment(Navigator.self) private var nav8 @Environment(\.dismiss) private var dismiss9 @Environment(\.mccTheme) private var theme10 @Session private var session11 let typeName: String12 let target: API.ActionTarget13 let targetLabel: String14 let preset: [String: JSONValue]15 @State private var model: ActionFormModel?16 @State private var reviewing = false1718 var body: some View {19 ScrollView {20 VStack(alignment: .leading, spacing: Tokens.Space.s5) {21 if let model {22 if reviewing { review(model) } else { form(model) }23 } else if session.actions.types.value != nil {24 InfoBanner(tone: .warning, title: tr("actions.request.unknown_type"), message: typeName)25 } else {26 ResourceSection(resource: session.actions.types, showsStamp: false) { _ in EmptyView() }27 }28 }29 .padding(Tokens.Space.s5)30 .frame(maxWidth: 640, alignment: .leading)31 .frame(maxWidth: .infinity)32 }33 .scrollDismissesKeyboard(.interactively)34 .background(theme.canvas.ignoresSafeArea())35 .navigationTitle(model?.type.title ?? typeName)36 .navigationBarTitleDisplayMode(.inline)37 .toolbar {38 ToolbarItem(placement: .cancellationAction) { Button(tr("common.cancel")) { dismiss() }.accessibilityIdentifier("action-request-cancel") }39 }40 .task {41 await session.actions.types.loadIfNeeded(maxAge: 120)42 makeModel()43 }44 .onChange(of: session.actions.types.value?.count) { makeModel() }45 .interactiveDismissDisabled(model?.isSubmitting ?? false)46 .accessibilityIdentifier("screen-action-request")47 }4849 private func makeModel() {50 guard model == nil, let type = session.actions.type(named: typeName) else { return }51 model = ActionFormModel(type: type, target: target, targetLabel: targetLabel, presetParams: preset)52 }5354 // MARK: Form5556 @ViewBuilder private func form(_ m: ActionFormModel) -> some View {57 @Bindable var m = m58 Card {59 VStack(alignment: .leading, spacing: Tokens.Space.s3) {60 InfoRow(tr("actions.target"), targetLabel)61 InfoRow(tr("actions.target.kind"), target.kind.label)62 InfoRow(tr("actions.type"), m.type._type, monospaced: true)63 AdaptiveRow {64 Text(tr("actions.criticality")).font(.callout).foregroundStyle(theme.textSecondary)65 } trailing: {66 StatusBadge(m.type.criticality.label, tone: m.type.criticality.tone, compact: true)67 }68 .accessibilityElement(children: .ignore)69 .accessibilityLabel(tr("actions.criticality"))70 .accessibilityValue(m.type.criticality.label)71 if !m.type.description.isEmpty { Text(m.type.description).font(.callout).foregroundStyle(theme.textSecondary) }72 }73 }74 if !m.type.available {75 InfoBanner(tone: .stale, title: tr("actions.unavailable"), message: m.type.unavailableReason).accessibilityIdentifier("action-unavailable")76 }77 requirements(m.type)78 if !m.fields.isEmpty {79 SectionCard(tr("actions.parameters")) { SchemaFormView(model: m) }80 }81 if m.type.requiresJustification {82 SectionCard(tr("actions.justification")) {83 TextField(tr("actions.justification.placeholder"), text: $m.justification, axis: .vertical)84 .lineLimit(2...6)85 .textFieldStyle(.roundedBorder)86 .frame(minHeight: Tokens.Size.touchMin)87 .accessibilityLabel(tr("actions.justification"))88 .accessibilityIdentifier("action-justification")89 }90 }91 if m.type.requiresConfirmation {92 SectionCard(tr("actions.confirmation")) {93 Text(tr("actions.confirmation.help", targetLabel)).font(.callout).foregroundStyle(theme.textSecondary)94 TextField(targetLabel, text: $m.confirmation)95 .textInputAutocapitalization(.never).autocorrectionDisabled()96 .textFieldStyle(.roundedBorder)97 .frame(minHeight: Tokens.Size.touchMin)98 .accessibilityLabel(tr("actions.confirmation"))99 .accessibilityIdentifier("action-confirmation")100 }101 }102 if let e = m.error { errorView(e) }103 if !session.canMutate {104 InfoBanner(tone: .offline, title: tr("actions.offline.title"), message: tr("mutation.offline_hint")).accessibilityIdentifier("action-offline")105 }106 Button {107 if ActionsLogic.needsReviewStep(m.type.criticality) { reviewing = true } else { submit(m) }108 } label: {109 HStack {110 if m.isSubmitting { ProgressView().tint(theme.accentContrast) }111 Text(tr(ActionsLogic.needsReviewStep(m.type.criticality) ? "actions.request.review" : "actions.request.send"))112 }113 .frame(maxWidth: .infinity, minHeight: Tokens.Size.touchMin)114 }115 .buttonStyle(.mccProminent)116 .disabled(!m.canSubmit || !session.canMutate)117 .accessibilityHint(!session.canMutate ? tr("mutation.offline_hint") : "")118 .accessibilityIdentifier("action-submit")119 }120121 @ViewBuilder private func requirements(_ t: API.ActionType) -> some View {122 let reqs = ActionsLogic.requirements(of: t)123 if !reqs.isEmpty {124 SectionCard(tr("actions.requires")) {125 ForEach(reqs, id: \.self) { r in126 Label(r.title, systemImage: r.symbol).font(.callout).foregroundStyle(theme.textPrimary)127 }128 if t.requiresStepUp { Text(tr("actions.requires.step_up.note")).font(.footnote).foregroundStyle(theme.textTertiary) }129 if t.requiresApproval { Text(tr("actions.requires.approval.note")).font(.footnote).foregroundStyle(theme.textTertiary) }130 }131 }132 }133134 private func errorView(_ e: APIError) -> some View {135 VStack(alignment: .leading, spacing: Tokens.Space.s2) {136 InlineErrorView(e)137 if e.isForbidden { Text(tr("error.forbidden_hint")).font(.footnote).foregroundStyle(theme.textSecondary) }138 }139 .accessibilityElement(children: .contain)140 .accessibilityIdentifier("action-error")141 }142143 // MARK: Review step (medium criticality and above)144145 @ViewBuilder private func review(_ m: ActionFormModel) -> some View {146 InfoBanner(tone: m.type.criticality.tone, title: tr("actions.review.title"), message: tr("actions.review.message"))147 Card {148 VStack(alignment: .leading, spacing: Tokens.Space.s3) {149 InfoRow(tr("actions.type"), m.type._type, monospaced: true)150 InfoRow(tr("actions.target"), targetLabel)151 InfoRow(tr("actions.criticality"), m.type.criticality.label)152 let lines = ActionsLogic.parameterLines(m.params())153 if lines.isEmpty {154 InfoRow(tr("actions.parameters"), tr("common.none"))155 } else {156 Text(tr("actions.parameters")).font(.callout).foregroundStyle(theme.textSecondary)157 Text(lines.joined(separator: "\n")).font(.callout.monospaced()).foregroundStyle(theme.textPrimary).textSelection(.enabled)158 .accessibilityIdentifier("action-review-params")159 }160 if m.type.requiresJustification { InfoRow(tr("actions.justification"), m.justification) }161 if m.type.requiresConfirmation { InfoRow(tr("actions.confirmation"), m.confirmation, monospaced: true) }162 }163 }164 .accessibilityIdentifier("action-review")165 if let e = m.error { errorView(e) }166 Button { submit(m) } label: {167 HStack {168 if m.isSubmitting { ProgressView().tint(theme.accentContrast) }169 Label(tr("actions.review.send"), systemImage: "paperplane")170 }171 .frame(maxWidth: .infinity, minHeight: Tokens.Size.touchMin)172 }173 .buttonStyle(.mccProminent)174 .disabled(!m.canSubmit || !session.canMutate)175 .accessibilityIdentifier("action-confirm-send")176 Button { reviewing = false } label: { Text(tr("actions.review.back")).frame(maxWidth: .infinity, minHeight: Tokens.Size.touchMin) }177 .buttonStyle(.bordered)178 .disabled(m.isSubmitting)179 .accessibilityIdentifier("action-review-back")180 }181182 private func submit(_ m: ActionFormModel) {183 let nav = nav184 Task {185 guard let created = await m.submit(using: session.actions) else { return }186 nav.sheet = nil187 dismiss()188 nav.go(to: .action(created.id))189 }190 }191}192