import CottonKit import SwiftUI /// What a personal table's page draws while its own database is not yet in /// hand: never blank cream for good (architect, 14 Oct 2026 — the fan's /// Machines page drew nothing while the held list was unread). A skeleton /// while the held list is on its way, else why the records cannot be read. enum PersonalScopeWait: Equatable { case reading case unread(title: String, message: String, retries: Bool) static func says(plural: String, core: Bool, listed: Bool?, refusal: String?, personal: String?) -> Self { let title = ShellLines.line("reading", "list.could_not_read", ["plural": plural]) guard core, let listed else { return .unread(title: title, message: Words.noRecords, retries: false) } if personal != nil || !listed { return .reading } if let refusal { return .unread(title: title, message: refusal, retries: true) } return .unread(title: title, message: Words.noPersonal, retries: true) } enum Words { static var noRecords: String { ShellLines.line("boot", "personal.no_records") } static var noPersonal: String { ShellLines.line("boot", "personal.no_personal") } static var retry: String { ShellLines.line("boot", "personal.retry") } } } /// A personal tool keeps its own database even while the window shows another. /// The content uses the ordinary record surfaces and the native record store. struct PersonalRecordScope: View { /// The table's plural, for the words while it waits. let plural: String let content: Content @Environment(\.cottonCore) private var core @Environment(\.records) private var parent @Environment(DatabasesModel.self) private var databases: DatabasesModel? /// The personal database's reader, made in the frame the page opens in: /// a local list never waits a frame behind a skeleton for it (owner, /// 15 Oct 2026: "we're on the local database, everything should load /// instantly"). One per identity, so the list's model keeps its store. @State private var made = Made() init(plural: String, @ViewBuilder content: () -> Content) { self.plural = plural self.content = content() } private struct Identity: Hashable { let backend: ObjectIdentifier? let personal: String? let corePresent: Bool } private var identity: Identity { Identity(backend: parent.map { ObjectIdentifier($0) }, personal: databases?.held.first(where: \.isPersonal)?.databaseId, corePresent: core != nil) } var body: some View { Group { if let records = made.records(for: identity, core: core) { content.environment(\.records, records) } else { waiting } } // The page needs the held list: it asks for it when nobody has yet. .task { if let databases, !databases.listed { await databases.refresh() } } } /// Holds the reader across draws without being state a draw writes. @MainActor private final class Made { private var built: (identity: Identity, records: CoreRecords)? func records(for identity: Identity, core: (any CottonCalling)?) -> CoreRecords? { if let built, built.identity == identity { return built.records } guard let core, let personal = identity.personal else { return nil } let records = CoreRecords(core: DatabaseScopedCore(underlying: core, database: personal)) built = (identity, records) return records } } @ViewBuilder private var waiting: some View { let says = PersonalScopeWait.says( plural: plural, core: core != nil, listed: databases?.listed, refusal: databases?.readRefusal, personal: identity.personal) PaperScreen { switch says { case .reading: SkeletonPage(identifier: "personal-scope-reading") case .unread(let title, let message, let retries): QuietState( title: title, message: message, actionLabel: retries ? PersonalScopeWait.Words.retry : nil, action: retries ? { Task { await databases?.refresh() } } : nil) } } } } /// Session defaults (`/code/defaults`): drawn once this app's remembered /// model has reached the core (`CodeDefaults.carryOver`), so the page's /// Model card reads the device's choice rather than none. struct CodeDefaultsCarried: View { let content: Content @Environment(\.cottonCore) private var core @State private var carried = false @State private var refused: String? init(@ViewBuilder content: () -> Content) { self.content = content() } var body: some View { Group { if carried { content } else if let refused { PaperScreen { QuietState(title: ShellLines.line("boot", "defaults.unread_title"), message: refused, actionLabel: ShellLines.line("refusal", "actions.try_again"), action: { self.refused = nil }) } } else { PaperScreen { EmptyView() } } } .task(id: refused == nil) { guard let core, !carried, refused == nil else { return } do { try await CodeDefaults.carryOver(core) carried = true } catch let said as ActionRefused { refused = said.sentence } catch { refused = ShellLines.line("empty", "unchanged") } } } }