SPB Git

Repository Public

ActionsScreen.swift 4.7 KB 104 lines swift

1import MCCCore2import SwiftUI34/// Action history, with entries to the catalog and the approvals.5struct ActionsScreen: View {6    @Environment(Navigator.self) private var nav7    @Environment(\.mccTheme) private var theme8    @Session private var session9    @State private var state: API.ActionState?1011    var body: some View {12        ScreenStack {13            TileGrid(minimum: 160) {14                Button { nav.open(.actionCatalog) } label: {15                    Label(tr("actions.catalog"), systemImage: "square.grid.2x2").frame(maxWidth: .infinity, minHeight: Tokens.Size.touchMin)16                }17                .buttonStyle(.mccProminent)18                .accessibilityIdentifier("actions-catalog")19                Button { nav.open(.approvals) } label: {20                    Label(approvalsTitle, systemImage: "checkmark.seal").frame(maxWidth: .infinity, minHeight: Tokens.Size.touchMin)21                }22                .buttonStyle(.bordered)23                .accessibilityIdentifier("actions-approvals")24            }25            Picker(tr("actions.filter.state"), selection: $state) {26                Text(tr("common.all")).tag(API.ActionState?.none)27                ForEach(API.ActionState.allCases, id: \.self) { Text($0.label).tag(API.ActionState?.some($0)) }28            }29            .pickerStyle(.menu)30            .frame(minHeight: Tokens.Size.touchMin)31            .accessibilityIdentifier("actions-filter")3233            ResourceSection(resource: session.actions.recent) { all in34                let items = ActionsLogic.filter(all, state: state)35                if items.isEmpty {36                    EmptyStateView(tr("actions.empty"), message: tr("actions.empty.message"), systemImage: "bolt.slash").accessibilityIdentifier("actions-empty")37                } else {38                    LazyVStack(spacing: Tokens.Space.s3) {39                        ForEach(items, id: \.id) { a in40                            RouteRow(route: .action(a.id), identifier: "action-row-" + a.id) { ActionRowLabel(action: a) }41                        }42                    }43                }44            }45        }46        .screen("actions", title: tr("section.actions"), load: { await session.actions.recent.loadIfNeeded() }, reload: { await session.actions.recent.load() })47    }4849    private var approvalsTitle: String {50        let n = ActionsLogic.pendingApprovalCount(session.actions.recent.value ?? [])51        return n > 0 ? tr("actions.approvals.count", n) : tr("actions.approvals")52    }53}5455struct ActionRowLabel: View {56    @Environment(\.mccTheme) private var theme57    let action: API.Action58    var body: some View {59        VStack(alignment: .leading, spacing: Tokens.Space.s1) {60            AdaptiveRow {61                Text(action._type).font(.callout.monospaced().weight(.medium)).foregroundStyle(theme.textPrimary)62            } trailing: {63                StatusBadge(action.state.label, tone: action.state.tone, compact: true)64            }65            Text(ActionsLogic.targetLabel(action)).font(.callout).foregroundStyle(theme.textPrimary)66            Text([action.requestedByDisplayName, action.criticality.label, Format.relative(action.createdAt)].compactMap { $0 }.joined(separator: " · "))67                .font(.footnote).foregroundStyle(theme.textSecondary)68            if action.outcomeUnknown { StatusLabel(tr("actions.outcome_unknown"), tone: .warning).font(.footnote) }69        }70    }71}7273/// Actions waiting for an approval.74struct ApprovalsScreen: View {75    @Session private var session7677    private var resource: Resource<[API.Action]> {78        var f = ActionFilter()79        f.state = .pendingApproval80        return session.actions.list[f]81    }8283    var body: some View {84        ScreenStack {85            if !session.can("actions.approve") {86                InfoBanner(tone: .info, title: tr("actions.approvals.read_only"), message: tr("actions.approvals.read_only.message"))87            }88            ResourceSection(resource: resource) { all in89                let items = ActionsLogic.filter(all, state: .pendingApproval)90                if items.isEmpty {91                    EmptyStateView(tr("actions.approvals.empty"), systemImage: "checkmark.seal").accessibilityIdentifier("approvals-empty")92                } else {93                    LazyVStack(spacing: Tokens.Space.s3) {94                        ForEach(items, id: \.id) { a in95                            RouteRow(route: .action(a.id), identifier: "approval-row-" + a.id) { ActionRowLabel(action: a) }96                        }97                    }98                }99            }100        }101        .screen("approvals", title: tr("actions.approvals"), load: { await resource.loadIfNeeded(maxAge: 5) }, reload: { await resource.load() })102    }103}104
Files main
Menu

Theme