// `/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.importCottonKitimportFoundation@MainActor@ObservablepublicfinalclassRejectedModel {/// One parked write, as the screen draws it.publicstructRow: Equatable, Sendable, Identifiable {publiclet id: String/// The table in plain words — `cotton_money_transactions` reads as/// "Money transactions".publiclet 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.publiclet reason: String/// `Arriving` or `Sending` — which way the write was going.publiclet 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.publiclet stuckSince: String/// Whether `Retry all` can move this one.publiclet retryable: Bool }publicprivate(set) var rows: [Row] = []publicprivate(set) var isFirstRead = true/// The list itself could not be read — a sentence, never a spinner that/// never ends.publicprivate(set) var refusal: String?/// A retry is in flight; the verb is inert while it is.publicprivate(set) var isBusy = falseprivatelet core: any CottonCallingprivatelet toasts: ToastCenterprivatelet now: Datepublicinit(core: any CottonCalling, toasts: ToastCenter, now: Date = Date()) {self.core = coreself.toasts = toastsself.now = now }/// Everything on the list is retryable, so the verb has nothing to warn/// about.publicvar everyRowCanBeRetried: Bool { rows.allSatisfy(\.retryable) }/// How many `Retry all` would actually offer to the fold again.publicvar retryableCount: Int { rows.filter(\.retryable).count }publicfunc load() async {do {let answer = tryawait core.callJSON(CottonMethod.syncRejected) rows = answer.list("rejected").map { row inSelf.display(row, now: now) } refusal = nil } catchlet error asCottonCallError { 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.publicfunc retryAll() async {guard !isBusy else { return } isBusy = truedefer { isBusy = false }do {let answer = tryawait core.callJSON(CottonMethod.syncRetryRejected)let folded = answer.list("folded").countlet stuck = answer.list("stillRejected").count toasts.show(Self.retrySentence(folded: folded, stillRejected: stuck), tone: folded > 0 ? .success : .notice) } catchlet error asCottonCallError { 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.staticfunc 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.staticfunc plainTable(_ table: String) -> String {guard table.hasPrefix("cotton_") else { return table }let words = table.dropFirst("cotton_".count).replacingOccurrences(of: "_", with: " ")guardlet 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.staticfunc directionLabel(_ direction: String) -> String {switch direction {case"pull": return"Arriving"case"push": return"Sending"default: return direction } }staticfunc retrySentence(folded: Int, stillRejected: Int) -> String {switch (folded, stillRejected) {case (0, 0): returnShellLines.line("empty", "retry.nothing")case (0, _): returnShellLines.line("empty", "retry.stuck")case (1, 0): returnShellLines.line("empty", "retry.through_one")case (_, 0): returnShellLines.line("empty", "retry.through", ["n": "\(folded)"])case (1, _): returnShellLines.line("empty", "retry.through_one_stuck", ["stuck": "\(stillRejected)"])default: returnShellLines.line("empty", "retry.through_stuck", ["n": "\(folded)", "stuck": "\(stillRejected)"]) } }}