Cotton code
73 lines · 2.9 KB · SwiftRaw
// OPEN ACCESS (the owner, 17 Nov 2026): an assistant's unlock window, once// allowed, no longer stands as a banner at the foot over the fan. Account// lists the windows open now on one card — who (the assistant), what (the// tables) and the time left, each row the core's words — and its fan holds// End for each. No window open draws no card.//// Every word is the core's (`unlock.windows`: `windows[].said`, `client`,// `heading`, and End as `acts` names it); this file decides nothing about// what a window is. The time left counts down as Account reads the answer// again (`AccountPage`, every sync pass and `HomeSyncKnobs.every`).import CottonFacesimport CottonKitimport CottonValuesimport Foundation/// The windows open now, as Account's Open access card draws them.struct OpenAccess: Equatable, Sendable {    struct Row: Equatable, Sendable {        let grantId: String        /// The assistant holding it, as the core names it.        let client: String?        /// The tables and the time left, in the core's words.        let said: String    }    /// End, as the core names it, and the receipt it says.    struct End: Equatable, Sendable {        let label: String        let receipt: String?    }    let heading: String    let rows: [Row]    let end: End?    /// The card, or nil when no window is open: an empty list draws nothing.    init?(_ answer: JSONValue?) {        guard let answer else { return nil }        let rows: [Row] = answer.list("windows").compactMap { window in            guard let grant = window.text("grantId"), let said = window.text("said") else { return nil }            return Row(grantId: grant, client: window.text("client"), said: said)        }        guard !rows.isEmpty else { return nil }        self.rows = rows        heading = answer.text("heading") ?? ""        end = answer.list("acts").first.flatMap { act in            act.text("label").map { End(label: $0, receipt: act.text("receipt")) }        }    }    /// The card on the plain face: the heading, then each window under its    /// assistant's name.    var drawn: DeckCard {        DeckCard(face: "plain", corner: nil, pressing: false, pace: nil, plate: nil, fields: [            "title": .text(heading),            "subtitle": .text(""),            "facts": .list(rows.map { .object(["label": .text($0.client ?? ""), "said": .text($0.said)]) }),        ])    }    /// End's word on the fan for one window: named by its assistant.    func endWord(_ row: Row) -> String? {        guard let end else { return nil }        return row.client.map { "\(end.label) · \($0)" } ?? end.label    }    /// End one window: `unlock.end` for its grant. The receipt is the    /// caller's to say; a refusal throws.    static func end(_ row: Row, core: any CottonCalling) async throws {        _ = try await core.callJSON(CottonMethod.unlockEnd, UnlockEndRequest(grantId: row.grantId))    }}