Merge branch 'board-c'
Browse files at this commit3 changed files +380 -12
- crates/izlek-topcoat/src/board.rs +377 -0
- crates/izlek-topcoat/src/lib.rs +1 -0
- crates/izlek-topcoat/src/pages.rs +2 -12
crates/izlek-topcoat/src/board.rsadded+377 -0
| @@ -0,0 +1,377 @@ | |||
| 1 | +//! The board, from the Main artboard. | ||
| 2 | +//! | ||
| 3 | +//! The whole column list is one shard: the server renders every column and | ||
| 4 | +//! card, and a drop re-runs it in place. There is no client board state to | ||
| 5 | +//! keep in sync with the server — a drag's only client-side memory is the | ||
| 6 | +//! browser's own drag session (`dataTransfer`), read back by the column that | ||
| 7 | +//! catches the drop. | ||
| 8 | + | ||
| 9 | +use izlek_core::board::{Moved, Person, TaskCard}; | ||
| 10 | +use izlek_core::store::{NewTask, User}; | ||
| 11 | +use time::{Date, OffsetDateTime}; | ||
| 12 | +use topcoat::Result; | ||
| 13 | +use topcoat::context::Cx; | ||
| 14 | +use topcoat::router::content::Form; | ||
| 15 | +use topcoat::router::request::headers; | ||
| 16 | +use topcoat::router::{HeaderName, StatusCode, header, route}; | ||
| 17 | +use topcoat::runtime::{Event, procedure, shard}; | ||
| 18 | +use topcoat::view::view; | ||
| 19 | + | ||
| 20 | +use crate::server::{Refusal, accounts, mail, require_user, require_writer}; | ||
| 21 | + | ||
| 22 | +/// The board a task belongs to, checked against this person's workspace | ||
| 23 | +/// before anything trusts an id the browser sent. | ||
| 24 | +async fn task_board_id(cx: &Cx, user: &User, task_id: &str) -> Result<Option<String>> { | ||
| 25 | + let store = accounts(cx).store().clone(); | ||
| 26 | + match store.task(task_id).await? { | ||
| 27 | + Some(facts) if facts.workspace_id == user.workspace_id => Ok(Some(facts.board_id)), | ||
| 28 | + _ => Ok(None), | ||
| 29 | + } | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +/// Moves a card. The one path `/api/move_card` and the drop procedure both | ||
| 33 | +/// call, so the two cannot drift. | ||
| 34 | +async fn move_card_shared( | ||
| 35 | + cx: &Cx, | ||
| 36 | + task_id: &str, | ||
| 37 | + from_column_id: &str, | ||
| 38 | + to_column_id: &str, | ||
| 39 | +) -> Result<Option<Refusal>> { | ||
| 40 | + let user = match require_writer(cx).await { | ||
| 41 | + Ok(user) => user, | ||
| 42 | + Err(refusal) => return Ok(Some(refusal)), | ||
| 43 | + }; | ||
| 44 | + let Some(board_id) = task_board_id(cx, &user, task_id).await? else { | ||
| 45 | + return Ok(Some(Refusal::NotFound)); | ||
| 46 | + }; | ||
| 47 | + let store = accounts(cx).store().clone(); | ||
| 48 | + let columns = store.columns(&board_id).await?; | ||
| 49 | + if !columns.iter().any(|column| column.id == to_column_id) { | ||
| 50 | + return Ok(Some(Refusal::Forbidden)); | ||
| 51 | + } | ||
| 52 | + match store | ||
| 53 | + .move_task(task_id, from_column_id, to_column_id, &user.id, OffsetDateTime::now_utc()) | ||
| 54 | + .await | ||
| 55 | + { | ||
| 56 | + Ok(Moved::Recorded(transition)) => { | ||
| 57 | + mail(cx).after(transition); | ||
| 58 | + Ok(None) | ||
| 59 | + } | ||
| 60 | + Ok(Moved::Unchanged) => Ok(None), | ||
| 61 | + Ok(Moved::Stale) => Ok(Some(Refusal::MovedAlready)), | ||
| 62 | + Err(izlek_core::store::StoreError::NotFound) => Ok(Some(Refusal::NotFound)), | ||
| 63 | + Err(error) => { | ||
| 64 | + eprintln!("store error: {error}"); | ||
| 65 | + Ok(Some(Refusal::Unavailable)) | ||
| 66 | + } | ||
| 67 | + } | ||
| 68 | +} | ||
| 69 | + | ||
| 70 | +/// Adds a card to a column. A Viewer is refused here, in the handler. | ||
| 71 | +async fn create_task_shared(cx: &Cx, title: &str, column_id: &str) -> Result<Option<Refusal>> { | ||
| 72 | + let user = match require_writer(cx).await { | ||
| 73 | + Ok(user) => user, | ||
| 74 | + Err(refusal) => return Ok(Some(refusal)), | ||
| 75 | + }; | ||
| 76 | + let title = title.trim(); | ||
| 77 | + if title.is_empty() { | ||
| 78 | + return Ok(Some(Refusal::EmptyTitle)); | ||
| 79 | + } | ||
| 80 | + let store = accounts(cx).store().clone(); | ||
| 81 | + let Some(board) = store.board(&user.workspace_id).await? else { | ||
| 82 | + return Ok(Some(Refusal::Unavailable)); | ||
| 83 | + }; | ||
| 84 | + let columns = store.columns(&board.id).await?; | ||
| 85 | + if !columns.iter().any(|column| column.id == column_id) { | ||
| 86 | + return Ok(Some(Refusal::Forbidden)); | ||
| 87 | + } | ||
| 88 | + let created = store | ||
| 89 | + .create_task(NewTask { | ||
| 90 | + board_id: &board.id, | ||
| 91 | + column_id, | ||
| 92 | + title, | ||
| 93 | + description: "", | ||
| 94 | + deadline: None, | ||
| 95 | + created_by: &user.id, | ||
| 96 | + }) | ||
| 97 | + .await?; | ||
| 98 | + mail(cx).after_activity(store, created.activity_id); | ||
| 99 | + Ok(None) | ||
| 100 | +} | ||
| 101 | + | ||
| 102 | +type Redirect = Result<(StatusCode, [(HeaderName, String); 1])>; | ||
| 103 | + | ||
| 104 | +fn redirect(cx: &Cx) -> Redirect { | ||
| 105 | + let back = headers(cx) | ||
| 106 | + .get(header::REFERER) | ||
| 107 | + .and_then(|value| value.to_str().ok()) | ||
| 108 | + .unwrap_or("/") | ||
| 109 | + .to_string(); | ||
| 110 | + Ok((StatusCode::SEE_OTHER, [(header::LOCATION, back)])) | ||
| 111 | +} | ||
| 112 | + | ||
| 113 | +#[derive(serde::Deserialize)] | ||
| 114 | +struct CreateTaskForm { | ||
| 115 | + title: String, | ||
| 116 | + column_id: String, | ||
| 117 | +} | ||
| 118 | + | ||
| 119 | +/// A browser without script's way onto the board: a real form post, same | ||
| 120 | +/// fields the procedure below trades over the wire. | ||
| 121 | +#[route(POST "/api/create_task")] | ||
| 122 | +async fn create_task(cx: &Cx, Form(input): Form<CreateTaskForm>) -> Redirect { | ||
| 123 | + let _ = create_task_shared(cx, &input.title, &input.column_id).await?; | ||
| 124 | + redirect(cx) | ||
| 125 | +} | ||
| 126 | + | ||
| 127 | +#[derive(serde::Deserialize)] | ||
| 128 | +struct MoveCardForm { | ||
| 129 | + task_id: String, | ||
| 130 | + from_column_id: String, | ||
| 131 | + to_column_id: String, | ||
| 132 | +} | ||
| 133 | + | ||
| 134 | +/// The same move the drop procedure performs, reachable without script. | ||
| 135 | +#[route(POST "/api/move_card")] | ||
| 136 | +async fn move_card(cx: &Cx, Form(input): Form<MoveCardForm>) -> Redirect { | ||
| 137 | + let _ = move_card_shared(cx, &input.task_id, &input.from_column_id, &input.to_column_id).await?; | ||
| 138 | + redirect(cx) | ||
| 139 | +} | ||
| 140 | + | ||
| 141 | +/// What a drop calls: performs the move and hands back a refusal code, if | ||
| 142 | +/// any, for the drop handler to leave alone. Callable with any arguments — | ||
| 143 | +/// the checks above are what actually guard it. | ||
| 144 | +#[procedure] | ||
| 145 | +async fn move_card_procedure( | ||
| 146 | + cx: &Cx, | ||
| 147 | + task_id: String, | ||
| 148 | + from_column_id: String, | ||
| 149 | + to_column_id: String, | ||
| 150 | +) -> Result<Result<bool, String>> { | ||
| 151 | + match move_card_shared(cx, &task_id, &from_column_id, &to_column_id).await? { | ||
| 152 | + Some(refusal) => Ok(Err(refusal.code().to_string())), | ||
| 153 | + None => Ok(Ok(true)), | ||
| 154 | + } | ||
| 155 | +} | ||
| 156 | + | ||
| 157 | +/// The whole column list. `version` carries no meaning of its own — bumping | ||
| 158 | +/// it is what tells the browser to ask for this again after a drop lands. | ||
| 159 | +#[shard] | ||
| 160 | +async fn board_columns(cx: &Cx, version: f64) -> Result { | ||
| 161 | + let _ = version; | ||
| 162 | + let user = match require_user(cx).await { | ||
| 163 | + Ok(user) => user, | ||
| 164 | + Err(refusal) => { | ||
| 165 | + return view! { | ||
| 166 | + cx => | ||
| 167 | + <div class="scaffold-note"><p>(refusal.message())</p></div> | ||
| 168 | + }; | ||
| 169 | + } | ||
| 170 | + }; | ||
| 171 | + let store = accounts(cx).store().clone(); | ||
| 172 | + let Some(view_data) = izlek_core::board::load(store.as_ref(), &user.workspace_id).await? | ||
| 173 | + else { | ||
| 174 | + return view! { | ||
| 175 | + cx => | ||
| 176 | + <div class="scaffold-note"><p>"Something went wrong."</p></div> | ||
| 177 | + }; | ||
| 178 | + }; | ||
| 179 | + let today = OffsetDateTime::now_utc().date(); | ||
| 180 | + let may_write = user.role.can_write_tasks(); | ||
| 181 | + let empty = view_data.is_empty(); | ||
| 182 | + | ||
| 183 | + let mut columns = Vec::new(); | ||
| 184 | + for column in view_data.columns { | ||
| 185 | + columns.push(render_column(cx, column, today, may_write).await?); | ||
| 186 | + } | ||
| 187 | + | ||
| 188 | + view! { | ||
| 189 | + cx => | ||
| 190 | + for column in (columns) { (column) } | ||
| 191 | + if (empty) { | ||
| 192 | + <div class="board-empty"> | ||
| 193 | + <div class="board-empty-title">"This board is empty"</div> | ||
| 194 | + </div> | ||
| 195 | + } | ||
| 196 | + } | ||
| 197 | +} | ||
| 198 | + | ||
| 199 | +async fn render_column( | ||
| 200 | + cx: &Cx, | ||
| 201 | + column: izlek_core::board::ColumnView, | ||
| 202 | + today: Date, | ||
| 203 | + may_write: bool, | ||
| 204 | +) -> Result { | ||
| 205 | + let column_id = column.column.id; | ||
| 206 | + let name = column.column.name; | ||
| 207 | + let is_done_column = column.column.is_done; | ||
| 208 | + let count = column.cards.len(); | ||
| 209 | + let mut cards = Vec::new(); | ||
| 210 | + for card in column.cards { | ||
| 211 | + cards.push(render_card(cx, card, today, is_done_column, &column_id, may_write).await?); | ||
| 212 | + } | ||
| 213 | + | ||
| 214 | + view! { | ||
| 215 | + cx => | ||
| 216 | + signal composing = false; | ||
| 217 | + <section class="column" id=(column_id.clone())> | ||
| 218 | + <header class="column-head"> | ||
| 219 | + <span class="column-name">(name)</span> | ||
| 220 | + <span class="column-count">(count)</span> | ||
| 221 | + <div class="spacer"></div> | ||
| 222 | + if (may_write) { | ||
| 223 | + <button class="column-add" title="Add a task" @click=$(|_e: Event| composing.set(true))> | ||
| 224 | + "+" | ||
| 225 | + </button> | ||
| 226 | + } | ||
| 227 | + </header> | ||
| 228 | + <div class="column-cards" id=(column_id.clone())> | ||
| 229 | + for card in (cards) { (card) } | ||
| 230 | + if (may_write) { | ||
| 231 | + <form method="post" action="/api/create_task" class="composer" :hidden=$(!composing.get())> | ||
| 232 | + <input type="hidden" name="column_id" value=(column_id.clone())> | ||
| 233 | + <input class="composer-input" type="text" name="title" placeholder="What needs doing?" required=""> | ||
| 234 | + <div class="composer-row"> | ||
| 235 | + <button class="composer-add" type="submit">"Add"</button> | ||
| 236 | + <button class="composer-cancel" type="button" @click=$(|_e: Event| composing.set(false))> | ||
| 237 | + "Cancel" | ||
| 238 | + </button> | ||
| 239 | + </div> | ||
| 240 | + </form> | ||
| 241 | + } | ||
| 242 | + </div> | ||
| 243 | + </section> | ||
| 244 | + } | ||
| 245 | +} | ||
| 246 | + | ||
| 247 | +async fn render_card( | ||
| 248 | + cx: &Cx, | ||
| 249 | + card: TaskCard, | ||
| 250 | + today: Date, | ||
| 251 | + done_column: bool, | ||
| 252 | + column_id: &str, | ||
| 253 | + draggable: bool, | ||
| 254 | +) -> Result { | ||
| 255 | + let blocks = card.blocks.len(); | ||
| 256 | + let blocked_by = card.blocked_by.join(", "); | ||
| 257 | + let deadline = card.deadline_label(today); | ||
| 258 | + let overdue = card.is_overdue(today); | ||
| 259 | + let dated = card.deadline.is_some() || card.is_done(); | ||
| 260 | + let comments = card.comment_count; | ||
| 261 | + let mut assignees = Vec::new(); | ||
| 262 | + for person in card.assignees.iter() { | ||
| 263 | + assignees.push(avatar(cx, person, "").await?); | ||
| 264 | + } | ||
| 265 | + let has_assignees = !card.assignees.is_empty(); | ||
| 266 | + let task_id = card.id.clone(); | ||
| 267 | + let from_column = column_id.to_string(); | ||
| 268 | + let href = format!("/?task={}", card.id); | ||
| 269 | + | ||
| 270 | + view! { | ||
| 271 | + cx => | ||
| 272 | + <a class="card" id=(column_id.to_string()) class:card-done=(done_column) href=(href) | ||
| 273 | + draggable=(if draggable { "true" } else { "false" }) | ||
| 274 | + @dragstart=$(|e: Event| raw!( | ||
| 275 | + "(() => { ${e}.inner.dataTransfer.setData('izlek-task', ${task_id}); ${e}.inner.dataTransfer.setData('izlek-from', ${from_column}); })()", | ||
| 276 | + () | ||
| 277 | + )) | ||
| 278 | + > | ||
| 279 | + <div class="card-keys"> | ||
| 280 | + <span class="card-key">(card.task_key.clone())</span> | ||
| 281 | + if (blocks > 0) { <span class="card-blocks">(format!("blocks {blocks}"))</span> } | ||
| 282 | + if (!blocked_by.is_empty()) { | ||
| 283 | + <span class="card-blocked-by">(format!("blocked by {blocked_by}"))</span> | ||
| 284 | + } | ||
| 285 | + </div> | ||
| 286 | + <div class="card-title">(card.title.clone())</div> | ||
| 287 | + <div class="card-foot"> | ||
| 288 | + <span class="card-deadline" class:card-deadline-overdue=(overdue) class:card-deadline-none=(!dated)> | ||
| 289 | + (deadline) | ||
| 290 | + </span> | ||
| 291 | + if (comments > 0) { <span class="card-comments">(format!("{comments} \u{270e}"))</span> } | ||
| 292 | + <div class="spacer"></div> | ||
| 293 | + <div class="avatars"> | ||
| 294 | + for person in (assignees) { (person) } | ||
| 295 | + if (!has_assignees) { | ||
| 296 | + <span class="avatar avatar-none" role="img" aria-label="nobody assigned" title="Nobody assigned"></span> | ||
| 297 | + } | ||
| 298 | + </div> | ||
| 299 | + </div> | ||
| 300 | + </a> | ||
| 301 | + } | ||
| 302 | +} | ||
| 303 | + | ||
| 304 | +pub(crate) async fn avatar(cx: &Cx, person: &Person, extra: &str) -> Result { | ||
| 305 | + let initials = person.initials(); | ||
| 306 | + let tone = person | ||
| 307 | + .id | ||
| 308 | + .bytes() | ||
| 309 | + .fold(0u32, |acc, b| acc.wrapping_mul(31).wrapping_add(b as u32)) | ||
| 310 | + % 5; | ||
| 311 | + let class = format!("avatar avatar-tone-{tone} {extra}"); | ||
| 312 | + let title = person.display_name.clone(); | ||
| 313 | + view! { | ||
| 314 | + cx => | ||
| 315 | + <span class=(class) title=(title)>(initials)</span> | ||
| 316 | + } | ||
| 317 | +} | ||
| 318 | + | ||
| 319 | +/// The signed-in board: the topbar, the filter chips and the shard that owns | ||
| 320 | +/// every column and card. | ||
| 321 | +pub async fn board_page(cx: &Cx, user: &User) -> Result { | ||
| 322 | + let view_data = { | ||
| 323 | + let store = accounts(cx).store().clone(); | ||
| 324 | + izlek_core::board::load(store.as_ref(), &user.workspace_id).await? | ||
| 325 | + }; | ||
| 326 | + let Some(view_data) = view_data else { | ||
| 327 | + return view! { | ||
| 328 | + cx => | ||
| 329 | + <main class="scaffold-note"><p>"Something went wrong."</p></main> | ||
| 330 | + }; | ||
| 331 | + }; | ||
| 332 | + let today = OffsetDateTime::now_utc().date(); | ||
| 333 | + let overdue = view_data.overdue_count(today); | ||
| 334 | + let blocked = view_data.blocked_count(); | ||
| 335 | + let may_write = user.role.can_write_tasks(); | ||
| 336 | + | ||
| 337 | + view! { | ||
| 338 | + cx => | ||
| 339 | + <header class="topbar"> | ||
| 340 | + <div class="wordmark"> | ||
| 341 | + <span class="wordmark-text">"izlek"</span> | ||
| 342 | + <span class="wordmark-dot"></span> | ||
| 343 | + </div> | ||
| 344 | + <div class="spacer"></div> | ||
| 345 | + <span class="topbar-who" title=(user.email.clone())>(user.display_name.clone())</span> | ||
| 346 | + <a class="topbar-link" href="/settings">"Settings"</a> | ||
| 347 | + if (may_write) { | ||
| 348 | + <button class="new-task">"New task"</button> | ||
| 349 | + } | ||
| 350 | + </header> | ||
| 351 | + <div class="filterbar"> | ||
| 352 | + <div class="topbar-divider"></div> | ||
| 353 | + <span class="sort-note">"Sort: deadline"</span> | ||
| 354 | + <div class="spacer"></div> | ||
| 355 | + if (overdue > 0) { <span class="chip chip-overdue">(format!("{overdue} overdue"))</span> } | ||
| 356 | + if (blocked > 0) { <span class="chip chip-blocked">(format!("{blocked} blocked"))</span> } | ||
| 357 | + </div> | ||
| 358 | + <main class="board-stage"> | ||
| 359 | + signal version = 0.0; | ||
| 360 | + <div class="board-columns" | ||
| 361 | + @dragover=$(|e: Event| e.prevent_default()) | ||
| 362 | + @drop=$(async move |e: Event| { | ||
| 363 | + e.prevent_default(); | ||
| 364 | + let to = e.target.id.clone(); | ||
| 365 | + let task_id = raw!("cx.hydrate(${e}.inner.dataTransfer.getData('izlek-task'))", "".to_owned()); | ||
| 366 | + let from = raw!("cx.hydrate(${e}.inner.dataTransfer.getData('izlek-from'))", "".to_owned()); | ||
| 367 | + if !task_id.is_empty() { | ||
| 368 | + let result = move_card_procedure(task_id, from, to).await; | ||
| 369 | + version.set(version.get() + 1.0); | ||
| 370 | + } | ||
| 371 | + }) | ||
| 372 | + > | ||
| 373 | + board_columns(version: $(version.get())) | ||
| 374 | + </div> | ||
| 375 | + </main> | ||
| 376 | + } | ||
| 377 | +} | ||
crates/izlek-topcoat/src/lib.rsmodified+1 -0
| @@ -4,6 +4,7 @@ | |||
| 4 | #![recursion_limit = "256"] | 4 | #![recursion_limit = "256"] |
| 5 | 5 | ||
| 6 | pub mod auth; | 6 | pub mod auth; |
| 7 | +pub mod board; | ||
| 7 | pub mod detail; | 8 | pub mod detail; |
| 8 | pub mod files; | 9 | pub mod files; |
| 9 | pub mod layout; | 10 | pub mod layout; |
crates/izlek-topcoat/src/pages.rsmodified+2 -12
| @@ -260,17 +260,7 @@ async fn join_card(cx: &Cx, token: &str, person: Invited) -> Result { | |||
| 260 | } | 260 | } |
| 261 | } | 261 | } |
| 262 | 262 | ||
| 263 | -/// The signed-in landing, once a session resolves to a real user. Only the | 263 | +/// The signed-in landing, once a session resolves to a real user: the board. |
| 264 | -/// topbar and an empty stage for now. | ||
| 265 | async fn signed_in_shell(cx: &Cx, user: User) -> Result { | 264 | async fn signed_in_shell(cx: &Cx, user: User) -> Result { |
| 266 | - view! { | 265 | + crate::board::board_page(cx, &user).await |
| 267 | - cx => | ||
| 268 | - (topbar(cx).await?) | ||
| 269 | - <main class="scaffold-note"> | ||
| 270 | - <p>"Signed in as " (user.display_name) "."</p> | ||
| 271 | - <div class="board-stage"> | ||
| 272 | - // board lands in S4 | ||
| 273 | - </div> | ||
| 274 | - </main> | ||
| 275 | - } | ||
| 276 | } | 266 | } |