OpenTalon / app · opentalon.ai / native / Sources / OpenTalon / DashboardView.swift
| 1 | import CompanionCore |
| 2 | import SwiftUI |
| 3 | |
| 4 | private enum Attention: String, CaseIterable, Identifiable { |
| 5 | case urgent = "Urgent to-dos", all = "All to-dos", overdue = "Overdue", today = "Due today", done = "Done" |
| 6 | var id: Self { self } |
| 7 | func matches(_ item: WorkItem, today: String) -> Bool { |
| 8 | switch self { |
| 9 | case .all: item.status == .todo |
| 10 | case .urgent: item.status == .todo && item.priority == .urgent |
| 11 | case .overdue: item.dueDate.map { $0 < today } ?? false |
| 12 | case .today: item.dueDate == today |
| 13 | case .done: item.status == .done |
| 14 | } |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | private enum ProgressBucket: String, CaseIterable, Identifiable { |
| 19 | case urgent = "Urgent to-dos", todo = "Other to-dos", sent = "Sent to agent", working = "In progress", done = "Done" |
| 20 | var id: Self { self } |
| 21 | |
| 22 | init(_ item: WorkItem) { |
| 23 | switch item.status { |
| 24 | case .todo: self = item.priority == .urgent ? .urgent : .todo |
| 25 | case .sent: self = .sent |
| 26 | case .working: self = .working |
| 27 | case .done: self = .done |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | var columnWidth: CGFloat { |
| 32 | switch self { |
| 33 | case .urgent, .todo: 68 |
| 34 | case .sent, .working: 73 |
| 35 | case .done: 40 |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func color(_ palette: Palette) -> Color { |
| 40 | switch self { |
| 41 | case .urgent: Priority.urgent.color(palette.dark) |
| 42 | case .todo: palette.flow(.todo) |
| 43 | case .sent: palette.flow(.sent) |
| 44 | case .working: palette.flow(.working) |
| 45 | case .done: palette.flow(.done) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | struct DashboardView: View { |
| 51 | @Environment(WorkspaceModel.self) private var model |
| 52 | @Environment(\.palette) private var palette |
| 53 | @State private var scope = "" |
| 54 | @AppStorage("dashboard.openTaskSort") private var sort = OpenTaskSort.newest |
| 55 | @State private var priority: Priority? |
| 56 | @State private var filter = Attention.all |
| 57 | @State private var page = TaskPageWindow() |
| 58 | private let scrollSpace = "Dashboard tasks viewport" |
| 59 | |
| 60 | private var projects: [Board] { model.boards.filter { scope.isEmpty || $0.id == scope } } |
| 61 | private var active: [WorkItem] { |
| 62 | model.tasks.filter { !$0.deleted && $0.archivedAt == nil && (scope.isEmpty || $0.boardId == scope) } |
| 63 | } |
| 64 | private var open: [WorkItem] { active.filter { $0.status != .done } } |
| 65 | private var attention: [WorkItem] { |
| 66 | if filter == .done { |
| 67 | return DoneOrder.recent.ordered(active.filter { $0.status == .done }, |
| 68 | completionDate: \.completionDate, creationDate: \.creationDate) |
| 69 | } |
| 70 | let today = CalendarDay.today |
| 71 | return sort.ordered(open.filter { filter.matches($0, today: today) && (priority == nil || $0.priority == priority) }, |
| 72 | created: \.creationDate, priorityRank: { $0.priority.rank }, due: \.dueDate) |
| 73 | } |
| 74 | |
| 75 | var body: some View { |
| 76 | GeometryReader { viewport in |
| 77 | ThemedScrollView { |
| 78 | VStack(alignment: .leading, spacing: 26) { |
| 79 | header |
| 80 | overview |
| 81 | DashboardCapture(scope: scope) |
| 82 | VStack(alignment: .leading, spacing: 15) { |
| 83 | sectionHeader("Task list") |
| 84 | attentionCard(viewportHeight: viewport.size.height) |
| 85 | } |
| 86 | VStack(alignment: .leading, spacing: 15) { |
| 87 | sectionHeader("Project overview", "\(active.count) current tasks · Completion is Done divided by total tasks.") |
| 88 | projectList |
| 89 | } |
| 90 | HStack { |
| 91 | Button("Archive", systemImage: "archivebox") { model.navigate(.archive) } |
| 92 | Button("Trash", systemImage: "trash") { model.navigate(.trash) } |
| 93 | } |
| 94 | } |
| 95 | .padding(.horizontal, 35) |
| 96 | .padding(.vertical, 31) |
| 97 | .frame(maxWidth: 1540) |
| 98 | .frame(maxWidth: .infinity) |
| 99 | } |
| 100 | .coordinateSpace(name: scrollSpace) |
| 101 | } |
| 102 | .toolbar { |
| 103 | if filter != .done { |
| 104 | Menu { |
| 105 | Picker("Sort tasks", selection: $sort) { |
| 106 | ForEach(OpenTaskSort.allCases, id: \.self) { Text($0.rawValue).tag($0) } |
| 107 | } |
| 108 | Picker("Filter by priority", selection: $priority) { |
| 109 | Text("All priorities").tag(nil as Priority?) |
| 110 | ForEach(Priority.allCases) { Text($0.label).tag(Optional($0)) } |
| 111 | } |
| 112 | } label: { Label("Sort and filter tasks", systemImage: "arrow.up.arrow.down") } |
| 113 | .help("Sort and filter tasks") |
| 114 | } |
| 115 | } |
| 116 | .onChange(of: sort) { page.reset() } |
| 117 | .onChange(of: priority) { page.reset() } |
| 118 | .onChange(of: scope) { _, _ in page.reset() } |
| 119 | .onChange(of: model.boards) { _, boards in |
| 120 | if !scope.isEmpty && !boards.contains(where: { $0.id == scope }) { scope = "" } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | private var header: some View { |
| 125 | HStack { |
| 126 | VStack(alignment: .leading, spacing: 9) { |
| 127 | Text(Date.now.formatted(.dateTime.weekday(.wide).month(.wide).day()).uppercased()) |
| 128 | .font(.system(size: 10)).tracking(0.9).foregroundStyle(palette.muted) |
| 129 | Text("Your day, at a glance.") |
| 130 | .font(palette.display(40)).accessibilityAddTraits(.isHeader) |
| 131 | Text(scope.isEmpty ? "What needs your attention across all your projects." : |
| 132 | "What needs your attention on \(model.projectName(scope)).") |
| 133 | .font(.system(size: 12)).foregroundStyle(palette.muted) |
| 134 | } |
| 135 | Spacer() |
| 136 | BoardDropdown(selection: $scope, title: "Project", symbol: "folder", |
| 137 | choices: [""] + model.boards.map(\.id), compact: true) { |
| 138 | $0.isEmpty ? "All projects" : model.projectName($0) |
| 139 | } |
| 140 | .frame(width: 190) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | private var overview: some View { |
| 145 | let counts = progressCounts(active) |
| 146 | return ViewThatFits(in: .horizontal) { |
| 147 | HStack(spacing: 37) { |
| 148 | openCount |
| 149 | VStack(alignment: .leading, spacing: 12) { |
| 150 | flowBar(counts) |
| 151 | HStack(spacing: 14) { |
| 152 | ForEach(ProgressBucket.allCases) { bucket in legend(bucket, counts[bucket, default: 0]) } |
| 153 | Spacer(minLength: 4) |
| 154 | completion(counts) |
| 155 | } |
| 156 | .fixedSize(horizontal: false, vertical: true) |
| 157 | } |
| 158 | .frame(minWidth: 500) |
| 159 | } |
| 160 | VStack(alignment: .leading, spacing: 12) { |
| 161 | HStack { openCount; Spacer(); completion(counts) } |
| 162 | flowBar(counts) |
| 163 | LazyVGrid(columns: [GridItem(.adaptive(minimum: 105), alignment: .leading)], spacing: 9) { |
| 164 | ForEach(ProgressBucket.allCases) { bucket in legend(bucket, counts[bucket, default: 0]) } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | .padding(.vertical, 22) |
| 169 | .overlay(alignment: .top) { Rectangle().fill(palette.line).frame(height: 1) } |
| 170 | .overlay(alignment: .bottom) { Rectangle().fill(palette.line).frame(height: 1) } |
| 171 | } |
| 172 | |
| 173 | private var openCount: some View { |
| 174 | HStack(alignment: .firstTextBaseline, spacing: 9) { |
| 175 | Text(open.count.formatted()).font(palette.display(54, weight: .medium)).monospacedDigit() |
| 176 | Text("open \(open.count == 1 ? "task" : "tasks")").font(.system(size: 13)).foregroundStyle(palette.muted) |
| 177 | } |
| 178 | .fixedSize() |
| 179 | } |
| 180 | |
| 181 | private func completion(_ counts: [ProgressBucket: Int]) -> some View { |
| 182 | HStack(spacing: 8) { |
| 183 | Text("Completion").foregroundStyle(palette.muted) |
| 184 | Text("\(percent(counts[.done, default: 0], of: active.count))%").bold().monospacedDigit() |
| 185 | } |
| 186 | .font(.system(size: 12)) |
| 187 | .fixedSize() |
| 188 | } |
| 189 | |
| 190 | private func show(_ option: Attention) { |
| 191 | filter = option |
| 192 | page.reset() |
| 193 | } |
| 194 | |
| 195 | private func attentionCard(viewportHeight: CGFloat) -> some View { |
| 196 | let items = attention |
| 197 | let today = CalendarDay.today |
| 198 | return LazyVStack(spacing: 0) { |
| 199 | HStack(spacing: 0) { |
| 200 | ForEach(Attention.allCases) { option in |
| 201 | let count = (option == .done ? active : open).filter { option.matches($0, today: today) }.count |
| 202 | let selected = option == filter |
| 203 | let alert = (option == .overdue || option == .urgent) && count > 0 |
| 204 | Button { show(option) } label: { |
| 205 | ViewThatFits(in: .horizontal) { |
| 206 | HStack(alignment: .firstTextBaseline, spacing: 8) { |
| 207 | filterCount(count, alert: alert) |
| 208 | filterLabel(option, alert: alert) |
| 209 | } |
| 210 | .fixedSize() |
| 211 | VStack(alignment: .leading, spacing: 2) { |
| 212 | filterCount(count, alert: alert) |
| 213 | filterLabel(option, alert: alert) |
| 214 | } |
| 215 | } |
| 216 | .padding(.horizontal, 14) |
| 217 | .frame(maxWidth: .infinity, minHeight: 64, alignment: .leading) |
| 218 | .background(selected ? palette.card : palette.canvas) |
| 219 | .overlay(alignment: .top) { |
| 220 | if selected { Rectangle().fill(alert ? palette.danger : palette.accent).frame(height: 2) } |
| 221 | } |
| 222 | .contentShape(Rectangle()) |
| 223 | } |
| 224 | .buttonStyle(.plain) |
| 225 | .accessibilityLabel("\(option.rawValue): \(count)") |
| 226 | .accessibilityAddTraits(selected ? .isSelected : []) |
| 227 | if option != Attention.allCases.last { Rectangle().fill(palette.line).frame(width: 1) } |
| 228 | } |
| 229 | } |
| 230 | .fixedSize(horizontal: false, vertical: true) |
| 231 | Rectangle().fill(palette.line).frame(height: 1) |
| 232 | if items.isEmpty { |
| 233 | VStack(spacing: 8) { |
| 234 | Image(systemName: "checkmark.circle").font(.system(size: 25)) |
| 235 | Text(filter == .done ? "No completed tasks yet." : "You’re all caught up.").fontWeight(.medium) |
| 236 | Text(filter == .done ? "Completed tasks appear here for seven days, then move to Archive." : |
| 237 | "Tasks that match this filter will appear here.").font(.caption) |
| 238 | } |
| 239 | .foregroundStyle(palette.muted) |
| 240 | .frame(maxWidth: .infinity) |
| 241 | .padding(.vertical, 31) |
| 242 | } |
| 243 | ForEach(items.prefix(page.limit)) { item in |
| 244 | DashboardTask(item: item) |
| 245 | Rectangle().fill(palette.line).frame(height: 1) |
| 246 | } |
| 247 | if items.count > page.limit { |
| 248 | TaskPageBoundary(space: scrollSpace, viewportHeight: viewportHeight) { |
| 249 | page.revealNext(total: items.count) |
| 250 | } |
| 251 | } |
| 252 | if !items.isEmpty { |
| 253 | HStack { |
| 254 | if filter == .done { |
| 255 | Text("Recently completed first · Automatically archived after seven days.") |
| 256 | .font(.system(size: 11)).foregroundStyle(palette.muted) |
| 257 | Button("View Archive") { model.navigate(.archive) } |
| 258 | } else { |
| 259 | Text("Ignored tasks can be restored from Trash.") |
| 260 | .font(.system(size: 11)).foregroundStyle(palette.muted) |
| 261 | } |
| 262 | Spacer() |
| 263 | } |
| 264 | .padding(.horizontal, 18).padding(.vertical, 12) |
| 265 | } |
| 266 | } |
| 267 | .clipShape(.rect(cornerRadius: 10)) |
| 268 | .surface(10) |
| 269 | } |
| 270 | |
| 271 | private func filterCount(_ count: Int, alert: Bool) -> some View { |
| 272 | Text(count.formatted()) |
| 273 | .font(palette.display(27, weight: .medium)).monospacedDigit() |
| 274 | .foregroundStyle(alert ? palette.danger : palette.text) |
| 275 | } |
| 276 | |
| 277 | private func filterLabel(_ option: Attention, alert: Bool) -> some View { |
| 278 | Text(option.rawValue).font(.system(size: 11)) |
| 279 | .foregroundStyle(alert ? palette.danger : palette.muted) |
| 280 | } |
| 281 | |
| 282 | private var projectList: some View { |
| 283 | let columns = ProgressBucket.allCases.filter { $0 != .sent } |
| 284 | return GeometryReader { geometry in |
| 285 | ThemedScrollView(.horizontal, track: palette.card) { |
| 286 | Grid(alignment: .leading, horizontalSpacing: 10, verticalSpacing: 0) { |
| 287 | GridRow { |
| 288 | Text("Project").frame(minWidth: 125, maxWidth: .infinity, alignment: .leading) |
| 289 | Text("Tasks").frame(width: 38, alignment: .trailing) |
| 290 | Text("Status mix").frame(width: 86, alignment: .leading) |
| 291 | ForEach(columns) { bucket in |
| 292 | Text(bucket.rawValue) |
| 293 | .frame(width: bucket.columnWidth, alignment: .trailing) |
| 294 | } |
| 295 | Text("Completion").frame(width: 74, alignment: .trailing) |
| 296 | } |
| 297 | .font(.system(size: 11, weight: .medium)) |
| 298 | .foregroundStyle(palette.muted) |
| 299 | .frame(height: 37) |
| 300 | Rectangle().fill(palette.line).frame(height: 1) |
| 301 | ForEach(projects) { board in |
| 302 | let items = active.filter { $0.boardId == board.id } |
| 303 | let counts = progressCounts(items) |
| 304 | GridRow { |
| 305 | Button { |
| 306 | model.navigate(.board(board.id)) |
| 307 | } label: { |
| 308 | HStack(spacing: 7) { |
| 309 | ProjectIcon(key: board.icon) |
| 310 | Text(board.name).fontWeight(.semibold).lineLimit(1) |
| 311 | Image(systemName: "arrow.up.right").font(.system(size: 8)).foregroundStyle(palette.muted) |
| 312 | } |
| 313 | .frame(minWidth: 125, maxWidth: .infinity, alignment: .leading) |
| 314 | .contentShape(Rectangle()) |
| 315 | } |
| 316 | .buttonStyle(.plain) |
| 317 | .accessibilityLabel("Open project \(board.name)") |
| 318 | tableCount(items.count).frame(width: 38, alignment: .trailing) |
| 319 | flowBar(counts, compact: true).frame(width: 86) |
| 320 | ForEach(columns) { bucket in |
| 321 | tableCount(counts[bucket, default: 0]).frame(width: bucket.columnWidth, alignment: .trailing) |
| 322 | } |
| 323 | Text("\(percent(counts[.done, default: 0], of: items.count))%") |
| 324 | .fontWeight(.semibold).frame(width: 74, alignment: .trailing) |
| 325 | } |
| 326 | .font(.system(size: 12)).monospacedDigit() |
| 327 | .frame(height: 41) |
| 328 | if board.id != projects.last?.id { Rectangle().fill(palette.line).frame(height: 1) } |
| 329 | } |
| 330 | } |
| 331 | .padding(.horizontal, 18) |
| 332 | .frame(width: max(geometry.size.width, 682)) |
| 333 | } |
| 334 | .scrollBounceBehavior(.basedOnSize) |
| 335 | } |
| 336 | .frame(height: 37 + CGFloat(projects.count) * 42) |
| 337 | .surface(10) |
| 338 | } |
| 339 | |
| 340 | private func tableCount(_ count: Int) -> some View { |
| 341 | Text(count.formatted()).foregroundStyle(count == 0 ? palette.muted : palette.text) |
| 342 | } |
| 343 | |
| 344 | private func sectionHeader(_ title: String, _ description: String? = nil) -> some View { |
| 345 | VStack(alignment: .leading, spacing: 5) { |
| 346 | Text(title).font(palette.display(25)).accessibilityAddTraits(.isHeader) |
| 347 | if let description { |
| 348 | Text(description).font(.system(size: 12)).foregroundStyle(palette.muted) |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | private func progressCounts(_ tasks: [WorkItem]) -> [ProgressBucket: Int] { |
| 354 | Dictionary(grouping: tasks, by: ProgressBucket.init).mapValues(\.count) |
| 355 | } |
| 356 | |
| 357 | private func percent(_ done: Int, of total: Int) -> Int { |
| 358 | total == 0 ? 0 : Int((Double(done) / Double(total) * 100).rounded()) |
| 359 | } |
| 360 | |
| 361 | @ViewBuilder |
| 362 | private func legend(_ bucket: ProgressBucket, _ count: Int) -> some View { |
| 363 | let item = HStack(spacing: 5) { |
| 364 | RoundedRectangle(cornerRadius: 2).fill(bucket.color(palette)).frame(width: 8, height: 8) |
| 365 | Text(bucket.rawValue).foregroundStyle(bucket == .urgent && count > 0 ? bucket.color(palette) : palette.muted) |
| 366 | Text(count.formatted()).fontWeight(.semibold).monospacedDigit() |
| 367 | } |
| 368 | .font(.system(size: 11)) |
| 369 | .fixedSize() |
| 370 | if bucket == .urgent || bucket == .done { |
| 371 | Button { show(bucket == .done ? .done : .urgent) } label: { item.contentShape(Rectangle()) } |
| 372 | .buttonStyle(.plain) |
| 373 | .accessibilityLabel(bucket == .done ? "View Done tasks: \(count)" : "Filter Urgent to-dos: \(count)") |
| 374 | .help(bucket == .done ? "View recently completed tasks" : "Show only Urgent tasks in To do") |
| 375 | } else { |
| 376 | item |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | private func flowBar(_ counts: [ProgressBucket: Int], compact: Bool = false) -> some View { |
| 381 | GeometryReader { geometry in |
| 382 | let visible = ProgressBucket.allCases.filter { counts[$0, default: 0] > 0 } |
| 383 | let total = counts.values.reduce(0, +) |
| 384 | let gap: CGFloat = compact ? 1 : 2 |
| 385 | let minimum: CGFloat = compact ? 2 : 3 |
| 386 | let proportional = max(0, geometry.size.width - CGFloat(visible.count) * minimum - CGFloat(max(0, visible.count - 1)) * gap) |
| 387 | HStack(spacing: gap) { |
| 388 | ForEach(visible) { bucket in |
| 389 | let width = minimum + proportional * CGFloat(counts[bucket, default: 0]) / CGFloat(total) |
| 390 | if bucket == .urgent && !compact { |
| 391 | Button { show(.urgent) } label: { |
| 392 | Rectangle().fill(bucket.color(palette)) |
| 393 | } |
| 394 | .buttonStyle(.plain) |
| 395 | .help("Show only Urgent to-dos") |
| 396 | .accessibilityHidden(true) |
| 397 | .frame(width: width) |
| 398 | } else { |
| 399 | Rectangle().fill(bucket.color(palette)).frame(width: width) |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading) |
| 404 | .background(palette.line.opacity(0.65)) |
| 405 | } |
| 406 | .frame(height: compact ? 6 : 8) |
| 407 | .clipShape(.capsule) |
| 408 | .accessibilityHidden(true) |
| 409 | } |
| 410 | |
| 411 | } |
| 412 | |
| 413 | private struct DashboardCapture: View { |
| 414 | private struct AgentDraft: Decodable { |
| 415 | let details: SuggestedTaskDetails |
| 416 | let projectId: String? |
| 417 | let newProjectName: String? |
| 418 | let priority: Priority? |
| 419 | |
| 420 | init(from decoder: Decoder) throws { |
| 421 | details = try SuggestedTaskDetails(from: decoder) |
| 422 | let fields = try decoder.container(keyedBy: CodingKeys.self) |
| 423 | projectId = try fields.decodeIfPresent(String.self, forKey: .projectId) |
| 424 | newProjectName = try fields.decodeIfPresent(String.self, forKey: .newProjectName) |
| 425 | priority = try? fields.decodeIfPresent(Priority.self, forKey: .priority) |
| 426 | } |
| 427 | |
| 428 | private enum CodingKeys: String, CodingKey { case projectId, newProjectName, priority } |
| 429 | } |
| 430 | |
| 431 | private struct PendingCapture { |
| 432 | let details: SuggestedTaskDetails |
| 433 | let project: String |
| 434 | let newProjectName: String? |
| 435 | let priority: Priority |
| 436 | let drafted: Bool |
| 437 | } |
| 438 | |
| 439 | @Environment(WorkspaceModel.self) private var model |
| 440 | @Environment(\.palette) private var palette |
| 441 | @State private var idea = "" |
| 442 | @State private var selectedProject: String? |
| 443 | @State private var priority: Priority? |
| 444 | @State private var saving = false |
| 445 | @State private var feedback: String? |
| 446 | @State private var failed = false |
| 447 | @State private var requestKey = UUID() |
| 448 | @State private var retryDraft: PendingCapture? |
| 449 | |
| 450 | let scope: String |
| 451 | |
| 452 | private var project: String? { |
| 453 | let preferredProject = selectedProject ?? (scope.isEmpty ? model.selectedBoard : scope) |
| 454 | return model.boards.first { $0.id == preferredProject }?.id ?? model.boards.first?.id |
| 455 | } |
| 456 | |
| 457 | private var input: String { idea.trimmingCharacters(in: .whitespacesAndNewlines) } |
| 458 | private var tooLong: Bool { input.utf16.count > SuggestedTaskDetails.captureLimit } |
| 459 | |
| 460 | var body: some View { |
| 461 | VStack(alignment: .leading, spacing: 8) { |
| 462 | Text("Quick add").font(.system(size: 13, weight: .semibold)) |
| 463 | HStack(spacing: 10) { |
| 464 | TextField("What needs doing?", text: $idea) |
| 465 | .textFieldStyle(.plain) |
| 466 | .padding(.horizontal, 12) |
| 467 | .frame(height: 34) |
| 468 | .surface(9) |
| 469 | .accessibilityLabel("To-do idea") |
| 470 | .onSubmit(add) |
| 471 | BoardDropdown(selection: $selectedProject, |
| 472 | title: "Project for new task", symbol: "folder", |
| 473 | choices: [nil] + model.boards.map { Optional($0.id) }, compact: true) { id in |
| 474 | id.map(model.projectName) ?? "Auto" |
| 475 | } |
| 476 | .frame(width: 176) |
| 477 | BoardDropdown(selection: $priority, title: "Priority", symbol: "flag", |
| 478 | choices: [nil] + Priority.allCases.map(Optional.some), compact: true) { |
| 479 | $0?.label ?? "Auto" |
| 480 | } |
| 481 | .frame(width: 158) |
| 482 | Button(action: add) { |
| 483 | if saving { |
| 484 | ProgressView().controlSize(.small).accessibilityLabel("Adding to-do") |
| 485 | } else { |
| 486 | Label("Add to-do", systemImage: "plus") |
| 487 | } |
| 488 | } |
| 489 | .glassPrimary() |
| 490 | .frame(minWidth: 96) |
| 491 | .disabled(input.isEmpty || tooLong || project == nil || saving) |
| 492 | .help(model.canUseAgents ? "Draft with your default task agent" : "Save your task") |
| 493 | } |
| 494 | .disabled(saving) |
| 495 | if tooLong { |
| 496 | Text("Keep your idea to 2,000 characters.").font(.caption).foregroundStyle(palette.danger) |
| 497 | } else if let feedback { |
| 498 | Text(feedback).font(.caption).foregroundStyle(failed ? palette.danger : palette.muted) |
| 499 | .accessibilityAddTraits(.updatesFrequently) |
| 500 | } |
| 501 | } |
| 502 | .onChange(of: idea) { resetRetry() } |
| 503 | .onChange(of: priority) { resetRetry() } |
| 504 | .onChange(of: selectedProject) { resetRetry() } |
| 505 | .onChange(of: project) { resetRetry() } |
| 506 | .onChange(of: scope) { selectedProject = nil } |
| 507 | .onChange(of: model.boards.map(\.id)) { _, ids in |
| 508 | if let selectedProject, !ids.contains(selectedProject) { self.selectedProject = nil } |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | private func resetRetry() { |
| 513 | requestKey = UUID() |
| 514 | retryDraft = nil |
| 515 | if failed { feedback = nil } |
| 516 | } |
| 517 | |
| 518 | private func add() { |
| 519 | guard !input.isEmpty, !tooLong, !saving, let fallbackProject = project else { return } |
| 520 | let idea = input |
| 521 | let autoProject = selectedProject == nil |
| 522 | let selectedPriority = priority |
| 523 | let key = requestKey |
| 524 | let previousDraft = retryDraft |
| 525 | saving = true |
| 526 | model.isQuickAdding = true |
| 527 | feedback = nil |
| 528 | failed = false |
| 529 | Task { |
| 530 | defer { |
| 531 | saving = false |
| 532 | model.isQuickAdding = false |
| 533 | } |
| 534 | let capture: PendingCapture |
| 535 | if let previousDraft { |
| 536 | capture = previousDraft |
| 537 | } else { |
| 538 | let draft: AgentDraft? |
| 539 | if model.canUseAgents { |
| 540 | var draftInput: [String: Any] = [ |
| 541 | "idea": idea, "autoProject": autoProject, "autoPriority": selectedPriority == nil |
| 542 | ] |
| 543 | if !autoProject { draftInput["boardId"] = fallbackProject } |
| 544 | draft = try? await model.service.call("native_draft_task", draftInput) |
| 545 | } else { draft = nil } |
| 546 | let details = draft.flatMap { $0.details.isUsableForCapture ? $0.details : nil } |
| 547 | let suggestedProject = autoProject ? draft?.projectId : nil |
| 548 | let newProjectName = autoProject ? draft?.newProjectName.flatMap { $0.isEmpty ? nil : $0 } : nil |
| 549 | let destination = model.boards.first { $0.id == suggestedProject }?.id ?? fallbackProject |
| 550 | capture = PendingCapture(details: details ?? .fromIdea(idea), |
| 551 | project: destination, newProjectName: newProjectName, |
| 552 | priority: selectedPriority ?? draft?.priority ?? .none, |
| 553 | drafted: details != nil) |
| 554 | } |
| 555 | do { |
| 556 | _ = try await model.saveNewTask([ |
| 557 | "requestKey": key.uuidString, "boardId": capture.project, "status": WorkStatus.todo.rawValue, |
| 558 | "priority": capture.priority.rawValue, "title": capture.details.title, |
| 559 | "issueType": IssueType.inferred(from: idea).rawValue, |
| 560 | "notes": capture.details.notes, "definitionOfDone": capture.details.definitionOfDone |
| 561 | ], newProjectName: capture.newProjectName) |
| 562 | self.idea = "" |
| 563 | selectedProject = nil |
| 564 | priority = nil |
| 565 | feedback = capture.drafted || !model.canUseAgents ? nil : "Agent drafting was unavailable. Your original idea was saved." |
| 566 | } catch { |
| 567 | if key == requestKey { retryDraft = capture } |
| 568 | failed = true |
| 569 | feedback = error.localizedDescription |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | private struct DashboardTask: View { |
| 576 | @Environment(WorkspaceModel.self) private var model |
| 577 | @Environment(\.palette) private var palette |
| 578 | let item: WorkItem |
| 579 | var assigned: Bool { model.workspace?.agentAssignments[item.id] != nil } |
| 580 | |
| 581 | var body: some View { |
| 582 | HStack(spacing: 11) { |
| 583 | Button { model.select(item) } label: { |
| 584 | HStack(spacing: 11) { |
| 585 | Image(systemName: item.status.symbol).foregroundStyle(palette.muted) |
| 586 | VStack(alignment: .leading, spacing: 6) { |
| 587 | HStack(spacing: 6) { |
| 588 | if item.kind == .bug { IssueTypeBadge(.bug, tint: palette.actionAccent) } |
| 589 | Text(item.title).font(.system(size: 13, weight: .medium)).lineLimit(2) |
| 590 | } |
| 591 | Text([model.projectName(item.boardId), item.status.rawValue, assigned ? "Agent task" : nil] |
| 592 | .compactMap { $0 }.joined(separator: " · ")) |
| 593 | .font(.system(size: 10)).foregroundStyle(palette.muted).lineLimit(1) |
| 594 | if let date = item.status == .done ? item.completionDate : item.creationDate { |
| 595 | Text("\(item.status == .done ? "Completed" : "Created") \(date.formatted(date: .abbreviated, time: .omitted))") |
| 596 | .font(.system(size: 10)).foregroundStyle(palette.muted) |
| 597 | } |
| 598 | } |
| 599 | Spacer(minLength: 0) |
| 600 | } |
| 601 | .frame(minWidth: 180, maxWidth: .infinity, alignment: .leading) |
| 602 | .contentShape(Rectangle()) |
| 603 | } |
| 604 | .buttonStyle(.plain) |
| 605 | .accessibilityLabel("Open \(item.title)") |
| 606 | if item.status != .done, let label = item.dueLabel { |
| 607 | Label(item.dueDate == CalendarDay.today ? "Due today" : item.late ? "Overdue · \(label)" : "Due \(label)", |
| 608 | systemImage: "calendar") |
| 609 | .font(.system(size: 10)) |
| 610 | .foregroundStyle(item.late ? palette.danger : palette.muted) |
| 611 | .lineLimit(1) |
| 612 | } |
| 613 | if item.status != .done { |
| 614 | BoardDropdown(selection: Binding(get: { item.priority }, set: { priority in |
| 615 | model.perform { |
| 616 | var input = item.write |
| 617 | input["priority"] = priority.rawValue |
| 618 | try await model.mutate("update_task", input) |
| 619 | } |
| 620 | }), title: "Priority for \(item.title)", symbol: "chart.bar.fill", choices: Priority.allCases, |
| 621 | compact: true, symbolColor: item.priority.color(palette.dark), label: \.label) |
| 622 | .frame(width: 128) |
| 623 | } |
| 624 | Button { |
| 625 | if item.status == .done { model.move(item, to: .todo) } |
| 626 | else if assigned { model.select(item) } |
| 627 | else { model.move(item, to: .done) } |
| 628 | } label: { |
| 629 | Label(item.status == .done ? "Reopen" : assigned ? "Review" : "Complete", |
| 630 | systemImage: item.status == .done ? "arrow.uturn.backward" : assigned ? "arrow.up.right" : "checkmark") |
| 631 | .font(.system(size: 11)) |
| 632 | .foregroundStyle(palette.muted) |
| 633 | } |
| 634 | .buttonStyle(.plain) |
| 635 | .fixedSize() |
| 636 | .accessibilityLabel(item.status == .done ? "Reopen \(item.title)" : assigned ? "Review \(item.title)" : "Complete \(item.title)") |
| 637 | IconButton(symbol: "trash", label: "Move \(item.title) to Trash") { model.trash(item) } |
| 638 | } |
| 639 | .padding(.horizontal, 18) |
| 640 | .frame(minHeight: 64) |
| 641 | } |
| 642 | } |