steid

@jamesgill /

Rust30.2 KBCode·Blame·History·Raw
1//! Issues — `/{handle}/repos/{name}/issues`.
2//!
3//! Three pages: the list, the new-issue form, and one issue with its conversation.
4//! `new` is a static segment and `{number}` a parameterised one, so the router prefers
5//! `new` — and a number can never be the word `new` anyway, so unlike repository names
6//! there is nothing to reserve.
7//!
8//! **Bodies are markdown, rendered through [`markdown`](super::markdown), which never
9//! passes raw HTML from the source through.** That module writes every tag itself, so a
10//! `<script>` someone types into an issue arrives as visible, inert text. This is user
11//! content on the same origin as the session cookie, so that property is the reason it
12//! can be rendered at all — see that module's header before changing anything here.
13//!
14//! A visitor who may read the repository may read its issues; every write is the
15//! owner's. The use cases decide both — the guards on this page only keep a form from
16//! rendering for someone whose submission would be refused anyway.
17
18use std::time::SystemTime;
19
20use serde::Deserialize;
21use 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
34use 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
50use 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/// `{number}` from the path, raw — validation is [`IssueNumber`]'s job.
59#[path_param]
60struct Number(str);
61
62/// `?state=closed`. Anything else, including nothing, means open.
63#[query_params(error = bad_request)]
64struct StateQuery {
65 state: Option<String>,
66}
67
68#[derive(Debug, Deserialize)]
69struct IssueForm {
70 title: String,
71 body: String,
72}
73
74#[derive(Debug, Deserialize)]
75struct CommentForm {
76 body: String,
77}
78
79#[derive(Debug, Deserialize)]
80struct StateForm {
81 state: String,
82}
83
84// --- URLs ---------------------------------------------------------------------------
85
86fn issues_url(handle: &str, name: &str) -> String {
87 format!("/{handle}/repos/{name}/issues")
88}
89
90fn issue_url(handle: &str, name: &str, number: IssueNumber) -> String {
91 format!("/{handle}/repos/{name}/issues/{number}")
92}
93
94// --- Resolution ---------------------------------------------------------------------
95
96/// The number in the path, or 404.
97///
98/// A number that does not parse is not a bad request: `/issues/banana` is a URL that
99/// names nothing, exactly like `/issues/9999`, and answering differently would tell a
100/// prober which of the two they had found.
101fn number_for(cx: &Cx) -> Result<IssueNumber> {
102 path_param::<Number>(cx)
103 .parse::<IssueNumber>()
104 .map_err(|_| not_found().into())
105}
106
107/// The repository's issues in the state the query string asks for.
108async fn listing(cx: &Cx, repo: &RepoView, state: IssueState) -> Result<IssueList> {
109 let listed = list_issues(
110 &current_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 // `repo_for` already resolved the repository as visible, so `None` here would mean
124 // it vanished between the two reads. A 404 is the honest answer to that.
125 listed.ok_or_else(|| not_found().into())
126}
127
128/// One issue, or 404 — for a number that is not there and for a repository that is not
129/// visible alike.
130async fn detail(cx: &Cx, repo: &RepoView, number: IssueNumber) -> Result<IssueDetail> {
131 view_issue(
132 &current_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/// The repository, refused for anyone who does not own it.
148///
149/// A non-owner gets a 404 rather than a 403, the rule the settings page already sets:
150/// a 403 on a repository they cannot see would confirm it exists.
151async 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/// Turns a use case failure into either a message to re-render with, or a response.
162///
163/// `Forbidden` reaching here means the actor lost ownership between the guard and the
164/// write, and `NotFound` that the issue went away — both are pages, not messages.
165fn 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// --- The list -------------------------------------------------------------------------
177
178#[page("/{handle}/repos/{name}/issues")]
179async fn issues_page(cx: &Cx) -> Result {
180 let repo = repo_for(cx).await?;
181
182 // An unrecognised `?state=` shows the open list rather than erroring: this is a
183 // link someone may have mangled, not a form, and the only thing it can reveal is
184 // which issues are open anyway.
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/// The state filter and the way in, on one row.
210#[component]
211async fn issues_toolbar(handle: &str, name: &str, listed: &IssueList) -> Result {
212 // The primary colour marks which list you are looking at, the same way the tab
213 // strip marks which page you are on. Nothing else on the row is coloured.
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/// The state marker: open in the success colour, closed muted.
250///
251/// The one live thing about an issue is whether it is still open, so that is what
252/// carries the colour. A closed issue is history and reads as history.
253#[component]
254async 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]
276async 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 // No "New issue" button here: the toolbar already carries one for the
286 // owner, forty pixels above, and offering it twice reads as two things.
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 // Said rather than silently true: a list that quietly stops at a cap is a
300 // list you cannot trust. There is no paging yet, so there is no cap either.
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/// One issue on one line: what it is, then who and when, then how much talk.
310#[component]
311async 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// --- Opening one ----------------------------------------------------------------------
353
354#[page("/{handle}/repos/{name}/issues/new")]
355async 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/// Opens the issue, then redirects to it.
379///
380/// The redirect is a 303 rather than `redirect()`'s 307, which would re-POST the form
381/// to the new issue's own URL — see [`location`].
382#[page(POST "/{handle}/repos/{name}/issues")]
383async fn create(cx: &Cx, Form(submitted): Form<IssueForm>) -> Result {
384 let repo = owned_repo(cx).await?;
385
386 let outcome = create_issue(
387 &current_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 // Re-rendered with what was typed. Bouncing back to an empty form would throw away
415 // a body someone spent ten minutes on to tell them the title was too long.
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]
436async fn new_issue_form(handle: &str, name: &str, title: &str, body: &str, error: &str) -> Result {
437 // `max-w-3xl` without `mx-auto`: a centred column inside the full-width repository
438 // header leaves the two edges out of line, and the header is what the eye anchors on.
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// --- One issue ------------------------------------------------------------------------
492
493#[page("/{handle}/repos/{name}/issues/{number}")]
494async 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/// Adds a comment, then redirects so a reload cannot repeat it.
520///
521/// Named `add_comment` rather than `comment` because `#[page]` turns a handler's name
522/// into a unit struct in module scope, which would shadow every `comment` binding in
523/// this file — including the loop variable that renders one.
524#[page(POST "/{handle}/repos/{name}/issues/{number}/comments")]
525async 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 &current_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 // Kept, so a rejected comment is not silently thrown away.
570 comment: submitted.body.as_str(),
571 editing: false,
572 )
573 )
574 }
575}
576
577/// Closes or reopens, then redirects.
578///
579/// One route for both, because the two differ only in the value posted — and because
580/// two routes would mean two copies of the same guard.
581#[page(POST "/{handle}/repos/{name}/issues/{number}/state")]
582async 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 // A value neither state can name is a tampered form, not something to default:
587 // defaulting would close an issue somebody asked to reopen.
588 let state = submitted
589 .state
590 .parse::<IssueState>()
591 .map_err(|_| bad_request("unknown issue state"))?;
592
593 match set_issue_state(
594 &current_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 // Nothing typed to preserve and nothing to explain, so a failure here is a
609 // response rather than a re-render.
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/// Changes the title and body, then redirects.
622#[page(POST "/{handle}/repos/{name}/issues/{number}/edit")]
623async 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 &current_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 // The stored issue, with the rejected edit put back into the form and the panel
655 // opened on it — otherwise the reason is visible but what caused it is hidden
656 // behind a closed disclosure.
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]
686async fn issue_view(
687 handle: &str,
688 name: &str,
689 issue: &IssueDetail,
690 error: &str,
691 /// A comment that was rejected, put back in the box.
692 comment: &str,
693 /// Whether the edit panel starts open — true only when an edit was refused.
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 // The badge is the only coloured thing here, because whether the issue is
716 // still open is the only live fact on the page.
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 // Close/Reopen and Edit are forms of their own rather than buttons in
780 // the comment form: a form cannot nest, and posting a draft comment
781 // along with a close would be a surprise either way. The state posted is
782 // the one being moved *to*.
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 // On its own row rather than beside the button: the form it reveals is
798 // full width, and nesting it in a flex row indents the whole thing.
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]
846async 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/// A markdown body inside a panel.
865///
866/// Split out so the one `view!` holding rendered markdown is a single line. Safe
867/// because [`markdown`](super::markdown) writes every tag itself and never passes
868/// source HTML through — the same reason the README can be rendered.
869#[component]
870async 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}