| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | use std::collections::HashMap; |
| 12 | |
| 13 | use forge_core::{Side, Thread}; |
| 14 | use forge_git::history::{CommitDiff, DiffLimits, DiffStatus, FileDiff, LineKind}; |
| 15 | use topcoat::Result; |
| 16 | use topcoat::context::Cx; |
| 17 | use topcoat::cookie::{Cookie, Cookies, SameSite, cookies, time}; |
| 18 | use topcoat::router::request::uri; |
| 19 | use topcoat::view::{View, component, view}; |
| 20 | |
| 21 | use crate::i18n::Lang; |
| 22 | use crate::i18n::browse as b; |
| 23 | use crate::i18n::code::{self as g, K}; |
| 24 | use crate::pages::commits::url_path; |
| 25 | use crate::views::review_thread::{ThreadCtx, review_thread_in}; |
| 26 | |
| 27 | |
| 28 | pub const LIMITS: DiffLimits = DiffLimits { |
| 29 | max_files: MAX_FILES, |
| 30 | max_lines_per_file: 2000, |
| 31 | max_total_lines: 20_000, |
| 32 | }; |
| 33 | |
| 34 | |
| 35 | pub const MAX_FILES: usize = 300; |
| 36 | |
| 37 | |
| 38 | |
| 39 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 40 | pub struct DiffLang(pub Lang); |
| 41 | |
| 42 | impl Default for DiffLang { |
| 43 | fn default() -> Self { |
| 44 | DiffLang(Lang::En) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | |
| 49 | #[derive(Default)] |
| 50 | pub struct LineComments { |
| 51 | |
| 52 | |
| 53 | pub anchors: bool, |
| 54 | |
| 55 | pub threads: HashMap<(usize, Side, u32), Vec<Thread>>, |
| 56 | |
| 57 | pub open_form: Option<(usize, Side, u32)>, |
| 58 | |
| 59 | |
| 60 | pub files_href: String, |
| 61 | |
| 62 | pub has_pending: bool, |
| 63 | |
| 64 | pub ctx: ThreadCtx, |
| 65 | } |
| 66 | |
| 67 | |
| 68 | pub const DIFF_VIEW_COOKIE: &str = "forge_diff"; |
| 69 | |
| 70 | |
| 71 | |
| 72 | #[derive(Clone, Default)] |
| 73 | pub struct DiffOpts { |
| 74 | |
| 75 | pub enabled: bool, |
| 76 | pub split: bool, |
| 77 | pub ignore_ws: bool, |
| 78 | |
| 79 | pub path: String, |
| 80 | |
| 81 | pub query: Vec<(String, String)>, |
| 82 | |
| 83 | pub downloads: Option<(String, String)>, |
| 84 | } |
| 85 | |
| 86 | impl DiffOpts { |
| 87 | |
| 88 | |
| 89 | pub fn from_cx(cx: &Cx, downloads: Option<(String, String)>) -> Self { |
| 90 | let request = uri(cx); |
| 91 | let mut view_param = None; |
| 92 | let mut ignore_ws = false; |
| 93 | let mut query = Vec::new(); |
| 94 | if let Some(raw) = request.query() { |
| 95 | for (key, value) in form_urlencoded::parse(raw.as_bytes()) { |
| 96 | match key.as_ref() { |
| 97 | "view" => view_param = Some(value.into_owned()), |
| 98 | "w" => ignore_ws = value == "1", |
| 99 | "comment" | "review_error" => {} |
| 100 | _ => query.push((key.into_owned(), value.into_owned())), |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | let split = match view_param.as_deref() { |
| 105 | Some(v @ ("split" | "unified")) => { |
| 106 | let max_age = time::Duration::seconds(365 * 24 * 60 * 60); |
| 107 | cookies(cx).add( |
| 108 | Cookie::build((DIFF_VIEW_COOKIE, v.to_owned())) |
| 109 | .path("/") |
| 110 | .secure(true) |
| 111 | .same_site(SameSite::Lax) |
| 112 | .max_age(max_age) |
| 113 | .build(), |
| 114 | ); |
| 115 | v == "split" |
| 116 | } |
| 117 | _ => cookies(cx) |
| 118 | .get(DIFF_VIEW_COOKIE) |
| 119 | .is_some_and(|c| c.value() == "split"), |
| 120 | }; |
| 121 | Self { |
| 122 | enabled: true, |
| 123 | split, |
| 124 | ignore_ws, |
| 125 | path: request.path().to_owned(), |
| 126 | query, |
| 127 | downloads, |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | |
| 132 | pub fn href(&self, split: bool, ignore_ws: bool) -> String { |
| 133 | let mut ser = form_urlencoded::Serializer::new(String::new()); |
| 134 | for (k, v) in &self.query { |
| 135 | ser.append_pair(k, v); |
| 136 | } |
| 137 | ser.append_pair("view", if split { "split" } else { "unified" }); |
| 138 | if ignore_ws { |
| 139 | ser.append_pair("w", "1"); |
| 140 | } |
| 141 | format!("{}?{}", self.path, ser.finish()) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | |
| 146 | struct Toolbar { |
| 147 | unified_href: String, |
| 148 | split_href: String, |
| 149 | ws_href: String, |
| 150 | split: bool, |
| 151 | ignore_ws: bool, |
| 152 | downloads: Option<(String, String)>, |
| 153 | } |
| 154 | |
| 155 | |
| 156 | struct InlineForm { |
| 157 | action: String, |
| 158 | cancel_href: String, |
| 159 | csrf: String, |
| 160 | file_index: String, |
| 161 | side: &'static str, |
| 162 | line: String, |
| 163 | head_oid: String, |
| 164 | has_pending: bool, |
| 165 | can_review: bool, |
| 166 | } |
| 167 | |
| 168 | |
| 169 | struct RowBelow { |
| 170 | threads: Vec<Thread>, |
| 171 | form: Option<InlineForm>, |
| 172 | ctx: ThreadCtx, |
| 173 | } |
| 174 | |
| 175 | |
| 176 | struct DiffRow { |
| 177 | class: &'static str, |
| 178 | old: String, |
| 179 | new: String, |
| 180 | code: String, |
| 181 | anchored: bool, |
| 182 | |
| 183 | anchor: Option<String>, |
| 184 | |
| 185 | plus_href: Option<String>, |
| 186 | below: Option<RowBelow>, |
| 187 | } |
| 188 | |
| 189 | |
| 190 | struct FileView { |
| 191 | anchor: String, |
| 192 | label: String, |
| 193 | status: &'static str, |
| 194 | additions: u32, |
| 195 | deletions: u32, |
| 196 | binary: bool, |
| 197 | truncated: bool, |
| 198 | has_blob: bool, |
| 199 | blob_href: String, |
| 200 | rows: Vec<DiffRow>, |
| 201 | |
| 202 | split_rows: Vec<SplitRow>, |
| 203 | } |
| 204 | |
| 205 | |
| 206 | struct SplitSide { |
| 207 | |
| 208 | class: &'static str, |
| 209 | num: String, |
| 210 | code: String, |
| 211 | anchor: Option<String>, |
| 212 | plus_href: Option<String>, |
| 213 | } |
| 214 | |
| 215 | impl SplitSide { |
| 216 | fn empty() -> Self { |
| 217 | Self { |
| 218 | class: "empty", |
| 219 | num: String::new(), |
| 220 | code: String::new(), |
| 221 | anchor: None, |
| 222 | plus_href: None, |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | fn from_row(row: DiffRow, class: &'static str, num: String) -> (Self, Option<RowBelow>) { |
| 227 | ( |
| 228 | Self { |
| 229 | class, |
| 230 | num, |
| 231 | code: row.code, |
| 232 | anchor: row.anchor, |
| 233 | plus_href: row.plus_href, |
| 234 | }, |
| 235 | row.below, |
| 236 | ) |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | |
| 241 | struct SplitRow { |
| 242 | hunk: Option<String>, |
| 243 | left: SplitSide, |
| 244 | right: SplitSide, |
| 245 | below: Option<RowBelow>, |
| 246 | } |
| 247 | |
| 248 | fn merge_below(a: Option<RowBelow>, b: Option<RowBelow>) -> Option<RowBelow> { |
| 249 | match (a, b) { |
| 250 | (Some(mut a), Some(b)) => { |
| 251 | a.threads.extend(b.threads); |
| 252 | if a.form.is_none() { |
| 253 | a.form = b.form; |
| 254 | } |
| 255 | Some(a) |
| 256 | } |
| 257 | (a, None) => a, |
| 258 | (None, b) => b, |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | |
| 263 | fn flush_pairs(out: &mut Vec<SplitRow>, dels: &mut Vec<DiffRow>, adds: &mut Vec<DiffRow>) { |
| 264 | let mut dels = std::mem::take(dels).into_iter(); |
| 265 | let mut adds = std::mem::take(adds).into_iter(); |
| 266 | loop { |
| 267 | let (d, a) = (dels.next(), adds.next()); |
| 268 | if d.is_none() && a.is_none() { |
| 269 | break; |
| 270 | } |
| 271 | let (left, left_below) = match d { |
| 272 | Some(row) => { |
| 273 | let num = row.old.clone(); |
| 274 | SplitSide::from_row(row, "del", num) |
| 275 | } |
| 276 | None => (SplitSide::empty(), None), |
| 277 | }; |
| 278 | let (right, right_below) = match a { |
| 279 | Some(row) => { |
| 280 | let num = row.new.clone(); |
| 281 | SplitSide::from_row(row, "add", num) |
| 282 | } |
| 283 | None => (SplitSide::empty(), None), |
| 284 | }; |
| 285 | out.push(SplitRow { |
| 286 | hunk: None, |
| 287 | left, |
| 288 | right, |
| 289 | below: merge_below(left_below, right_below), |
| 290 | }); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | fn split_rows(rows: Vec<DiffRow>) -> Vec<SplitRow> { |
| 295 | let mut out = Vec::new(); |
| 296 | let mut dels: Vec<DiffRow> = Vec::new(); |
| 297 | let mut adds: Vec<DiffRow> = Vec::new(); |
| 298 | for row in rows { |
| 299 | match row.class { |
| 300 | "diff-line del" => { |
| 301 | if !adds.is_empty() { |
| 302 | flush_pairs(&mut out, &mut dels, &mut adds); |
| 303 | } |
| 304 | dels.push(row); |
| 305 | } |
| 306 | "diff-line add" => adds.push(row), |
| 307 | "diff-line hunk" => { |
| 308 | flush_pairs(&mut out, &mut dels, &mut adds); |
| 309 | out.push(SplitRow { |
| 310 | hunk: Some(row.code), |
| 311 | left: SplitSide::empty(), |
| 312 | right: SplitSide::empty(), |
| 313 | below: None, |
| 314 | }); |
| 315 | } |
| 316 | _ => { |
| 317 | flush_pairs(&mut out, &mut dels, &mut adds); |
| 318 | let left = SplitSide { |
| 319 | class: "ctx", |
| 320 | num: row.old.clone(), |
| 321 | code: row.code.clone(), |
| 322 | anchor: None, |
| 323 | plus_href: None, |
| 324 | }; |
| 325 | let new = row.new.clone(); |
| 326 | let (right, below) = SplitSide::from_row(row, "ctx dup", new); |
| 327 | out.push(SplitRow { |
| 328 | hunk: None, |
| 329 | left, |
| 330 | right, |
| 331 | below, |
| 332 | }); |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | flush_pairs(&mut out, &mut dels, &mut adds); |
| 337 | out |
| 338 | } |
| 339 | |
| 340 | impl FileView { |
| 341 | |
| 342 | |
| 343 | |
| 344 | #[allow(clippy::too_many_arguments)] |
| 345 | fn new( |
| 346 | index: usize, |
| 347 | file: &FileDiff, |
| 348 | oid: &str, |
| 349 | parent: Option<&str>, |
| 350 | owner: &str, |
| 351 | repo: &str, |
| 352 | lang: Lang, |
| 353 | comments: &mut LineComments, |
| 354 | ) -> Self { |
| 355 | let label = match (&file.old_path, &file.new_path) { |
| 356 | (Some(old), Some(new)) if old != new => format!("{old} → {new}"), |
| 357 | (_, Some(new)) => new.clone(), |
| 358 | (Some(old), None) => old.clone(), |
| 359 | (None, None) => String::new(), |
| 360 | }; |
| 361 | let status = match file.status { |
| 362 | DiffStatus::Added => g::t(lang, K::StatusAdded), |
| 363 | DiffStatus::Deleted => g::t(lang, K::StatusDeleted), |
| 364 | DiffStatus::Modified => g::t(lang, K::StatusModified), |
| 365 | DiffStatus::Renamed => g::t(lang, K::StatusRenamed), |
| 366 | DiffStatus::Copied => g::t(lang, K::StatusCopied), |
| 367 | DiffStatus::TypeChanged => g::t(lang, K::StatusTypeChanged), |
| 368 | }; |
| 369 | |
| 370 | let blob_commit = match file.status { |
| 371 | DiffStatus::Deleted => parent, |
| 372 | _ => Some(oid), |
| 373 | }; |
| 374 | let blob_path = file.new_path.as_ref().or(file.old_path.as_ref()); |
| 375 | let blob_href = match (blob_commit, blob_path) { |
| 376 | (Some(commit), Some(path)) => { |
| 377 | format!("/{owner}/{repo}/blob/{commit}/{}", url_path(path)) |
| 378 | } |
| 379 | _ => String::new(), |
| 380 | }; |
| 381 | |
| 382 | let mut rows = Vec::new(); |
| 383 | for hunk in &file.hunks { |
| 384 | rows.push(DiffRow { |
| 385 | class: "diff-line hunk", |
| 386 | old: String::new(), |
| 387 | new: String::new(), |
| 388 | code: hunk.header.clone(), |
| 389 | anchored: comments.anchors, |
| 390 | anchor: None, |
| 391 | plus_href: None, |
| 392 | below: None, |
| 393 | }); |
| 394 | for line in &hunk.lines { |
| 395 | let (class, code) = match line.kind { |
| 396 | LineKind::Add => ("diff-line add", format!("+{}", line.text)), |
| 397 | LineKind::Del => ("diff-line del", format!("-{}", line.text)), |
| 398 | LineKind::Context => ("diff-line ctx", format!(" {}", line.text)), |
| 399 | LineKind::NoNewline => ("diff-line ctx", line.text.clone()), |
| 400 | }; |
| 401 | |
| 402 | let key = match line.kind { |
| 403 | LineKind::Add | LineKind::Context => line.new_no.map(|n| (Side::New, n)), |
| 404 | LineKind::Del => line.old_no.map(|n| (Side::Old, n)), |
| 405 | LineKind::NoNewline => None, |
| 406 | }; |
| 407 | let (anchor, plus_href, below) = |
| 408 | match key { |
| 409 | Some((side, number)) => { |
| 410 | let id = format!("L-{index}-{}-{number}", side.as_str()); |
| 411 | let anchor = comments.anchors.then(|| id.clone()); |
| 412 | let plus_href = comments.anchors.then(|| { |
| 413 | format!( |
| 414 | "{}?comment={index}:{}:{number}#{id}", |
| 415 | comments.files_href, |
| 416 | side.as_str() |
| 417 | ) |
| 418 | }); |
| 419 | let target = (index, side, number); |
| 420 | let threads = comments.threads.remove(&target).unwrap_or_default(); |
| 421 | let form = (comments.anchors && comments.open_form == Some(target)) |
| 422 | .then(|| InlineForm { |
| 423 | action: format!("{}/review_comments", comments.ctx.pr_href), |
| 424 | cancel_href: comments.files_href.clone(), |
| 425 | csrf: comments.ctx.csrf.clone(), |
| 426 | file_index: index.to_string(), |
| 427 | side: side.as_str(), |
| 428 | line: number.to_string(), |
| 429 | head_oid: oid.to_owned(), |
| 430 | has_pending: comments.has_pending, |
| 431 | can_review: comments.ctx.can_review, |
| 432 | }); |
| 433 | let below = (!threads.is_empty() || form.is_some()).then(|| RowBelow { |
| 434 | threads, |
| 435 | form, |
| 436 | ctx: comments.ctx.clone(), |
| 437 | }); |
| 438 | (anchor, plus_href, below) |
| 439 | } |
| 440 | None => (None, None, None), |
| 441 | }; |
| 442 | rows.push(DiffRow { |
| 443 | class, |
| 444 | old: line.old_no.map(|n| n.to_string()).unwrap_or_default(), |
| 445 | new: line.new_no.map(|n| n.to_string()).unwrap_or_default(), |
| 446 | code, |
| 447 | anchored: comments.anchors, |
| 448 | anchor, |
| 449 | plus_href, |
| 450 | below, |
| 451 | }); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | Self { |
| 456 | anchor: format!("file-{}", index + 1), |
| 457 | label, |
| 458 | status, |
| 459 | additions: file.additions, |
| 460 | deletions: file.deletions, |
| 461 | binary: file.is_binary, |
| 462 | truncated: file.truncated, |
| 463 | has_blob: !blob_href.is_empty(), |
| 464 | blob_href, |
| 465 | rows, |
| 466 | split_rows: Vec::new(), |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | |
| 472 | struct NavItem { |
| 473 | anchor_href: String, |
| 474 | label: String, |
| 475 | additions: u32, |
| 476 | deletions: u32, |
| 477 | } |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | #[allow(clippy::too_many_arguments)] |
| 485 | #[component] |
| 486 | pub async fn diff_section( |
| 487 | diff: &CommitDiff, |
| 488 | owner: &str, |
| 489 | repo: &str, |
| 490 | oid: &str, |
| 491 | parent: Option<&str>, |
| 492 | subject: &str, |
| 493 | #[default] lang: DiffLang, |
| 494 | #[default] comments: LineComments, |
| 495 | #[default] opts: DiffOpts, |
| 496 | ) -> Result<impl View> { |
| 497 | let lang = lang.0; |
| 498 | let mut comments = comments; |
| 499 | let anchored = comments.anchors; |
| 500 | let split = opts.split; |
| 501 | let mut files: Vec<FileView> = diff |
| 502 | .files |
| 503 | .iter() |
| 504 | .enumerate() |
| 505 | .map(|(i, file)| FileView::new(i, file, oid, parent, owner, repo, lang, &mut comments)) |
| 506 | .collect(); |
| 507 | if split { |
| 508 | for file in &mut files { |
| 509 | file.split_rows = split_rows(std::mem::take(&mut file.rows)); |
| 510 | } |
| 511 | } |
| 512 | let toolbar = opts.enabled.then(|| Toolbar { |
| 513 | unified_href: opts.href(false, opts.ignore_ws), |
| 514 | split_href: opts.href(true, opts.ignore_ws), |
| 515 | ws_href: opts.href(opts.split, !opts.ignore_ws), |
| 516 | split: opts.split, |
| 517 | ignore_ws: opts.ignore_ws, |
| 518 | downloads: opts.downloads.clone(), |
| 519 | }); |
| 520 | let nav: Vec<NavItem> = files |
| 521 | .iter() |
| 522 | .map(|f| NavItem { |
| 523 | anchor_href: format!("#{}", f.anchor), |
| 524 | label: f.label.clone(), |
| 525 | additions: f.additions, |
| 526 | deletions: f.deletions, |
| 527 | }) |
| 528 | .collect(); |
| 529 | let total_add: u32 = files.iter().map(|f| f.additions).sum(); |
| 530 | let total_del: u32 = files.iter().map(|f| f.deletions).sum(); |
| 531 | let file_count = files.len(); |
| 532 | let no_files = files.is_empty(); |
| 533 | let files_label = g::changed_files(lang, file_count); |
| 534 | let truncated_note = g::diff_truncated(lang, MAX_FILES, subject); |
| 535 | let truncated_files = diff.truncated_files; |
| 536 | let add_label = format!("+{total_add}"); |
| 537 | let del_label = format!("-{total_del}"); |
| 538 | |
| 539 | Ok(view! { |
| 540 | <section class="diff-section"> |
| 541 | <h2>(files_label.as_str()) " " <span class="diff-stats"><span class="add">(add_label.as_str())</span> " " <span class="del">(del_label.as_str())</span></span></h2> |
| 542 | if let Some(bar) = toolbar { |
| 543 | diff_toolbar(bar: bar, lang: lang) |
| 544 | } |
| 545 | if truncated_files { |
| 546 | <p class="muted">(truncated_note.as_str())</p> |
| 547 | } |
| 548 | if no_files { |
| 549 | <p class="muted">(g::t(lang, K::NoFileChanges))</p> |
| 550 | } else { |
| 551 | <ul class="diff-index"> |
| 552 | for item in nav.iter() { |
| 553 | <li> |
| 554 | <a href=(item.anchor_href.as_str())>(item.label.as_str())</a> |
| 555 | " " |
| 556 | <span class="diff-stats"><span class="add">(format!("+{}", item.additions))</span> " " <span class="del">(format!("-{}", item.deletions))</span></span> |
| 557 | </li> |
| 558 | } |
| 559 | </ul> |
| 560 | for file in files.into_iter() { |
| 561 | diff_file(file: file, lang: lang, split: split, anchored: anchored) |
| 562 | } |
| 563 | } |
| 564 | </section> |
| 565 | }) |
| 566 | } |
| 567 | |
| 568 | |
| 569 | #[component] |
| 570 | async fn diff_toolbar(bar: Toolbar, lang: Lang) -> Result<impl View> { |
| 571 | let unified_on = if bar.split { "false" } else { "true" }; |
| 572 | let split_on = if bar.split { "true" } else { "false" }; |
| 573 | let ws_on = if bar.ignore_ws { "true" } else { "false" }; |
| 574 | Ok(view! { |
| 575 | <div class="diff-toolbar"> |
| 576 | <div class="seg" role="group" aria-label=(b::t(lang, b::K::DiffView))> |
| 577 | <a class="seg-item" href=(bar.unified_href.as_str()) aria-pressed=(unified_on)>(b::t(lang, b::K::Unified))</a> |
| 578 | <a class="seg-item" href=(bar.split_href.as_str()) aria-pressed=(split_on)>(b::t(lang, b::K::Split))</a> |
| 579 | </div> |
| 580 | <a class="seg-item toggle" href=(bar.ws_href.as_str()) aria-pressed=(ws_on)>(b::t(lang, b::K::IgnoreWhitespace))</a> |
| 581 | if let Some((patch, diff)) = bar.downloads { |
| 582 | <span class="diff-downloads"> |
| 583 | <a href=(patch.as_str()) title=(b::t(lang, b::K::PatchTitle))>(b::t(lang, b::K::PatchLink))</a> |
| 584 | <a href=(diff.as_str()) title=(b::t(lang, b::K::DiffTitle))>(b::t(lang, b::K::DiffLink))</a> |
| 585 | </span> |
| 586 | } |
| 587 | </div> |
| 588 | }) |
| 589 | } |
| 590 | |
| 591 | #[component] |
| 592 | async fn diff_file(file: FileView, lang: Lang, split: bool, anchored: bool) -> Result<impl View> { |
| 593 | let add_label = format!("+{}", file.additions); |
| 594 | let del_label = format!("-{}", file.deletions); |
| 595 | let rows = file.rows; |
| 596 | let pairs = file.split_rows; |
| 597 | let has_rows = !rows.is_empty() || !pairs.is_empty(); |
| 598 | let table_class = match (split, anchored) { |
| 599 | (true, true) => "diff-table diff-split anchored", |
| 600 | (true, false) => "diff-table diff-split", |
| 601 | _ => "diff-table", |
| 602 | }; |
| 603 | Ok(view! { |
| 604 | <section class="diff-file" id=(file.anchor.as_str()) data-keep-anchor=(file.label.as_str())> |
| 605 | <header class="diff-file-header"> |
| 606 | <code class="diff-path">(file.label.as_str())</code> |
| 607 | <span class="diff-file-meta"> |
| 608 | <span class="muted small">(file.status)</span> |
| 609 | <span class="diff-stats"><span class="add">(add_label.as_str())</span> " " <span class="del">(del_label.as_str())</span></span> |
| 610 | </span> |
| 611 | </header> |
| 612 | if file.binary { |
| 613 | <p class="muted diff-collapsed">(g::t(lang, K::BinaryFileChanged))</p> |
| 614 | } else { |
| 615 | if file.truncated { |
| 616 | if file.has_blob { |
| 617 | <p class="muted diff-collapsed">(g::t(lang, K::DiffTooLargePrefix)) <a href=(file.blob_href.as_str())>(g::t(lang, K::ViewFile))</a></p> |
| 618 | } else { |
| 619 | <p class="muted diff-collapsed">(g::t(lang, K::DiffTooLarge))</p> |
| 620 | } |
| 621 | } |
| 622 | if has_rows { |
| 623 | <div class="diff-file-body"> |
| 624 | <table class=(table_class)> |
| 625 | for row in rows.into_iter() { |
| 626 | diff_line(row: row, lang: lang) |
| 627 | } |
| 628 | for row in pairs.into_iter() { |
| 629 | diff_split_line(row: row, lang: lang, anchored: anchored) |
| 630 | } |
| 631 | </table> |
| 632 | </div> |
| 633 | } |
| 634 | } |
| 635 | </section> |
| 636 | }) |
| 637 | } |
| 638 | |
| 639 | |
| 640 | |
| 641 | #[component] |
| 642 | async fn diff_split_line(row: SplitRow, lang: Lang, anchored: bool) -> Result<impl View> { |
| 643 | let SplitRow { |
| 644 | hunk, |
| 645 | left, |
| 646 | right, |
| 647 | below, |
| 648 | } = row; |
| 649 | let (threads, form, ctx) = match below { |
| 650 | Some(below) => (below.threads, below.form, Some(below.ctx)), |
| 651 | None => (Vec::new(), None, None), |
| 652 | }; |
| 653 | let span = if anchored { "6" } else { "4" }; |
| 654 | let is_hunk = hunk.is_some(); |
| 655 | let hunk_text = hunk.unwrap_or_default(); |
| 656 | let left_plus = format!("diff-plus side-{}", left.class); |
| 657 | let left_ln = format!("diff-ln side-{}", left.class); |
| 658 | let left_code = format!("diff-code side-{}", left.class); |
| 659 | let right_plus = format!("diff-plus side-{}", right.class); |
| 660 | let right_ln = format!("diff-ln side-{}", right.class); |
| 661 | let right_code = format!("diff-code side-{}", right.class); |
| 662 | Ok(view! { |
| 663 | <tbody> |
| 664 | if is_hunk { |
| 665 | <tr class="diff-line hunk"> |
| 666 | <td colspan=(span) class="diff-code">(hunk_text.as_str())</td> |
| 667 | </tr> |
| 668 | } else { |
| 669 | <tr class="diff-split-row"> |
| 670 | if anchored { |
| 671 | <td class=(left_plus.as_str())> |
| 672 | if let Some(href) = left.plus_href { |
| 673 | <a href=(href.as_str()) title=(g::t(lang, K::CommentOnLine)) aria-label=(g::t(lang, K::CommentOnLine))>"+"</a> |
| 674 | } |
| 675 | </td> |
| 676 | } |
| 677 | <td class=(left_ln.as_str())>(left.num.as_str())</td> |
| 678 | <td class=(left_code.as_str()) id=(left.anchor.as_deref())>(left.code.as_str())</td> |
| 679 | if anchored { |
| 680 | <td class=(right_plus.as_str())> |
| 681 | if let Some(href) = right.plus_href { |
| 682 | <a href=(href.as_str()) title=(g::t(lang, K::CommentOnLine)) aria-label=(g::t(lang, K::CommentOnLine))>"+"</a> |
| 683 | } |
| 684 | </td> |
| 685 | } |
| 686 | <td class=(right_ln.as_str())>(right.num.as_str())</td> |
| 687 | <td class=(right_code.as_str()) id=(right.anchor.as_deref())>(right.code.as_str())</td> |
| 688 | </tr> |
| 689 | } |
| 690 | if let Some(ctx) = ctx { |
| 691 | <tr class="review-row"> |
| 692 | <td colspan=(span)> |
| 693 | <div class="review-threads"> |
| 694 | for thread in threads.into_iter() { |
| 695 | review_thread_in(lang: lang, thread: thread, ctx: ctx.clone(), show_hunk: false, return_to: "files") |
| 696 | } |
| 697 | if let Some(form) = form { |
| 698 | inline_comment_form(form: form, lang: lang) |
| 699 | } |
| 700 | </div> |
| 701 | </td> |
| 702 | </tr> |
| 703 | } |
| 704 | </tbody> |
| 705 | }) |
| 706 | } |
| 707 | |
| 708 | |
| 709 | |
| 710 | #[component] |
| 711 | async fn diff_line(row: DiffRow, lang: Lang) -> Result<impl View> { |
| 712 | let DiffRow { |
| 713 | class, |
| 714 | old, |
| 715 | new, |
| 716 | code, |
| 717 | anchored, |
| 718 | anchor, |
| 719 | plus_href, |
| 720 | below, |
| 721 | } = row; |
| 722 | let (threads, form, ctx) = match below { |
| 723 | Some(below) => (below.threads, below.form, Some(below.ctx)), |
| 724 | None => (Vec::new(), None, None), |
| 725 | }; |
| 726 | let span = if anchored { "4" } else { "3" }; |
| 727 | Ok(view! { |
| 728 | <tbody> |
| 729 | <tr class=(class) id=(anchor.as_deref())> |
| 730 | if anchored { |
| 731 | <td class="diff-plus"> |
| 732 | if let Some(href) = plus_href { |
| 733 | <a href=(href.as_str()) title=(g::t(lang, K::CommentOnLine)) aria-label=(g::t(lang, K::CommentOnLine))>"+"</a> |
| 734 | } |
| 735 | </td> |
| 736 | } |
| 737 | <td class="diff-ln">(old.as_str())</td> |
| 738 | <td class="diff-ln">(new.as_str())</td> |
| 739 | <td class="diff-code">(code.as_str())</td> |
| 740 | </tr> |
| 741 | if let Some(ctx) = ctx { |
| 742 | <tr class="review-row"> |
| 743 | <td colspan=(span)> |
| 744 | <div class="review-threads"> |
| 745 | for thread in threads.into_iter() { |
| 746 | review_thread_in(lang: lang, thread: thread, ctx: ctx.clone(), show_hunk: false, return_to: "files") |
| 747 | } |
| 748 | if let Some(form) = form { |
| 749 | inline_comment_form(form: form, lang: lang) |
| 750 | } |
| 751 | </div> |
| 752 | </td> |
| 753 | </tr> |
| 754 | } |
| 755 | </tbody> |
| 756 | }) |
| 757 | } |
| 758 | |
| 759 | |
| 760 | |
| 761 | #[component] |
| 762 | async fn inline_comment_form(form: InlineForm, lang: Lang) -> Result<impl View> { |
| 763 | let InlineForm { |
| 764 | action, |
| 765 | cancel_href, |
| 766 | csrf, |
| 767 | file_index, |
| 768 | side, |
| 769 | line, |
| 770 | head_oid, |
| 771 | has_pending, |
| 772 | can_review, |
| 773 | } = form; |
| 774 | let review_label = if has_pending { |
| 775 | g::t(lang, K::AddReviewComment) |
| 776 | } else { |
| 777 | g::t(lang, K::StartReview) |
| 778 | }; |
| 779 | Ok(view! { |
| 780 | <form method="post" action=(action.as_str()) class="review-inline-form stack"> |
| 781 | <input type="hidden" name="csrf" value=(csrf.as_str())> |
| 782 | <input type="hidden" name="file_index" value=(file_index.as_str())> |
| 783 | <input type="hidden" name="side" value=(side)> |
| 784 | <input type="hidden" name="line" value=(line.as_str())> |
| 785 | <input type="hidden" name="head_oid" value=(head_oid.as_str())> |
| 786 | <label> |
| 787 | <span>(g::t(lang, K::CommentLabel))</span> |
| 788 | <textarea name="body" rows="4"></textarea> |
| 789 | </label> |
| 790 | <div class="issue-actions"> |
| 791 | <button type="submit" name="action" value="single">(g::t(lang, K::AddSingleComment))</button> |
| 792 | if can_review { |
| 793 | <button type="submit" name="action" value="review">(review_label)</button> |
| 794 | } |
| 795 | <a href=(cancel_href.as_str())>(g::t(lang, K::CancelLink))</a> |
| 796 | </div> |
| 797 | </form> |
| 798 | }) |
| 799 | } |