Branchwell

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

Raw 4.7 KB on 8f75dd7d901e2186ca9d9e1cf2f061105c02ae42
1import AppKit
2import SwiftUI
3
4struct VaultSettings: View {
5 let workspace: Workspace
6 @State private var notes: [[String: Any]] = []
7 @State private var selected: Set<String> = []
8 @State private var previews: [[String: String]] = []
9 @State private var query = ""
10 @State private var busy = false
11
12 private var config: [String: Any] { workspace.settings["obsidianVault"] as? [String: Any] ?? [:] }
13
14 var body: some View {
15 GroupBox("Obsidian vault") {
16 VStack(alignment: .leading, spacing: 10) {
17 HStack {
18 Text(config["name"] as? String ?? "Choose a folder of Markdown notes.")
19 .foregroundStyle(.secondary)
20 Spacer()
21 Button("Choose folder…", action: chooseFolder)
22 if config["path"] is String {
23 Button("Disconnect") {
24 workspace.perform {
25 try workspace.set("obsidianVault", to: NSNull())
26 notes = []
27 selected = []
28 previews = []
29 }
30 }
31 }
32 }
33 if config["path"] is String {
34 Toggle("Allow connected agents to browse and read notes", isOn: Binding(get: {
35 config["allowAgents"] as? Bool == true
36 }, set: { enabled in
37 workspace.perform {
38 var next = config
39 next["allowAgents"] = enabled
40 try workspace.set("obsidianVault", to: next)
41 }
42 }))
43 HStack {
44 TextField("Search note titles", text: $query).studioInput()
45 Button("List notes", action: load)
46 Button("Preview selection", action: preview).disabled(selected.isEmpty)
47 }
48 ForEach(Array(notes.enumerated()), id: \.offset) { _, note in
49 if let id = note["id"] as? String {
50 Toggle(id, isOn: Binding(get: { selected.contains(id) }, set: { include in
51 if include && selected.count < 8 { selected.insert(id) }
52 else if !include { selected.remove(id) }
53 previews = []
54 }))
55 }
56 }
57 if !previews.isEmpty {
58 ForEach(Array(previews.enumerated()), id: \.offset) { _, note in
59 DisclosureGroup(note["title"] ?? "Note") {
60 Text(note["text"] ?? "").textSelection(.enabled)
61 }
62 }
63 if workspace.writingToolsAvailable {
64 Button("Suggest ideas from these notes") { Task { await suggest() } }
65 .disabled(workspace.project == nil || busy)
66 }
67 }
68 if busy { ProgressView().controlSize(.small) }
69 }
70 }
71 .frame(maxWidth: .infinity, alignment: .leading)
72 }
73 }
74
75 private func chooseFolder() {
76 let panel = NSOpenPanel()
77 panel.canChooseDirectories = true
78 panel.canChooseFiles = false
79 guard panel.runModal() == .OK, let url = panel.url else { return }
80 workspace.perform {
81 let vault = try ObsidianVault(url.path)
82 try workspace.set("obsidianVault", to: ["path": vault.root.path, "name": vault.root.lastPathComponent, "allowAgents": false])
83 notes = []
84 selected = []
85 previews = []
86 }
87 }
88
89 private func load() {
90 workspace.perform {
91 let result = try workspace.connectedVault().list(query: query)
92 notes = result.notes
93 selected = []
94 previews = []
95 }
96 }
97
98 private func preview() {
99 workspace.perform { previews = try workspace.connectedVault().preview(selected.sorted()).notes }
100 }
101
102 private func suggest() async {
103 guard !busy, let project = workspace.project else { return }
104 busy = true
105 defer { busy = false }
106 do {
107 let count = try await ContentStudio(workspace: workspace).suggestVaultIdeas(
108 projectID: project.id, references: previews
109 )
110 workspace.notice = "\(count) suggestions saved for review"
111 } catch { workspace.error = error.localizedDescription }
112 }
113}