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