Branchwell

Inkspring / app · inkspring.ai / native-mac / iPhone / MobileContentPlans.swift

Raw 9.2 KB on 8f75dd7d901e2186ca9d9e1cf2f061105c02ae42
1import SwiftUI
2
3extension MobileLibrary {
4 func planText(_ item: CompanionLibrary.Item) -> String {
5 let current = ((snapshot?.items ?? []) + (snapshot?.newsletters ?? []) + (snapshot?.scripts ?? []) + (snapshot?.articles ?? [])).first { $0.id == item.id } ?? item
6 return pendingChanges(for: item.id).last(where: { $0.contentOperation == .savePlan })?.text ?? current.planMarkdown ?? ""
7 }
8 func savePlan(_ text: String, base: String, item: CompanionLibrary.Item, workspaceID: UUID) throws {
9 try ContentPlan.validate(text)
10 guard planText(item) == base else { throw ContentPlanError.conflict }
11 var change = CompanionLibrary.Change(action: .editDraft, itemID: item.id, revision: item.revision, text: text)
12 change.contentOperation = .savePlan; change.basePlan = base
13 try saveChange(change, workspaceID: workspaceID)
14 }
15 var assistantContents: [AssistantContent] {
16 let projects = (snapshot?.projects ?? []).map {
17 AssistantContent(id: $0.id, reference: $0.id, kind: "project", title: $0.name, project: "", revision: "", body: "")
18 }
19 return projects + ((snapshot?.items ?? []) + (snapshot?.newsletters ?? [])).filter { $0.isTrashed != true }.map {
20 let edit = pendingChanges(for: $0.id).last { [.editDraft, .editIdea].contains($0.action) && $0.contentOperation == nil && $0.error == nil }
21 return .init(id: $0.id, reference: $0.referenceID ?? $0.id, kind: $0.kind.rawValue,
22 title: edit?.title.isEmpty == false ? edit!.title : $0.title, project: $0.projectID,
23 revision: $0.revision, body: edit?.text ?? $0.body)
24 }
25 }
26 func executeAssistant(_ command: LocalContentCommand, request: String) throws -> AssistantActionResult {
27 try command.validateIntent(request)
28 guard let snapshot else { throw PlanAIError("Sync your library first. Nothing changed.") }
29 if command.operation == .find {
30 let query = command.target.lowercased()
31 let matches = assistantContents.filter { query.isEmpty || query == "drafts" && $0.kind == "draft" ||
32 query == "my drafts" && $0.kind == "draft" || $0.kind == query || $0.title.localizedCaseInsensitiveContains(query) || $0.reference.localizedCaseInsensitiveContains(query) }
33 return .init(message: matches.isEmpty ? "No matches. Try an ID or title words." : matches.map { "\($0.reference) · \($0.kind) · \($0.title)" }.joined(separator: "\n"))
34 }
35 if command.operation == .review { return .init(message: "Use the item’s Schedule, Trash or publishing review controls. Nothing changed.") }
36 func project(_ query: String) throws -> String {
37 if query.isEmpty, let first = snapshot.projects.first { return first.id }
38 let matches = snapshot.projects.filter { $0.id.caseInsensitiveCompare(query) == .orderedSame || $0.name.caseInsensitiveCompare(query) == .orderedSame }
39 guard matches.count == 1, let match = matches.first else { throw PlanAIError("Use the exact project name or ID.") }
40 return match.id
41 }
42 if [.createIdea, .createDraft, .createNewsletter, .createProject].contains(command.operation) {
43 let projectID = try project(command.project)
44 if command.operation == .createIdea {
45 try saveIdea(title: command.title, text: command.value, projectID: projectID,
46 plan: ContentPlan.requested(in: request) ? ContentPlan.starter(command.title) : nil)
47 } else {
48 guard snapshot.supportsContentPlans == true else { throw PlanAIError("This Mac version cannot accept this action yet. Your writing is unchanged.") }
49 var change = CompanionLibrary.Change(action: .editDraft, itemID: projectID, title: command.title, text: command.value)
50 change.contentOperation = command.operation == .createDraft ? .createDraft : command.operation == .createNewsletter ? .createNewsletter : .createProject
51 if command.operation != .createProject, ContentPlan.requested(in: request) { change.initialPlan = ContentPlan.starter(command.title) }
52 try saveChange(change, workspaceID: snapshot.workspaceID)
53 }
54 return .init(message: "Saved the request on this device. Waiting for your Mac to create “\(command.title)”. It has not been published.")
55 }
56 let target = try command.resolve(in: assistantContents, request: request)
57 if command.operation == .read { return .init(message: "\(target.reference) · \(target.title)\n\n\(target.body)", itemID: target.id) }
58 if command.operation == .plan { return .init(message: "Open \(target.reference) to edit its Plan and review suggestions. No saved text changed.", itemID: target.id) }
59 guard let item = ((snapshot.items) + (snapshot.newsletters ?? [])).first(where: { $0.id == target.id }) else { throw PlanAIError("Choose writing from your synced library.") }
60 var change: CompanionLibrary.Change
61 if command.operation == .project {
62 change = .init(action: .editDraft, itemID: item.id, revision: item.revision, text: try project(command.value))
63 change.contentOperation = .moveProject
64 } else if item.kind == .newsletter {
65 if command.operation == .title {
66 change = .init(action: .editDraft, itemID: item.id, revision: item.revision, text: command.value)
67 change.contentOperation = .setTitle
68 } else {
69 change = .init(action: .editDraft, itemID: item.id, revision: item.revision, text: command.value, editsNewsletter: true, baseNewsletterText: target.body)
70 }
71 } else {
72 change = .init(action: item.kind == .idea ? .editIdea : .editDraft, itemID: item.id, revision: item.revision,
73 title: command.operation == .title ? command.value : target.title,
74 text: command.operation == .body ? command.value : target.body)
75 }
76 try saveChange(change, workspaceID: snapshot.workspaceID)
77 return .init(message: "Saved the edit on this device. Waiting for your Mac to apply it to \(target.reference).", itemID: target.id)
78 }
79}
80
81struct MobilePlanSection: View {
82 let item: CompanionLibrary.Item
83 let library: MobileLibrary
84 let workspaceID: UUID
85 let close: () -> Void
86 @State private var conflictMessage: String?
87 var body: some View {
88 ScrollView {
89 VStack(alignment: .leading, spacing: 12) {
90 if item.isTrashed != true && library.snapshot?.supportsContentPlans == true {
91 ContentPlanEditor(markdown: library.planText(item), reference: item.referenceID ?? item.id, context: item.title,
92 recoveryID: library.planRecoveryID(item.id, workspaceID: workspaceID),
93 save: { text, base in
94 try library.savePlan(text, base: base, item: item, workspaceID: workspaceID)
95 }, close: close)
96 } else {
97 Button("Back", systemImage: "chevron.left", action: close)
98 PlanMarkdownPreview(markdown: library.planText(item))
99 }
100 if let previous = item.previousPlanMarkdown, library.snapshot?.supportsContentPlans == true, item.isTrashed != true {
101 Button("Restore previous plan", systemImage: "arrow.uturn.backward") {
102 do { try library.savePlan(previous, base: library.planText(item), item: item, workspaceID: workspaceID) }
103 catch { library.error = error.localizedDescription }
104 }
105 }
106 if library.snapshot?.supportsContentPlans != true { Text("Plan editing will be available after your Mac shares support for Plans.").font(.caption) }
107 if let entry = library.pendingChanges(for: item.id).last(where: { $0.contentOperation == .savePlan }) {
108 Text(entry.error ?? "Plan saved on this device. Waiting for your Mac to apply it.").font(.caption)
109 if entry.error != nil {
110 DisclosureGroup("Compare the Plan on your Mac") {
111 PlanMarkdownPreview(markdown: item.planMarkdown ?? "")
112 Button("Replace Mac Plan with my saved version") {
113 do {
114 var change = CompanionLibrary.Change(action: .editDraft, itemID: item.id, revision: item.revision, text: entry.text)
115 change.contentOperation = .savePlan; change.basePlan = item.planMarkdown ?? ""
116 try library.saveChange(change, workspaceID: workspaceID)
117 conflictMessage = nil
118 } catch { conflictMessage = error.localizedDescription }
119 }
120 }
121 }
122 if let conflictMessage { Text(conflictMessage).font(.caption) }
123 }
124 }
125 .padding()
126 }
127 .navigationTitle("Outline")
128 .navigationBarTitleDisplayMode(.inline)
129 }
130}