Branchwell

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

1import SwiftUI
2
3struct RepliesView: View {
4 let workspace: Workspace
5 let openDraft: (WorkspaceRecord) -> Void
6 @State private var filter = "Inbox"
7 @State private var ignoring: Set<String> = []
8 @State private var confirmsIgnoreAll = false
9
10 private var inbox: ReplyInbox { ReplyInbox(workspace: workspace) }
11 private var rows: [WorkspaceRecord] {
12 inbox.rows.filter { filter == "Ignored" ? $0["status"] == "archived" : $0["status"] != "archived" }
13 }
14
15 var body: some View {
16 StudioScrollView {
17 VStack(alignment: .leading, spacing: 15) {
18 HStack {
19 PageHeading(title: "Keep the conversation going.",
20 subtitle: "Read replies and mentions, then draft a response for review.")
21 Spacer()
22 if workspace.refreshingReplies { ProgressView().controlSize(.small) }
23 Button("Refresh X", systemImage: "arrow.clockwise") { Task { await refresh() } }
24 .disabled(workspace.refreshingReplies)
25 }
26 VStack(alignment: .leading, spacing: 8) {
27 Toggle("Check X every five minutes while Inkspring is open", isOn: Binding(
28 get: { inbox.monitoring }, set: { value in workspace.perform { try inbox.setMonitoring(value) } }
29 ))
30 if let date = inbox.lastChecked {
31 Text("Last checked \(date.formatted(date: .abbreviated, time: .shortened))").foregroundStyle(.secondary)
32 }
33 }
34 .frame(maxWidth: .infinity, alignment: .leading)
35 .padding(15)
36 .studioSurface()
37 HStack {
38 StudioTabs(selection: $filter, tabs: ["Inbox", "Ignored"], label: { $0 })
39 Spacer()
40 if filter == "Inbox" && !rows.isEmpty {
41 Button("Ignore all", systemImage: "tray.and.arrow.down") {
42 ignoring = Set(rows.map(\.id))
43 confirmsIgnoreAll = true
44 }
45 .help("Move the replies currently in your inbox to Ignored.")
46 }
47 }
48 if rows.isEmpty {
49 ContentUnavailableView("No replies here", systemImage: "bubble.left.and.bubble.right")
50 }
51 ForEach(rows) { reply in
52 GroupBox(reply.title) {
53 VStack(alignment: .leading, spacing: 8) {
54 if let received = scheduleDate(reply["createdAt"]) {
55 Text(received.formatted(date: .abbreviated, time: .shortened))
56 .font(.caption)
57 .foregroundStyle(.secondary)
58 }
59 Text(reply.body).textSelection(.enabled)
60 if let parent = reply.fields["parent"] as? [String: Any], let text = parent["text"] as? String {
61 DisclosureGroup("Conversation context") { Text(text).textSelection(.enabled) }
62 }
63 HStack {
64 Text(reply["status"] == "archived" ? "Ignored" : reply["status"].capitalized)
65 .foregroundStyle(.secondary)
66 if let url = URL(string: reply["url"]), url.scheme == "https", url.host == "x.com" {
67 StudioExternalLink("View on X", destination: url)
68 }
69 Spacer()
70 if reply["status"] != "replied" && reply["status"] != "archived" {
71 Button(reply["draftId"].isEmpty ? "Draft a reply" : "Open draft") { openDraft(reply) }
72 if reply["status"] == "unread" {
73 Button("Mark read") { mark(reply, "read") }
74 }
75 }
76 Button(reply["status"] == "archived" ? "Move to inbox" : "Ignore") {
77 mark(reply, reply["status"] == "archived" ? "read" : "archived")
78 }
79 }
80 }
81 .frame(maxWidth: .infinity, alignment: .leading)
82 }
83 }
84 if inbox.hasOlder && filter != "Ignored" {
85 Button("Load older replies") { Task { await refresh(older: true) } }
86 .disabled(workspace.refreshingReplies)
87 }
88 }
89 .studioPage()
90 }
91 .confirmationDialog("Ignore \(ignoring.count) replies?", isPresented: $confirmsIgnoreAll, titleVisibility: .visible) {
92 Button("Ignore all", role: .destructive) {
93 workspace.perform { try workspace.ignoreReplies(ignoring) }
94 ignoring.removeAll()
95 }
96 } message: {
97 Text("You can move any of these replies back from Ignored.")
98 }
99 }
100
101 private func mark(_ reply: WorkspaceRecord, _ status: String) {
102 workspace.perform { try workspace.markReply(reply.id, status: status) }
103 }
104
105 private func refresh(older: Bool = false) async {
106 do {
107 let added = try await inbox.refresh(older: older)
108 workspace.notice = "\(added) new replies"
109 } catch { workspace.error = error.localizedDescription }
110 }
111}