| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#![allow(clippy::needless_pass_by_value)] |
| 37 |
|
| 38 |
use chrono::{Local, NaiveDate, TimeZone, Utc}; |
| 39 |
use goingson_core::TimelineItem; |
| 40 |
use makeover_layout::{Placement, Tone, Track}; |
| 41 |
use quasi_declare::declare; |
| 42 |
use quasi_router::screen::{Choice, Tag}; |
| 43 |
use quasi_router::{Response, RouteError, Router, Slot}; |
| 44 |
|
| 45 |
use crate::commands::{ContextResponse, DayPlanningResponse, TaskResponse, day_plan}; |
| 46 |
use crate::state::{AppState, DESKTOP_USER_ID}; |
| 47 |
|
| 48 |
#[cfg(test)] |
| 49 |
mod tests; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
fn date_of(request: &quasi_router::Request) -> Result<NaiveDate, RouteError> { |
| 58 |
let Some(raw) = request.captures.get("date") else { |
| 59 |
return Ok(Local::now().date_naive()); |
| 60 |
}; |
| 61 |
NaiveDate::parse_from_str(raw, "%Y-%m-%d").map_err(|_| RouteError::not_found("not a date")) |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
fn plan(state: &AppState, date: NaiveDate) -> Result<DayPlanningResponse, RouteError> { |
| 66 |
day_plan(state, date).map_err(|error| RouteError::internal(error.to_string())) |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
const FOCUS_MINUTES: u16 = 9 * 60; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
const SLOT_MINUTES: i32 = 15; |
| 81 |
|
| 82 |
|
| 83 |
const DAY_MINUTES: i32 = 24 * 60; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
const UNESTIMATED_MINUTES: i32 = 15; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
fn slots() -> Vec<Choice> { |
| 98 |
(0..DAY_MINUTES / SLOT_MINUTES) |
| 99 |
.map(|slot| { |
| 100 |
let minutes = slot * SLOT_MINUTES; |
| 101 |
Choice::new( |
| 102 |
minutes.to_string(), |
| 103 |
format!("{:02}:{:02}", minutes / 60, minutes % 60), |
| 104 |
) |
| 105 |
}) |
| 106 |
.collect() |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
fn estimated(task: &TaskResponse) -> bool { |
| 114 |
task.estimated_minutes.is_some_and(|minutes| minutes > 0) |
| 115 |
} |
| 116 |
|
| 117 |
declare! { |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
shape placing(task: &TaskResponse, date: NaiveDate) -> Act; |
| 133 |
|
| 134 |
act "Place on the day" to post "/day/{date}/schedule/{task.id}/place" { |
| 135 |
field Select "at" "Start at" { |
| 136 |
options slots(); |
| 137 |
value FOCUS_MINUTES.to_string(); |
| 138 |
required; |
| 139 |
} |
| 140 |
|
| 141 |
field Number "minutes" "How long it takes" unless estimated(task) { |
| 142 |
within "1" DAY_MINUTES.to_string(); |
| 143 |
value UNESTIMATED_MINUTES.to_string(); |
| 144 |
hint "Placing it records this as the task's estimate."; |
| 145 |
required; |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
struct Entry { |
| 152 |
item: TimelineItem, |
| 153 |
|
| 154 |
conflicted: bool, |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
placement: Placement, |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
struct Waiting { |
| 165 |
task: TaskResponse, |
| 166 |
|
| 167 |
gate: Option<String>, |
| 168 |
|
| 169 |
out_of_order: bool, |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
struct Day { |
| 174 |
date: NaiveDate, |
| 175 |
|
| 176 |
on_axis: Vec<Entry>, |
| 177 |
|
| 178 |
covering: Vec<Entry>, |
| 179 |
|
| 180 |
pool: Vec<Waiting>, |
| 181 |
|
| 182 |
contexts: Vec<ContextResponse>, |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
fn read(state: &AppState, date: NaiveDate) -> Result<Day, RouteError> { |
| 190 |
let response = plan(state, date)?; |
| 191 |
|
| 192 |
let clashing: std::collections::HashSet<_> = response |
| 193 |
.conflicts |
| 194 |
.iter() |
| 195 |
.flat_map(|conflict| [conflict.item1_id, conflict.item2_id]) |
| 196 |
.collect(); |
| 197 |
|
| 198 |
let (all_day, on_axis): (Vec<TimelineItem>, Vec<TimelineItem>) = response |
| 199 |
.timeline_items |
| 200 |
.into_iter() |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
.partition(|item| item.is_all_day); |
| 205 |
|
| 206 |
let placed = |item: TimelineItem, conflicted: bool| Entry { |
| 207 |
placement: Placement::new( |
| 208 |
u16::try_from(item.day_offset_minutes.max(0)).unwrap_or(0), |
| 209 |
u16::try_from(item.visible_duration_minutes.max(1)).unwrap_or(1), |
| 210 |
), |
| 211 |
conflicted, |
| 212 |
item, |
| 213 |
}; |
| 214 |
|
| 215 |
let pool = response |
| 216 |
.unscheduled_tasks |
| 217 |
.into_iter() |
| 218 |
.map(|task| { |
| 219 |
let gate = response.gates.get(&task.id); |
| 220 |
Waiting { |
| 221 |
gate: gate.and_then(|gate| { |
| 222 |
gate.after.first().map(|first| { |
| 223 |
if gate.after.len() > 1 { |
| 224 |
format!("after {} +{}", first.title, gate.after.len() - 1) |
| 225 |
} else { |
| 226 |
format!("after {}", first.title) |
| 227 |
} |
| 228 |
}) |
| 229 |
}), |
| 230 |
out_of_order: gate.is_some_and(|gate| gate.out_of_order), |
| 231 |
task, |
| 232 |
} |
| 233 |
}) |
| 234 |
.collect(); |
| 235 |
|
| 236 |
Ok(Day { |
| 237 |
date, |
| 238 |
on_axis: on_axis |
| 239 |
.into_iter() |
| 240 |
.map(|item| { |
| 241 |
let conflicted = clashing.contains(&item.id); |
| 242 |
placed(item, conflicted) |
| 243 |
}) |
| 244 |
.collect(), |
| 245 |
covering: all_day |
| 246 |
.into_iter() |
| 247 |
.map(|item| placed(item, false)) |
| 248 |
.collect(), |
| 249 |
pool, |
| 250 |
contexts: response.contexts, |
| 251 |
}) |
| 252 |
} |
| 253 |
|
| 254 |
declare! { |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
shape row_for(day: &Day, entry: &Entry) -> Row; |
| 281 |
|
| 282 |
row &entry.item.title { |
| 283 |
for project in entry.item.project_name.iter() { |
| 284 |
meta project; |
| 285 |
} |
| 286 |
|
| 287 |
token Tag::badge("from earlier").tone(Tone::Neutral) when entry.item.continues_before; |
| 288 |
token Tag::badge("continues").tone(Tone::Neutral) when entry.item.continues_after; |
| 289 |
token Tag::badge("clashes").tone(Tone::Danger) when entry.conflicted; |
| 290 |
token Tag::badge("block").tone(Tone::Info) when entry.item.item_type is "block"; |
| 291 |
|
| 292 |
for task in entry.item.linked_task_id.iter() { |
| 293 |
activate to get "/tasks/{task}"; |
| 294 |
act "Move earlier" to post "/day/{day.date}/schedule/{task}/move" with "by" "-15"; |
| 295 |
act "Move later" to post "/day/{day.date}/schedule/{task}/move" with "by" "15"; |
| 296 |
act "Unschedule" to post "/day/{day.date}/schedule/{task}/unschedule"; |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
declare! { |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
shape timeline(day: &Day) -> Node; |
| 309 |
|
| 310 |
timeline Track::DAY { |
| 311 |
focus FOCUS_MINUTES; |
| 312 |
|
| 313 |
for entry in day.on_axis.iter() { |
| 314 |
at entry.placement include row_for(day, entry); |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
declare! { |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
shape all_day(day: &Day) -> Option<Node>; |
| 330 |
|
| 331 |
list { |
| 332 |
for entry in day.covering.iter() { |
| 333 |
include row_for(day, entry); |
| 334 |
} |
| 335 |
} unless day.covering.is_empty(); |
| 336 |
} |
| 337 |
|
| 338 |
declare! { |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
shape pool(day: &Day) -> Node; |
| 350 |
|
| 351 |
given day.pool.is_empty() { |
| 352 |
true -> text "Nothing else due today."; |
| 353 |
otherwise -> list { |
| 354 |
for waiting in day.pool.iter() { |
| 355 |
row &waiting.task.title { |
| 356 |
for project in waiting.task.project_name.iter() { |
| 357 |
meta project; |
| 358 |
} |
| 359 |
|
| 360 |
for gate in waiting.gate.iter() { |
| 361 |
token Tag::badge(gate).tone(Tone::Warning); |
| 362 |
} |
| 363 |
|
| 364 |
token Tag::badge("out of order").tone(Tone::Warning) |
| 365 |
when waiting.out_of_order; |
| 366 |
|
| 367 |
for marker in super::Availability::reported(&waiting.task) |
| 368 |
.frees_marker() |
| 369 |
.into_iter() |
| 370 |
{ |
| 371 |
token marker; |
| 372 |
} |
| 373 |
|
| 374 |
include placing(&waiting.task, day.date); |
| 375 |
activate to get "/tasks/{waiting.task.id}"; |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
fn previous(day: &Day) -> NaiveDate { |
| 384 |
day.date.pred_opt().unwrap_or(day.date) |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
fn next(day: &Day) -> NaiveDate { |
| 389 |
day.date.succ_opt().unwrap_or(day.date) |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
fn title(day: &Day) -> String { |
| 394 |
day.date.format("%A, %-d %B").to_string() |
| 395 |
} |
| 396 |
|
| 397 |
declare! { |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
shape band(day: &Day) -> Slot; |
| 410 |
|
| 411 |
region "day-band" as Band { |
| 412 |
page title(day); |
| 413 |
|
| 414 |
chip "Previous" to get "/day/{previous(day)}"; |
| 415 |
chip "Next" to get "/day/{next(day)}"; |
| 416 |
|
| 417 |
for context in day.contexts.iter() { |
| 418 |
banner Tone::Info context.label.clone(); |
| 419 |
} |
| 420 |
|
| 421 |
chip "Contexts" to get "/contexts"; |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
declare! { |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
shape screen(day: &Day, tracked: Slot) -> Screen; |
| 433 |
|
| 434 |
screen list_detail "Day" false { |
| 435 |
at_place super::shell::DAY; |
| 436 |
|
| 437 |
include band(day); |
| 438 |
|
| 439 |
region "day-timeline" as Pane { |
| 440 |
for strip in all_day(day).into_iter() { |
| 441 |
include strip; |
| 442 |
} |
| 443 |
include timeline(day); |
| 444 |
} |
| 445 |
|
| 446 |
include tracked; |
| 447 |
|
| 448 |
region "day-pool" as Pane { |
| 449 |
include pool(day); |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
fn day(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 456 |
let date = date_of(&request)?; |
| 457 |
let read = read(state, date)?; |
| 458 |
Ok(screen( |
| 459 |
&read, |
| 460 |
super::time_tracking::summary_panel(&super::time_tracking::tracked(state)?), |
| 461 |
) |
| 462 |
.into()) |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
fn timeline_only(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 467 |
let date = date_of(&request)?; |
| 468 |
Ok(Response::fragment( |
| 469 |
"day-timeline", |
| 470 |
timeline(&read(state, date)?), |
| 471 |
)) |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
fn task_of(request: &quasi_router::Request) -> Result<goingson_core::TaskId, RouteError> { |
| 476 |
let raw = request |
| 477 |
.captures |
| 478 |
.get("task") |
| 479 |
.ok_or_else(|| RouteError::not_found("no task id"))?; |
| 480 |
Ok(goingson_core::TaskId::from( |
| 481 |
uuid::Uuid::parse_str(raw).map_err(|_| RouteError::not_found("not a task id"))?, |
| 482 |
)) |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
fn move_scheduled( |
| 500 |
state: &AppState, |
| 501 |
request: quasi_router::Request, |
| 502 |
) -> Result<Response, RouteError> { |
| 503 |
let date = date_of(&request)?; |
| 504 |
let id = task_of(&request)?; |
| 505 |
let by: i64 = request |
| 506 |
.payload |
| 507 |
.get("by") |
| 508 |
.and_then(|raw| raw.parse().ok()) |
| 509 |
.ok_or_else(|| RouteError::not_found("move by a number of minutes"))?; |
| 510 |
|
| 511 |
let task = state |
| 512 |
.tasks |
| 513 |
.get_by_id(id, DESKTOP_USER_ID) |
| 514 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 515 |
.ok_or_else(|| RouteError::not_found("no such task"))?; |
| 516 |
let Some(start) = task.scheduled_start else { |
| 517 |
return Err(RouteError::not_found("that task is not on the day")); |
| 518 |
}; |
| 519 |
|
| 520 |
let moved = start + chrono::Duration::minutes(by); |
| 521 |
|
| 522 |
|
| 523 |
if moved.with_timezone(&chrono::Local).date_naive() == date { |
| 524 |
crate::commands::day_planning::schedule_task_now(state, id, moved, task.scheduled_duration) |
| 525 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 526 |
} |
| 527 |
|
| 528 |
timeline_fragment(state, date) |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
fn unschedule(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 533 |
let date = date_of(&request)?; |
| 534 |
let id = task_of(&request)?; |
| 535 |
crate::commands::day_planning::unschedule_task_now(state, id) |
| 536 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 537 |
timeline_fragment(state, date) |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
fn place(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 553 |
let date = date_of(&request)?; |
| 554 |
let id = task_of(&request)?; |
| 555 |
|
| 556 |
let at: i32 = request |
| 557 |
.payload |
| 558 |
.get("at") |
| 559 |
.and_then(|raw| raw.parse().ok()) |
| 560 |
.filter(|minutes| (0..DAY_MINUTES).contains(minutes)) |
| 561 |
.ok_or_else(|| RouteError::not_found("place it at a slot of this day"))?; |
| 562 |
|
| 563 |
let task = state |
| 564 |
.tasks |
| 565 |
.get_by_id(id, DESKTOP_USER_ID) |
| 566 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 567 |
.ok_or_else(|| RouteError::not_found("no such task"))?; |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
let estimated = task.estimated_minutes.filter(|minutes| *minutes > 0); |
| 572 |
let minutes = match estimated { |
| 573 |
Some(estimate) => estimate, |
| 574 |
None => request |
| 575 |
.payload |
| 576 |
.get("minutes") |
| 577 |
.and_then(|raw| raw.parse().ok()) |
| 578 |
.filter(|asked| (1..=DAY_MINUTES).contains(asked)) |
| 579 |
.ok_or_else(|| RouteError::not_found("say how long it takes"))?, |
| 580 |
}; |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
if estimated.is_none() { |
| 587 |
state |
| 588 |
.tasks |
| 589 |
.update( |
| 590 |
id, |
| 591 |
DESKTOP_USER_ID, |
| 592 |
goingson_core::UpdateTask { |
| 593 |
project_id: task.project_id, |
| 594 |
milestone_id: task.milestone_id, |
| 595 |
contact_id: task.contact_id, |
| 596 |
title: task.title.clone(), |
| 597 |
description: task.description.clone(), |
| 598 |
status: task.status.clone(), |
| 599 |
priority: task.priority.clone(), |
| 600 |
due: task.due, |
| 601 |
tags: task.tags.clone(), |
| 602 |
recurrence: task.recurrence.clone(), |
| 603 |
recurrence_rule: task.recurrence_rule.clone(), |
| 604 |
urgency: task.urgency, |
| 605 |
scheduled_start: task.scheduled_start, |
| 606 |
scheduled_duration: task.scheduled_duration, |
| 607 |
estimated_minutes: Some(minutes), |
| 608 |
}, |
| 609 |
) |
| 610 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 611 |
.ok_or_else(|| RouteError::not_found("no such task"))?; |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
let civil = date |
| 620 |
.and_hms_opt( |
| 621 |
u32::try_from(at / 60).unwrap_or(0), |
| 622 |
u32::try_from(at % 60).unwrap_or(0), |
| 623 |
0, |
| 624 |
) |
| 625 |
.ok_or_else(|| RouteError::not_found("not a time of day"))?; |
| 626 |
let start = Local |
| 627 |
.from_local_datetime(&civil) |
| 628 |
.earliest() |
| 629 |
.ok_or_else(|| RouteError::conflict("the clock skips that time on this day"))? |
| 630 |
.with_timezone(&Utc); |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
crate::commands::day_planning::schedule_task_now(state, id, start, Some(minutes)) |
| 636 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 637 |
|
| 638 |
timeline_fragment(state, date) |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
fn timeline_fragment(state: &AppState, date: NaiveDate) -> Result<Response, RouteError> { |
| 650 |
let read = read(state, date)?; |
| 651 |
Ok(Response::fragment("day-timeline", timeline(&read)).also("day-pool", pool(&read))) |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
pub fn routes(router: Router<AppState>) -> Router<AppState> { |
| 656 |
router |
| 657 |
.get("/day", day) |
| 658 |
.get("/day/{date}", day) |
| 659 |
.get("/day/{date}/timeline", timeline_only) |
| 660 |
.post("/day/{date}/schedule/{task}/place", place) |
| 661 |
.post("/day/{date}/schedule/{task}/move", move_scheduled) |
| 662 |
.post("/day/{date}/schedule/{task}/unschedule", unschedule) |
| 663 |
} |
| 664 |
|