| 1 | import SwiftUI |
| 2 | |
| 3 | struct CommentEntry: Decodable, Identifiable { |
| 4 | let ID: UInt64 |
| 5 | let Author: String |
| 6 | let Body: String |
| 7 | var id: UInt64 { self.ID } |
| 8 | } |
| 9 | struct CommentThread: Decodable, Identifiable { |
| 10 | let RowPos: Int |
| 11 | let ColPos: Int |
| 12 | let Ref: String |
| 13 | let Entries: [CommentEntry] |
| 14 | let Resolved: Bool |
| 15 | var id: String { Ref } |
| 16 | } |
| 17 | struct CommentsPage: Decodable { let Threads: [CommentThread]? } |
| 18 | struct SheetComments: View { |
| 19 | @Bindable var model: SheetModel |
| 20 | @State private var threads: [CommentThread] = [] |
| 21 | @State private var bodyText = "" |
| 22 | var body: some View { |
| 23 | Group { |
| 24 | if model.page?.MayComment == true { |
| 25 | Text("Comment on " + model.selection.start.ref) |
| 26 | TextField("Comment", text: $bodyText, axis: .vertical) |
| 27 | Button("Add comment") { |
| 28 | Task { |
| 29 | await model.command( |
| 30 | "/comments", |
| 31 | [ |
| 32 | "r": String(model.selection.start.r), "c": String(model.selection.start.c), |
| 33 | "body": bodyText, |
| 34 | ]) |
| 35 | if model.lastNotice == "added" { bodyText = "" } |
| 36 | await load() |
| 37 | } |
| 38 | }.disabled( |
| 39 | bodyText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || model.dirty |
| 40 | || model.busy) |
| 41 | } |
| 42 | ForEach(threads) { thread in |
| 43 | Section(thread.Ref + (thread.Resolved ? " · Resolved" : "")) { |
| 44 | ForEach(thread.Entries) { comment in |
| 45 | VStack(alignment: .leading) { |
| 46 | Text(comment.Author).font(.caption) |
| 47 | Text(comment.Body).textSelection(.enabled) |
| 48 | } |
| 49 | } |
| 50 | if !thread.Resolved && model.page?.MayComment == true { |
| 51 | Button("Resolve thread") { |
| 52 | Task { |
| 53 | await model.command( |
| 54 | "/comments/resolve", ["r": String(thread.RowPos), "c": String(thread.ColPos)]) |
| 55 | await load() |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | }.task { await load() } |
| 62 | } |
| 63 | private func load() async { |
| 64 | do { |
| 65 | let response: CommentsPage = try await model.client.request(model.base + "/comments") |
| 66 | threads = response.Threads ?? [] |
| 67 | } catch { model.error = error.localizedDescription } |
| 68 | } |
| 69 | } |
| 70 | |