// `/rejected` — the writes this device would not fold. // // **What a parked write IS.** Sync is an exchange of operations. When one // arrives that this file will not apply — an unknown table, a shape it cannot // read, a row it will not overwrite — it is kept whole in `_parked` // rather than dropped, and nothing has ever been able to look at it without // opening the database in a SQLite browser. That is what this screen is. // // **The list is this DEVICE's, and two devices can honestly differ.** // `_parked` is device-local and never syncs, so "my laptop has this note // and my phone does not" is a question with an answer here rather than a shrug. // The join sheet's merge report already counts them; this is where the count // leads. // // **There is no row title, and that is not an omission.** A parked PULL is an // operation this file never applied, so the row it describes does not exist on // this device — a title lookup would answer nothing for exactly the rows a // person is asking about. What is drawn is what the core projects: the table in // plain words, the reason, the direction and how long it has been stuck. // // **Only the pull half can move.** A parked `push` row is the SERVER's verdict // on a write this device made, and nothing on this device can change it. The // core marks each row `retryable`, the screen says which are not, and "Retry // all" is honest about it rather than offering a button that does nothing. import CottonKit import Foundation @MainActor @Observable public final class RejectedModel { /// One parked write, as the screen draws it. public struct Row: Equatable, Sendable, Identifiable { public let id: String /// The table in plain words — `cotton_money_transactions` reads as /// "Money transactions". public let table: String /// The core's own sentence for why this file would not take it. Drawn /// verbatim; nothing here composes prose about somebody's failed write. public let reason: String /// `Arriving` or `Sending` — which way the write was going. public let direction: String /// When it was FIRST refused, already formatted. It never moves, so how /// long a write has been stuck is readable at a glance. public let stuckSince: String /// Whether `Retry all` can move this one. public let retryable: Bool } public private(set) var rows: [Row] = [] public private(set) var isFirstRead = true /// The list itself could not be read — a sentence, never a spinner that /// never ends. public private(set) var refusal: String? /// A retry is in flight; the verb is inert while it is. public private(set) var isBusy = false private let core: any CottonCalling private let toasts: ToastCenter private let now: Date public init(core: any CottonCalling, toasts: ToastCenter, now: Date = Date()) { self.core = core self.toasts = toasts self.now = now } /// Everything on the list is retryable, so the verb has nothing to warn /// about. public var everyRowCanBeRetried: Bool { rows.allSatisfy(\.retryable) } /// How many `Retry all` would actually offer to the fold again. public var retryableCount: Int { rows.filter(\.retryable).count } public func load() async { do { let answer = try await core.callJSON(CottonMethod.syncRejected) rows = answer.list("rejected").map { row in Self.display(row, now: now) } refusal = nil } catch let error as CottonCallError { refusal = error.message } catch { refusal = CottonStrings.somethingWentWrong } isFirstRead = false } /// Offer every parked write to the fold again. /// /// **No `ids`.** The core reads an absent list as "all of them", and a /// screen that sent the ids it happens to be drawing would skip whatever /// arrived since the last read — on a screen whose whole job is that /// nothing is quietly left behind. public func retryAll() async { guard !isBusy else { return } isBusy = true defer { isBusy = false } do { let answer = try await core.callJSON(CottonMethod.syncRetryRejected) let folded = answer.list("folded").count let stuck = answer.list("stillRejected").count toasts.show(Self.retrySentence(folded: folded, stillRejected: stuck), tone: folded > 0 ? .success : .notice) } catch let error as CottonCallError { toasts.show(error.message, tone: .error) } catch { toasts.show(CottonStrings.somethingWentWrong, tone: .error) } await load() } // ── What a row says ────────────────────────────────────────────────── // // Pure statics: a test drives them with no core and no screen. static func display(_ row: JSONValue, now: Date) -> Row { Row( id: row.text("opId") ?? "", table: plainTable(row.text("table") ?? ""), reason: row.text("reason") ?? "", direction: directionLabel(row.text("direction") ?? ""), stuckSince: row.day("at")?.recent ?? "", retryable: row.flag("retryable") ) } /// `cotton_money_transactions` → `Money transactions`. /// /// **Mechanical, and total on purpose.** A hand-written table of nice names /// would answer nothing for the table added next week, and a row whose name /// came back blank is exactly the row somebody is here to look at. A /// physical name left as it is reads oddly; a missing one reads as nothing. /// /// **Only a `cotton_` name is rewritten.** Every synced table carries that /// prefix, so a parked write's table normally has one. Anything else — a /// device-local table like `_media`, or a name from a build nobody here /// knows — is the store's own bookkeeping rather than a kind of record, and /// is drawn EXACTLY as it came: this is the screen a person opens when /// something is wrong, and a prettied-up name would be this code guessing /// in the one place a guess is worth nothing. static func plainTable(_ table: String) -> String { guard table.hasPrefix("cotton_") else { return table } let words = table.dropFirst("cotton_".count).replacingOccurrences(of: "_", with: " ") guard let first = words.first else { return table } return first.uppercased() + words.dropFirst() } /// Which way the write was going, in the words a person uses. /// /// A word this build does not know is drawn AS ITSELF rather than mapped /// onto the nearest one — the same rule the activity screen's states follow. static func directionLabel(_ direction: String) -> String { switch direction { case "pull": return "Arriving" case "push": return "Sending" default: return direction } } static func retrySentence(folded: Int, stillRejected: Int) -> String { switch (folded, stillRejected) { case (0, 0): return ShellLines.line("empty", "retry.nothing") case (0, _): return ShellLines.line("empty", "retry.stuck") case (1, 0): return ShellLines.line("empty", "retry.through_one") case (_, 0): return ShellLines.line("empty", "retry.through", ["n": "\(folded)"]) case (1, _): return ShellLines.line("empty", "retry.through_one_stuck", ["stuck": "\(stillRejected)"]) default: return ShellLines.line("empty", "retry.through_stuck", ["n": "\(folded)", "stuck": "\(stillRejected)"]) } } }