From db3d53f2aa3afe0f0bce0d503c03aa80d6d40690 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Thu, 26 Nov 2026 21:11:05 +0700 Subject: [PATCH] chore: rework pull request detail into a split layout --- CHANGELOG.md | 5 + crates/signed_app/src/issues/detail.rs | 52 +- crates/signed_app/src/issues/helpers.rs | 14 +- crates/signed_app/src/pull_requests/detail.rs | 856 +++++++++--------- crates/signed_app/src/pull_requests/mod.rs | 36 +- crates/signed_app/src/repo/actions.rs | 25 +- crates/signed_app/src/repo/commit_diff.rs | 87 +- crates/signed_git/src/patch.rs | 62 +- crates/signed_git/src/tests.rs | 5 + crates/ui/src/icons/assets/message.svg | 3 + crates/ui/src/icons/mod.rs | 2 + 11 files changed, 596 insertions(+), 551 deletions(-) create mode 100644 crates/ui/src/icons/assets/message.svg diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fcc448..f25e6ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,11 @@ ### Changed +- Show a pull request's description, commits and changes in one scrollable main panel, and its discussion with a pinned comment form in the split pane +- Show the repository owner's avatar and the pull request title in a pull request's tab +- Show each pull request commit's id, title and changed line counts on one row +- Show who opened a pull request, when, and its target and source branches under the title +- Show the commits and discussions titles with an icon and a count badge - Opening a repository no longer runs a forced checkout of its worktree - Deleting a repository now waits until its deletion events are accepted by a relay - Keep the Explore list at its scroll position across background refreshes, a new search or filter still starts at the top diff --git a/crates/signed_app/src/issues/detail.rs b/crates/signed_app/src/issues/detail.rs index 76b99f0..940415c 100644 --- a/crates/signed_app/src/issues/detail.rs +++ b/crates/signed_app/src/issues/detail.rs @@ -41,6 +41,32 @@ pub struct IssueDetailView { } impl IssueDetailView { + pub fn new(store: Entity, issue_id: EventId, cx: &mut Context) -> Self { + let comment_input = InputState::builder().multiline().build(cx); + let directory = ProfileDirectory::new(cx); + let weak = cx.entity().downgrade(); + + let subscription = cx.observe(&store, |this, _store, cx| { + this.track_participants(cx); + cx.notify(); + }); + + cx.defer(move |cx| { + log_dropped(weak.update(cx, |this, cx| { + this.track_participants(cx); + })); + }); + + Self { + store, + issue_id, + comment_input, + directory, + scroll_handle: ScrollHandle::new(), + _subscription: subscription, + } + } + /// Opens the issue's detail as the main panel and its sidebar in the split pane. pub fn open( workspace: &WeakEntity, @@ -83,32 +109,6 @@ impl IssueDetailView { })); } - pub fn new(store: Entity, issue_id: EventId, cx: &mut Context) -> Self { - let comment_input = InputState::builder().multiline().build(cx); - let directory = ProfileDirectory::new(cx); - let weak = cx.entity().downgrade(); - - let subscription = cx.observe(&store, |this, _store, cx| { - this.track_participants(cx); - cx.notify(); - }); - - cx.defer(move |cx| { - log_dropped(weak.update(cx, |this, cx| { - this.track_participants(cx); - })); - }); - - Self { - store, - issue_id, - comment_input, - directory, - scroll_handle: ScrollHandle::new(), - _subscription: subscription, - } - } - /// Track the issue author, everyone who commented and the repository owner. fn track_participants(&mut self, cx: &mut Context) { let store = self.store.read(cx); diff --git a/crates/signed_app/src/issues/helpers.rs b/crates/signed_app/src/issues/helpers.rs index 70641dc..f5ab0a2 100644 --- a/crates/signed_app/src/issues/helpers.rs +++ b/crates/signed_app/src/issues/helpers.rs @@ -6,6 +6,7 @@ use nostr::prelude::{Event, EventId}; use signed_core::RepoStatus; use signed_state::RepoStore; use ui::avatar::Avatar; +use ui::badge::Badge; use ui::button::Button; use ui::common::{Sizable as _, StyledExt as _, h_flex, v_flex}; use ui::icons::{Icon, IconRole}; @@ -82,11 +83,20 @@ pub(crate) fn comments_section( ) -> AnyElement { let store = store.read(cx); let comments: Vec<&Event> = store.comments_of(&root).collect(); - let title = SharedString::from(format!("Discussions {}", comments.len())); + let count = comments.len(); v_flex() .gap_4() - .child(div().text_xs().font_semibold().child(title)) + .child( + h_flex() + .gap_2() + .child(Icon::named(IconRole::Message)) + .child(div().text_sm().font_semibold().child("Discussions")) + .child(Badge::new( + format!("discussion-count:{root}"), + count.to_string(), + )), + ) .children(comments.iter().map(|comment| { let profile = directory.read(cx).profile(&comment.pubkey, cx); let author = profile.name(); diff --git a/crates/signed_app/src/pull_requests/detail.rs b/crates/signed_app/src/pull_requests/detail.rs index 760afc6..707a5e5 100644 --- a/crates/signed_app/src/pull_requests/detail.rs +++ b/crates/signed_app/src/pull_requests/detail.rs @@ -4,40 +4,39 @@ use std::sync::Arc; use gpui::prelude::FluentBuilder as _; use gpui::{ AnyElement, App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement, - ParentElement as _, Render, SharedString, StatefulInteractiveElement as _, StyleRefinement, - Styled as _, Subscription, UniformListScrollHandle, WeakEntity, Window, div, px, uniform_list, + ParentElement as _, Pixels, Render, ScrollHandle, SharedString, + StatefulInteractiveElement as _, Styled as _, Subscription, WeakEntity, Window, div, px, }; use metadata::ProfileDirectory; use nostr::prelude::{EventId, Kind, Url}; use signed_core::{GitEvent, PullRequest, RepoAddr}; use signed_git::{FileCommit, PatchParser}; -use signed_state::{Backend, BackendEvent, Mirrors, RepoStore}; +use signed_state::{Backend, Mirrors, RepoStore}; use ui::avatar::Avatar; -use ui::badge::{Badge, BadgeVariant}; +use ui::badge::{Badge, BadgeVariant, GroupedBadge}; use ui::button::Button; -use ui::clipboard::ClipboardButton; use ui::common::{Sizable as _, StyledExt as _, h_flex, v_flex}; use ui::empty_state::EmptyState; use ui::icons::{Icon, IconRole}; use ui::input::InputState; -use ui::layout::panel::{Panel, PanelSlot}; +use ui::layout::panel::{Panel, PanelSlot, SplitSide}; use ui::layout::root::RootContext as _; use ui::scroll_area::ScrollArea; -use ui::scrollbar::Scrollbar; use ui::spinner::Spinner; -use ui::tabs::{Tab, Tabs}; use ui::theme::ActiveTheme as _; -use utils::{relative_time, relative_time_secs}; +use utils::relative_time; use crate::app::AppRoot; use crate::issues::helpers::{comment_form, comments_section}; use crate::pull_requests::dialogs::update::UpdatePrDialog; use crate::repo::commit_diff::{CommitDiffView, DiffPane}; use crate::repo::helpers::short_id; -use crate::shared::log_dropped; use crate::shared::widgets::status_badge; +use crate::shared::{log_dropped, track_event_participants}; const NOT_FOUND: &str = "Pull request not found"; +const SIDEBAR_ID: &str = "Discussion"; +const SIDEBAR_WIDTH: Pixels = px(320.); #[derive(Clone, PartialEq, Eq)] struct PrBinding { @@ -50,65 +49,50 @@ struct PrBinding { has_patch_link: bool, } +struct CommitRow { + commit: FileCommit, + insertions: usize, + deletions: usize, +} + pub struct PullRequestDetailView { workspace: WeakEntity, store: Entity, pr_id: EventId, - comment_input: Entity, + directory: Entity, worktree: Option, description: SharedString, current_commit: Option, - commits: Arc>, + commits: Arc>, loading: bool, error: Option, bound: Option, load_generation: u64, - active_tab: usize, pane: Entity, - commit_scroll_handle: UniformListScrollHandle, + scroll_handle: ScrollHandle, tasks: Vec>>, - directory: Entity, - _subscriptions: Vec, } impl PullRequestDetailView { - pub fn new( + fn new( workspace: WeakEntity, store: Entity, pr_id: EventId, - _window: &mut Window, + directory: Entity, + window: &mut Window, cx: &mut Context, ) -> Self { - let comment_input = InputState::builder().multiline().build(cx); - let directory = ProfileDirectory::new(cx); - let pane = cx.new(DiffPane::new); - let weak = cx.entity().downgrade(); - let backend = Backend::global(cx); - let mut subscriptions = vec![]; - - subscriptions.push(cx.observe(&store, |this, _store, cx| { + cx.defer_in(window, move |this, _window, cx| { this.sync(cx); - })); - - subscriptions.push(cx.subscribe(&backend, |_, _, event, cx| { - if matches!(event, BackendEvent::SignerChanged) { - cx.notify(); - } - })); - - cx.defer(move |cx| { - log_dropped(weak.update(cx, |this, cx| { - this.sync(cx); - })); }); Self { workspace, store, pr_id, - comment_input, + directory, worktree: None, description: SharedString::default(), current_commit: None, @@ -117,38 +101,65 @@ impl PullRequestDetailView { error: None, bound: None, load_generation: 0, - active_tab: 0, pane, - commit_scroll_handle: UniformListScrollHandle::new(), + scroll_handle: ScrollHandle::new(), tasks: Vec::new(), - directory, - _subscriptions: subscriptions, } } - fn track_participants(&mut self, cx: &mut Context) { - let store = self.store.read(cx); - let author = store - .pull_requests - .iter() - .find(|pr| pr.id == self.pr_id && pr.kind == Kind::GitPullRequest) - .map(|root| root.pubkey); - let owner = store.announcement.as_ref().map(|a| a.owner); - let comments: Vec<_> = store - .comments_of(&self.pr_id) - .map(|comment| comment.pubkey) - .collect(); - crate::shared::track_event_participants( - author, - owner, - comments.into_iter(), - &self.directory, - cx, - ); + /// Opens the pull request in the main panel and its discussion in the split pane. + pub fn open( + workspace: &WeakEntity, + store: Entity, + pr_id: EventId, + window: &mut Window, + cx: &mut App, + ) { + let id = SharedString::from(format!("pr:{pr_id}")); + let workspace_for_build = workspace.clone(); + let detail_store = store.clone(); + let directory = ProfileDirectory::new(cx); + + log_dropped(workspace.update(cx, |root, cx| { + root.panels().update(cx, |panels, cx| { + panels.open( + PanelSlot::Main, + id, + { + let directory = directory.clone(); + move |window, cx| { + cx.new(|cx| { + Self::new( + workspace_for_build.clone(), + detail_store.clone(), + pr_id, + directory.clone(), + window, + cx, + ) + }) + } + }, + window, + cx, + ); + + let sidebar = cx.new(|cx| { + PullRequestSidebarView::new(store.clone(), pr_id, directory.clone(), cx) + }); + + panels.insert_split_at( + SIDEBAR_ID, + SIDEBAR_WIDTH, + SplitSide::Trailing, + Arc::new(sidebar), + cx, + ); + }) + })); } fn sync(&mut self, cx: &mut Context) { - self.track_participants(cx); let loaded = self.store.read(cx).loaded; let binding = { let store = self.store.read(cx); @@ -162,13 +173,16 @@ impl PullRequestDetailView { let tip = update .and_then(GitEvent::current_commit) .or_else(|| root.current_commit()); + let base = update .and_then(GitEvent::merge_base) .or_else(|| root.merge_base()); + let clone_urls = root .clone_urls() .or_else(|| store.announcement.as_ref().map(|a| a.clone.clone())) .unwrap_or_default(); + PrBinding { description: root.content.clone(), patch: PullRequest::new(root).patch(store.patches.iter()), @@ -198,16 +212,11 @@ impl PullRequestDetailView { if self.bound.as_ref() == Some(&binding) { return; } + self.bound = Some(binding.clone()); self.load_diff(binding, cx); } - /// Reaps finished tasks, then queues another. - fn push_task(&mut self, task: gpui::Task>) { - self.tasks.retain(|task| !task.is_ready()); - self.tasks.push(task); - } - fn load_diff(&mut self, binding: PrBinding, cx: &mut Context) { self.loading = true; self.error = None; @@ -217,6 +226,7 @@ impl PullRequestDetailView { self.load_generation = self.load_generation.wrapping_add(1); let generation = self.load_generation; + let PrBinding { patch, tip, @@ -234,10 +244,20 @@ impl PullRequestDetailView { async move { PatchParser::patch_diffs(&patch) } }) .await; + let nostr_commits = cx .background_spawn({ let patch = patch.clone(); - async move { PatchParser::patch_commits(&patch) } + async move { + PatchParser::patch_commits_with_stats(&patch) + .into_iter() + .map(|(commit, insertions, deletions)| CommitRow { + commit, + insertions, + deletions, + }) + .collect::>() + } }) .await; @@ -257,12 +277,15 @@ impl PullRequestDetailView { cx.background_spawn(async move { let repo = Mirrors::ensure(&addr, &clone_urls)?; let gix_repo = repo.inner(); + let workdir = gix_repo .workdir() .ok_or_else(|| anyhow::anyhow!("repository has no worktree"))? .to_path_buf(); + let tip = tip.ok_or_else(|| anyhow::anyhow!("pull request has no tip commit"))?; + let base = match base { Some(base) => base, None => { @@ -273,8 +296,33 @@ impl PullRequestDetailView { gix_repo.merge_base(tip_id, head)?.to_string() } }; + let diff = repo.range_diff(&base, &tip)?; - let commits = repo.commit_range(&base, &tip)?; + let commits = repo + .commit_range(&base, &tip)? + .into_iter() + .map(|commit| { + let (insertions, deletions) = match repo.commit_diff(&commit.id) { + Ok(diff) => ( + diff.files.iter().map(|file| file.insertions).sum(), + diff.files.iter().map(|file| file.deletions).sum(), + ), + Err(error) => { + log::warn!( + "failed to read stats for commit {}: {error}", + commit.id + ); + (0, 0) + } + }; + CommitRow { + insertions, + deletions, + commit, + } + }) + .collect::>(); + Ok::<_, anyhow::Error>((diff, commits, workdir)) }) .await, @@ -294,15 +342,19 @@ impl PullRequestDetailView { this.loading = false; this.worktree = worktree; this.commits = Arc::new(commits); + match diff { Ok(diff) => this.pane.update(cx, |pane, cx| pane.set_diff(diff, cx)), Err(error) => this.error = Some(error.to_string().into()), } + cx.notify(); })?; + Ok(()) }); - self.push_task(task); + + self.tasks.push(task); } fn open_commit_diff( @@ -343,329 +395,111 @@ impl PullRequestDetailView { })); } - fn render_tabs(&self, cx: &mut Context) -> AnyElement { - let files = self.pane.read(cx).diff().map(|diff| diff.files.len()); - let commits = (!self.commits.is_empty()).then_some(self.commits.len()); - let weak = cx.entity().downgrade(); - let files_label = match files { - Some(count) => format!("Files ({count})"), - None => "Files".to_owned(), - }; - let commits_label = match commits { - Some(count) => format!("Commits ({count})"), - None => "Commits".to_owned(), + fn render_body(&self, cx: &mut Context) -> AnyElement { + if self.bound.is_none() { + return div().into_any_element(); + } + + let description = if self.description.is_empty() { + SharedString::from("No description provided.") + } else { + self.description.clone() }; - Tabs::new("pr-tabs") - .selected(self.active_tab) - .tabs(move |_, _| { - vec![ - Tab::new("discussion", "Discussion"), - Tab::new("files", files_label.clone()), - Tab::new("commits", commits_label.clone()), - ] - }) - .on_change(move |index, _, cx| { - log_dropped(weak.update(cx, |this, cx| { - this.active_tab = index; - cx.notify(); - })); - }) - .into_any_element() - } - - fn render_discussion(&mut self, cx: &mut Context) -> AnyElement { - if self.loading { - return Spinner::centered("pr-loading"); - } - if let Some(error) = self.error.clone() { - return EmptyState::centered(error); - } - let (author, picture, age, root_id, pubkey) = { - let store = self.store.read(cx); - let Some(root) = store - .pull_requests - .iter() - .find(|pr| pr.id == self.pr_id && pr.kind == Kind::GitPullRequest) - else { - return EmptyState::centered(NOT_FOUND); - }; - let profile = self.directory.read(cx).profile(&root.pubkey, cx); - ( - profile.name(), - profile.picture(), - relative_time(root.created_at), - root.id, - root.pubkey, - ) - }; - - let store = self.store.read(cx); - - let details = store - .pull_requests - .iter() - .find(|pr| pr.id == root_id) - .map(|root| { - let mut participants = vec![root.pubkey]; - participants.extend(store.comments_of(&root.id).map(|comment| comment.pubkey)); - participants.sort_by_key(|key| key.to_hex()); - participants.dedup(); - - let labels: Vec = root.tags.hashtags().map(|tag| tag.to_string()).collect(); - - v_flex() - .gap_4() - .child( - v_flex() - .gap_2() - .child( - div() - .text_xs() - .font_medium() - .text_color(cx.theme().colors.muted_foreground) - .child("Participants"), - ) - .children(participants.iter().map(|pubkey| { - let profile = self.directory.read(cx).profile(pubkey, cx); - let name = profile.name(); - - h_flex() - .gap_1() - .child( - Avatar::new(*pubkey) - .name(name.clone()) - .picture(profile.picture()) - .small(), - ) - .child( - div() - .flex_1() - .min_w_0() - .whitespace_nowrap() - .text_sm() - .text_ellipsis() - .child(name), - ) - .into_any_element() - })), - ) - .child( - v_flex() - .gap_2() - .child( - div() - .text_xs() - .font_medium() - .text_color(cx.theme().colors.muted_foreground) - .child("Labels"), - ) - .map(|this| { - if labels.is_empty() { - this.child( - div() - .text_sm() - .text_color(cx.theme().colors.muted_foreground) - .child("None yet."), - ) - } else { - this.child(v_flex().gap_1().items_start().children( - labels.iter().map(|label| { - Badge::new( - SharedString::from(format!("label:{label}")), - label.clone(), - ) - .variant(BadgeVariant::Outline) - .small() - .flex_none() - }), - )) - } - }), - ) - .into_any_element() - }) - .unwrap_or_else(|| div().into_any_element()); - - h_flex() - .size_full() - .min_h_0() - .child( - ScrollArea::new("pr-discussion-scroll") - .flex_1() - .min_w_0() - .child( - v_flex() - .p_4() - .gap_6() - .child( - v_flex() - .gap_4() - .child( - h_flex() - .gap_2() - .text_sm() - .child( - h_flex() - .gap_1() - .child( - Avatar::new(pubkey) - .name(author.clone()) - .picture(picture) - .small(), - ) - .child(author), - ) - .child(SharedString::from("commented")) - .child( - div() - .text_color(cx.theme().colors.muted_foreground) - .child(SharedString::from(age)), - ), - ) - .when(!self.description.is_empty(), |this| { - this.child(div().text_sm().child(self.description.clone())) - }), - ) - .child(comments_section(&self.store, root_id, &self.directory, cx)) - .child(comment_form( - &self.store, - root_id, - &store.pull_requests, - &self.comment_input, - "pr-comment", - cx, - )), - ), - ) - .child( - v_flex() - .w(px(240.)) - .h_full() - .flex_none() - .px_4() - .pt_4() - .border_l_1() - .border_color(cx.theme().colors.border) - .child(details), - ) - .into_any_element() - } - - fn render_files(&self, _cx: &mut Context) -> AnyElement { - if self.loading { - return Spinner::centered("pr-loading"); - } - - if let Some(error) = self.error.clone() { - return EmptyState::centered(error); - } - - self.pane - .clone() - .cached(StyleRefinement::default().size_full().min_h_0()) + div() + .flex_none() + .border_b_1() + .border_color(cx.theme().colors.border) + .p_2() + .text_sm() + .child(description) .into_any_element() } fn render_commits(&self, cx: &mut Context) -> AnyElement { - if self.loading { - return Spinner::centered("pr-loading"); - } - if let Some(error) = self.error.clone() { - return EmptyState::centered(error); - } - if self.commits.is_empty() { - return EmptyState::centered("No commits found"); - } - let commits = Arc::clone(&self.commits); + let count = self.commits.len(); let worktree = self.worktree.clone(); let view = cx.entity().downgrade(); - let scroll_handle = self.commit_scroll_handle.clone(); - v_flex() - .relative() - .flex_1() - .min_h_0() + .w_full() + .flex_none() .child( - uniform_list("pr-commits", commits.len(), move |range, _, cx| { - range - .map(|ix| { - let Some(commit) = commits.get(ix) else { - return div().into_any_element(); - }; - let row_id = commit.id.clone(); - let worktree = worktree.clone(); - let view = view.clone(); - - h_flex() - .id(commit.id.clone()) - .px_4() - .h_16() - .w_full() - .gap_3() - .border_b_1() - .border_color(cx.theme().colors.border) - .hover(|this| this.bg(cx.theme().colors.muted_hover)) - .child( - v_flex() - .flex_1() - .min_w_0() - .gap_1() - .justify_center() - .child( - h_flex() - .gap_2() - .overflow_hidden() - .child( - div() - .font_family(cx.theme().mono_font) - .text_sm() - .text_color( - cx.theme().colors.muted_foreground, - ) - .child(short_id(&commit.id)), - ) - .child( - div() - .flex_1() - .min_w_0() - .text_sm() - .text_ellipsis() - .whitespace_nowrap() - .child(commit.summary.clone()), - ), - ) - .child( - h_flex() - .gap_2() - .text_xs() - .text_color(cx.theme().colors.muted_foreground) - .child(commit.author.clone()) - .child(relative_time_secs(commit.time)), - ), - ) - .on_click(move |_, window, cx| { - if let Some(worktree) = worktree.clone() { - log_dropped(view.update(cx, |this, cx| { - this.open_commit_diff(worktree, &row_id, window, cx); - })); - } - }) - .into_any_element() - }) - .collect() - }) - .track_scroll(&scroll_handle) - .size_full(), + h_flex() + .flex_none() + .h_10() + .px_2() + .gap_2() + .border_b_1() + .border_color(cx.theme().colors.border) + .child(Icon::named(IconRole::GitCommit)) + .child(div().text_sm().font_semibold().child("Commits")) + .child(Badge::new("pr-commits-count", count.to_string())), ) - .child(Scrollbar::vertical("pr-commits-scroll", scroll_handle)) + .when(count == 0, |this| { + this.child( + div() + .p_2() + .text_sm() + .text_color(cx.theme().colors.muted_foreground) + .child("No commits found"), + ) + }) + .children(self.commits.iter().map(|row| { + let commit = &row.commit; + let row_id = commit.id.clone(); + let worktree = worktree.clone(); + let view = view.clone(); + + h_flex() + .id(commit.id.clone()) + .px_2() + .h_12() + .w_full() + .gap_3() + .border_b_1() + .border_color(cx.theme().colors.border) + .hover(|this| this.bg(cx.theme().colors.muted_hover)) + .child( + div() + .w_20() + .flex_none() + .text_sm() + .text_color(cx.theme().colors.muted_foreground) + .child(short_id(&commit.id)), + ) + .child( + div() + .flex_1() + .min_w_0() + .text_sm() + .text_ellipsis() + .whitespace_nowrap() + .child(commit.summary.clone()), + ) + .child(GroupedBadge::new( + format!("pr-commit-stats:{}", commit.id), + row.insertions, + row.deletions, + )) + .on_click(move |_, window, cx| { + if let Some(worktree) = worktree.clone() { + log_dropped(view.update(cx, |this, cx| { + this.open_commit_diff(worktree, &row_id, window, cx); + })); + } + }) + .into_any_element() + })) .into_any_element() } + fn render_changes(&self, cx: &App) -> AnyElement { + self.pane.read(cx).render_flow(self.pane.downgrade(), cx) + } + fn render_header(&self, cx: &mut Context) -> AnyElement { - let current_commit = self.current_commit.clone(); - let (title, status, branch, author) = { + let (title, status, branch, author, created_at, target_branch) = { let store = self.store.read(cx); let Some(root) = store .pull_requests @@ -679,40 +513,73 @@ impl PullRequestDetailView { store.status_of(root), root.branch_name(), root.pubkey, + root.created_at, + store.head.clone(), ) }; - let can_update = Backend::global(cx).read(cx).current_user() == Some(author); + let profile = self.directory.read(cx).profile(&author, cx); + let author_name = profile.name(); + let author_picture = profile.picture(); + + let backend = Backend::global(cx); + let can_update = backend.read(cx).current_user() == Some(author); let weak = cx.entity().downgrade(); + let muted = cx.theme().colors.muted_foreground; v_flex() .flex_none() - .px_4() - .py_2() + .p_2() .gap_2() - // The top right corner is the panel toolbar's. .child( - h_flex().gap_2().pr_16().child(status_badge(status)).child( - div() - .flex_1() - .min_w_0() - .text_sm() - .font_semibold() - .text_ellipsis() - .child(title), - ), + div() + .flex_1() + .min_w_0() + .text_sm() + .font_semibold() + .text_ellipsis() + .child(title), ) .child( h_flex() - .gap_3() + .gap_2() + .text_sm() + .child(status_badge(status)) + .child(div().text_color(muted).child("opened by")) + .child( + h_flex() + .gap_1() + .child( + Avatar::new(author) + .name(author_name.clone()) + .picture(author_picture) + .small(), + ) + .child(author_name), + ) + .child( + div() + .text_color(muted) + .child(SharedString::from(relative_time(created_at))), + ) + .when_some(target_branch, |this, target| { + this.child(div().text_color(muted).child("targeting")) + .child( + Badge::new(format!("target-branch:{target}"), target) + .variant(BadgeVariant::Outline) + .small() + .flex_none(), + ) + }) .when_some(branch, |this, branch| { - this.child( - Button::new("branch") - .label(branch) - .icon(Icon::named(IconRole::GitBranch)) - .small(), + this.child(div().text_color(muted).child("from")).child( + Badge::new(format!("source-branch:{branch}"), branch) + .variant(BadgeVariant::Outline) + .small() + .flex_none(), ) }) + .child(div().flex_1()) .when(can_update, |this| { this.child(Button::new("update-pr").label("Update").small().on_click( move |_, _, cx| { @@ -735,51 +602,186 @@ impl PullRequestDetailView { })); }, )) - }) - .when_some(current_commit, |this, id| { - this.child( - h_flex() - .gap_1() - .font_family(cx.theme().mono_font) - .text_xs() - .text_color(cx.theme().colors.muted_foreground) - .child(id.clone()) - .child(ClipboardButton::new("pr-commit", &id)), - ) }), ) .into_any_element() } } -impl Panel for PullRequestDetailView { +/// The pull request discussion, shown in the split pane. +struct PullRequestSidebarView { + store: Entity, + pr_id: EventId, + comment_input: Entity, + directory: Entity, + scroll_handle: ScrollHandle, + _subscription: Subscription, +} + +impl PullRequestSidebarView { + fn new( + store: Entity, + pr_id: EventId, + directory: Entity, + cx: &mut Context, + ) -> Self { + let comment_input = InputState::builder().multiline().build(cx); + let weak = cx.entity().downgrade(); + + let subscription = cx.observe(&store, |this, _store, cx| { + this.track_participants(cx); + cx.notify(); + }); + + cx.defer(move |cx| { + log_dropped(weak.update(cx, |this, cx| { + this.track_participants(cx); + })); + }); + + Self { + store, + pr_id, + comment_input, + directory, + scroll_handle: ScrollHandle::new(), + _subscription: subscription, + } + } + + fn track_participants(&mut self, cx: &mut Context) { + let store = self.store.read(cx); + + let author = store + .pull_requests + .iter() + .find(|pr| pr.id == self.pr_id && pr.kind == Kind::GitPullRequest) + .map(|root| root.pubkey); + let owner = store.announcement.as_ref().map(|a| a.owner); + + let comments: Vec<_> = store + .comments_of(&self.pr_id) + .map(|comment| comment.pubkey) + .collect(); + + track_event_participants(author, owner, comments.into_iter(), &self.directory, cx); + } +} + +impl Panel for PullRequestSidebarView { fn title(&self, _cx: &App) -> SharedString { - let hex = self.pr_id.to_hex(); - let short = hex.get(..8).unwrap_or(&hex); - SharedString::from(format!("PR {short}")) + SharedString::from(SIDEBAR_ID) + } +} + +impl Render for PullRequestSidebarView { + fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { + let store = self.store.read(cx); + + let Some(root) = store + .pull_requests + .iter() + .find(|pr| pr.id == self.pr_id && pr.kind == Kind::GitPullRequest) + else { + return if store.loaded { + EmptyState::centered(NOT_FOUND) + } else { + Spinner::centered("pr-loading") + }; + }; + let root_id = root.id; + + let discussion = ScrollArea::new("pr-discussion-scroll") + .handle(self.scroll_handle.clone()) + .flex_1() + .min_h_0() + .child(v_flex().p_2().child(comments_section( + &self.store, + root_id, + &self.directory, + cx, + ))) + .into_any_element(); + + v_flex() + .size_full() + .min_h_0() + .child(discussion) + .child(div().flex_none().p_2().child(comment_form( + &self.store, + root_id, + &store.pull_requests, + &self.comment_input, + "pr-comment", + cx, + ))) + .into_any_element() + } +} + +impl Panel for PullRequestDetailView { + fn title(&self, cx: &App) -> SharedString { + let store = self.store.read(cx); + match store + .pull_requests + .iter() + .find(|pr| pr.id == self.pr_id && pr.kind == Kind::GitPullRequest) + { + Some(root) => SharedString::from(root.activity_subject()), + None => { + let hex = self.pr_id.to_hex(); + let short = hex.get(..8).unwrap_or(&hex); + SharedString::from(format!("PR {short}")) + } + } + } + + fn prefix(&self, cx: &App) -> Option { + let owner = self.store.read(cx).announcement.as_ref().map(|a| a.owner)?; + let profile = self.directory.read(cx).profile(&owner, cx); + + Some( + Avatar::new(owner) + .name(profile.name()) + .picture(profile.picture()) + .small() + .into_any_element(), + ) } fn loading(&self, _cx: &App) -> bool { self.loading } - - fn toolbar(&self, _cx: &App) -> bool { - true - } } impl Render for PullRequestDetailView { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { - v_flex() - .id("pull-request-detail") + if self.loading { + return v_flex() + .size_full() + .child(Spinner::centered("pr-loading")) + .into_any_element(); + } + + if let Some(error) = self.error.clone() { + return v_flex() + .size_full() + .child(EmptyState::centered(error)) + .into_any_element(); + } + + ScrollArea::new("pr-detail-scroll") + .handle(self.scroll_handle.clone()) .size_full() - .min_h_0() - .child(self.render_header(cx)) - .child(self.render_tabs(cx)) - .map(|this| match self.active_tab { - 0 => this.child(self.render_discussion(cx)), - 1 => this.child(self.render_files(cx)), - _ => this.child(self.render_commits(cx)), - }) + .child( + v_flex() + .id("pull-request-detail") + .w_full() + .child(self.render_header(cx)) + .child(self.render_body(cx)) + .child(self.render_commits(cx)) + .child(self.render_changes(cx)), + ) + .into_any_element() } } diff --git a/crates/signed_app/src/pull_requests/mod.rs b/crates/signed_app/src/pull_requests/mod.rs index 5c98f77..caf4527 100644 --- a/crates/signed_app/src/pull_requests/mod.rs +++ b/crates/signed_app/src/pull_requests/mod.rs @@ -371,35 +371,13 @@ impl Render for PullRequestsView { .border_color(cx.theme().colors.border) .hover(|this| this.bg(cx.theme().colors.muted_hover)) .on_click(move |_, window, cx| { - let id = SharedString::from(format!("pr:{pr_id}")); - let workspace_for_build = workspace.clone(); - let store = store.clone(); - - log_dropped(workspace.update(cx, |root, cx| { - root.panels().clone().update( - cx, - |panels, cx| { - panels.open( - PanelSlot::Main, - id, - move |window, cx| { - cx.new(|cx| { - PullRequestDetailView::new( - workspace_for_build - .clone(), - store.clone(), - pr_id, - window, - cx, - ) - }) - }, - window, - cx, - ) - }, - ) - })); + PullRequestDetailView::open( + &workspace, + store.clone(), + pr_id, + window, + cx, + ); }) .child( h_flex() diff --git a/crates/signed_app/src/repo/actions.rs b/crates/signed_app/src/repo/actions.rs index ccd8933..9927830 100644 --- a/crates/signed_app/src/repo/actions.rs +++ b/crates/signed_app/src/repo/actions.rs @@ -64,30 +64,7 @@ pub fn open_repo_item( IssueDetailView::open(workspace, store, issue_id, window, cx); } RepoItem::PullRequest(pr_id) => { - let id = SharedString::from(format!("pr:{pr_id}")); - let workspace_for_build = workspace.clone(); - let store = store.clone(); - log_dropped(workspace.update(cx, |root, cx| { - root.panels().update(cx, |panels, cx| { - panels.open( - PanelSlot::Main, - id, - move |window, cx| { - cx.new(|cx| { - PullRequestDetailView::new( - workspace_for_build.clone(), - store.clone(), - pr_id, - window, - cx, - ) - }) - }, - window, - cx, - ) - }) - })); + PullRequestDetailView::open(workspace, store.clone(), pr_id, window, cx); } // Opening a patch series directly is not a feature. _ => {} diff --git a/crates/signed_app/src/repo/commit_diff.rs b/crates/signed_app/src/repo/commit_diff.rs index 07ee9f1..dd5a658 100644 --- a/crates/signed_app/src/repo/commit_diff.rs +++ b/crates/signed_app/src/repo/commit_diff.rs @@ -320,8 +320,12 @@ impl DiffPane { } } - fn render_toolbar(&self, cx: &mut Context) -> AnyElement { - let view = cx.entity().downgrade(); + fn render_toolbar( + &self, + show_tree_toggle: bool, + view: WeakEntity, + cx: &App, + ) -> AnyElement { let (insertions, deletions) = self.diff().map_or((0, 0), |diff| { ( diff.files.iter().map(|file| file.insertions).sum(), @@ -338,22 +342,24 @@ impl DiffPane { .flex_none() .border_b_1() .border_color(cx.theme().colors.border) - .child( - Toggle::new("toggle-tree") - .icon(Icon::named(IconRole::PanelLeft)) - .pressed(self.tree_visible) - .accessibility_label("Toggle file tree") - .on_change({ - let view = view.clone(); - move |pressed, _, cx| { - if let Err(error) = - view.update(cx, |this, cx| this.set_tree_visible(pressed, cx)) - { - log::warn!("failed to toggle file tree: {error}"); + .when(show_tree_toggle, |this| { + this.child( + Toggle::new("toggle-tree") + .icon(Icon::named(IconRole::PanelLeft)) + .pressed(self.tree_visible) + .accessibility_label("Toggle file tree") + .on_change({ + let view = view.clone(); + move |pressed, _, cx| { + if let Err(error) = + view.update(cx, |this, cx| this.set_tree_visible(pressed, cx)) + { + log::warn!("failed to toggle file tree: {error}"); + } } - } - }), - ) + }), + ) + }) .when(insertions > 0 || deletions > 0, |this| { this.child(GroupedBadge::new( "toolbar-diff-stats", @@ -393,7 +399,7 @@ impl DiffPane { .variant(ButtonVariant::Ghost) .small() .on_click({ - let view = view; + let view = view.clone(); move |_, _, cx| { if let Err(error) = view.update(cx, |this, cx| this.toggle_all_files(cx)) @@ -411,9 +417,13 @@ impl DiffPane { .variant(ButtonVariant::Ghost) .tooltip(Tooltip::text("Download commit")) .small() - .on_click(cx.listener(|_, _, _, cx| { - cx.emit(DiffPaneEvent::DownloadCommit); - })), + .on_click(move |_, _, cx| { + if let Err(error) = view.update(cx, |_, cx| { + cx.emit(DiffPaneEvent::DownloadCommit); + }) { + log::warn!("failed to download commit patch: {error}"); + } + }), ) }) .into_any_element() @@ -448,6 +458,39 @@ impl DiffPane { .child(Scrollbar::vertical("commit-diff-files", self.list.clone())) .into_any_element() } + + /// Renders every diff row inline, for embedding in a page that scrolls as a whole. + pub fn render_flow(&self, view: WeakEntity, cx: &App) -> AnyElement { + let Some(diff) = self.diff.as_ref() else { + return div() + .p_4() + .text_sm() + .text_color(cx.theme().colors.muted_foreground) + .child("No changes") + .into_any_element(); + }; + if diff.files.is_empty() { + return div() + .p_4() + .text_sm() + .text_color(cx.theme().colors.muted_foreground) + .child("No files changed") + .into_any_element(); + } + + let wrap = self.wrap; + let rows = self + .pane_rows + .iter() + .map(|row| render_pane_row(row, &diff.files, &view, wrap, cx)) + .collect::>(); + + v_flex() + .w_full() + .child(self.render_toolbar(false, view, cx)) + .child(v_flex().w_full().p_2().children(rows)) + .into_any_element() + } } fn render_pane_row( @@ -572,7 +615,7 @@ impl Render for DiffPane { return EmptyState::centered("No files changed"); } - let toolbar = self.render_toolbar(cx); + let toolbar = self.render_toolbar(true, cx.entity().downgrade(), cx); let file_list = self.render_file_list(cx); let content = if self.tree_visible { diff --git a/crates/signed_git/src/patch.rs b/crates/signed_git/src/patch.rs index badc493..f89e21a 100644 --- a/crates/signed_git/src/patch.rs +++ b/crates/signed_git/src/patch.rs @@ -31,35 +31,55 @@ impl PatchParser { Ok(CommitDiff::new(files)) } - /// Lists the commits represented by the mbox messages that carry an id. - pub fn patch_commits(patch: &str) -> Vec { + /// Lists the commits represented by the mbox messages that carry an id, with their total insertions and deletions. + pub fn patch_commits_with_stats(patch: &str) -> Vec<(FileCommit, usize, usize)> { Self::envelopes(patch) .into_iter() .filter(|message| !message.id.is_empty()) .map(|message| { - let time = message - .header("Date") - .and_then(|value| gix::date::parse(value.trim(), None).ok()); - - FileCommit::new( - message.id.to_string(), - message - .header("Subject") - .map(Self::strip_patch_prefix) - .unwrap_or_default(), - None, - message - .header("From") - .map(Self::name_from_address) - .unwrap_or_default(), - time.map(|time| time.seconds).unwrap_or(0), - time.map(format_commit_date).unwrap_or_default(), - Vec::new(), - ) + let (insertions, deletions) = match Self::patch_diffs(message.text) { + Ok(diff) => ( + diff.files.iter().map(|file| file.insertions).sum(), + diff.files.iter().map(|file| file.deletions).sum(), + ), + Err(_) => (0, 0), + }; + (Self::commit_from_envelope(&message), insertions, deletions) }) .collect() } + /// Lists the commits represented by the mbox messages that carry an id. + pub fn patch_commits(patch: &str) -> Vec { + Self::patch_commits_with_stats(patch) + .into_iter() + .map(|(commit, _, _)| commit) + .collect() + } + + /// Creates a commit from one parsed mbox message. + fn commit_from_envelope(message: &Envelope<'_>) -> FileCommit { + let time = message + .header("Date") + .and_then(|value| gix::date::parse(value.trim(), None).ok()); + + FileCommit::new( + message.id.to_string(), + message + .header("Subject") + .map(Self::strip_patch_prefix) + .unwrap_or_default(), + None, + message + .header("From") + .map(Self::name_from_address) + .unwrap_or_default(), + time.map(|time| time.seconds).unwrap_or(0), + time.map(format_commit_date).unwrap_or_default(), + Vec::new(), + ) + } + /// Splits `patch` on `From <40-hex> ` envelopes, falling back to one message. fn envelopes(patch: &str) -> Vec> { let mut messages: Vec> = Vec::new(); diff --git a/crates/signed_git/src/tests.rs b/crates/signed_git/src/tests.rs index a56f6c9..c22622b 100644 --- a/crates/signed_git/src/tests.rs +++ b/crates/signed_git/src/tests.rs @@ -928,6 +928,11 @@ diff --git a/b.txt b/b.txt assert_eq!(commits[1].summary, "second"); assert_eq!(commits[1].author, "Bob"); assert_eq!(commits[1].time, 1690975800); + + let stats = PatchParser::patch_commits_with_stats(patch); + assert_eq!(stats.len(), 2); + assert_eq!((stats[0].1, stats[0].2), (1, 0)); + assert_eq!((stats[1].1, stats[1].2), (1, 0)); } #[test] diff --git a/crates/ui/src/icons/assets/message.svg b/crates/ui/src/icons/assets/message.svg new file mode 100644 index 0000000..03a0c2b --- /dev/null +++ b/crates/ui/src/icons/assets/message.svg @@ -0,0 +1,3 @@ + + + diff --git a/crates/ui/src/icons/mod.rs b/crates/ui/src/icons/mod.rs index 1dfc866..ce29de6 100644 --- a/crates/ui/src/icons/mod.rs +++ b/crates/ui/src/icons/mod.rs @@ -59,6 +59,7 @@ pub enum IconRole { ArrowUp, Inbox, Stack, + Message, PanelLeft, UnfoldVertical, Download, @@ -111,6 +112,7 @@ impl IconRole { Self::ArrowUp => "arrow-up", Self::Inbox => "inbox", Self::Stack => "stack", + Self::Message => "message", Self::PanelLeft => "panel-left", Self::UnfoldVertical => "unfold-vertical", Self::Download => "download",