import RastrilloApple import SwiftUI struct AddNoteView: View { let model: MemoriaModel let client: NativeHTTP @State private var title = "" @State private var bodyText = "" @State private var saving = false @State private var error = "" // One per sheet, reused by every Save press, so a retry after a lost response // replays the same draft instead of creating a second note. @State private var clientID = UUID() @Environment(\.dismiss) private var dismiss var body: some View { NavigationStack { Form { Section { TextField("Title", text: $title) } Section("Note") { TextEditor(text: $bodyText).frame(minHeight: 220).accessibilityLabel("Note") } Section { Text("Stored in your team’s Memoria and available to people admitted to it.").font(.footnote).foregroundStyle(.secondary) } if !error.isEmpty { Section { Text(error).foregroundStyle(.red) } } }.formStyle(.grouped).navigationTitle("Add note") .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Cancel") { dismiss() }.disabled(saving) } ToolbarItem(placement: .confirmationAction) { Button(saving ? "Saving…" : "Save") { saving = true Task { do { let indexed = try await model.storeNote(title: title, body: bodyText, clientID: clientID, client: client); model.query = title; if indexed { await model.search(client) } else { model.error = "Note saved. Search will find it after the server restarts." }; dismiss() } catch { self.error = error.localizedDescription; saving = false } } }.disabled(saving || title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || bodyText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) } } } #if os(macOS) .frame(width: 520, height: 520) #endif .interactiveDismissDisabled(saving) } }