| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | use std::time::SystemTime; |
| 18 | |
| 19 | use serde::Deserialize; |
| 20 | use topcoat::{ |
| 21 | Result, |
| 22 | context::Cx, |
| 23 | icon::{icon, iconify::iconify_icon}, |
| 24 | router::{ |
| 25 | StatusCode, |
| 26 | content::Form, |
| 27 | error::{bad_request, not_found}, |
| 28 | page, path_param, query_params, |
| 29 | }, |
| 30 | view::{attributes, component, view}, |
| 31 | }; |
| 32 | |
| 33 | use crate::{ |
| 34 | application::{ |
| 35 | Error, LabelEdit, LabelPage, LabelRow, LabelView, NewLabel, RepoView, create_label, |
| 36 | delete_label, list_labels, set_issue_labels, set_pull_labels, update_label, |
| 37 | }, |
| 38 | components::{ |
| 39 | button::{ButtonSize, ButtonVariant, button}, |
| 40 | flash::{FlashKind, flash}, |
| 41 | input::input, |
| 42 | label::label, |
| 43 | }, |
| 44 | domain::{DomainError, IssueNumber, Label, LabelColour, LabelId}, |
| 45 | }; |
| 46 | |
| 47 | use super::{ |
| 48 | context::{ |
| 49 | current_actor, issues, labels, location, memberships, orgs, pulls, repos, server_error, |
| 50 | }, |
| 51 | layout::wide, |
| 52 | repo::{Tab, repo_for, repo_header}, |
| 53 | }; |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | #[path_param] |
| 61 | struct Id(str); |
| 62 | |
| 63 | |
| 64 | |
| 65 | #[path_param] |
| 66 | struct Number(str); |
| 67 | |
| 68 | |
| 69 | #[query_params(error = bad_request)] |
| 70 | struct Removed { |
| 71 | removed: Option<String>, |
| 72 | } |
| 73 | |
| 74 | #[derive(Debug, Deserialize)] |
| 75 | struct LabelForm { |
| 76 | name: String, |
| 77 | colour: String, |
| 78 | description: String, |
| 79 | } |
| 80 | |
| 81 | |
| 82 | |
| 83 | pub(super) fn labels_url(handle: &str, name: &str) -> String { |
| 84 | format!("/{handle}/repos/{name}/labels") |
| 85 | } |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | pub(super) fn filtered_issues_url(handle: &str, name: &str, pill: &str) -> String { |
| 93 | format!("/{handle}/repos/{name}/issues?label={}", encode(pill)) |
| 94 | } |
| 95 | |
| 96 | |
| 97 | pub(super) fn filtered_pulls_url(handle: &str, name: &str, pill: &str) -> String { |
| 98 | format!("/{handle}/repos/{name}/pulls?label={}", encode(pill)) |
| 99 | } |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 108 | pub(super) enum Filtering { |
| 109 | #[default] |
| 110 | Issues, |
| 111 | Pulls, |
| 112 | } |
| 113 | |
| 114 | impl Filtering { |
| 115 | fn url(self, handle: &str, name: &str, pill: &str) -> String { |
| 116 | match self { |
| 117 | Self::Issues => filtered_issues_url(handle, name, pill), |
| 118 | Self::Pulls => filtered_pulls_url(handle, name, pill), |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | pub(super) fn encode(value: &str) -> String { |
| 129 | let mut encoded = String::with_capacity(value.len()); |
| 130 | |
| 131 | for byte in value.as_bytes() { |
| 132 | match byte { |
| 133 | b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => { |
| 134 | encoded.push(*byte as char) |
| 135 | } |
| 136 | other => encoded.push_str(&format!("%{other:02X}")), |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | encoded |
| 141 | } |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | async fn owned(cx: &Cx) -> Result<RepoView> { |
| 150 | let repo = repo_for(cx).await?; |
| 151 | |
| 152 | if !repo.viewer_is_owner { |
| 153 | return Err(not_found().into()); |
| 154 | } |
| 155 | |
| 156 | Ok(repo) |
| 157 | } |
| 158 | |
| 159 | async fn listing(cx: &Cx, repo: &RepoView) -> Result<LabelPage> { |
| 160 | list_labels( |
| 161 | ¤t_actor(cx).await?, |
| 162 | &repo.handle, |
| 163 | &repo.name, |
| 164 | &orgs(cx), |
| 165 | &memberships(cx), |
| 166 | &repos(cx), |
| 167 | &labels(cx), |
| 168 | ) |
| 169 | .await |
| 170 | .map_err(server_error)? |
| 171 | |
| 172 | |
| 173 | .ok_or_else(|| not_found().into()) |
| 174 | } |
| 175 | |
| 176 | |
| 177 | fn write_failure(error: Error) -> std::result::Result<String, topcoat::Error> { |
| 178 | match error { |
| 179 | Error::Domain(DomainError::Validation { field, reason }) => { |
| 180 | Ok(format!("That {field} is no good: {reason}.")) |
| 181 | } |
| 182 | |
| 183 | |
| 184 | Error::Domain(DomainError::Forbidden | DomainError::NotFound { .. }) => { |
| 185 | Err(not_found().into()) |
| 186 | } |
| 187 | other => Err(server_error(std::io::Error::other(other.to_string()))), |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | fn colour_of(value: &str) -> Result<LabelColour> { |
| 196 | value |
| 197 | .parse::<LabelColour>() |
| 198 | .map_err(|_| bad_request("unknown label colour").into()) |
| 199 | } |
| 200 | |
| 201 | |
| 202 | fn described(value: &str) -> Option<String> { |
| 203 | let trimmed = value.trim(); |
| 204 | |
| 205 | (!trimmed.is_empty()).then(|| trimmed.to_owned()) |
| 206 | } |
| 207 | |
| 208 | |
| 209 | |
| 210 | #[page("/{handle}/repos/{name}/labels")] |
| 211 | async fn labels_page(cx: &Cx) -> Result { |
| 212 | let repo = repo_for(cx).await?; |
| 213 | let listed = listing(cx, &repo).await?; |
| 214 | let removed = query_params::<Removed>(cx)?.removed.is_some(); |
| 215 | |
| 216 | view! { |
| 217 | wide( |
| 218 | repo_header(repo: &repo, rev: "", active: Tab::Issues) |
| 219 | labels_view( |
| 220 | handle: repo.handle.as_str(), |
| 221 | name: repo.name.as_str(), |
| 222 | listed: &listed, |
| 223 | notice: Notice { removed, ..Notice::default() }, |
| 224 | ) |
| 225 | ) |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | |
| 230 | #[page(POST "/{handle}/repos/{name}/labels")] |
| 231 | async fn add(cx: &Cx, Form(submitted): Form<LabelForm>) -> Result { |
| 232 | let repo = owned(cx).await?; |
| 233 | let colour = colour_of(&submitted.colour)?; |
| 234 | |
| 235 | let outcome = create_label( |
| 236 | ¤t_actor(cx).await?, |
| 237 | &repo.handle, |
| 238 | &repo.name, |
| 239 | &NewLabel { |
| 240 | name: submitted.name.clone(), |
| 241 | colour, |
| 242 | description: described(&submitted.description), |
| 243 | }, |
| 244 | SystemTime::now(), |
| 245 | &orgs(cx), |
| 246 | &memberships(cx), |
| 247 | &repos(cx), |
| 248 | &labels(cx), |
| 249 | ) |
| 250 | .await; |
| 251 | |
| 252 | let message = match outcome { |
| 253 | Ok(()) => { |
| 254 | return view! { |
| 255 | (StatusCode::SEE_OTHER) |
| 256 | (location(&labels_url(repo.handle.as_str(), repo.name.as_str()))?) |
| 257 | }; |
| 258 | } |
| 259 | Err(error) => write_failure(error)?, |
| 260 | }; |
| 261 | |
| 262 | |
| 263 | |
| 264 | let listed = listing(cx, &repo).await?; |
| 265 | |
| 266 | view! { |
| 267 | wide( |
| 268 | repo_header(repo: &repo, rev: "", active: Tab::Issues) |
| 269 | labels_view( |
| 270 | handle: repo.handle.as_str(), |
| 271 | name: repo.name.as_str(), |
| 272 | listed: &listed, |
| 273 | notice: Notice { |
| 274 | error: message.as_str(), |
| 275 | typed: submitted.name.as_str(), |
| 276 | described: submitted.description.as_str(), |
| 277 | colour, |
| 278 | ..Notice::default() |
| 279 | }, |
| 280 | ) |
| 281 | ) |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | |
| 286 | #[page(POST "/{handle}/repos/{name}/labels/{id}/edit")] |
| 287 | async fn edit(cx: &Cx, Form(submitted): Form<LabelForm>) -> Result { |
| 288 | let repo = owned(cx).await?; |
| 289 | let id = LabelId::from_trusted(path_param::<Id>(cx)); |
| 290 | let colour = colour_of(&submitted.colour)?; |
| 291 | |
| 292 | let outcome = update_label( |
| 293 | ¤t_actor(cx).await?, |
| 294 | &repo.handle, |
| 295 | &repo.name, |
| 296 | &id, |
| 297 | &LabelEdit { |
| 298 | name: submitted.name.clone(), |
| 299 | colour, |
| 300 | description: described(&submitted.description), |
| 301 | }, |
| 302 | &orgs(cx), |
| 303 | &memberships(cx), |
| 304 | &repos(cx), |
| 305 | &labels(cx), |
| 306 | ) |
| 307 | .await; |
| 308 | |
| 309 | let message = match outcome { |
| 310 | Ok(()) => { |
| 311 | return view! { |
| 312 | (StatusCode::SEE_OTHER) |
| 313 | (location(&labels_url(repo.handle.as_str(), repo.name.as_str()))?) |
| 314 | }; |
| 315 | } |
| 316 | Err(error) => write_failure(error)?, |
| 317 | }; |
| 318 | |
| 319 | |
| 320 | |
| 321 | let listed = listing(cx, &repo).await?; |
| 322 | |
| 323 | view! { |
| 324 | wide( |
| 325 | repo_header(repo: &repo, rev: "", active: Tab::Issues) |
| 326 | labels_view( |
| 327 | handle: repo.handle.as_str(), |
| 328 | name: repo.name.as_str(), |
| 329 | listed: &listed, |
| 330 | notice: Notice { |
| 331 | error: message.as_str(), |
| 332 | editing: Some(&id), |
| 333 | ..Notice::default() |
| 334 | }, |
| 335 | ) |
| 336 | ) |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | |
| 341 | #[page(POST "/{handle}/repos/{name}/labels/{id}/delete")] |
| 342 | async fn remove(cx: &Cx) -> Result { |
| 343 | let repo = owned(cx).await?; |
| 344 | let id = LabelId::from_trusted(path_param::<Id>(cx)); |
| 345 | |
| 346 | match delete_label( |
| 347 | ¤t_actor(cx).await?, |
| 348 | &repo.handle, |
| 349 | &repo.name, |
| 350 | &id, |
| 351 | &orgs(cx), |
| 352 | &memberships(cx), |
| 353 | &repos(cx), |
| 354 | &labels(cx), |
| 355 | ) |
| 356 | .await |
| 357 | { |
| 358 | Ok(()) => {} |
| 359 | |
| 360 | |
| 361 | Err(Error::Domain(DomainError::NotFound { .. })) => {} |
| 362 | Err(other) => { |
| 363 | write_failure(other)?; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | view! { |
| 368 | (StatusCode::SEE_OTHER) |
| 369 | (location(&format!( |
| 370 | "{}?removed", |
| 371 | labels_url(repo.handle.as_str(), repo.name.as_str()) |
| 372 | ))?) |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | #[page(POST "/{handle}/repos/{name}/issues/{number}/labels")] |
| 382 | async fn set_labels(cx: &Cx, Form(submitted): Form<Vec<(String, String)>>) -> Result { |
| 383 | let repo = owned(cx).await?; |
| 384 | |
| 385 | let number = path_param::<Number>(cx) |
| 386 | .parse::<IssueNumber>() |
| 387 | .map_err(|_| not_found())?; |
| 388 | |
| 389 | let chosen: Vec<LabelId> = submitted |
| 390 | .into_iter() |
| 391 | .filter(|(field, _)| field == "label") |
| 392 | .map(|(_, id)| LabelId::from_trusted(id)) |
| 393 | .collect(); |
| 394 | |
| 395 | match set_issue_labels( |
| 396 | ¤t_actor(cx).await?, |
| 397 | &repo.handle, |
| 398 | &repo.name, |
| 399 | number, |
| 400 | &chosen, |
| 401 | &orgs(cx), |
| 402 | &memberships(cx), |
| 403 | &repos(cx), |
| 404 | &issues(cx), |
| 405 | &labels(cx), |
| 406 | ) |
| 407 | .await |
| 408 | { |
| 409 | Ok(()) => {} |
| 410 | |
| 411 | |
| 412 | Err(error) => { |
| 413 | write_failure(error)?; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | view! { |
| 418 | (StatusCode::SEE_OTHER) |
| 419 | (location(&format!( |
| 420 | "/{}/repos/{}/issues/{number}", |
| 421 | repo.handle, repo.name |
| 422 | ))?) |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | #[page(POST "/{handle}/repos/{name}/pulls/{number}/labels")] |
| 433 | async fn set_pull_request_labels(cx: &Cx, Form(submitted): Form<Vec<(String, String)>>) -> Result { |
| 434 | let repo = owned(cx).await?; |
| 435 | |
| 436 | let number = path_param::<Number>(cx) |
| 437 | .parse::<IssueNumber>() |
| 438 | .map_err(|_| not_found())?; |
| 439 | |
| 440 | let chosen: Vec<LabelId> = submitted |
| 441 | .into_iter() |
| 442 | .filter(|(field, _)| field == "label") |
| 443 | .map(|(_, id)| LabelId::from_trusted(id)) |
| 444 | .collect(); |
| 445 | |
| 446 | match set_pull_labels( |
| 447 | ¤t_actor(cx).await?, |
| 448 | &repo.handle, |
| 449 | &repo.name, |
| 450 | number, |
| 451 | &chosen, |
| 452 | &orgs(cx), |
| 453 | &memberships(cx), |
| 454 | &repos(cx), |
| 455 | &pulls(cx), |
| 456 | &labels(cx), |
| 457 | ) |
| 458 | .await |
| 459 | { |
| 460 | Ok(()) => {} |
| 461 | Err(error) => { |
| 462 | write_failure(error)?; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | view! { |
| 467 | (StatusCode::SEE_OTHER) |
| 468 | (location(&format!( |
| 469 | "/{}/repos/{}/pulls/{number}", |
| 470 | repo.handle, repo.name |
| 471 | ))?) |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | #[component] |
| 484 | pub(super) async fn label_pill( |
| 485 | handle: &str, |
| 486 | name: &str, |
| 487 | pill: &LabelView, |
| 488 | |
| 489 | #[default] |
| 490 | to: Filtering, |
| 491 | ) -> Result { |
| 492 | view! { |
| 493 | <a |
| 494 | href=(to.url(handle, name, &pill.name)) |
| 495 | class=(format!("label-pill label-{}", pill.colour)) |
| 496 | title=(pill.description.as_deref().unwrap_or("")) |
| 497 | >(pill.name.as_str())</a> |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | |
| 502 | #[component] |
| 503 | async fn swatches(field: &str, chosen: LabelColour) -> Result { |
| 504 | view! { |
| 505 | <div class="flex flex-wrap items-center gap-2"> |
| 506 | for colour in LabelColour::ALL { |
| 507 | <label class=(format!("label-swatch label-{colour}")) title=(colour.as_str())> |
| 508 | <input |
| 509 | type="radio" |
| 510 | name=(field) |
| 511 | value=(colour.as_str()) |
| 512 | checked=(colour == chosen) |
| 513 | /> |
| 514 | <span></span> |
| 515 | </label> |
| 516 | } |
| 517 | </div> |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | #[derive(Debug, Clone, Copy, Default)] |
| 527 | struct Notice<'a> { |
| 528 | removed: bool, |
| 529 | error: &'a str, |
| 530 | |
| 531 | typed: &'a str, |
| 532 | |
| 533 | described: &'a str, |
| 534 | |
| 535 | colour: LabelColour, |
| 536 | |
| 537 | editing: Option<&'a LabelId>, |
| 538 | } |
| 539 | |
| 540 | #[component] |
| 541 | async fn labels_view(handle: &str, name: &str, listed: &LabelPage, notice: Notice<'_>) -> Result { |
| 542 | let Notice { |
| 543 | removed, |
| 544 | error, |
| 545 | typed, |
| 546 | described, |
| 547 | colour, |
| 548 | editing, |
| 549 | } = notice; |
| 550 | |
| 551 | view! { |
| 552 | <div class="max-w-3xl"> |
| 553 | <div class="flex flex-wrap items-baseline gap-x-3 gap-y-1"> |
| 554 | <h1 class="text-lg font-semibold tracking-tight">"Labels"</h1> |
| 555 | <span class="font-mono text-xs text-muted-foreground">(listed.labels.len())</span> |
| 556 | <a |
| 557 | href=(format!("/{handle}/repos/{name}/issues")) |
| 558 | class="ml-auto text-sm text-muted-foreground hover:text-foreground" |
| 559 | >"Back to issues"</a> |
| 560 | </div> |
| 561 | <p class="mt-1 text-sm text-muted-foreground"> |
| 562 | "A label is this repository's own — the same word on another repository is " |
| 563 | "a different label. Issues and pull requests share the set; click a pill " |
| 564 | "for the issues, or the count for the pull requests." |
| 565 | </p> |
| 566 | |
| 567 | if removed { |
| 568 | <div class="mt-4"> |
| 569 | flash(kind: FlashKind::Success, "Label deleted. The issues that had it kept everything else.") |
| 570 | </div> |
| 571 | } |
| 572 | |
| 573 | if !error.is_empty() { |
| 574 | <div class="mt-4"> |
| 575 | flash(kind: FlashKind::Error, (error)) |
| 576 | </div> |
| 577 | } |
| 578 | |
| 579 | if listed.viewer_is_owner { |
| 580 | add_form( |
| 581 | handle: handle, |
| 582 | name: name, |
| 583 | typed: typed, |
| 584 | described: described, |
| 585 | colour: colour, |
| 586 | ) |
| 587 | } |
| 588 | |
| 589 | if listed.labels.is_empty() { |
| 590 | <div class="mt-4 rounded-lg border border-border px-4 py-10 text-center"> |
| 591 | <p class="text-sm text-muted-foreground">"No labels yet."</p> |
| 592 | if !listed.viewer_is_owner { |
| 593 | <p class="mt-1 text-xs text-muted-foreground"> |
| 594 | "Only the owner can make one." |
| 595 | </p> |
| 596 | } |
| 597 | </div> |
| 598 | } else { |
| 599 | <ul class="mt-4 divide-y divide-border rounded-lg border border-border"> |
| 600 | for row in &listed.labels { |
| 601 | label_row( |
| 602 | handle: handle, |
| 603 | name: name, |
| 604 | row: row, |
| 605 | editable: listed.viewer_is_owner, |
| 606 | open: editing == Some(&row.label.id), |
| 607 | ) |
| 608 | } |
| 609 | </ul> |
| 610 | } |
| 611 | </div> |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | |
| 616 | #[component] |
| 617 | async fn add_form( |
| 618 | handle: &str, |
| 619 | name: &str, |
| 620 | typed: &str, |
| 621 | described: &str, |
| 622 | colour: LabelColour, |
| 623 | ) -> Result { |
| 624 | view! { |
| 625 | <form |
| 626 | method="post" |
| 627 | action=(labels_url(handle, name)) |
| 628 | class="mt-4 space-y-3 rounded-lg border border-border p-4" |
| 629 | > |
| 630 | <h2 class="text-xs font-medium uppercase tracking-wider text-muted-foreground"> |
| 631 | "New label" |
| 632 | </h2> |
| 633 | |
| 634 | |
| 635 | |
| 636 | <div class="flex flex-wrap items-end gap-x-4 gap-y-3"> |
| 637 | <div class="min-w-48 flex-1 space-y-2"> |
| 638 | label(attrs: attributes! { for="label-name" }, "Name") |
| 639 | input(attrs: attributes! { |
| 640 | id="label-name" |
| 641 | name="name" |
| 642 | type="text" |
| 643 | value=(typed) |
| 644 | placeholder="bug" |
| 645 | required=(true) |
| 646 | autocomplete="off" |
| 647 | maxlength=(Label::MAX_NAME_LEN.to_string()) |
| 648 | }) |
| 649 | </div> |
| 650 | <div class="space-y-2"> |
| 651 | label(attrs: attributes! { for="" }, "Colour") |
| 652 | swatches(field: "colour", chosen: colour) |
| 653 | </div> |
| 654 | </div> |
| 655 | |
| 656 | <div class="space-y-2"> |
| 657 | label(attrs: attributes! { for="label-description" }, "Description") |
| 658 | input(attrs: attributes! { |
| 659 | id="label-description" |
| 660 | name="description" |
| 661 | type="text" |
| 662 | value=(described) |
| 663 | placeholder="Something is broken (optional)" |
| 664 | autocomplete="off" |
| 665 | maxlength=(Label::MAX_DESCRIPTION_LEN.to_string()) |
| 666 | }) |
| 667 | </div> |
| 668 | |
| 669 | button(size: ButtonSize::Sm, attrs: attributes! { type="submit" }, "Add label") |
| 670 | </form> |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | #[component] |
| 684 | async fn label_row(handle: &str, name: &str, row: &LabelRow, editable: bool, open: bool) -> Result { |
| 685 | let id = row.label.id.as_str(); |
| 686 | let action = |what: &str| format!("{}/{id}/{what}", labels_url(handle, name)); |
| 687 | |
| 688 | view! { |
| 689 | <li class="px-4 py-2.5 text-sm"> |
| 690 | if editable { |
| 691 | <details open=(open)> |
| 692 | <summary class="flex cursor-pointer list-none flex-wrap items-center gap-x-3 gap-y-1.5"> |
| 693 | label_summary(handle: handle, name: name, row: row) |
| 694 | <span class="shrink-0 text-xs text-muted-foreground">"Edit"</span> |
| 695 | </summary> |
| 696 | |
| 697 | <div class="mt-3 space-y-4"> |
| 698 | <form method="post" action=(action("edit")) class="space-y-3"> |
| 699 | <div class="flex flex-wrap items-end gap-x-4 gap-y-3"> |
| 700 | <div class="min-w-48 flex-1 space-y-2"> |
| 701 | label(attrs: attributes! { for=(format!("name-{id}")) }, "Name") |
| 702 | input(attrs: attributes! { |
| 703 | id=(format!("name-{id}")) |
| 704 | name="name" |
| 705 | type="text" |
| 706 | value=(row.label.name.as_str()) |
| 707 | required=(true) |
| 708 | autocomplete="off" |
| 709 | maxlength=(Label::MAX_NAME_LEN.to_string()) |
| 710 | }) |
| 711 | </div> |
| 712 | <div class="space-y-2"> |
| 713 | label(attrs: attributes! { for="" }, "Colour") |
| 714 | swatches(field: "colour", chosen: row.label.colour) |
| 715 | </div> |
| 716 | </div> |
| 717 | |
| 718 | <div class="space-y-2"> |
| 719 | label( |
| 720 | attrs: attributes! { for=(format!("description-{id}")) }, |
| 721 | "Description" |
| 722 | ) |
| 723 | input(attrs: attributes! { |
| 724 | id=(format!("description-{id}")) |
| 725 | name="description" |
| 726 | type="text" |
| 727 | value=(row.label.description.as_deref().unwrap_or("")) |
| 728 | autocomplete="off" |
| 729 | maxlength=(Label::MAX_DESCRIPTION_LEN.to_string()) |
| 730 | }) |
| 731 | </div> |
| 732 | |
| 733 | button( |
| 734 | size: ButtonSize::Sm, |
| 735 | attrs: attributes! { type="submit" }, |
| 736 | "Save label" |
| 737 | ) |
| 738 | </form> |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | <form |
| 745 | method="post" |
| 746 | action=(action("delete")) |
| 747 | class="flex flex-wrap items-center gap-x-3 gap-y-2 border-t border-border pt-3" |
| 748 | > |
| 749 | <label class="flex items-center gap-2 text-xs text-muted-foreground"> |
| 750 | <input type="checkbox" name="confirm" value="yes" required=(true) /> |
| 751 | |
| 752 | |
| 753 | |
| 754 | if row.issues > 0 || row.pulls > 0 { |
| 755 | "Delete this label — it comes off " |
| 756 | if row.issues > 0 { |
| 757 | (row.issues) |
| 758 | if row.issues == 1 { " issue" } else { " issues" } |
| 759 | } |
| 760 | if row.issues > 0 && row.pulls > 0 { " and " } |
| 761 | if row.pulls > 0 { |
| 762 | (row.pulls) |
| 763 | if row.pulls == 1 { |
| 764 | " pull request" |
| 765 | } else { |
| 766 | " pull requests" |
| 767 | } |
| 768 | } |
| 769 | "." |
| 770 | } else { |
| 771 | "Delete this label." |
| 772 | } |
| 773 | </label> |
| 774 | button( |
| 775 | variant: ButtonVariant::Ghost, |
| 776 | size: ButtonSize::Sm, |
| 777 | attrs: attributes! { |
| 778 | type="submit" |
| 779 | class="text-destructive hover:bg-destructive/10" |
| 780 | }, |
| 781 | icon( |
| 782 | data: iconify_icon!("feather:trash-2"), |
| 783 | attrs: attributes! { class="size-3.5" } |
| 784 | ) |
| 785 | "Delete" |
| 786 | ) |
| 787 | </form> |
| 788 | </div> |
| 789 | </details> |
| 790 | } else { |
| 791 | <div class="flex flex-wrap items-center gap-x-3 gap-y-1.5"> |
| 792 | label_summary(handle: handle, name: name, row: row) |
| 793 | </div> |
| 794 | } |
| 795 | </li> |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | |
| 800 | #[component] |
| 801 | async fn label_summary(handle: &str, name: &str, row: &LabelRow) -> Result { |
| 802 | view! { |
| 803 | label_pill(handle: handle, name: name, pill: &row.label) |
| 804 | |
| 805 | if let Some(description) = row.label.description.as_deref() { |
| 806 | <span class="min-w-0 flex-1 truncate text-xs text-muted-foreground">(description)</span> |
| 807 | } else { |
| 808 | <span class="min-w-0 flex-1"></span> |
| 809 | } |
| 810 | |
| 811 | |
| 812 | |
| 813 | |
| 814 | <a |
| 815 | href=(filtered_issues_url(handle, name, &row.label.name)) |
| 816 | class="shrink-0 text-xs text-muted-foreground hover:text-foreground" |
| 817 | > |
| 818 | <span class="font-mono">(row.issues)</span> |
| 819 | if row.issues == 1 { " issue" } else { " issues" } |
| 820 | </a> |
| 821 | |
| 822 | if row.pulls > 0 { |
| 823 | <a |
| 824 | href=(filtered_pulls_url(handle, name, &row.label.name)) |
| 825 | class="shrink-0 text-xs text-muted-foreground hover:text-foreground" |
| 826 | > |
| 827 | <span class="font-mono">(row.pulls)</span> |
| 828 | if row.pulls == 1 { " pull request" } else { " pull requests" } |
| 829 | </a> |
| 830 | } |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | #[cfg(test)] |
| 835 | mod tests { |
| 836 | use crate::domain::LabelColour; |
| 837 | |
| 838 | |
| 839 | |
| 840 | |
| 841 | |
| 842 | const STYLES: &str = include_str!("../../../styles.css"); |
| 843 | |
| 844 | |
| 845 | |
| 846 | |
| 847 | const NOT_COLOURS: [&str; 4] = ["fill", "edge", "ink", "tint"]; |
| 848 | |
| 849 | #[test] |
| 850 | fn every_label_colour_has_its_tokens_in_both_schemes() { |
| 851 | for colour in LabelColour::ALL { |
| 852 | let name = colour.as_str(); |
| 853 | |
| 854 | |
| 855 | |
| 856 | assert_eq!( |
| 857 | STYLES.matches(&format!("--label-{name}: ")).count(), |
| 858 | 2, |
| 859 | "--label-{name} should be defined for the light and dark schemes" |
| 860 | ); |
| 861 | assert!( |
| 862 | STYLES.contains(&format!(".label-{name} {{")), |
| 863 | "no .label-{name} rule to give a pill its tint" |
| 864 | ); |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | #[test] |
| 869 | fn no_token_names_a_colour_that_no_longer_exists() { |
| 870 | |
| 871 | |
| 872 | for line in STYLES.lines() { |
| 873 | let Some(rest) = line.trim().strip_prefix("--label-") else { |
| 874 | continue; |
| 875 | }; |
| 876 | let Some(name) = rest.split(':').next() else { |
| 877 | continue; |
| 878 | }; |
| 879 | |
| 880 | if NOT_COLOURS.contains(&name) { |
| 881 | continue; |
| 882 | } |
| 883 | |
| 884 | assert!( |
| 885 | LabelColour::ALL |
| 886 | .into_iter() |
| 887 | .any(|colour| colour.as_str() == name), |
| 888 | "--label-{name} names no LabelColour variant" |
| 889 | ); |
| 890 | } |
| 891 | } |
| 892 | } |