| 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 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
use quasi_router::layout::{Notice, Tone}; |
| 67 |
use quasi_router::{ |
| 68 |
Act, Action, Figure, Node, Prose, RegionKind, Request, Response, Rest, RouteError, Router, Row, |
| 69 |
Screen, Slot, |
| 70 |
}; |
| 71 |
|
| 72 |
use super::{Group, Panels, Queued, Scope}; |
| 73 |
|
| 74 |
|
| 75 |
const HEAD: &str = "review-head"; |
| 76 |
|
| 77 |
|
| 78 |
const TAGS: &str = "review-tags"; |
| 79 |
|
| 80 |
|
| 81 |
const GROUP: &str = "review-group"; |
| 82 |
|
| 83 |
|
| 84 |
const FOOT: &str = "review-foot"; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
pub const RENDER_ROWS: usize = 200; |
| 98 |
|
| 99 |
|
| 100 |
pub fn routes(router: Router<Panels<'_>>) -> Router<Panels<'_>> { |
| 101 |
router |
| 102 |
.get("/review", index) |
| 103 |
.post("/review/accept-confident", accept_confident) |
| 104 |
.post("/review/rows/check", check_shown) |
| 105 |
.post("/review/rows/uncheck", uncheck_shown) |
| 106 |
.post("/review/rows/{at}/tick", tick) |
| 107 |
.post("/review/accept/{scope}", accept) |
| 108 |
.post("/review/dismiss", dismiss) |
| 109 |
.post("/review/rescan", rescan) |
| 110 |
.post("/review/close", close) |
| 111 |
.post("/review/{at}/read", read) |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
fn index(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 119 |
Ok(screen(&queued(state)?).into()) |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
fn read(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 124 |
let queued = queued(state)?; |
| 125 |
let at = group_at(&queued, &request)?; |
| 126 |
state.queue.read(at); |
| 127 |
Ok(screen(&queued).into()) |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
fn tick(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 136 |
let queued = queued(state)?; |
| 137 |
let at: usize = request |
| 138 |
.captures |
| 139 |
.require("at")? |
| 140 |
.parse() |
| 141 |
.map_err(|_| RouteError::not_found("no such candidate"))?; |
| 142 |
if at >= queued.shown.len() { |
| 143 |
return Err(RouteError::not_found("no such candidate")); |
| 144 |
} |
| 145 |
state.queue.tick(at); |
| 146 |
Ok(screen(&queued).into()) |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
fn check_shown(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 151 |
let queued = queued(state)?; |
| 152 |
state.queue.tick_shown(true); |
| 153 |
Ok(screen(&queued).into()) |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
fn uncheck_shown(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 158 |
let queued = queued(state)?; |
| 159 |
state.queue.tick_shown(false); |
| 160 |
Ok(screen(&queued).into()) |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
fn accept(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 169 |
let queued = queued(state)?; |
| 170 |
let name = request.captures.require("scope")?; |
| 171 |
let scope = Scope::from_key(name).ok_or_else(|| RouteError::not_found("no such scope"))?; |
| 172 |
let open = open_group(&queued)?; |
| 173 |
|
| 174 |
let counted = match scope { |
| 175 |
Scope::All => open.candidates, |
| 176 |
Scope::Confident => open.confident, |
| 177 |
Scope::Checked => open.checked, |
| 178 |
}; |
| 179 |
if counted == 0 { |
| 180 |
return Err(RouteError::not_found("nothing is in that scope")); |
| 181 |
} |
| 182 |
|
| 183 |
state.queue.accept(scope); |
| 184 |
Ok(screen(&queued).into()) |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
fn accept_confident(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 189 |
let queued = queued(state)?; |
| 190 |
if queued.confident == 0 { |
| 191 |
return Err(RouteError::not_found("nothing clears its threshold")); |
| 192 |
} |
| 193 |
state.queue.accept_confident(); |
| 194 |
Ok(screen(&queued).into()) |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
fn dismiss(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 199 |
let queued = queued(state)?; |
| 200 |
open_group(&queued)?; |
| 201 |
state.queue.dismiss(); |
| 202 |
Ok(screen(&queued).into()) |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
fn rescan(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 211 |
let queued = queued(state)?; |
| 212 |
if queued.rescanning { |
| 213 |
return Err(RouteError::not_found("a pass is already running")); |
| 214 |
} |
| 215 |
state.queue.rescan(); |
| 216 |
Ok(screen(&queued).into()) |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
fn close(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 221 |
let queued = queued(state)?; |
| 222 |
state.queue.close(); |
| 223 |
Ok(screen(&queued).into()) |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
fn queued(state: &Panels<'_>) -> Result<Queued, RouteError> { |
| 228 |
state |
| 229 |
.queue |
| 230 |
.queued() |
| 231 |
.ok_or_else(|| RouteError::not_found("nothing is waiting to be reviewed")) |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
fn group_at(queued: &Queued, request: &Request) -> Result<usize, RouteError> { |
| 236 |
let at: usize = request |
| 237 |
.captures |
| 238 |
.require("at")? |
| 239 |
.parse() |
| 240 |
.map_err(|_| RouteError::not_found("no such tag"))?; |
| 241 |
if at >= queued.groups.len() { |
| 242 |
return Err(RouteError::not_found("no such tag")); |
| 243 |
} |
| 244 |
Ok(at) |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
fn open_group(queued: &Queued) -> Result<&Group, RouteError> { |
| 249 |
queued |
| 250 |
.groups |
| 251 |
.get(queued.at) |
| 252 |
.ok_or_else(|| RouteError::not_found("no tag is open")) |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
fn screen(queued: &Queued) -> Screen { |
| 257 |
Screen::sidebar_content("Review Tags") |
| 258 |
.with(head(queued)) |
| 259 |
.with( |
| 260 |
Slot::new("review-split", RegionKind::Split) |
| 261 |
.with(Node::Region(tags(queued))) |
| 262 |
.with(Node::Region(group(queued))), |
| 263 |
) |
| 264 |
.with(foot(queued)) |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
fn head(queued: &Queued) -> Slot { |
| 269 |
let mut band = Slot::new(HEAD, RegionKind::Band) |
| 270 |
.with(Node::page("Review Tags")) |
| 271 |
.with(Node::text(format!( |
| 272 |
"{} suggestion{} across {} tag{} \u{b7} {} of {} sample{}", |
| 273 |
total_of(queued), |
| 274 |
plural(total_of(queued)), |
| 275 |
queued.groups.len(), |
| 276 |
plural(queued.groups.len()), |
| 277 |
queued.suggested, |
| 278 |
queued.considered, |
| 279 |
plural(queued.considered), |
| 280 |
))) |
| 281 |
|
| 282 |
|
| 283 |
.with(Node::text("Nothing is applied until you accept it.")); |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
if queued.confident > 0 { |
| 289 |
band = band.with(Node::Act( |
| 290 |
Act::new( |
| 291 |
format!("Accept {} confident", queued.confident), |
| 292 |
Action::post("/review/accept-confident"), |
| 293 |
) |
| 294 |
.confirm(format!( |
| 295 |
"{} suggestions above their tags' thresholds will be applied across every tag. \ |
| 296 |
Accept?", |
| 297 |
queued.confident |
| 298 |
)), |
| 299 |
)); |
| 300 |
} |
| 301 |
|
| 302 |
band |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
fn tags(queued: &Queued) -> Slot { |
| 307 |
Slot::new(TAGS, RegionKind::Pane) |
| 308 |
.with(Node::section("Tags")) |
| 309 |
.with(Node::list(queued.groups.iter().enumerate().map( |
| 310 |
|(at, group)| { |
| 311 |
let mut row = Row::new(group.tag.clone()) |
| 312 |
.secondary(Prose::Text(format!( |
| 313 |
"{} suggestion{}{}", |
| 314 |
group.candidates, |
| 315 |
plural(group.candidates), |
| 316 |
if group.confident > 0 { |
| 317 |
format!(", {} confident", group.confident) |
| 318 |
} else { |
| 319 |
String::new() |
| 320 |
} |
| 321 |
))) |
| 322 |
.activate(Action::post(format!("/review/{at}/read"))); |
| 323 |
if at == queued.at { |
| 324 |
row.current = true; |
| 325 |
} |
| 326 |
row |
| 327 |
}, |
| 328 |
))) |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
fn group(queued: &Queued) -> Slot { |
| 333 |
let pane = Slot::new(GROUP, RegionKind::Pane); |
| 334 |
let Some(open) = queued.groups.get(queued.at) else { |
| 335 |
return pane.with(Node::empty("Choose a tag to review.")); |
| 336 |
}; |
| 337 |
|
| 338 |
let mut pane = pane |
| 339 |
.with(Node::section(open.tag.clone())) |
| 340 |
.with(Node::text(format!( |
| 341 |
"{} sample{} would get this tag.", |
| 342 |
open.candidates, |
| 343 |
plural(open.candidates) |
| 344 |
))) |
| 345 |
.with(Node::Act(Act::new( |
| 346 |
format!("Accept all {}", open.candidates), |
| 347 |
Action::post("/review/accept/all"), |
| 348 |
))); |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
if open.confident > 0 && open.confident < open.candidates { |
| 353 |
pane = pane.with(Node::Act(Act::new( |
| 354 |
format!("Accept {} confident", open.confident), |
| 355 |
Action::post("/review/accept/confident"), |
| 356 |
))); |
| 357 |
} |
| 358 |
if open.checked > 0 { |
| 359 |
pane = pane.with(Node::Act(Act::new( |
| 360 |
format!("Accept {} checked", open.checked), |
| 361 |
Action::post("/review/accept/checked"), |
| 362 |
))); |
| 363 |
} |
| 364 |
|
| 365 |
pane = pane |
| 366 |
.with(Node::Act( |
| 367 |
Act::new("Dismiss tag", Action::post("/review/dismiss")) |
| 368 |
.tone(Tone::Danger) |
| 369 |
.confirm(format!( |
| 370 |
"\"{}\" and its {} suggestions will be dropped from the queue. Dismiss?", |
| 371 |
open.tag, open.candidates |
| 372 |
)), |
| 373 |
)) |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
.with(Node::Act(Act::new( |
| 379 |
"Check all shown", |
| 380 |
Action::post("/review/rows/check"), |
| 381 |
))); |
| 382 |
if open.checked > 0 { |
| 383 |
pane = pane.with(Node::Act(Act::new( |
| 384 |
"Uncheck all shown", |
| 385 |
Action::post("/review/rows/uncheck"), |
| 386 |
))); |
| 387 |
} |
| 388 |
|
| 389 |
if queued.shown.is_empty() { |
| 390 |
return pane.with(Node::empty("Nothing is waiting under this tag.")); |
| 391 |
} |
| 392 |
|
| 393 |
pane.with(Node::List { |
| 394 |
rows: queued |
| 395 |
.shown |
| 396 |
.iter() |
| 397 |
.enumerate() |
| 398 |
.map(|(at, candidate)| { |
| 399 |
Row::new(candidate.name.clone()) |
| 400 |
|
| 401 |
|
| 402 |
.meta(format!( |
| 403 |
"{:.0}%{}", |
| 404 |
candidate.score * 100.0, |
| 405 |
if candidate.confident { |
| 406 |
" confident" |
| 407 |
} else { |
| 408 |
"" |
| 409 |
} |
| 410 |
)) |
| 411 |
.toggling( |
| 412 |
candidate.accepted, |
| 413 |
Action::post(format!("/review/rows/{at}/tick")), |
| 414 |
) |
| 415 |
}) |
| 416 |
.collect(), |
| 417 |
|
| 418 |
|
| 419 |
more: (open.candidates > queued.shown.len()) |
| 420 |
.then(|| Rest::page(0, queued.shown.len()).of(open.candidates)), |
| 421 |
}) |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
fn foot(queued: &Queued) -> Slot { |
| 426 |
let mut band = Slot::new(FOOT, RegionKind::Band) |
| 427 |
.with(Node::Act(Act::new("Close", Action::post("/review/close")))); |
| 428 |
|
| 429 |
let mut again = Act::new("Rescan library", Action::post("/review/rescan")); |
| 430 |
if queued.rescanning { |
| 431 |
|
| 432 |
|
| 433 |
band = band.with(Node::text("A pass is running.")); |
| 434 |
again = again.disabled(); |
| 435 |
} |
| 436 |
band = band.with(Node::Act(again)); |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
match &queued.said { |
| 442 |
Some(said) => band.with(Node::Notice { |
| 443 |
kind: Notice::Toast, |
| 444 |
tone: Tone::Success, |
| 445 |
text: said.clone(), |
| 446 |
}), |
| 447 |
None => band.with(Node::Figure(Figure::new( |
| 448 |
queued.confident.to_string(), |
| 449 |
"confident", |
| 450 |
))), |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
fn total_of(queued: &Queued) -> usize { |
| 456 |
queued.groups.iter().map(|group| group.candidates).sum() |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
const fn plural(count: usize) -> &'static str { |
| 461 |
if count == 1 { "" } else { "s" } |
| 462 |
} |
| 463 |
|