Branchwell

Inkspring / app · inkspring.ai / native-mac / Sources / Inkspring / PublishingHistory.swift

1import SwiftUI
2
3struct PublishingHistory: View {
4 let workspace: Workspace
5 @State private var retry: WorkspaceRecord?
6
7 private var receipts: [WorkspaceRecord] {
8 Array(workspace.records.filter { $0.kind == "publication" }.prefix(50))
9 }
10
11 var body: some View {
12 GroupBox("Publishing history") {
13 if receipts.isEmpty {
14 Text("No publishing attempts yet.").foregroundStyle(.secondary)
15 .frame(maxWidth: .infinity, alignment: .leading)
16 } else {
17 VStack(alignment: .leading, spacing: 12) {
18 ForEach(receipts) { receipt in
19 VStack(alignment: .leading, spacing: 6) {
20 HStack {
21 Text(receipt.title).font(.headline)
22 Spacer()
23 StudioPlatformLabel(name: receipt["platform"],
24 title: (receipt["contentKind"] == "article" ? "X article" : platformName(receipt["platform"])) + " · " + receipt["status"].capitalized)
25 .foregroundStyle(.secondary)
26 }
27 if !receipt["error"].isEmpty { Text(receipt["error"]).foregroundStyle(.secondary) }
28 let steps = receipt.fields["steps"] as? [[String: Any]] ?? []
29 ForEach(Array(steps.enumerated()), id: \.offset) { index, step in
30 if let raw = step["url"] as? String, let url = URL(string: raw),
31 url.scheme == "https", url.user == nil, url.password == nil {
32 StudioExternalLink(receipt["contentKind"] == "article" ? "View article" : steps.count == 1 ? "View post" : "View post \(index + 1)", destination: url)
33 }
34 }
35 if ["uncertain", "publishing"].contains(receipt["status"]) {
36 Button("Allow retry after checking…") { retry = receipt }
37 }
38 }
39 Divider()
40 }
41 }
42 }
43 }
44 .alert("Did you check the post on the platform?", isPresented: Binding(
45 get: { retry != nil }, set: { if !$0 { retry = nil } }
46 )) {
47 Button("Cancel", role: .cancel) { retry = nil }
48 Button("Allow another attempt") {
49 if let receipt = retry { workspace.perform { try workspace.allowPublicationRetry(receipt.id) } }
50 retry = nil
51 }
52 } message: {
53 Text("Only allow a retry if you checked the connected account and the post is missing. An unconfirmed request can still have gone out.")
54 }
55 }
56}