// // MicropubPublishSheet.swift // Museion // // #33 (S6.1): the publish ask — draft first, then publish (G9: // publish is ask-before; each step is its own explicit click). The // sheet owns the whole two-step flow over the OPEN piece: Send as // Draft (h=entry, name, markdown content) → review on Micro.blog → // Publish (the post-status update against the piece's remembered // draft URL). The app token is entered here ONCE and lives in the // Keychain (G11; re-offered only on an auth failure). Refusals show // inline with the buttons kept (ApplyAskSheet posture). // import SwiftUI /// #33 (S6.1): the two-step Micro.blog ask over the open piece. struct MicropubPublishSheet: View { let posts: PostsModel let home: URL @Environment(\.dismiss) private var dismiss private enum Phase: Equatable { case idle case drafting case drafted case publishing case published } @State private var phase: Phase = .idle /// The draft URL this sheet knows: fresh from a send, or the piece's /// remembered `micropub_url` (a draft sent in an earlier session). @State private var draftURL: String? @State private var previewURL: String? @State private var refusal: String? @State private var tokenField = "" @State private var reentry = false /// Token entry shows when none is saved — or again after an auth /// failure (the entered-once token can be replaced, never a dead end). private var showsTokenField: Bool { reentry || MicropubTokenStore.read() == nil } var body: some View { VStack(alignment: .leading, spacing: 12) { Text("Publish to Micro.blog?") .font(.headline) Text(pieceTitle) .font(.callout.weight(.semibold)) .lineLimit(2) Text("Museion sends it as a draft first — nothing goes out until you publish.") .font(.callout) .foregroundStyle(DesignTokens.muted) if showsTokenField { SecureField("App token", text: $tokenField) .textFieldStyle(.roundedBorder) .font(.system(size: 12, design: DesignTokens.mono)) Text("From micro.blog → Account → App Tokens. Stored in your Keychain.") .font(.system(size: 11, design: DesignTokens.mono)) .foregroundStyle(DesignTokens.muted) } if phase == .drafting || phase == .publishing { ProgressView() } if let url = statusURL { // The draft lives on Micro.blog — the preview link goes // there for review; once published it's the post itself. VStack(alignment: .leading, spacing: 4) { Text(phase == .published ? "Published" : "Draft on Micro.blog") .font(.system(size: 11, design: DesignTokens.mono)) .foregroundStyle(DesignTokens.muted) if let link = URL(string: phase == .published ? url : (previewURL ?? url)) { Link(destination: link) { Text(link.absoluteString) .font(.system(size: 11, design: DesignTokens.mono)) .lineLimit(1) .truncationMode(.middle) } } } } if let refusal { Text(refusal) .font(.callout) .foregroundStyle(.orange) } HStack(spacing: 8) { if phase == .published { Button("Done") { dismiss() } .buttonStyle(.borderedProminent) } else { Button("Publish") { publish() } .buttonStyle(.borderedProminent) .disabled(draftURL == nil || phase == .drafting || phase == .publishing) if draftURL != nil { // The piece changed since the draft — a fresh send // replaces it (and the remembered URL). Button("Send Again") { sendDraft() } .buttonStyle(.bordered) .disabled(phase == .drafting || phase == .publishing) } else { Button("Send as Draft") { sendDraft() } .buttonStyle(.bordered) .disabled(phase == .drafting || phase == .publishing) } Button("Cancel") { dismiss() } .buttonStyle(.bordered) } } .frame(maxWidth: .infinity, alignment: .trailing) } .padding(16) .frame(width: 340, alignment: .leading) .onAppear { // A draft sent in an earlier session is still publishable: // the piece remembers its URL. if draftURL == nil, let id = posts.session?.postID { if let remembered = posts.posts.first(where: { $0.id == id })?.micropubURL { draftURL = remembered } } } } // MARK: Steps /// The open piece's Title (the sheet is only reachable with a piece /// open; the id is the honest fallback). private var pieceTitle: String { posts.session?.postID.map { posts.title(of: $0) } ?? "This piece" } private var statusURL: String? { phase == .drafted || phase == .published || phase == .publishing ? draftURL : nil } /// Resolve the token: a freshly entered one is saved ONCE (rotation /// replaces); otherwise the Keychain's. Nil → the inline ask, never /// a silent send. private func resolveToken() -> String? { if !tokenField.trimmingCharacters(in: .whitespaces).isEmpty { let token = tokenField.trimmingCharacters(in: .whitespaces) MicropubTokenStore.save(token) reentry = false return token } guard let saved = MicropubTokenStore.read() else { return nil } return saved } private func sendDraft() { guard let token = resolveToken() else { refusal = "Paste your Micro.blog app token first." return } refusal = nil phase = .drafting Task { switch await posts.micropubSendDraft(home: home, token: token) { case let .drafted(url, preview): draftURL = url previewURL = preview phase = .drafted case let .authFailed(detail): reentry = true // the entered-once token may be wrong refusal = "Micro.blog rejected the token: \(detail)" phase = draftURL == nil ? .idle : .drafted case let .refused(reason), let .unreachable(reason): refusal = reason phase = draftURL == nil ? .idle : .drafted case let .requestFailed(status, detail): refusal = "Micro.blog said \(status): \(detail)" phase = draftURL == nil ? .idle : .drafted case .published: break } } } private func publish() { guard let url = draftURL, let token = resolveToken() else { refusal = "Paste your Micro.blog app token first." return } refusal = nil phase = .publishing Task { switch await Micropub.publish(url: url, token: token) { case .published: phase = .published case let .authFailed(detail): reentry = true refusal = "Micro.blog rejected the token: \(detail)" phase = .drafted case let .refused(reason), let .unreachable(reason): refusal = reason phase = .drafted case let .requestFailed(status, detail): refusal = "Micro.blog said \(status): \(detail)" phase = .drafted case .drafted: break } } } }