// `/notes/note/[id]`: a note's page — or, for an id that is a project's // journal page (a project's journal section and Activity name one by its id // alone), that page in the journal table, headed by its day. One read of the // note says which and is the page's first frame: a note is drawn from it at // once, and an id the note table does not hold opens in the journal table. // `new`, what the add fan pushes, is a blank note page that creates the note // on its first value. import CottonKit import CottonValues import SwiftUI struct NoteRoute: View { let id: String @Environment(\.records) private var store @Environment(\.tableDefinitions) private var definitions /// What the one read found. @State private var found: Found? enum Found: Equatable { /// The note, drawn from this read. case note(TableRecord) /// The note table holds no such id: a journal page's. case notANote /// The read failed: the note's own page reads again and says so. case unread } var body: some View { if id == "new", let note = definitions["note"] { RecordOpening(definition: note, opens: .blank([:])) } else { switch found { case .note(let record)?: TablePageRoute(table: "note", record: record) case let other?: TablePageRoute(table: Self.table(of: other), id: id) case nil: PaperScreen { EmptyView() } // no loader: the row is local and lands next frame .task(id: id) { found = await Self.read(id, in: store) } } } } /// The note with this id; `notANote` when the note table holds none. static func read(_ id: String, in store: (any Records)?) async -> Found { guard let store else { return .unread } do { guard let record = try await store.get(table: "note", id: id) else { return .notANote } return .note(record) } catch { return .unread } } /// The table the id's page opens in. static func table(of found: Found) -> String { found == .notANote ? "journal" : "note" } }