| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | use std::collections::HashMap; |
| 7 | use std::sync::Arc; |
| 8 | use topcoat::context::Cx; |
| 9 | |
| 10 | use forge_core::suggestions::{Part, hunk_line, split_body}; |
| 11 | use forge_core::{ReviewComment, Thread}; |
| 12 | use topcoat::Result; |
| 13 | use topcoat::view::{Unescaped, View, component, view}; |
| 14 | |
| 15 | use crate::i18n::Lang; |
| 16 | use crate::i18n::pulls_review::{self as g, K}; |
| 17 | use crate::render::markdown::{LinkContext, render_markdown}; |
| 18 | use crate::views::parts::{confirm_act, post_act, reveal}; |
| 19 | use crate::views::{When, avatar_url_unknown, display_name, now_unix, user_href, user_link}; |
| 20 | |
| 21 | |
| 22 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 23 | pub enum SuggestState { |
| 24 | |
| 25 | Ready, |
| 26 | |
| 27 | Outdated, |
| 28 | } |
| 29 | |
| 30 | |
| 31 | |
| 32 | #[derive(Clone, Default)] |
| 33 | pub struct ThreadCtx { |
| 34 | |
| 35 | pub pr_href: String, |
| 36 | pub csrf: String, |
| 37 | |
| 38 | pub viewer_id: Option<String>, |
| 39 | |
| 40 | pub can_comment: bool, |
| 41 | |
| 42 | pub can_resolve: bool, |
| 43 | |
| 44 | pub can_moderate: bool, |
| 45 | |
| 46 | pub can_review: bool, |
| 47 | |
| 48 | pub tz: String, |
| 49 | |
| 50 | pub link: Option<LinkContext>, |
| 51 | |
| 52 | pub edits: Arc<HashMap<String, u64>>, |
| 53 | |
| 54 | pub suggest: Arc<HashMap<String, SuggestState>>, |
| 55 | } |
| 56 | |
| 57 | |
| 58 | #[derive(Clone)] |
| 59 | struct PartRow { |
| 60 | |
| 61 | html: String, |
| 62 | suggestion: bool, |
| 63 | |
| 64 | old: String, |
| 65 | |
| 66 | new_lines: Vec<String>, |
| 67 | |
| 68 | block: String, |
| 69 | apply_action: Option<String>, |
| 70 | outdated: bool, |
| 71 | } |
| 72 | |
| 73 | |
| 74 | #[derive(Clone)] |
| 75 | struct CommentRow { |
| 76 | author: String, |
| 77 | login: String, |
| 78 | avatar: String, |
| 79 | when: When, |
| 80 | parts: Vec<PartRow>, |
| 81 | history_href: Option<String>, |
| 82 | edit_href: Option<String>, |
| 83 | delete_href: Option<String>, |
| 84 | } |
| 85 | |
| 86 | |
| 87 | |
| 88 | fn body_parts( |
| 89 | comment: &ReviewComment, |
| 90 | ctx: &ThreadCtx, |
| 91 | state: Option<SuggestState>, |
| 92 | ) -> Vec<PartRow> { |
| 93 | let old = hunk_line(&comment.diff_hunk).unwrap_or_default().to_owned(); |
| 94 | let mut block = 0usize; |
| 95 | split_body(&comment.body) |
| 96 | .into_iter() |
| 97 | .map(|part| match part { |
| 98 | Part::Text(text) => PartRow { |
| 99 | html: render_markdown(&text, ctx.link.as_ref()), |
| 100 | suggestion: false, |
| 101 | old: String::new(), |
| 102 | new_lines: Vec::new(), |
| 103 | block: String::new(), |
| 104 | apply_action: None, |
| 105 | outdated: false, |
| 106 | }, |
| 107 | Part::Suggestion(lines) => { |
| 108 | let index = block; |
| 109 | block += 1; |
| 110 | let apply_action = (state == Some(SuggestState::Ready)) |
| 111 | .then(|| format!("{}/review_comments/{}/apply", ctx.pr_href, comment.id)); |
| 112 | PartRow { |
| 113 | html: String::new(), |
| 114 | suggestion: true, |
| 115 | old: old.clone(), |
| 116 | new_lines: lines, |
| 117 | block: index.to_string(), |
| 118 | apply_action, |
| 119 | outdated: state == Some(SuggestState::Outdated), |
| 120 | } |
| 121 | } |
| 122 | }) |
| 123 | .collect() |
| 124 | } |
| 125 | |
| 126 | |
| 127 | struct ThreadParts { |
| 128 | hunk: Option<String>, |
| 129 | comments: Vec<CommentRow>, |
| 130 | resolve_action: Option<String>, |
| 131 | resolve_value: String, |
| 132 | resolve_label: String, |
| 133 | reply_action: Option<String>, |
| 134 | reply_id: String, |
| 135 | csrf: String, |
| 136 | return_to: String, |
| 137 | lang: Lang, |
| 138 | } |
| 139 | |
| 140 | fn comment_row( |
| 141 | comment: ReviewComment, |
| 142 | ctx: &ThreadCtx, |
| 143 | now: i64, |
| 144 | lang: Lang, |
| 145 | state: Option<SuggestState>, |
| 146 | ) -> CommentRow { |
| 147 | let mine = ctx.viewer_id.as_deref() == Some(comment.author_id.as_str()); |
| 148 | let base = format!("{}/review_comments/{}", ctx.pr_href, comment.id); |
| 149 | let edited = ctx.edits.get(&comment.id).is_some_and(|count| *count > 0); |
| 150 | let parts = body_parts(&comment, ctx, state); |
| 151 | CommentRow { |
| 152 | avatar: avatar_url_unknown(&comment.author_name), |
| 153 | author: display_name(lang, &comment.author_name), |
| 154 | login: comment.author_name.clone(), |
| 155 | when: When::new(lang, &comment.created_at, &ctx.tz, now), |
| 156 | parts, |
| 157 | history_href: edited |
| 158 | .then(|| format!("{}/history/review_comment/{}", ctx.pr_href, comment.id)), |
| 159 | edit_href: (mine && ctx.can_comment).then(|| format!("{base}/edit")), |
| 160 | delete_href: ((mine || ctx.can_moderate) && ctx.can_comment) |
| 161 | .then(|| format!("{base}/delete")), |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | #[component] |
| 169 | pub async fn review_thread( |
| 170 | cx: &Cx, |
| 171 | thread: Thread, |
| 172 | ctx: ThreadCtx, |
| 173 | show_hunk: bool, |
| 174 | return_to: &str, |
| 175 | ) -> Result<impl View> { |
| 176 | Ok(view! { |
| 177 | review_thread_in(lang: Lang::En, thread: thread, ctx: ctx, show_hunk: show_hunk, |
| 178 | return_to: return_to) |
| 179 | }) |
| 180 | } |
| 181 | |
| 182 | |
| 183 | #[component] |
| 184 | pub async fn review_thread_in( |
| 185 | cx: &Cx, |
| 186 | lang: Lang, |
| 187 | thread: Thread, |
| 188 | ctx: ThreadCtx, |
| 189 | show_hunk: bool, |
| 190 | return_to: &str, |
| 191 | ) -> Result<impl View> { |
| 192 | let now = now_unix(); |
| 193 | let return_to = return_to.to_owned(); |
| 194 | let Thread { root, replies } = thread; |
| 195 | let suggest = ctx.suggest.get(&root.id).copied(); |
| 196 | let anchor = format!("rc-{}", root.id); |
| 197 | let reply_id = format!("reply-{}", root.id); |
| 198 | let location = format!("{}:{}", root.path, root.line); |
| 199 | let hunk = (show_hunk && !root.diff_hunk.is_empty()).then(|| root.diff_hunk.clone()); |
| 200 | let resolved = root.resolved; |
| 201 | let pending = root.pending; |
| 202 | let outdated = root.outdated; |
| 203 | let root_base = format!("{}/review_comments/{}", ctx.pr_href, root.id); |
| 204 | let resolve_action = (ctx.can_resolve && !pending).then(|| format!("{root_base}/resolve")); |
| 205 | let reply_action = (ctx.can_comment && !pending).then(|| format!("{root_base}/reply")); |
| 206 | let (resolve_value, resolve_label) = if resolved { |
| 207 | ("0", g::t(lang, K::Unresolve)) |
| 208 | } else { |
| 209 | ("1", g::t(lang, K::Resolve)) |
| 210 | }; |
| 211 | |
| 212 | let comments: Vec<CommentRow> = std::iter::once(root) |
| 213 | .chain(replies) |
| 214 | .enumerate() |
| 215 | .map(|(index, comment)| { |
| 216 | let state = if index == 0 { suggest } else { None }; |
| 217 | comment_row(comment, &ctx, now, lang, state) |
| 218 | }) |
| 219 | .collect(); |
| 220 | |
| 221 | let parts = ThreadParts { |
| 222 | hunk, |
| 223 | comments, |
| 224 | resolve_action, |
| 225 | resolve_value: resolve_value.to_owned(), |
| 226 | resolve_label: resolve_label.to_owned(), |
| 227 | reply_action, |
| 228 | reply_id, |
| 229 | csrf: ctx.csrf.clone(), |
| 230 | return_to, |
| 231 | lang, |
| 232 | }; |
| 233 | |
| 234 | |
| 235 | |
| 236 | let summary = if resolved { |
| 237 | g::resolved_conversation(lang, &location) |
| 238 | } else { |
| 239 | location |
| 240 | }; |
| 241 | let outdated_text = g::t(lang, K::Outdated); |
| 242 | let pending_text = g::t(lang, K::Pending); |
| 243 | Ok(view! { |
| 244 | <details class="row-open review-thread" id=(anchor.as_str()) open=(!resolved)> |
| 245 | <summary class="line"> |
| 246 | <span class="name">(summary.as_str())</span> |
| 247 | <span class="val"> |
| 248 | if outdated { |
| 249 | <span class="badge">(outdated_text)</span> |
| 250 | } |
| 251 | if pending { |
| 252 | <span class="badge">(pending_text)</span> |
| 253 | } |
| 254 | </span> |
| 255 | </summary> |
| 256 | <div class="opened"> |
| 257 | thread_parts(parts: parts) |
| 258 | </div> |
| 259 | </details> |
| 260 | }) |
| 261 | } |
| 262 | |
| 263 | |
| 264 | |
| 265 | #[component] |
| 266 | async fn thread_parts(cx: &Cx, parts: ThreadParts) -> Result<impl View> { |
| 267 | let ThreadParts { |
| 268 | hunk, |
| 269 | comments, |
| 270 | resolve_action, |
| 271 | resolve_value, |
| 272 | resolve_label, |
| 273 | reply_action, |
| 274 | reply_id, |
| 275 | csrf, |
| 276 | return_to, |
| 277 | lang, |
| 278 | } = parts; |
| 279 | let footer = ThreadFooter { |
| 280 | resolve_action, |
| 281 | resolve_value, |
| 282 | resolve_label, |
| 283 | reply_action, |
| 284 | reply_id, |
| 285 | csrf: csrf.clone(), |
| 286 | return_to: return_to.clone(), |
| 287 | lang, |
| 288 | }; |
| 289 | Ok(view! { |
| 290 | if let Some(text) = hunk { |
| 291 | <pre class="review-hunk"><code>(text.as_str())</code></pre> |
| 292 | } |
| 293 | <div class="thread"> |
| 294 | for comment in comments.into_iter() { |
| 295 | thread_comment(comment: comment, csrf: csrf.clone(), return_to: return_to.clone(), |
| 296 | lang: lang) |
| 297 | } |
| 298 | </div> |
| 299 | thread_footer(footer: footer) |
| 300 | }) |
| 301 | } |
| 302 | |
| 303 | |
| 304 | struct ThreadFooter { |
| 305 | resolve_action: Option<String>, |
| 306 | resolve_value: String, |
| 307 | resolve_label: String, |
| 308 | reply_action: Option<String>, |
| 309 | reply_id: String, |
| 310 | csrf: String, |
| 311 | return_to: String, |
| 312 | lang: Lang, |
| 313 | } |
| 314 | |
| 315 | |
| 316 | |
| 317 | #[component] |
| 318 | async fn thread_footer(cx: &Cx, footer: ThreadFooter) -> Result<impl View> { |
| 319 | let ThreadFooter { |
| 320 | resolve_action, |
| 321 | resolve_value, |
| 322 | resolve_label, |
| 323 | reply_action, |
| 324 | reply_id, |
| 325 | csrf, |
| 326 | return_to, |
| 327 | lang, |
| 328 | } = footer; |
| 329 | let reply_label = g::t(lang, K::ReplyLabel); |
| 330 | let reply_button = g::t(lang, K::ReplyButton); |
| 331 | Ok(view! { |
| 332 | if let Some(action) = resolve_action { |
| 333 | <div class="acts"> |
| 334 | post_act(label: resolve_label.as_str(), action: action.as_str(), csrf: csrf.as_str(), |
| 335 | fields: vec![("resolved".to_owned(), resolve_value.clone()), ("return".to_owned(), return_to.clone())]) |
| 336 | </div> |
| 337 | } |
| 338 | if let Some(action) = reply_action { |
| 339 | reveal(label: reply_button, |
| 340 | <form method="post" action=(action.as_str()) class="fields"> |
| 341 | <input type="hidden" name="csrf" value=(csrf.as_str())> |
| 342 | <input type="hidden" name="return" value=(return_to.as_str())> |
| 343 | <div class="field"> |
| 344 | <label for=(reply_id.as_str())>(reply_label)</label> |
| 345 | <textarea id=(reply_id.as_str()) name="body" rows="3"></textarea> |
| 346 | </div> |
| 347 | <button type="submit" class="primary">(reply_button)</button> |
| 348 | </form> |
| 349 | ) |
| 350 | } |
| 351 | }) |
| 352 | } |
| 353 | |
| 354 | #[component] |
| 355 | async fn thread_comment( |
| 356 | cx: &Cx, |
| 357 | comment: CommentRow, |
| 358 | csrf: String, |
| 359 | return_to: String, |
| 360 | lang: Lang, |
| 361 | ) -> Result<impl View> { |
| 362 | let CommentRow { |
| 363 | author, |
| 364 | login, |
| 365 | avatar, |
| 366 | when, |
| 367 | parts, |
| 368 | history_href, |
| 369 | edit_href, |
| 370 | delete_href, |
| 371 | } = comment; |
| 372 | let profile = user_href(&login); |
| 373 | let edited = g::t(lang, K::Edited); |
| 374 | let edit = g::t(lang, K::EditLink); |
| 375 | let delete = g::t(lang, K::DeleteButton); |
| 376 | let has_acts = edit_href.is_some() || delete_href.is_some(); |
| 377 | Ok(view! { |
| 378 | <div> |
| 379 | <p class="who"> |
| 380 | <a href=(profile.as_str()) aria-label=(login.as_str())><img class="avatar avatar-sm" src=(avatar.as_str()) alt="" width="24" height="24"></a> |
| 381 | user_link(shown: author.as_str(), login: login.as_str()) |
| 382 | <span><time data-rel="true" datetime=(when.iso.as_str()) title=(when.absolute.as_str())>(when.relative.as_str())</time></span> |
| 383 | if let Some(href) = history_href { |
| 384 | <a class="act" href=(href.as_str())>(edited)</a> |
| 385 | } |
| 386 | </p> |
| 387 | for part in parts.into_iter() { |
| 388 | comment_part(part: part, csrf: csrf.clone(), return_to: return_to.clone(), |
| 389 | lang: lang) |
| 390 | } |
| 391 | if has_acts { |
| 392 | <div class="acts"> |
| 393 | if let Some(href) = edit_href { |
| 394 | <a class="act" href=(href.as_str())>(edit)</a> |
| 395 | } |
| 396 | if let Some(action) = delete_href { |
| 397 | confirm_act(lang: lang, label: delete, action: action.as_str(), csrf: csrf.as_str(), |
| 398 | fields: vec![("return".to_owned(), return_to.clone())]) |
| 399 | } |
| 400 | </div> |
| 401 | } |
| 402 | </div> |
| 403 | }) |
| 404 | } |
| 405 | |
| 406 | /// One part of a comment body: its markdown, or a suggested change with its removed and added |
| 407 | /// lines, the Apply control when it applies, and a note when it is outdated. |
| 408 | #[component] |
| 409 | async fn comment_part( |
| 410 | cx: &Cx, |
| 411 | part: PartRow, |
| 412 | csrf: String, |
| 413 | return_to: String, |
| 414 | lang: Lang, |
| 415 | ) -> Result<impl View> { |
| 416 | let PartRow { |
| 417 | html, |
| 418 | suggestion, |
| 419 | old, |
| 420 | new_lines, |
| 421 | block, |
| 422 | apply_action, |
| 423 | outdated, |
| 424 | } = part; |
| 425 | let caption = g::t(lang, K::SuggestedChange); |
| 426 | let apply_label = g::t(lang, K::ApplySuggestion); |
| 427 | let outdated_text = g::t(lang, K::SuggestionOutdated); |
| 428 | let removed_sign = "-"; |
| 429 | let added_sign = "+"; |
| 430 | Ok(view! { |
| 431 | if !suggestion { |
| 432 | <div class="markdown-body">(Unescaped::new_unchecked(html))</div> |
| 433 | } |
| 434 | if suggestion { |
| 435 | <div class="review-suggestion"> |
| 436 | <p class="hint">(caption)</p> |
| 437 | if !old.is_empty() { |
| 438 | <div class="review-suggestion-line del"><span class="review-suggestion-sign">(removed_sign)</span><code>(old.as_str())</code></div> |
| 439 | } |
| 440 | for line in new_lines.into_iter() { |
| 441 | <div class="review-suggestion-line add"><span class="review-suggestion-sign">(added_sign)</span><code>(line.as_str())</code></div> |
| 442 | } |
| 443 | if let Some(action) = apply_action { |
| 444 | <div class="acts"> |
| 445 | post_act(label: apply_label, action: action.as_str(), csrf: csrf.as_str(), |
| 446 | fields: vec![("block".to_owned(), block.clone()), ("return".to_owned(), return_to.clone())]) |
| 447 | </div> |
| 448 | } |
| 449 | if outdated { |
| 450 | <p class="hint">(outdated_text)</p> |
| 451 | } |
| 452 | </div> |
| 453 | } |
| 454 | }) |
| 455 | } |