Branchwell

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

1import SwiftUI
2import InkspringSchedule
3
4struct PostArticlePicker: View {
5 @Environment(\.studioPalette) private var palette
6 let workspace: Workspace
7 let projectID: String
8 @Binding var articleID: String
9 let openArticle: (WorkspaceRecord) -> Void
10 var createArticle: (() -> Void)? = nil
11 var xReplyArticles: [WorkspaceRecord] = []
12 var allowsChanges = true
13 var publishTogether: (() -> Void)? = nil
14
15 var body: some View {
16 let articles = workspace.records.filter {
17 $0.projectID == projectID && $0.isXArticle
18 }
19 let defaultArticle = articleID.isEmpty && Set(xReplyArticles.map(\.id)).count == 1
20 ? xReplyArticles.first : nil
21 let selected = articles.first { $0.id == articleID } ?? defaultArticle
22 VStack(alignment: .leading, spacing: 6) {
23 Label("X article", systemImage: "doc.richtext")
24 .font(.callout.weight(.semibold))
25 if let selected {
26 Button(selected.title, systemImage: "arrow.up.right") { openArticle(selected) }
27 .accessibilityIdentifier("postArticle.open")
28 Text("Attached to this X post").font(.caption).foregroundStyle(palette.muted)
29 } else if !articleID.isEmpty {
30 Text("Linked article unavailable").font(.caption).foregroundStyle(palette.muted)
31 }
32 ForEach(xReplyArticles.filter { $0.id != selected?.id }) { article in
33 Button("In reply · " + article.title, systemImage: "arrow.turn.down.right") { openArticle(article) }
34 }
35 if allowsChanges {
36 if defaultArticle == nil && (!articles.isEmpty || !articleID.isEmpty) {
37 Picker("Attach an X article", selection: $articleID) {
38 Text("None").tag("")
39 if !articleID.isEmpty && selected == nil { Text("Unavailable article").tag(articleID) }
40 ForEach(articles) { article in Text(article.title).tag(article.id) }
41 }
42 .accessibilityIdentifier("xArticle.picker")
43 }
44 if selected == nil, xReplyArticles.isEmpty, let createArticle {
45 Button("Create linked article", systemImage: "doc.badge.plus", action: createArticle)
46 .accessibilityIdentifier("xArticle.createLinked")
47 } else if selected == nil, articles.isEmpty {
48 Text("Create an X article in this project to attach it here.")
49 .font(.caption).foregroundStyle(palette.muted)
50 }
51 }
52 if let publishTogether {
53 Button("Review post and article…", systemImage: "paperplane", action: publishTogether)
54 .accessibilityIdentifier("postArticle.reviewPair")
55 }
56 }
57 .frame(maxWidth: .infinity, alignment: .leading)
58 .padding(12)
59 .studioSurface(StudioRadius.control, fill: palette.well)
60 }
61}
62
63extension Workspace {
64 func xReplyArticles(for draft: WorkspaceRecord, body: String) -> [WorkspaceRecord] {
65 guard draft["platform"] == "x", ["followup", "thread"].contains(draft["format"]) else { return [] }
66 let posts = XThreadText.posts(in: body)
67 let links = try? XPostArticleLinks(draft: draft, posts: posts, records: records)
68 let articleIDs = Set(links?.replies.values.map(\.id) ?? [])
69 return records.filter { articleIDs.contains($0.id) }
70 }
71}