| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | use std::time::SystemTime; |
| 19 | |
| 20 | use serde::Deserialize; |
| 21 | use topcoat::{ |
| 22 | Result, |
| 23 | context::Cx, |
| 24 | icon::{icon, iconify::iconify_icon}, |
| 25 | router::{ |
| 26 | StatusCode, |
| 27 | content::Form, |
| 28 | error::{bad_request, forbidden, not_found}, |
| 29 | page, path_param, query_params, |
| 30 | }, |
| 31 | view::{attributes, component, view}, |
| 32 | }; |
| 33 | |
| 34 | use crate::{ |
| 35 | application::{ |
| 36 | CommentView, Error, IssueDetail, IssueEdit, IssueList, IssueSummary, NewIssue, RepoView, |
| 37 | comment_on_issue, create_issue, edit_issue, list_issues, set_issue_state, view_issue, |
| 38 | }, |
| 39 | components::{ |
| 40 | badge::{BadgeVariant, badge}, |
| 41 | button::{ButtonSize, ButtonVariant, button, button_variants}, |
| 42 | flash::{FlashKind, flash}, |
| 43 | input::input, |
| 44 | label::label, |
| 45 | textarea::textarea, |
| 46 | }, |
| 47 | domain::{DomainError, IssueNumber, IssueState, OrgName}, |
| 48 | }; |
| 49 | |
| 50 | use super::{ |
| 51 | browse::{ago, timestamp}, |
| 52 | context::{current_actor, issues, location, memberships, orgs, repos, server_error, users}, |
| 53 | layout::wide, |
| 54 | markdown, |
| 55 | repo::{Tab, repo_for, repo_header}, |
| 56 | }; |
| 57 | |
| 58 | |
| 59 | #[path_param] |
| 60 | struct Number(str); |
| 61 | |
| 62 | |
| 63 | #[query_params(error = bad_request)] |
| 64 | struct StateQuery { |
| 65 | state: Option<String>, |
| 66 | } |
| 67 | |
| 68 | #[derive(Debug, Deserialize)] |
| 69 | struct IssueForm { |
| 70 | title: String, |
| 71 | body: String, |
| 72 | } |
| 73 | |
| 74 | #[derive(Debug, Deserialize)] |
| 75 | struct CommentForm { |
| 76 | body: String, |
| 77 | } |
| 78 | |
| 79 | #[derive(Debug, Deserialize)] |
| 80 | struct StateForm { |
| 81 | state: String, |
| 82 | } |
| 83 | |
| 84 | |
| 85 | |
| 86 | fn issues_url(handle: &str, name: &str) -> String { |
| 87 | format!("/{handle}/repos/{name}/issues") |
| 88 | } |
| 89 | |
| 90 | fn issue_url(handle: &str, name: &str, number: IssueNumber) -> String { |
| 91 | format!("/{handle}/repos/{name}/issues/{number}") |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | fn number_for(cx: &Cx) -> Result<IssueNumber> { |
| 102 | path_param::<Number>(cx) |
| 103 | .parse::<IssueNumber>() |
| 104 | .map_err(|_| not_found().into()) |
| 105 | } |
| 106 | |
| 107 | |
| 108 | async fn listing(cx: &Cx, repo: &RepoView, state: IssueState) -> Result<IssueList> { |
| 109 | let listed = list_issues( |
| 110 | ¤t_actor(cx).await?, |
| 111 | &repo.handle, |
| 112 | &repo.name, |
| 113 | state, |
| 114 | &orgs(cx), |
| 115 | &memberships(cx), |
| 116 | &repos(cx), |
| 117 | &issues(cx), |
| 118 | &users(cx), |
| 119 | ) |
| 120 | .await |
| 121 | .map_err(server_error)?; |
| 122 | |
| 123 | |
| 124 | |
| 125 | listed.ok_or_else(|| not_found().into()) |
| 126 | } |
| 127 | |
| 128 | |
| 129 | |
| 130 | async fn detail(cx: &Cx, repo: &RepoView, number: IssueNumber) -> Result<IssueDetail> { |
| 131 | view_issue( |
| 132 | ¤t_actor(cx).await?, |
| 133 | &repo.handle, |
| 134 | &repo.name, |
| 135 | number, |
| 136 | &orgs(cx), |
| 137 | &memberships(cx), |
| 138 | &repos(cx), |
| 139 | &issues(cx), |
| 140 | &users(cx), |
| 141 | ) |
| 142 | .await |
| 143 | .map_err(server_error)? |
| 144 | .ok_or_else(|| not_found().into()) |
| 145 | } |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | async fn owned_repo(cx: &Cx) -> Result<RepoView> { |
| 152 | let repo = repo_for(cx).await?; |
| 153 | |
| 154 | if !repo.viewer_is_owner { |
| 155 | return Err(not_found().into()); |
| 156 | } |
| 157 | |
| 158 | Ok(repo) |
| 159 | } |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | fn write_failure(error: Error) -> std::result::Result<String, topcoat::Error> { |
| 166 | match error { |
| 167 | Error::Domain(DomainError::Validation { field, reason }) => { |
| 168 | Ok(format!("That {field} is no good: {reason}.")) |
| 169 | } |
| 170 | Error::Domain(DomainError::Forbidden) => Err(forbidden().into()), |
| 171 | Error::Domain(DomainError::NotFound { .. }) => Err(not_found().into()), |
| 172 | other => Err(server_error(std::io::Error::other(other.to_string()))), |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | |
| 177 | |
| 178 | #[page("/{handle}/repos/{name}/issues")] |
| 179 | async fn issues_page(cx: &Cx) -> Result { |
| 180 | let repo = repo_for(cx).await?; |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | let state = match query_params::<StateQuery>(cx)?.state.as_deref() { |
| 186 | Some("closed") => IssueState::Closed, |
| 187 | _ => IssueState::Open, |
| 188 | }; |
| 189 | |
| 190 | let listed = listing(cx, &repo, state).await?; |
| 191 | |
| 192 | view! { |
| 193 | wide( |
| 194 | repo_header( |
| 195 | repo: &repo, |
| 196 | rev: "", |
| 197 | active: Tab::Issues, |
| 198 | open_issues: Some(listed.open), |
| 199 | ) |
| 200 | issue_list( |
| 201 | handle: repo.handle.as_str(), |
| 202 | name: repo.name.as_str(), |
| 203 | listed: &listed, |
| 204 | ) |
| 205 | ) |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | |
| 210 | #[component] |
| 211 | async fn issues_toolbar(handle: &str, name: &str, listed: &IssueList) -> Result { |
| 212 | |
| 213 | |
| 214 | let current = |active: bool| { |
| 215 | if active { |
| 216 | "text-foreground" |
| 217 | } else { |
| 218 | "text-muted-foreground hover:text-foreground" |
| 219 | } |
| 220 | }; |
| 221 | let url = issues_url(handle, name); |
| 222 | |
| 223 | view! { |
| 224 | <div class="mb-3 flex flex-wrap items-center gap-x-4 gap-y-2 text-sm"> |
| 225 | <a href=(url.clone()) class=(format!("flex items-center gap-1.5 {}", current(listed.state.is_open())))> |
| 226 | state_icon(state: IssueState::Open, muted: !listed.state.is_open()) |
| 227 | "Open" |
| 228 | <span class="font-mono text-xs text-muted-foreground">(listed.open)</span> |
| 229 | </a> |
| 230 | <a href=(format!("{url}?state=closed")) class=(format!("flex items-center gap-1.5 {}", current(!listed.state.is_open())))> |
| 231 | state_icon(state: IssueState::Closed, muted: listed.state.is_open()) |
| 232 | "Closed" |
| 233 | <span class="font-mono text-xs text-muted-foreground">(listed.closed)</span> |
| 234 | </a> |
| 235 | |
| 236 | if listed.viewer_is_owner { |
| 237 | <a |
| 238 | href=(format!("{url}/new")) |
| 239 | class=(format!( |
| 240 | "ml-auto {}", |
| 241 | button_variants(ButtonVariant::Primary, ButtonSize::Sm) |
| 242 | )) |
| 243 | >"New issue"</a> |
| 244 | } |
| 245 | </div> |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | #[component] |
| 254 | async fn state_icon(state: IssueState, #[default] muted: bool) -> Result { |
| 255 | let tone = if muted { |
| 256 | "text-muted-foreground" |
| 257 | } else if state.is_open() { |
| 258 | "text-success" |
| 259 | } else { |
| 260 | "text-muted-foreground" |
| 261 | }; |
| 262 | |
| 263 | view! { |
| 264 | icon( |
| 265 | data: if state.is_open() { |
| 266 | iconify_icon!("feather:alert-circle") |
| 267 | } else { |
| 268 | iconify_icon!("feather:check-circle") |
| 269 | }, |
| 270 | attrs: attributes! { class=(format!("size-3.5 shrink-0 {tone}")) } |
| 271 | ) |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | #[component] |
| 276 | async fn issue_list(handle: &str, name: &str, listed: &IssueList) -> Result { |
| 277 | view! { |
| 278 | issues_toolbar(handle: handle, name: name, listed: listed) |
| 279 | |
| 280 | if listed.issues.is_empty() { |
| 281 | <div class="rounded-lg border border-border px-4 py-10 text-center"> |
| 282 | <p class="text-sm text-muted-foreground"> |
| 283 | if listed.state.is_open() { "No open issues." } else { "No closed issues." } |
| 284 | </p> |
| 285 | |
| 286 | |
| 287 | if !listed.viewer_is_owner && listed.state.is_open() { |
| 288 | <p class="mt-1 text-xs text-muted-foreground"> |
| 289 | "Only the owner can open one here." |
| 290 | </p> |
| 291 | } |
| 292 | </div> |
| 293 | } else { |
| 294 | <ul class="divide-y divide-border rounded-lg border border-border"> |
| 295 | for issue in &listed.issues { |
| 296 | issue_row(handle: handle, name: name, issue: issue) |
| 297 | } |
| 298 | </ul> |
| 299 | |
| 300 | |
| 301 | <p class="mt-2 text-xs text-muted-foreground"> |
| 302 | "Showing every " if listed.state.is_open() { "open" } else { "closed" } |
| 303 | " issue — this list is not paged." |
| 304 | </p> |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | |
| 310 | #[component] |
| 311 | async fn issue_row(handle: &str, name: &str, issue: &IssueSummary) -> Result { |
| 312 | let author = issue |
| 313 | .author |
| 314 | .as_ref() |
| 315 | .map(OrgName::as_str) |
| 316 | .unwrap_or("someone"); |
| 317 | |
| 318 | view! { |
| 319 | <li class="flex items-center gap-3 px-4 py-2.5 text-sm"> |
| 320 | state_icon(state: issue.state) |
| 321 | |
| 322 | <div class="min-w-0 flex-1"> |
| 323 | <a |
| 324 | href=(issue_url(handle, name, issue.number)) |
| 325 | class="block truncate font-medium hover:text-primary" |
| 326 | >(issue.title.as_str())</a> |
| 327 | <p class="mt-0.5 truncate text-xs text-muted-foreground"> |
| 328 | <span class="font-mono">"#" (issue.number.get())</span> |
| 329 | " opened " |
| 330 | <span title=(timestamp(issue.created_at))>(ago(issue.created_at))</span> |
| 331 | " by " |
| 332 | <span class="font-mono">"@" (author)</span> |
| 333 | </p> |
| 334 | </div> |
| 335 | |
| 336 | if issue.comments > 0 { |
| 337 | <span |
| 338 | class="flex shrink-0 items-center gap-1 text-xs text-muted-foreground" |
| 339 | title=(format!("{} comments", issue.comments)) |
| 340 | > |
| 341 | icon( |
| 342 | data: iconify_icon!("feather:message-square"), |
| 343 | attrs: attributes! { class="size-3.5" } |
| 344 | ) |
| 345 | <span class="font-mono">(issue.comments)</span> |
| 346 | </span> |
| 347 | } |
| 348 | </li> |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | |
| 353 | |
| 354 | #[page("/{handle}/repos/{name}/issues/new")] |
| 355 | async fn new_issue_page(cx: &Cx) -> Result { |
| 356 | let repo = owned_repo(cx).await?; |
| 357 | let counts = listing(cx, &repo, IssueState::Open).await?; |
| 358 | |
| 359 | view! { |
| 360 | wide( |
| 361 | repo_header( |
| 362 | repo: &repo, |
| 363 | rev: "", |
| 364 | active: Tab::Issues, |
| 365 | open_issues: Some(counts.open), |
| 366 | ) |
| 367 | new_issue_form( |
| 368 | handle: repo.handle.as_str(), |
| 369 | name: repo.name.as_str(), |
| 370 | title: "", |
| 371 | body: "", |
| 372 | error: "", |
| 373 | ) |
| 374 | ) |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | #[page(POST "/{handle}/repos/{name}/issues")] |
| 383 | async fn create(cx: &Cx, Form(submitted): Form<IssueForm>) -> Result { |
| 384 | let repo = owned_repo(cx).await?; |
| 385 | |
| 386 | let outcome = create_issue( |
| 387 | ¤t_actor(cx).await?, |
| 388 | &repo.handle, |
| 389 | &repo.name, |
| 390 | &NewIssue { |
| 391 | title: submitted.title.clone(), |
| 392 | body: submitted.body.clone(), |
| 393 | }, |
| 394 | SystemTime::now(), |
| 395 | &orgs(cx), |
| 396 | &memberships(cx), |
| 397 | &repos(cx), |
| 398 | &issues(cx), |
| 399 | ) |
| 400 | .await; |
| 401 | |
| 402 | let message = match outcome { |
| 403 | Ok(issue) => { |
| 404 | return view! { |
| 405 | (StatusCode::SEE_OTHER) |
| 406 | (location(&issue_url(repo.handle.as_str(), repo.name.as_str(), issue.number))?) |
| 407 | }; |
| 408 | } |
| 409 | Err(error) => write_failure(error)?, |
| 410 | }; |
| 411 | |
| 412 | let counts = listing(cx, &repo, IssueState::Open).await?; |
| 413 | |
| 414 | |
| 415 | |
| 416 | view! { |
| 417 | wide( |
| 418 | repo_header( |
| 419 | repo: &repo, |
| 420 | rev: "", |
| 421 | active: Tab::Issues, |
| 422 | open_issues: Some(counts.open), |
| 423 | ) |
| 424 | new_issue_form( |
| 425 | handle: repo.handle.as_str(), |
| 426 | name: repo.name.as_str(), |
| 427 | title: submitted.title.as_str(), |
| 428 | body: submitted.body.as_str(), |
| 429 | error: message.as_str(), |
| 430 | ) |
| 431 | ) |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | #[component] |
| 436 | async fn new_issue_form(handle: &str, name: &str, title: &str, body: &str, error: &str) -> Result { |
| 437 | |
| 438 | |
| 439 | view! { |
| 440 | <div class="max-w-3xl"> |
| 441 | <h1 class="text-lg font-semibold tracking-tight">"New issue"</h1> |
| 442 | |
| 443 | if !error.is_empty() { |
| 444 | <div class="mt-4"> |
| 445 | flash(kind: FlashKind::Error, (error)) |
| 446 | </div> |
| 447 | } |
| 448 | |
| 449 | <form method="post" action=(issues_url(handle, name)) class="mt-5 space-y-4"> |
| 450 | <div class="space-y-2"> |
| 451 | label(attrs: attributes! { for="title" }, "Title") |
| 452 | input(attrs: attributes! { |
| 453 | id="title" |
| 454 | name="title" |
| 455 | type="text" |
| 456 | value=(title) |
| 457 | placeholder="Blame is slow on large files" |
| 458 | required=(true) |
| 459 | autofocus=(true) |
| 460 | }) |
| 461 | </div> |
| 462 | |
| 463 | <div class="space-y-2"> |
| 464 | label(attrs: attributes! { for="body" }, "Description") |
| 465 | textarea( |
| 466 | attrs: attributes! { |
| 467 | id="body" |
| 468 | name="body" |
| 469 | rows="12" |
| 470 | placeholder="Markdown is supported." |
| 471 | }, |
| 472 | (body) |
| 473 | ) |
| 474 | <p class="text-xs text-muted-foreground"> |
| 475 | "Markdown. HTML in the source is shown as text, never rendered." |
| 476 | </p> |
| 477 | </div> |
| 478 | |
| 479 | <div class="flex items-center gap-3"> |
| 480 | button(attrs: attributes! { type="submit" }, "Open issue") |
| 481 | <a |
| 482 | href=(issues_url(handle, name)) |
| 483 | class="text-sm text-muted-foreground hover:text-foreground" |
| 484 | >"Cancel"</a> |
| 485 | </div> |
| 486 | </form> |
| 487 | </div> |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | |
| 492 | |
| 493 | #[page("/{handle}/repos/{name}/issues/{number}")] |
| 494 | async fn issue_page(cx: &Cx) -> Result { |
| 495 | let repo = repo_for(cx).await?; |
| 496 | let issue = detail(cx, &repo, number_for(cx)?).await?; |
| 497 | let counts = listing(cx, &repo, IssueState::Open).await?; |
| 498 | |
| 499 | view! { |
| 500 | wide( |
| 501 | repo_header( |
| 502 | repo: &repo, |
| 503 | rev: "", |
| 504 | active: Tab::Issues, |
| 505 | open_issues: Some(counts.open), |
| 506 | ) |
| 507 | issue_view( |
| 508 | handle: repo.handle.as_str(), |
| 509 | name: repo.name.as_str(), |
| 510 | issue: &issue, |
| 511 | error: "", |
| 512 | comment: "", |
| 513 | editing: false, |
| 514 | ) |
| 515 | ) |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | #[page(POST "/{handle}/repos/{name}/issues/{number}/comments")] |
| 525 | async fn add_comment(cx: &Cx, Form(submitted): Form<CommentForm>) -> Result { |
| 526 | let repo = owned_repo(cx).await?; |
| 527 | let number = number_for(cx)?; |
| 528 | |
| 529 | let outcome = comment_on_issue( |
| 530 | ¤t_actor(cx).await?, |
| 531 | &repo.handle, |
| 532 | &repo.name, |
| 533 | number, |
| 534 | &submitted.body, |
| 535 | SystemTime::now(), |
| 536 | &orgs(cx), |
| 537 | &memberships(cx), |
| 538 | &repos(cx), |
| 539 | &issues(cx), |
| 540 | ) |
| 541 | .await; |
| 542 | |
| 543 | let message = match outcome { |
| 544 | Ok(()) => { |
| 545 | return view! { |
| 546 | (StatusCode::SEE_OTHER) |
| 547 | (location(&issue_url(repo.handle.as_str(), repo.name.as_str(), number))?) |
| 548 | }; |
| 549 | } |
| 550 | Err(error) => write_failure(error)?, |
| 551 | }; |
| 552 | |
| 553 | let issue = detail(cx, &repo, number).await?; |
| 554 | let counts = listing(cx, &repo, IssueState::Open).await?; |
| 555 | |
| 556 | view! { |
| 557 | wide( |
| 558 | repo_header( |
| 559 | repo: &repo, |
| 560 | rev: "", |
| 561 | active: Tab::Issues, |
| 562 | open_issues: Some(counts.open), |
| 563 | ) |
| 564 | issue_view( |
| 565 | handle: repo.handle.as_str(), |
| 566 | name: repo.name.as_str(), |
| 567 | issue: &issue, |
| 568 | error: message.as_str(), |
| 569 | |
| 570 | comment: submitted.body.as_str(), |
| 571 | editing: false, |
| 572 | ) |
| 573 | ) |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | #[page(POST "/{handle}/repos/{name}/issues/{number}/state")] |
| 582 | async fn set_state(cx: &Cx, Form(submitted): Form<StateForm>) -> Result { |
| 583 | let repo = owned_repo(cx).await?; |
| 584 | let number = number_for(cx)?; |
| 585 | |
| 586 | |
| 587 | |
| 588 | let state = submitted |
| 589 | .state |
| 590 | .parse::<IssueState>() |
| 591 | .map_err(|_| bad_request("unknown issue state"))?; |
| 592 | |
| 593 | match set_issue_state( |
| 594 | ¤t_actor(cx).await?, |
| 595 | &repo.handle, |
| 596 | &repo.name, |
| 597 | number, |
| 598 | state, |
| 599 | SystemTime::now(), |
| 600 | &orgs(cx), |
| 601 | &memberships(cx), |
| 602 | &repos(cx), |
| 603 | &issues(cx), |
| 604 | ) |
| 605 | .await |
| 606 | { |
| 607 | Ok(()) => {} |
| 608 | |
| 609 | |
| 610 | Err(error) => { |
| 611 | write_failure(error)?; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | view! { |
| 616 | (StatusCode::SEE_OTHER) |
| 617 | (location(&issue_url(repo.handle.as_str(), repo.name.as_str(), number))?) |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | |
| 622 | #[page(POST "/{handle}/repos/{name}/issues/{number}/edit")] |
| 623 | async fn edit(cx: &Cx, Form(submitted): Form<IssueForm>) -> Result { |
| 624 | let repo = owned_repo(cx).await?; |
| 625 | let number = number_for(cx)?; |
| 626 | |
| 627 | let outcome = edit_issue( |
| 628 | ¤t_actor(cx).await?, |
| 629 | &repo.handle, |
| 630 | &repo.name, |
| 631 | number, |
| 632 | &IssueEdit { |
| 633 | title: submitted.title.clone(), |
| 634 | body: submitted.body.clone(), |
| 635 | }, |
| 636 | SystemTime::now(), |
| 637 | &orgs(cx), |
| 638 | &memberships(cx), |
| 639 | &repos(cx), |
| 640 | &issues(cx), |
| 641 | ) |
| 642 | .await; |
| 643 | |
| 644 | let message = match outcome { |
| 645 | Ok(()) => { |
| 646 | return view! { |
| 647 | (StatusCode::SEE_OTHER) |
| 648 | (location(&issue_url(repo.handle.as_str(), repo.name.as_str(), number))?) |
| 649 | }; |
| 650 | } |
| 651 | Err(error) => write_failure(error)?, |
| 652 | }; |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | let stored = detail(cx, &repo, number).await?; |
| 658 | let counts = listing(cx, &repo, IssueState::Open).await?; |
| 659 | let attempted = IssueDetail { |
| 660 | title: submitted.title.clone(), |
| 661 | body: submitted.body.clone(), |
| 662 | ..stored |
| 663 | }; |
| 664 | |
| 665 | view! { |
| 666 | wide( |
| 667 | repo_header( |
| 668 | repo: &repo, |
| 669 | rev: "", |
| 670 | active: Tab::Issues, |
| 671 | open_issues: Some(counts.open), |
| 672 | ) |
| 673 | issue_view( |
| 674 | handle: repo.handle.as_str(), |
| 675 | name: repo.name.as_str(), |
| 676 | issue: &attempted, |
| 677 | error: message.as_str(), |
| 678 | comment: "", |
| 679 | editing: true, |
| 680 | ) |
| 681 | ) |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | #[component] |
| 686 | async fn issue_view( |
| 687 | handle: &str, |
| 688 | name: &str, |
| 689 | issue: &IssueDetail, |
| 690 | error: &str, |
| 691 | |
| 692 | comment: &str, |
| 693 | |
| 694 | editing: bool, |
| 695 | ) -> Result { |
| 696 | let author = issue |
| 697 | .author |
| 698 | .as_ref() |
| 699 | .map(OrgName::as_str) |
| 700 | .unwrap_or("someone"); |
| 701 | let url = issue_url(handle, name, issue.number); |
| 702 | |
| 703 | view! { |
| 704 | <div class="max-w-3xl"> |
| 705 | <div class="flex items-start gap-3"> |
| 706 | <h1 class="min-w-0 flex-1 text-xl font-semibold tracking-tight"> |
| 707 | (issue.title.as_str()) |
| 708 | <span class="ml-2 font-mono text-lg font-normal text-muted-foreground"> |
| 709 | "#" (issue.number.get()) |
| 710 | </span> |
| 711 | </h1> |
| 712 | </div> |
| 713 | |
| 714 | <div class="mt-2 flex flex-wrap items-center gap-x-3 gap-y-2 text-xs text-muted-foreground"> |
| 715 | |
| 716 | |
| 717 | badge( |
| 718 | variant: if issue.state.is_open() { |
| 719 | BadgeVariant::Primary |
| 720 | } else { |
| 721 | BadgeVariant::Secondary |
| 722 | }, |
| 723 | if issue.state.is_open() { "Open" } else { "Closed" } |
| 724 | ) |
| 725 | <span> |
| 726 | <span class="font-mono">"@" (author)</span> |
| 727 | " opened this " |
| 728 | <span title=(timestamp(issue.created_at))>(ago(issue.created_at))</span> |
| 729 | </span> |
| 730 | if let Some(closed_at) = issue.closed_at { |
| 731 | <span> |
| 732 | "closed " <span title=(timestamp(closed_at))>(ago(closed_at))</span> |
| 733 | </span> |
| 734 | } |
| 735 | <span> |
| 736 | (issue.comments.len()) |
| 737 | if issue.comments.len() == 1 { " comment" } else { " comments" } |
| 738 | </span> |
| 739 | </div> |
| 740 | |
| 741 | if !error.is_empty() { |
| 742 | <div class="mt-4"> |
| 743 | flash(kind: FlashKind::Error, (error)) |
| 744 | </div> |
| 745 | } |
| 746 | |
| 747 | <div class="mt-4 space-y-3"> |
| 748 | <article class="rounded-lg border border-border"> |
| 749 | <header class="flex items-center gap-2 border-b border-border px-4 py-2 text-xs text-muted-foreground"> |
| 750 | <span class="font-mono">"@" (author)</span> |
| 751 | <span title=(timestamp(issue.created_at))>(ago(issue.created_at))</span> |
| 752 | </header> |
| 753 | body_panel(source: issue.body.as_str(), empty: "No description given.") |
| 754 | </article> |
| 755 | |
| 756 | for comment in &issue.comments { |
| 757 | issue_comment(comment: comment) |
| 758 | } |
| 759 | </div> |
| 760 | |
| 761 | if issue.viewer_is_owner { |
| 762 | <form method="post" action=(format!("{url}/comments")) class="mt-5 space-y-3"> |
| 763 | <div class="space-y-2"> |
| 764 | label(attrs: attributes! { for="comment-body" }, "Comment") |
| 765 | textarea( |
| 766 | attrs: attributes! { |
| 767 | id="comment-body" |
| 768 | name="body" |
| 769 | rows="5" |
| 770 | placeholder="Markdown is supported." |
| 771 | }, |
| 772 | (comment) |
| 773 | ) |
| 774 | </div> |
| 775 | |
| 776 | button(attrs: attributes! { type="submit" }, "Comment") |
| 777 | </form> |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | <form method="post" action=(format!("{url}/state")) class="mt-3"> |
| 784 | <input |
| 785 | type="hidden" |
| 786 | name="state" |
| 787 | value=(if issue.state.is_open() { "closed" } else { "open" }) |
| 788 | /> |
| 789 | button( |
| 790 | variant: ButtonVariant::Outline, |
| 791 | size: ButtonSize::Sm, |
| 792 | attrs: attributes! { type="submit" }, |
| 793 | if issue.state.is_open() { "Close issue" } else { "Reopen issue" } |
| 794 | ) |
| 795 | </form> |
| 796 | |
| 797 | |
| 798 | |
| 799 | <details class="mt-3" open=(editing)> |
| 800 | <summary class="cursor-pointer text-sm text-muted-foreground hover:text-foreground"> |
| 801 | "Edit title and description" |
| 802 | </summary> |
| 803 | |
| 804 | <form method="post" action=(format!("{url}/edit")) class="mt-3 space-y-4"> |
| 805 | <div class="space-y-2"> |
| 806 | label(attrs: attributes! { for="edit-title" }, "Title") |
| 807 | input(attrs: attributes! { |
| 808 | id="edit-title" |
| 809 | name="title" |
| 810 | type="text" |
| 811 | value=(issue.title.as_str()) |
| 812 | required=(true) |
| 813 | }) |
| 814 | </div> |
| 815 | <div class="space-y-2"> |
| 816 | label(attrs: attributes! { for="edit-body" }, "Description") |
| 817 | textarea( |
| 818 | attrs: attributes! { id="edit-body" name="body" rows="10" }, |
| 819 | (issue.body.as_str()) |
| 820 | ) |
| 821 | </div> |
| 822 | button( |
| 823 | size: ButtonSize::Sm, |
| 824 | attrs: attributes! { type="submit" }, |
| 825 | "Save changes" |
| 826 | ) |
| 827 | </form> |
| 828 | </details> |
| 829 | } else { |
| 830 | <p class="mt-5 rounded-lg border border-border px-4 py-3 text-xs text-muted-foreground"> |
| 831 | "Only the owner can comment on or change this issue." |
| 832 | </p> |
| 833 | } |
| 834 | |
| 835 | <p class="mt-8"> |
| 836 | <a |
| 837 | href=(issues_url(handle, name)) |
| 838 | class="text-sm text-muted-foreground hover:text-foreground" |
| 839 | >"Back to issues"</a> |
| 840 | </p> |
| 841 | </div> |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | #[component] |
| 846 | async fn issue_comment(comment: &CommentView) -> Result { |
| 847 | let author = comment |
| 848 | .author |
| 849 | .as_ref() |
| 850 | .map(OrgName::as_str) |
| 851 | .unwrap_or("someone"); |
| 852 | |
| 853 | view! { |
| 854 | <article class="rounded-lg border border-border"> |
| 855 | <header class="flex items-center gap-2 border-b border-border px-4 py-2 text-xs text-muted-foreground"> |
| 856 | <span class="font-mono">"@" (author)</span> |
| 857 | <span title=(timestamp(comment.created_at))>(ago(comment.created_at))</span> |
| 858 | </header> |
| 859 | body_panel(source: comment.body.as_str(), empty: "") |
| 860 | </article> |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | |
| 865 | |
| 866 | |
| 867 | |
| 868 | |
| 869 | #[component] |
| 870 | async fn body_panel(source: &str, empty: &str) -> Result { |
| 871 | view! { |
| 872 | if source.is_empty() { |
| 873 | <p class="px-4 py-3 text-sm text-muted-foreground italic">(empty)</p> |
| 874 | } else { |
| 875 | <div class="overflow-x-auto px-4 py-2 text-sm [&>*:first-child]:mt-0 [&>*:last-child]:mb-0"> |
| 876 | (markdown::render(source)) |
| 877 | </div> |
| 878 | } |
| 879 | } |
| 880 | } |