SPB Git

Repository Public

ActionCatalogScreen.swift 10.3 KB 209 lines swift

1import MCCCore2import SwiftUI34/// What an action type is and what the server will require to run it.5struct ActionTypeSummary: View {6    @Environment(\.mccTheme) private var theme7    let type: API.ActionType8    var body: some View {9        VStack(alignment: .leading, spacing: Tokens.Space.s2) {10            AdaptiveRow {11                Text(type.title).font(.body.weight(.medium)).foregroundStyle(theme.textPrimary).multilineTextAlignment(.leading)12            } trailing: {13                StatusBadge(type.criticality.label, tone: type.criticality.tone, compact: true)14                    .accessibilityLabel(tr("actions.criticality"))15                    .accessibilityValue(type.criticality.label)16            }17            Text(type._type).font(.footnote.monospaced()).foregroundStyle(theme.textSecondary)18            if !type.description.isEmpty {19                Text(type.description).font(.callout).foregroundStyle(theme.textSecondary).multilineTextAlignment(.leading)20            }21            ForEach(ActionsLogic.requirements(of: type), id: \.self) { r in22                Label(r.title, systemImage: r.symbol).font(.footnote).foregroundStyle(theme.textSecondary)23            }24            if !type.available {25                StatusLabel(type.unavailableReason ?? tr("actions.unavailable"), tone: .stale).font(.footnote)26            }27        }28        .frame(maxWidth: .infinity, alignment: .leading)29    }30}3132/// Catalog of `GET /action-types`, grouped by criticality. Picking a type asks for a target, then opens the request form.33struct ActionCatalogScreen: View {34    @Environment(Navigator.self) private var nav35    @Environment(\.mccTheme) private var theme36    @Session private var session37    @State private var picking: PickedType?3839    private struct PickedType: Identifiable { let type: API.ActionType; var id: String { type._type } }4041    var body: some View {42        ScreenStack {43            ResourceSection(resource: session.actions.types) { all in44                ForEach(ActionsLogic.grouped(all), id: \.criticality) { group in45                    SectionCard(tr("actions.catalog.group", group.criticality.label)) {46                        ForEach(group.types, id: \._type) { t in47                            Button { picking = PickedType(type: t) } label: {48                                HStack(spacing: Tokens.Space.s3) {49                                    ActionTypeSummary(type: t)50                                    Image(systemName: "chevron.right").font(.footnote.weight(.semibold)).foregroundStyle(theme.textTertiary).accessibilityHidden(true)51                                }52                                .padding(.vertical, Tokens.Space.s2)53                                .frame(minHeight: Tokens.Size.touchMin)54                                .contentShape(Rectangle())55                            }56                            .buttonStyle(.plain)57                            .disabled(!t.available)58                            .accessibilityIdentifier("catalog-type-" + t._type)59                            if t._type != group.types.last?._type { Divider().overlay(theme.borderSubtle) }60                        }61                    }62                }63            }64        }65        .screen("action-catalog", title: tr("actions.catalog"), load: { await session.actions.types.loadIfNeeded(maxAge: 120) }, reload: { await session.actions.types.load() })66        .sheet(item: $picking) { p in67            ActionTargetPicker(type: p.type) { candidate in68                picking = nil69                let nav = nav70                Task { @MainActor in71                    try? await Task.sleep(for: .milliseconds(400))72                    nav.sheet = .actionRequest(type: p.type._type, target: candidate.target, targetLabel: candidate.label, preset: [:])73                }74            }75        }76    }77}7879/// Chooses the target of an action type among the objects of its target kinds.80private struct ActionTargetPicker: View {81    @Environment(\.dismiss) private var dismiss82    @Environment(\.mccTheme) private var theme83    @Session private var session84    let type: API.ActionType85    let onPick: (ActionTargetCandidate) -> Void86    @State private var query = ""8788    var body: some View {89        NavigationStack {90            List {91                ForEach(type.targetKinds, id: \.self) { kind in92                    Section(kind.label) {93                        let items = candidates(kind).filter { query.isEmpty || $0.label.localizedCaseInsensitiveContains(query) || ($0.detail ?? "").localizedCaseInsensitiveContains(query) }94                        if items.isEmpty {95                            Text(stateText(kind)).font(.callout).foregroundStyle(theme.textSecondary)96                        }97                        ForEach(items) { c in98                            Button { onPick(c) } label: {99                                VStack(alignment: .leading, spacing: Tokens.Space.s1) {100                                    Text(c.label).font(.body).foregroundStyle(theme.textPrimary)101                                    if let d = c.detail { Text(d).font(.footnote).foregroundStyle(theme.textSecondary) }102                                }103                                .frame(maxWidth: .infinity, minHeight: Tokens.Size.touchMin, alignment: .leading)104                                .contentShape(Rectangle())105                            }106                            .buttonStyle(.plain)107                            .accessibilityIdentifier("target-" + c.label)108                        }109                    }110                }111            }112            .searchable(text: $query, prompt: tr("actions.target.search"))113            .navigationTitle(tr("actions.target.choose"))114            .navigationBarTitleDisplayMode(.inline)115            .toolbar { ToolbarItem(placement: .cancellationAction) { Button(tr("common.cancel")) { dismiss() } } }116            .task {117                for kind in type.targetKinds {118                    switch kind {119                    case .node: await session.nodes.list.loadIfNeeded(maxAge: 60)120                    case .service: await session.services.list.loadIfNeeded(maxAge: 60)121                    case .inferenceServer: await session.models.list.loadIfNeeded(maxAge: 60)122                    case .job: await session.jobs.jobs.loadIfNeeded(maxAge: 30)123                    }124                }125            }126            .accessibilityIdentifier("screen-action-target")127        }128        .mccThemed()129    }130131    private func candidates(_ kind: API.ActionTargetKind) -> [ActionTargetCandidate] {132        switch kind {133        case .node: ActionsLogic.nodeCandidates(session.nodes.list.value ?? [])134        case .service: ActionsLogic.serviceCandidates(session.services.list.value ?? [])135        case .inferenceServer: ActionsLogic.modelCandidates(session.models.list.value ?? [])136        case .job: ActionsLogic.jobCandidates(session.jobs.jobs.value ?? [])137        }138    }139140    private func stateText(_ kind: API.ActionTargetKind) -> String {141        let (loading, error): (Bool, APIError?) = switch kind {142        case .node: (session.nodes.list.state.isLoading, session.nodes.list.state.error)143        case .service: (session.services.list.state.isLoading, session.services.list.state.error)144        case .inferenceServer: (session.models.list.state.isLoading, session.models.list.state.error)145        case .job: (session.jobs.jobs.state.isLoading, session.jobs.jobs.state.error)146        }147        if loading { return tr("state.loading") }148        if let error { return error.message }149        return tr("actions.target.none")150    }151}152153/// The action types applicable to a given target (opened from a node, a service…).154struct ActionPickerSheet: View {155    @Environment(Navigator.self) private var nav156    @Environment(\.dismiss) private var dismiss157    @Environment(\.mccTheme) private var theme158    @Session private var session159    let target: API.ActionTarget160    let targetLabel: String161162    var body: some View {163        ScrollView {164            VStack(alignment: .leading, spacing: Tokens.Space.s4) {165                InfoRow(target.kind.label, targetLabel)166                ResourceSection(resource: session.actions.types) { all in167                    let types = ActionsLogic.types(for: target.kind, in: all)168                    if types.isEmpty {169                        EmptyStateView(tr("actions.picker.empty"), systemImage: "bolt.slash")170                    } else {171                        LazyVStack(spacing: Tokens.Space.s3) {172                            ForEach(types, id: \._type) { t in173                                Button { pick(t) } label: {174                                    ActionTypeSummary(type: t)175                                        .padding(Tokens.Space.s4)176                                        .frame(minHeight: Tokens.Size.touchMin)177                                        .background(theme.surface, in: RoundedRectangle(cornerRadius: Tokens.Radius.lg, style: .continuous))178                                        .overlay(RoundedRectangle(cornerRadius: Tokens.Radius.lg, style: .continuous).strokeBorder(theme.borderSubtle, lineWidth: 1))179                                        .contentShape(RoundedRectangle(cornerRadius: Tokens.Radius.lg, style: .continuous))180                                }181                                .buttonStyle(.plain)182                                .disabled(!t.available)183                                .accessibilityIdentifier("action-type-" + t._type)184                            }185                        }186                    }187                }188            }189            .padding(Tokens.Space.s5)190        }191        .background(theme.canvas.ignoresSafeArea())192        .navigationTitle(tr("actions.picker.title"))193        .navigationBarTitleDisplayMode(.inline)194        .toolbar { ToolbarItem(placement: .cancellationAction) { Button(tr("common.cancel")) { dismiss() }.accessibilityIdentifier("action-picker-cancel") } }195        .task { await session.actions.types.loadIfNeeded(maxAge: 120) }196        .accessibilityIdentifier("screen-action-picker")197    }198199    /// Replaces this sheet by the request form: close first, present on a later run loop.200    private func pick(_ t: API.ActionType) {201        let nav = nav, target = target, label = targetLabel202        nav.sheet = nil203        Task { @MainActor in204            try? await Task.sleep(for: .milliseconds(400))205            nav.sheet = .actionRequest(type: t._type, target: target, targetLabel: label, preset: [:])206        }207    }208}209
Files main
Menu

Theme