| 1 | import RastrilloApple |
| 2 | import SwiftUI |
| 3 | |
| 4 | struct AddNoteView: View { |
| 5 | let model: MemoriaModel |
| 6 | let client: NativeHTTP |
| 7 | @State private var title = "" |
| 8 | @State private var bodyText = "" |
| 9 | @State private var saving = false |
| 10 | @State private var error = "" |
| 11 | // One per sheet, reused by every Save press, so a retry after a lost response |
| 12 | // replays the same draft instead of creating a second note. |
| 13 | @State private var clientID = UUID() |
| 14 | @Environment(\.dismiss) private var dismiss |
| 15 | var body: some View { |
| 16 | NavigationStack { |
| 17 | Form { |
| 18 | Section { TextField("Title", text: $title) } |
| 19 | Section("Note") { TextEditor(text: $bodyText).frame(minHeight: 220).accessibilityLabel("Note") } |
| 20 | Section { Text("Stored in your team’s Memoria and available to people admitted to it.").font(.footnote).foregroundStyle(.secondary) } |
| 21 | if !error.isEmpty { Section { Text(error).foregroundStyle(.red) } } |
| 22 | }.formStyle(.grouped).navigationTitle("Add note") |
| 23 | .toolbar { |
| 24 | ToolbarItem(placement: .cancellationAction) { Button("Cancel") { dismiss() }.disabled(saving) } |
| 25 | ToolbarItem(placement: .confirmationAction) { |
| 26 | Button(saving ? "Saving…" : "Save") { |
| 27 | saving = true |
| 28 | Task { |
| 29 | 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() } |
| 30 | catch { self.error = error.localizedDescription; saving = false } |
| 31 | } |
| 32 | }.disabled(saving || title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || bodyText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | #if os(macOS) |
| 37 | .frame(width: 520, height: 520) |
| 38 | #endif |
| 39 | .interactiveDismissDisabled(saving) |
| 40 | } |
| 41 | } |
| 42 | |