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 )
199 issue_list(
200 handle: repo.handle.as_str(),
201 name: repo.name.as_str(),
202 listed: &listed,
203 )
204 )
205 }
206}
207
208/// The state filter and the way in, on one row.
209#[component]
210async fn issues_toolbar(handle: &str, name: &str, listed: &IssueList) -> Result {
211 // The primary colour marks which list you are looking at, the same way the tab
212 // strip marks which page you are on. Nothing else on the row is coloured.
213 let current = |active: bool| {
214 if active {
215 "text-foreground"
216 } else {
217 "text-muted-foreground hover:text-foreground"
218 }
219 };
220 let url = issues_url(handle, name);
221
222 view! {
223 <div class="mb-3 flex flex-wrap items-center gap-x-4 gap-y-2 text-sm">
224 <a href=(url.clone()) class=(format!("flex items-center gap-1.5 {}", current(listed.state.is_open())))>
225 state_icon(state: IssueState::Open, muted: !listed.state.is_open())
226 "Open"
227 <span class="font-mono text-xs text-muted-foreground">(listed.open)</span>
228 </a>
229 <a href=(format!("{url}?state=closed")) class=(format!("flex items-center gap-1.5 {}", current(!listed.state.is_open())))>
230 state_icon(state: IssueState::Closed, muted: listed.state.is_open())
231 "Closed"
232 <span class="font-mono text-xs text-muted-foreground">(listed.closed)</span>
233 </a>
234
235 if listed.viewer_is_owner {
236 <a
237 href=(format!("{url}/new"))
238 class=(format!(
239 "ml-auto {}",
240 button_variants(ButtonVariant::Primary, ButtonSize::Sm)
241 ))
242 >"New issue"</a>
243 }
244 </div>
245 }
246}
247
248/// The state marker: open in the success colour, closed muted.
249///
250/// The one live thing about an issue is whether it is still open, so that is what
251/// carries the colour. A closed issue is history and reads as history.
252#[component]
253async fn state_icon(state: IssueState, #[default] muted: bool) -> Result {
254 let tone = if muted {
255 "text-muted-foreground"
256 } else if state.is_open() {
257 "text-success"
258 } else {
259 "text-muted-foreground"
260 };
261
262 view! {
263 icon(
264 data: if state.is_open() {
265 iconify_icon!("feather:alert-circle")
266 } else {
267 iconify_icon!("feather:check-circle")
268 },
269 attrs: attributes! { class=(format!("size-3.5 shrink-0 {tone}")) }
270 )
271 }
272}
273
274#[component]
275async fn issue_list(handle: &str, name: &str, listed: &IssueList) -> Result {
276 view! {
277 issues_toolbar(handle: handle, name: name, listed: listed)
278
279 if listed.issues.is_empty() {
280 <div class="rounded-lg border border-border px-4 py-10 text-center">
281 <p class="text-sm text-muted-foreground">
282 if listed.state.is_open() { "No open issues." } else { "No closed issues." }
283 </p>
284 // No "New issue" button here: the toolbar already carries one for the
285 // owner, forty pixels above, and offering it twice reads as two things.
286 if !listed.viewer_is_owner && listed.state.is_open() {
287 <p class="mt-1 text-xs text-muted-foreground">
288 "Only the owner can open one here."
289 </p>
290 }
291 </div>
292 } else {
293 <ul class="divide-y divide-border rounded-lg border border-border">
294 for issue in &listed.issues {
295 issue_row(handle: handle, name: name, issue: issue)
296 }
297 </ul>
298 // Said rather than silently true: a list that quietly stops at a cap is a
299 // list you cannot trust. There is no paging yet, so there is no cap either.
300 <p class="mt-2 text-xs text-muted-foreground">
301 "Showing every " if listed.state.is_open() { "open" } else { "closed" }
302 " issue — this list is not paged."
303 </p>
304 }
305 }
306}
307
308/// One issue on one line: what it is, then who and when, then how much talk.
309#[component]
310async fn issue_row(handle: &str, name: &str, issue: &IssueSummary) -> Result {
311 let author = issue
312 .author
313 .as_ref()
314 .map(OrgName::as_str)
315 .unwrap_or("someone");
316
317 view! {
318 <li class="flex items-center gap-3 px-4 py-2.5 text-sm">
319 state_icon(state: issue.state)
320
321 <div class="min-w-0 flex-1">
322 <a
323 href=(issue_url(handle, name, issue.number))
324 class="block truncate font-medium hover:text-primary"
325 >(issue.title.as_str())</a>
326 <p class="mt-0.5 truncate text-xs text-muted-foreground">
327 <span class="font-mono">"#" (issue.number.get())</span>
328 " opened "
329 <span title=(timestamp(issue.created_at))>(ago(issue.created_at))</span>
330 " by "
331 <span class="font-mono">"@" (author)</span>
332 </p>
333 </div>
334
335 if issue.comments > 0 {
336 <span
337 class="flex shrink-0 items-center gap-1 text-xs text-muted-foreground"
338 title=(format!("{} comments", issue.comments))
339 >
340 icon(
341 data: iconify_icon!("feather:message-square"),
342 attrs: attributes! { class="size-3.5" }
343 )
344 <span class="font-mono">(issue.comments)</span>
345 </span>
346 }
347 </li>
348 }
349}
350
351// --- Opening one ----------------------------------------------------------------------
352
353#[page("/{handle}/repos/{name}/issues/new")]
354async fn new_issue_page(cx: &Cx) -> Result {
355 let repo = owned_repo(cx).await?;
356
357 view! {
358 wide(
359 repo_header(
360 repo: &repo,
361 rev: "",
362 active: Tab::Issues,
363 )
364 new_issue_form(
365 handle: repo.handle.as_str(),
366 name: repo.name.as_str(),
367 title: "",
368 body: "",
369 error: "",
370 )
371 )
372 }
373}
374
375/// Opens the issue, then redirects to it.
376///
377/// The redirect is a 303 rather than `redirect()`'s 307, which would re-POST the form
378/// to the new issue's own URL — see [`location`].
379#[page(POST "/{handle}/repos/{name}/issues")]
380async fn create(cx: &Cx, Form(submitted): Form<IssueForm>) -> Result {
381 let repo = owned_repo(cx).await?;
382
383 let outcome = create_issue(
384 &current_actor(cx).await?,
385 &repo.handle,
386 &repo.name,
387 &NewIssue {
388 title: submitted.title.clone(),
389 body: submitted.body.clone(),
390 },
391 SystemTime::now(),
392 &orgs(cx),
393 &memberships(cx),
394 &repos(cx),
395 &issues(cx),
396 )
397 .await;
398
399 let message = match outcome {
400 Ok(issue) => {
401 return view! {
402 (StatusCode::SEE_OTHER)
403 (location(&issue_url(repo.handle.as_str(), repo.name.as_str(), issue.number))?)
404 };
405 }
406 Err(error) => write_failure(error)?,
407 };
408
409 // Re-rendered with what was typed. Bouncing back to an empty form would throw away
410 // a body someone spent ten minutes on to tell them the title was too long.
411 view! {
412 wide(
413 repo_header(
414 repo: &repo,
415 rev: "",
416 active: Tab::Issues,
417 )
418 new_issue_form(
419 handle: repo.handle.as_str(),
420 name: repo.name.as_str(),
421 title: submitted.title.as_str(),
422 body: submitted.body.as_str(),
423 error: message.as_str(),
424 )
425 )
426 }
427}
428
429#[component]
430async fn new_issue_form(handle: &str, name: &str, title: &str, body: &str, error: &str) -> Result {
431 // `max-w-3xl` without `mx-auto`: a centred column inside the full-width repository
432 // header leaves the two edges out of line, and the header is what the eye anchors on.
433 view! {
434 <div class="max-w-3xl">
435 <h1 class="text-lg font-semibold tracking-tight">"New issue"</h1>
436
437 if !error.is_empty() {
438 <div class="mt-4">
439 flash(kind: FlashKind::Error, (error))
440 </div>
441 }
442
443 <form method="post" action=(issues_url(handle, name)) class="mt-5 space-y-4">
444 <div class="space-y-2">
445 label(attrs: attributes! { for="title" }, "Title")
446 input(attrs: attributes! {
447 id="title"
448 name="title"
449 type="text"
450 value=(title)
451 placeholder="Blame is slow on large files"
452 required=(true)
453 autofocus=(true)
454 })
455 </div>
456
457 <div class="space-y-2">
458 label(attrs: attributes! { for="body" }, "Description")
459 textarea(
460 attrs: attributes! {
461 id="body"
462 name="body"
463 rows="12"
464 placeholder="Markdown is supported."
465 },
466 (body)
467 )
468 <p class="text-xs text-muted-foreground">
469 "Markdown. HTML in the source is shown as text, never rendered."
470 </p>
471 </div>
472
473 <div class="flex items-center gap-3">
474 button(attrs: attributes! { type="submit" }, "Open issue")
475 <a
476 href=(issues_url(handle, name))
477 class="text-sm text-muted-foreground hover:text-foreground"
478 >"Cancel"</a>
479 </div>
480 </form>
481 </div>
482 }
483}
484
485// --- One issue ------------------------------------------------------------------------
486
487#[page("/{handle}/repos/{name}/issues/{number}")]
488async fn issue_page(cx: &Cx) -> Result {
489 let repo = repo_for(cx).await?;
490 let issue = detail(cx, &repo, number_for(cx)?).await?;
491
492 view! {
493 wide(
494 repo_header(
495 repo: &repo,
496 rev: "",
497 active: Tab::Issues,
498 )
499 issue_view(
500 handle: repo.handle.as_str(),
501 name: repo.name.as_str(),
502 issue: &issue,
503 error: "",
504 comment: "",
505 editing: false,
506 )
507 )
508 }
509}
510
511/// Adds a comment, then redirects so a reload cannot repeat it.
512///
513/// Named `add_comment` rather than `comment` because `#[page]` turns a handler's name
514/// into a unit struct in module scope, which would shadow every `comment` binding in
515/// this file — including the loop variable that renders one.
516#[page(POST "/{handle}/repos/{name}/issues/{number}/comments")]
517async fn add_comment(cx: &Cx, Form(submitted): Form<CommentForm>) -> Result {
518 let repo = owned_repo(cx).await?;
519 let number = number_for(cx)?;
520
521 let outcome = comment_on_issue(
522 &current_actor(cx).await?,
523 &repo.handle,
524 &repo.name,
525 number,
526 &submitted.body,
527 SystemTime::now(),
528 &orgs(cx),
529 &memberships(cx),
530 &repos(cx),
531 &issues(cx),
532 )
533 .await;
534
535 let message = match outcome {
536 Ok(()) => {
537 return view! {
538 (StatusCode::SEE_OTHER)
539 (location(&issue_url(repo.handle.as_str(), repo.name.as_str(), number))?)
540 };
541 }
542 Err(error) => write_failure(error)?,
543 };
544
545 let issue = detail(cx, &repo, number).await?;
546
547 view! {
548 wide(
549 repo_header(
550 repo: &repo,
551 rev: "",
552 active: Tab::Issues,
553 )
554 issue_view(
555 handle: repo.handle.as_str(),
556 name: repo.name.as_str(),
557 issue: &issue,
558 error: message.as_str(),
559 // Kept, so a rejected comment is not silently thrown away.
560 comment: submitted.body.as_str(),
561 editing: false,
562 )
563 )
564 }
565}
566
567/// Closes or reopens, then redirects.
568///
569/// One route for both, because the two differ only in the value posted — and because
570/// two routes would mean two copies of the same guard.
571#[page(POST "/{handle}/repos/{name}/issues/{number}/state")]
572async fn set_state(cx: &Cx, Form(submitted): Form<StateForm>) -> Result {
573 let repo = owned_repo(cx).await?;
574 let number = number_for(cx)?;
575
576 // A value neither state can name is a tampered form, not something to default:
577 // defaulting would close an issue somebody asked to reopen.
578 let state = submitted
579 .state
580 .parse::<IssueState>()
581 .map_err(|_| bad_request("unknown issue state"))?;
582
583 match set_issue_state(
584 &current_actor(cx).await?,
585 &repo.handle,
586 &repo.name,
587 number,
588 state,
589 SystemTime::now(),
590 &orgs(cx),
591 &memberships(cx),
592 &repos(cx),
593 &issues(cx),
594 )
595 .await
596 {
597 Ok(()) => {}
598 // Nothing typed to preserve and nothing to explain, so a failure here is a
599 // response rather than a re-render.
600 Err(error) => {
601 write_failure(error)?;
602 }
603 }
604
605 view! {
606 (StatusCode::SEE_OTHER)
607 (location(&issue_url(repo.handle.as_str(), repo.name.as_str(), number))?)
608 }
609}
610
611/// Changes the title and body, then redirects.
612#[page(POST "/{handle}/repos/{name}/issues/{number}/edit")]
613async fn edit(cx: &Cx, Form(submitted): Form<IssueForm>) -> Result {
614 let repo = owned_repo(cx).await?;
615 let number = number_for(cx)?;
616
617 let outcome = edit_issue(
618 &current_actor(cx).await?,
619 &repo.handle,
620 &repo.name,
621 number,
622 &IssueEdit {
623 title: submitted.title.clone(),
624 body: submitted.body.clone(),
625 },
626 SystemTime::now(),
627 &orgs(cx),
628 &memberships(cx),
629 &repos(cx),
630 &issues(cx),
631 )
632 .await;
633
634 let message = match outcome {
635 Ok(()) => {
636 return view! {
637 (StatusCode::SEE_OTHER)
638 (location(&issue_url(repo.handle.as_str(), repo.name.as_str(), number))?)
639 };
640 }
641 Err(error) => write_failure(error)?,
642 };
643
644 // The stored issue, with the rejected edit put back into the form and the panel
645 // opened on it — otherwise the reason is visible but what caused it is hidden
646 // behind a closed disclosure.
647 let stored = detail(cx, &repo, number).await?;
648 let attempted = IssueDetail {
649 title: submitted.title.clone(),
650 body: submitted.body.clone(),
651 ..stored
652 };
653
654 view! {
655 wide(
656 repo_header(
657 repo: &repo,
658 rev: "",
659 active: Tab::Issues,
660 )
661 issue_view(
662 handle: repo.handle.as_str(),
663 name: repo.name.as_str(),
664 issue: &attempted,
665 error: message.as_str(),
666 comment: "",
667 editing: true,
668 )
669 )
670 }
671}
672
673#[component]
674async fn issue_view(
675 handle: &str,
676 name: &str,
677 issue: &IssueDetail,
678 error: &str,
679 /// A comment that was rejected, put back in the box.
680 comment: &str,
681 /// Whether the edit panel starts open — true only when an edit was refused.
682 editing: bool,
683) -> Result {
684 let author = issue
685 .author
686 .as_ref()
687 .map(OrgName::as_str)
688 .unwrap_or("someone");
689 let url = issue_url(handle, name, issue.number);
690
691 view! {
692 <div class="max-w-3xl">
693 <div class="flex items-start gap-3">
694 <h1 class="min-w-0 flex-1 text-xl font-semibold tracking-tight">
695 (issue.title.as_str())
696 <span class="ml-2 font-mono text-lg font-normal text-muted-foreground">
697 "#" (issue.number.get())
698 </span>
699 </h1>
700 </div>
701
702 <div class="mt-2 flex flex-wrap items-center gap-x-3 gap-y-2 text-xs text-muted-foreground">
703 // The badge is the only coloured thing here, because whether the issue is
704 // still open is the only live fact on the page.
705 badge(
706 variant: if issue.state.is_open() {
707 BadgeVariant::Primary
708 } else {
709 BadgeVariant::Secondary
710 },
711 if issue.state.is_open() { "Open" } else { "Closed" }
712 )
713 <span>
714 <span class="font-mono">"@" (author)</span>
715 " opened this "
716 <span title=(timestamp(issue.created_at))>(ago(issue.created_at))</span>
717 </span>
718 if let Some(closed_at) = issue.closed_at {
719 <span>
720 "closed " <span title=(timestamp(closed_at))>(ago(closed_at))</span>
721 </span>
722 }
723 <span>
724 (issue.comments.len())
725 if issue.comments.len() == 1 { " comment" } else { " comments" }
726 </span>
727 </div>
728
729 if !error.is_empty() {
730 <div class="mt-4">
731 flash(kind: FlashKind::Error, (error))
732 </div>
733 }
734
735 <div class="mt-4 space-y-3">
736 <article class="rounded-lg border border-border">
737 <header class="flex items-center gap-2 border-b border-border px-4 py-2 text-xs text-muted-foreground">
738 <span class="font-mono">"@" (author)</span>
739 <span title=(timestamp(issue.created_at))>(ago(issue.created_at))</span>
740 </header>
741 body_panel(source: issue.body.as_str(), empty: "No description given.")
742 </article>
743
744 for comment in &issue.comments {
745 issue_comment(comment: comment)
746 }
747 </div>
748
749 if issue.viewer_is_owner {
750 // The two actions sit on one row under the textarea, per ui.md's
751 // density rule: they are the two things to do here and stacking them
752 // spent a whole row on each.
753 //
754 // They are still **two forms** — a form cannot nest, and posting a draft
755 // comment along with a close would be a surprise either way. The Comment
756 // button reaches its own form by id rather than by containment, which is
757 // what lets the row hold both without the textarea joining it.
758 <form
759 id="comment-form"
760 method="post"
761 action=(format!("{url}/comments"))
762 class="mt-5 space-y-2"
763 >
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 </form>
775
776 <div class="mt-3 flex flex-wrap items-center gap-2">
777 button(
778 size: ButtonSize::Sm,
779 attrs: attributes! { type="submit" form="comment-form" },
780 "Comment"
781 )
782
783 // The state posted is the one being moved *to*.
784 <form method="post" action=(format!("{url}/state"))>
785 <input
786 type="hidden"
787 name="state"
788 value=(if issue.state.is_open() { "closed" } else { "open" })
789 />
790 button(
791 variant: ButtonVariant::Outline,
792 size: ButtonSize::Sm,
793 attrs: attributes! { type="submit" },
794 if issue.state.is_open() { "Close issue" } else { "Reopen issue" }
795 )
796 </form>
797 </div>
798
799 // On its own row rather than beside the button: the form it reveals is
800 // full width, and nesting it in a flex row indents the whole thing.
801 <details class="mt-3" open=(editing)>
802 <summary class="cursor-pointer text-sm text-muted-foreground hover:text-foreground">
803 "Edit title and description"
804 </summary>
805
806 <form method="post" action=(format!("{url}/edit")) class="mt-3 space-y-4">
807 <div class="space-y-2">
808 label(attrs: attributes! { for="edit-title" }, "Title")
809 input(attrs: attributes! {
810 id="edit-title"
811 name="title"
812 type="text"
813 value=(issue.title.as_str())
814 required=(true)
815 })
816 </div>
817 <div class="space-y-2">
818 label(attrs: attributes! { for="edit-body" }, "Description")
819 textarea(
820 attrs: attributes! { id="edit-body" name="body" rows="10" },
821 (issue.body.as_str())
822 )
823 </div>
824 button(
825 size: ButtonSize::Sm,
826 attrs: attributes! { type="submit" },
827 "Save changes"
828 )
829 </form>
830 </details>
831 } else {
832 <p class="mt-5 rounded-lg border border-border px-4 py-3 text-xs text-muted-foreground">
833 "Only the owner can comment on or change this issue."
834 </p>
835 }
836
837 <p class="mt-8">
838 <a
839 href=(issues_url(handle, name))
840 class="text-sm text-muted-foreground hover:text-foreground"
841 >"Back to issues"</a>
842 </p>
843 </div>
844 }
845}
846
847#[component]
848async fn issue_comment(comment: &CommentView) -> Result {
849 let author = comment
850 .author
851 .as_ref()
852 .map(OrgName::as_str)
853 .unwrap_or("someone");
854
855 view! {
856 <article class="rounded-lg border border-border">
857 <header class="flex items-center gap-2 border-b border-border px-4 py-2 text-xs text-muted-foreground">
858 <span class="font-mono">"@" (author)</span>
859 <span title=(timestamp(comment.created_at))>(ago(comment.created_at))</span>
860 </header>
861 body_panel(source: comment.body.as_str(), empty: "")
862 </article>
863 }
864}
865
866/// A markdown body inside a panel.
867///
868/// Split out so the one `view!` holding rendered markdown is a single line. Safe
869/// because [`markdown`](super::markdown) writes every tag itself and never passes
870/// source HTML through — the same reason the README can be rendered.
871#[component]
872async fn body_panel(source: &str, empty: &str) -> Result {
873 view! {
874 if source.is_empty() {
875 <p class="px-4 py-3 text-sm text-muted-foreground italic">(empty)</p>
876 } else {
877 <div class="overflow-x-auto px-4 py-2 text-sm [&>*:first-child]:mt-0 [&>*:last-child]:mb-0">
878 (markdown::render(source))
879 </div>
880 }
881 }
882}