// // FixCardsWindow.swift // Museion // // #25 (S3.3): the Fix Cards palette becomes the card work surface. Open // cards lead with their actions — Apply (a blocking ask), Skip, Done, // Promote (#26), Jump (#204) — closed cards dim behind them. No apply-all, no batch, nothing applies // without the ask (S3.3). #23's show-only surface kept its bones. // import SwiftUI /// Palette body: the open piece's stored fix cards (`/cards/`, any /// MCP client's submissions — PostsModel re-derives on open/rescan, so this /// stays current via @Observable). Re-contextualizes as the piece changes. struct FixCardsPalette: View { @Environment(AppGateState.self) private var gate var body: some View { let posts = gate.posts VStack(alignment: .leading, spacing: 0) { if let title = openPieceTitle { Text(title) .font(.headline) .padding(.horizontal, 12) .padding(.top, 12) .padding(.bottom, 4) } if posts.openPieceCards.isEmpty { EmptyCardsState(hasPiece: posts.session?.postID != nil) } else { CardList(cards: ordered(posts.openPieceCards)) // The visible trust posture (S2.5): apply = ask-before, one // card at a time. Never-do needs no control here — nothing // auto-applies; the ask is the only path to a write. Text("apply = ask-before · one card at a time") .font(.system(size: 11, design: DesignTokens.mono)) .foregroundStyle(DesignTokens.muted) .padding(.horizontal, 12) .padding(.bottom, 10) .frame(maxWidth: .infinity, alignment: .leading) } } .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) // The window reads as app chrome, not a document — the same warm // cream the workspace root/rail paints (DesignTokens.background). .background(DesignTokens.background) } /// The pass label's title source — PostsModel's shared lookup. private var openPieceTitle: String? { guard let id = gate.posts.session?.postID else { return nil } return gate.posts.title(of: id) } /// Open cards first, closed after — both groups keep the scan's order /// (newest pass first, rank ascending; #25 S3.2). Live attention stays /// on open work. private func ordered(_ cards: [FixCardStore.StoredFixCard]) -> [FixCardStore.StoredFixCard] { cards.filter { $0.status == .open } + cards.filter { $0.status != .open } } } /// Nothing filed yet — names how cards arrive. private struct EmptyCardsState: View { let hasPiece: Bool var body: some View { VStack(alignment: .leading, spacing: 6) { Text(hasPiece ? "No fix cards yet." : "No piece open.") .font(.headline) Text(hasPiece ? "Run a pass — cards land here as the mentor files them." : "Open a post — its fix cards collect here.") .font(.callout) .foregroundStyle(.secondary) } .padding(12) .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) } } // ponytail: eager VStack in the ScrollView — the pass ceiling caps this at // ~tens of cards; LazyVStack if a piece ever accumulates hundreds. /// The piece's stored cards, open-first. The header counts OPEN work — /// closed cards must not read as pending (#25). private struct CardList: View { let cards: [FixCardStore.StoredFixCard] var body: some View { let openCount = cards.count(where: { $0.status == .open }) ScrollView { VStack(alignment: .leading, spacing: 8) { Text(openCount < cards.count ? "FIX CARDS · \(openCount) open" : "FIX CARDS · \(openCount)") .font(.system(size: 11, design: DesignTokens.mono)) .foregroundStyle(DesignTokens.muted) ForEach(cards, id: \.id) { card in CardRow(card: card) } } .padding(12) .frame(maxWidth: .infinity, alignment: .leading) } } } /// One card: pattern title, problem + fix body, rank · pass · lane · /// emitter mono meta (ADR-0006). A stale anchor reads distinct — orange — /// and never offers Apply or Jump (#204). Closed cards dim, record their /// status in the meta, and keep only Promote and Jump — a closed card that /// is already promoted keeps neither (its row is gone). The blocking Apply /// ask is the ROW's own sheet: one card asks at a time (S3.3). private struct CardRow: View { let card: FixCardStore.StoredFixCard @Environment(AppGateState.self) private var gate /// The row's own blocking ask; Apply arms it, the sheet answers it. @State private var asking = false /// #26: the promotion ask — Promote arms it, its sheet answers it. @State private var promoting = false var body: some View { let stale = card.anchorStatus == .stale let open = card.status == .open return VStack(alignment: .leading, spacing: 6) { Label(card.pattern, systemImage: "rectangle.and.pencil.and.ellipsis") .font(.callout.weight(.semibold)) .foregroundStyle(stale ? .orange : DesignTokens.foreground) if !card.problem.isEmpty { Text(card.problem).font(.callout) } if !card.fix.isEmpty { Text("Fix: \(card.fix)").font(.callout) } Text(meta) .font(.system(size: 11, design: DesignTokens.mono)) .foregroundStyle(stale ? .orange : DesignTokens.muted) if open || card.promotedAt == nil { // The actions this card offers (#25 S3.3, #26 S4.1, #204): // Jump on every card whose anchor still resolves (closed cards // included — the span is still a real place; stale anchors offer // none — nothing to land on); mechanical + valid → Apply (the // blocking ask) and Skip; demonstration → Done and Skip (never // Apply, ADR-0006); stale anchor → Skip only. Promote (its own // ask) sits between the primary action and Skip, and outlives // the card — closed cards keep it until promoted (#26: the // Learning is the durable artifact). HStack(spacing: 8) { if !stale { Button("Jump") { gate.posts.revealCard(card) } .buttonStyle(.borderless) .controlSize(.small) .help("Select the anchored span in the piece") } if open, !stale { if card.lane == .mechanical { Button("Apply") { asking = true } .buttonStyle(.bordered) .controlSize(.small) } else { Button("Done") { gate.posts.doneCard(card) } .buttonStyle(.bordered) .controlSize(.small) } } if card.promotedAt == nil { Button("Promote") { promoting = true } .buttonStyle(.bordered) .controlSize(.small) } if open { SkipButton(card: card) } } .frame(maxWidth: .infinity, alignment: .trailing) } } .padding(10) .frame(maxWidth: .infinity, alignment: .leading) .background(.quaternary.opacity(0.3), in: RoundedRectangle(cornerRadius: 8)) .opacity(open ? 1 : 0.45) // Apply = ask-before, a BLOCKING ask: the sheet is the only path // from the Apply button to a write. .sheet(isPresented: $asking) { ApplyAskSheet(card: card, posts: gate.posts) } // #26: Promote is its own blocking ask — one confirm, one Learning. .sheet(isPresented: $promoting) { PromoteAskSheet(card: card, posts: gate.posts) } } /// rank · pass · lane · emitter, plus the card's truth markers: the /// stale anchor, the trust level on appliable mechanical cards (S2.5), /// the close status, and the promotion stamp (#26). private var meta: String { var value = "\(card.lane.rawValue) · \(card.emitter.rawValue)" if let pass = card.provenance?.pass { value = "pass \(pass) · " + value } if let rank = card.rank { value = "#\(rank) · " + value } if card.anchorStatus == .stale { value += " · stale anchor" } if card.status == .open, card.lane == .mechanical, card.anchorStatus != .stale { value += " · apply · ask-before" } if card.status != .open { value += " · \(card.status.rawValue)" } if card.promotedAt != nil { value += " · promoted" } return value } } /// Skip — not this instance (CONTEXT.md): closes the card, records /// nothing beyond the flip. private struct SkipButton: View { let card: FixCardStore.StoredFixCard @Environment(AppGateState.self) private var gate var body: some View { Button("Skip") { gate.posts.skipCard(card) } .buttonStyle(.borderless) .controlSize(.small) } } /// The blocking ask (#25 S3.3): the exact edit, in full, before any write. /// No apply-all, no batch, nothing applies without the ask — Apply is the /// sheet's only write path, Cancel writes nothing. Outcome states: stale /// swaps the body (no retry — the anchor no longer resolves), a refusal /// shows its reason with the buttons kept. private struct ApplyAskSheet: View { let card: FixCardStore.StoredFixCard let posts: PostsModel @Environment(\.dismiss) private var dismiss @State private var wentStale = false @State private var refusal: String? var body: some View { VStack(alignment: .leading, spacing: 12) { if wentStale { Text("The text changed since this card was issued — nothing was selected or changed.") .font(.callout) Button("Close") { dismiss() } .frame(maxWidth: .infinity, alignment: .trailing) } else { // Untrusted text in bounded chrome: quote/fix are // client-authored, so the ask scrolls instead of growing // past the sheet and pushing the buttons out of reach. ScrollView { VStack(alignment: .leading, spacing: 12) { Text(card.pattern) .font(.headline) MonoBlock( label: card.anchor.occurrence > 1 ? "Anchored · occurrence \(card.anchor.occurrence)" : "Anchored", text: card.anchor.quote, ) if let context = card.anchor.context { Text(context) .font(.system(size: 11, design: DesignTokens.mono)) .foregroundStyle(DesignTokens.muted) } MonoBlock(label: "Replacement", text: card.fix.isEmpty ? "(cut — the span is removed)" : card.fix) Text("Apply writes this edit to the piece · ask-before") .font(.system(size: 11, design: DesignTokens.mono)) .foregroundStyle(DesignTokens.muted) if let refusal { Text(refusal) .font(.callout) .foregroundStyle(.orange) } } .frame(maxWidth: .infinity, alignment: .leading) } HStack(spacing: 8) { Button("Apply") { apply() } .buttonStyle(.borderedProminent) Button("Cancel") { dismiss() } .buttonStyle(.bordered) } .frame(maxWidth: .infinity, alignment: .trailing) } } .padding(16) .frame(width: 320, alignment: .leading) } private func apply() { switch posts.applyCard(card) { case .applied: dismiss() case .stale: wentStale = true case let .refused(reason): refusal = reason } } } /// One labeled mono text block — the ask's edit halves. private struct MonoBlock: View { let label: String let text: String var body: some View { VStack(alignment: .leading, spacing: 4) { Text(label) .font(.system(size: 11, design: DesignTokens.mono)) .foregroundStyle(DesignTokens.muted) Text(text) .font(.system(size: 12, design: DesignTokens.mono)) .textSelection(.enabled) .frame(maxWidth: .infinity, alignment: .leading) .padding(8) .background(.quaternary.opacity(0.3), in: RoundedRectangle(cornerRadius: 6)) } } }