Repository Public
1import MCCCore2import SwiftUI34/// Menu of the typed actions the catalog offers for a target kind. Nothing is hard-coded: a type absent from5/// `GET /action-types` is not shown, a type with `available = false` is shown disabled with the server's reason.6struct ActionMenu: View {7 @Environment(AppModel.self) private var app8 @Environment(\.actionHost) private var host9 let kind: API.ActionTargetKind10 let targetID: String11 let targetLabel: String12 var exclude: Set<String> = []1314 var body: some View {15 let types = ActionMenu.types(for: kind, in: app.session.actions.types.value, excluding: exclude)16 Menu {17 if app.session.actions.types.value == nil {18 Text(app.session.actions.types.state.unavailableReason != nil ? tr("actions.menu.unavailable") : tr("state.loading"))19 } else if types.isEmpty {20 Text(tr("actions.menu.none"))21 }22 ForEach(types, id: \._type) { type in23 Button {24 app.requestAction(type._type, on: kind, id: targetID, label: targetLabel, host: host)25 } label: {26 Text(type.available ? "\(type.title) — \(type.criticality.label)" : "\(type.title) — \(type.unavailableReason ?? tr("actions.unavailable.title"))")27 }28 .disabled(!type.available || app.mutationBlocker(permission: type.permission) != nil)29 .accessibilityIdentifier("action-type-\(type._type)")30 }31 } label: {32 Label(tr("actions.menu.title"), systemImage: "bolt")33 }34 .menuStyle(.borderlessButton)35 .fixedSize()36 .task { await app.session.actions.types.loadIfNeeded(maxAge: 120) }37 .accessibilityIdentifier("action-menu")38 }3940 /// Types applicable to a target kind, least critical first.41 static func types(for kind: API.ActionTargetKind, in catalog: [API.ActionType]?, excluding: Set<String> = []) -> [API.ActionType] {42 (catalog ?? []).filter { $0.targetKinds.contains(kind) && !excluding.contains($0._type) }43 .sorted { ($0.criticality.rank, $0._type) < ($1.criticality.rank, $1._type) }44 }45}46