Cotton code
94 lines · 4.0 KB · SwiftRaw
import CottonKitimport CottonValuesimport SwiftUI/// A table's app capabilities without its record list. Opening the chooser/// reads offered acts; only pressing one starts its existing flow.struct RecordActionChooser: View {    @Environment(\.theme) private var theme    let definition: TableDefinition    let title: String    let filter: [String: RecordValue]    let asking: String?    let onOpen: (String) -> Void    @Environment(\.records) private var store    @Environment(Navigator.self) private var navigator: Navigator?    @Environment(\.openURL) private var openURL    @Environment(\.keptFlow) private var keptFlow    @Environment(ToastCenter.self) private var toasts    @State private var acts: RecordActs    @State private var revealed: RevealedValue?    @State private var change: ChangeRecord?    @State private var undone = false    @State private var asked = false    @State private var problem: String?    init(definition: TableDefinition, title: String, where filter: [String: RecordValue] = [:], asking: String? = nil, onOpen: @escaping (String) -> Void) {        self.definition = definition        self.title = title        self.filter = filter        self.asking = asking        self.onOpen = onOpen        _acts = State(initialValue: RecordActs(table: definition.id, where: filter))    }    var body: some View {        PaperScreen {            if let revealed {                RevealedValueCard(revealed: revealed, identifier: "chooser-\(definition.id)", onCopy: {                    Task {                        guard let store else { return }                        do {                            if let copied = try await SecretCopy.copy(revealed, definition: definition, store: store) { receive(copied) }                            self.revealed = nil                        } catch { toasts.show(SecretCopy.problem(error), tone: .error) }                    }                }, onSaved: { self.revealed = nil })            }            RecordActionButtons(actions: acts.head, problem: problem ?? acts.headProblem,                                identifier: "chooser-\(definition.id)", note: acts.note) {                acts.tapped($0, on: nil, store: store, surface: surface)            }        }        // The receipt stands at the toast edge, the top (the owner, 17 Nov 2026).        .receiptAtToastEdge {            if let change {                ChangeReceipt(change: change, table: definition, undone: undone, onUndone: { _ in                    undone = true                    revealed = nil                    Task { await load() }                }, onDismiss: { self.change = nil })                .padding(.horizontal, ChromeKnobs.chromeSettingsMargin)            }        }        .navigationTitle(title)        .task { await load() }        .askingFirst(acts, identifier: "chooser-\(definition.id)-confirm", onConfirm: {            acts.perform($0.action, on: $0.record, store: store, surface: surface)        }, onAnswer: { await acts.answer($0, values: $1, store: store, surface: surface) })    }    private var surface: RecordActSurface {        RecordActSurface(open: { onOpen($0) }, reveal: { revealed = $0 },                         received: { receive($0) },                         reread: { await load() }, current: { _ in nil },                         keptFlow: keptFlow, navigator: navigator, openURL: openURL, toasts: toasts)    }    private func receive(_ change: ChangeRecord) {        self.change = change        undone = false    }    private func load() async {        do {            try await (store as? any CreationRecords)?.prepareCreation(table: definition.id, where: filter)            problem = nil            await acts.loadHead(store)            if !asked, let action = acts.head.first(where: { $0.id == asking }), !action.fields.isEmpty {                asked = true                acts.tapped(action, on: nil, store: store, surface: surface)            }        } catch { problem = ActionRefused.words(for: error) }    }}