2026-05-11 02:14:05 +08:00
|
|
|
use gpui::{
|
|
|
|
|
AppContext, InteractiveElement, IntoElement, ParentElement, StatefulInteractiveElement, Styled,
|
2026-05-12 01:34:33 +08:00
|
|
|
div, img, prelude::FluentBuilder,
|
2026-05-11 02:14:05 +08:00
|
|
|
};
|
2026-05-11 00:32:12 +08:00
|
|
|
|
|
|
|
|
use crate::{
|
2026-05-11 02:14:05 +08:00
|
|
|
api::{self},
|
2026-05-11 00:32:12 +08:00
|
|
|
app,
|
|
|
|
|
component::{
|
|
|
|
|
font_icon::{FontIcon, font_icon},
|
|
|
|
|
markdown::{self, MarkdownText},
|
|
|
|
|
text::text,
|
|
|
|
|
},
|
|
|
|
|
query::{self, QueryStatus, read_query, use_query},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub(crate) struct PullRequestView {
|
|
|
|
|
markdown_viewer: Option<gpui::Entity<MarkdownText>>,
|
|
|
|
|
|
|
|
|
|
pull_request_query: Option<query::Entity<api::issues::FetchPullRequest>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn new(cx: &mut gpui::Context<PullRequestView>) -> PullRequestView {
|
|
|
|
|
PullRequestView {
|
|
|
|
|
markdown_viewer: None,
|
|
|
|
|
pull_request_query: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PullRequestView {
|
|
|
|
|
pub(crate) fn change_displayed_pull_request(
|
|
|
|
|
&mut self,
|
|
|
|
|
id: api::issues::Id,
|
|
|
|
|
cx: &mut gpui::Context<Self>,
|
|
|
|
|
) {
|
|
|
|
|
let query = use_query(api::issues::FetchPullRequest { id }, cx);
|
|
|
|
|
|
|
|
|
|
self.pull_request_query = Some(query.clone());
|
|
|
|
|
|
|
|
|
|
_ = cx
|
|
|
|
|
.observe(&query.clone(), move |this, _, cx| {
|
2026-05-11 02:34:09 +08:00
|
|
|
this.load_markdown_content(cx);
|
2026-05-11 00:32:12 +08:00
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
|
2026-05-11 02:34:09 +08:00
|
|
|
// cached query will not trigger observe callback
|
|
|
|
|
// this is required so that content is loaded immediately for cached query
|
|
|
|
|
self.load_markdown_content(cx);
|
|
|
|
|
|
|
|
|
|
cx.notify();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn load_markdown_content(&mut self, cx: &mut gpui::Context<Self>) {
|
|
|
|
|
let Some(query) = &self.pull_request_query else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let maybe_content = {
|
|
|
|
|
let data = read_query(&query, cx);
|
|
|
|
|
if let QueryStatus::Loaded(pr) = data {
|
|
|
|
|
Some(gpui::SharedString::new(pr.body.as_str()))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.markdown_viewer = maybe_content.map(|content| cx.new(|cx| markdown::new(content, cx)));
|
|
|
|
|
|
2026-05-11 00:32:12 +08:00
|
|
|
cx.notify();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn pr_content(
|
|
|
|
|
&self,
|
|
|
|
|
pr: &api::issues::DetailedPullRequest,
|
|
|
|
|
cx: &gpui::Context<Self>,
|
2026-05-11 02:14:05 +08:00
|
|
|
) -> gpui::AnyElement {
|
2026-05-11 00:32:12 +08:00
|
|
|
let theme = app::current_theme(cx);
|
|
|
|
|
|
|
|
|
|
let mut status_pill = div()
|
|
|
|
|
.flex()
|
|
|
|
|
.flex_row()
|
|
|
|
|
.items_center()
|
|
|
|
|
.gap_1()
|
|
|
|
|
.px_2()
|
|
|
|
|
.rounded_full();
|
|
|
|
|
|
|
|
|
|
match pr.state {
|
|
|
|
|
| api::issues::PullRequestState::Open => {
|
|
|
|
|
status_pill = status_pill
|
|
|
|
|
.bg(theme.colors.success)
|
|
|
|
|
.child(
|
|
|
|
|
font_icon(FontIcon::PullRequestArrow)
|
|
|
|
|
.size_3()
|
|
|
|
|
.text_color(theme.colors.accent_text),
|
|
|
|
|
)
|
|
|
|
|
.child(text("Open").text_color(theme.colors.accent_text).text_xs());
|
|
|
|
|
}
|
|
|
|
|
| api::issues::PullRequestState::Closed => {
|
|
|
|
|
status_pill = status_pill
|
|
|
|
|
.bg(theme.colors.danger)
|
|
|
|
|
.child(
|
|
|
|
|
font_icon(FontIcon::PullRequestClosed)
|
|
|
|
|
.size_3()
|
|
|
|
|
.text_color(theme.colors.accent_text),
|
|
|
|
|
)
|
|
|
|
|
.child(
|
|
|
|
|
text("Closed")
|
|
|
|
|
.text_color(theme.colors.accent_text)
|
|
|
|
|
.text_xs(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
| api::issues::PullRequestState::Merged => {
|
|
|
|
|
status_pill = status_pill.bg(theme.colors.accent).child(
|
|
|
|
|
text("Merged")
|
|
|
|
|
.text_color(theme.colors.accent_text)
|
|
|
|
|
.text_xs(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-12 01:34:33 +08:00
|
|
|
let merge_text = match (
|
|
|
|
|
pr.author.as_ref(),
|
|
|
|
|
pr.base_branch_name.as_ref(),
|
|
|
|
|
pr.head_branch_name.as_ref(),
|
|
|
|
|
) {
|
|
|
|
|
| (Some(author), Some(base_branch), Some(head_branch)) => {
|
|
|
|
|
let str = format!(
|
|
|
|
|
"{} requested to merge {} into {}",
|
|
|
|
|
author.login, head_branch, base_branch
|
|
|
|
|
);
|
2026-05-11 00:32:12 +08:00
|
|
|
|
2026-05-12 01:34:33 +08:00
|
|
|
let head_branch_text_offset = author.login.len() + 20;
|
|
|
|
|
let base_branch_text_offset = head_branch_text_offset + head_branch.len() + 6;
|
|
|
|
|
|
|
|
|
|
let highlights = [
|
|
|
|
|
(
|
|
|
|
|
0..author.login.len(),
|
|
|
|
|
gpui::HighlightStyle {
|
|
|
|
|
font_weight: Some(gpui::FontWeight::BOLD),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
head_branch_text_offset..head_branch_text_offset + head_branch.len(),
|
|
|
|
|
gpui::HighlightStyle {
|
|
|
|
|
font_weight: Some(gpui::FontWeight::BOLD),
|
|
|
|
|
color: Some(theme.colors.accent.into()),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
base_branch_text_offset..base_branch_text_offset + base_branch.len(),
|
|
|
|
|
gpui::HighlightStyle {
|
|
|
|
|
font_weight: Some(gpui::FontWeight::BOLD),
|
|
|
|
|
color: Some(theme.colors.accent.into()),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
Some((
|
|
|
|
|
author,
|
|
|
|
|
gpui::StyledText::new(str).with_highlights(highlights),
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
| _ => None,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let metadata_line =
|
|
|
|
|
div()
|
|
|
|
|
.flex()
|
|
|
|
|
.flex_row()
|
|
|
|
|
.gap_2()
|
|
|
|
|
.when_some(merge_text, |it, (author, t)| {
|
|
|
|
|
it.child(
|
|
|
|
|
div()
|
|
|
|
|
.flex()
|
|
|
|
|
.flex_row()
|
|
|
|
|
.items_center()
|
|
|
|
|
.gap_1p5()
|
|
|
|
|
.child(img(author.avatar_url.clone()).size_4().rounded_full())
|
|
|
|
|
.child(
|
|
|
|
|
div()
|
|
|
|
|
.min_w_0()
|
|
|
|
|
.w_full()
|
|
|
|
|
.text_color(theme.colors.text)
|
|
|
|
|
.text_xs()
|
|
|
|
|
.font_weight(gpui::FontWeight::LIGHT)
|
|
|
|
|
.opacity(0.8)
|
|
|
|
|
.child(t),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
});
|
2026-05-11 00:32:12 +08:00
|
|
|
|
|
|
|
|
div()
|
|
|
|
|
.size_full()
|
|
|
|
|
.flex()
|
|
|
|
|
.flex_col()
|
2026-05-11 02:14:05 +08:00
|
|
|
.overflow_hidden()
|
2026-05-11 00:32:12 +08:00
|
|
|
.child(
|
|
|
|
|
div()
|
2026-05-12 02:44:42 +08:00
|
|
|
.flex()
|
|
|
|
|
.flex_row()
|
|
|
|
|
.justify_between()
|
|
|
|
|
.items_center()
|
2026-05-11 00:32:12 +08:00
|
|
|
.w_full()
|
|
|
|
|
.px_3p5()
|
|
|
|
|
.py_3()
|
|
|
|
|
.border_b_1()
|
|
|
|
|
.border_color(theme.colors.border)
|
2026-05-12 02:19:08 +08:00
|
|
|
.child(
|
|
|
|
|
div()
|
|
|
|
|
.w_full()
|
|
|
|
|
.flex()
|
2026-05-12 02:44:42 +08:00
|
|
|
.flex_col()
|
|
|
|
|
.items_start()
|
2026-05-12 02:19:08 +08:00
|
|
|
.child(text(pr.title.clone()).w_full().text_xl().mb_1())
|
2026-05-12 02:44:42 +08:00
|
|
|
.child(metadata_line),
|
2026-05-12 02:19:08 +08:00
|
|
|
)
|
2026-05-12 02:44:42 +08:00
|
|
|
.child(div().flex().flex_col().items_end().gap_1().when_some(
|
|
|
|
|
pr.created_at,
|
|
|
|
|
|it, created_at| {
|
|
|
|
|
it.child(
|
|
|
|
|
text(created_at.format("%Y-%m-%d %H:%M").to_string())
|
|
|
|
|
.opacity(0.5)
|
|
|
|
|
.text_xs(),
|
|
|
|
|
)
|
|
|
|
|
.child(status_pill)
|
|
|
|
|
},
|
|
|
|
|
)),
|
2026-05-11 00:32:12 +08:00
|
|
|
)
|
2026-05-11 02:14:05 +08:00
|
|
|
.child(
|
|
|
|
|
div().flex_1().min_h_0().w_full().child(
|
|
|
|
|
div()
|
|
|
|
|
.id("pr-body-content")
|
|
|
|
|
.size_full()
|
|
|
|
|
.overflow_y_scroll()
|
|
|
|
|
.when_some(self.markdown_viewer.as_ref(), |it, viewer| {
|
|
|
|
|
it.child(div().w_full().p_3p5().child(viewer.clone()))
|
|
|
|
|
}),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.into_any_element()
|
2026-05-11 00:32:12 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl gpui::Render for PullRequestView {
|
|
|
|
|
fn render(
|
|
|
|
|
&mut self,
|
2026-05-11 02:14:05 +08:00
|
|
|
_window: &mut gpui::Window,
|
2026-05-11 00:32:12 +08:00
|
|
|
cx: &mut gpui::Context<Self>,
|
|
|
|
|
) -> impl gpui::IntoElement {
|
2026-05-11 02:14:05 +08:00
|
|
|
div().size_full().child(match &self.pull_request_query {
|
2026-05-11 00:32:12 +08:00
|
|
|
| Some(q) => match read_query(q, cx) {
|
|
|
|
|
| QueryStatus::Loaded(pr) => self.pr_content(pr, cx),
|
2026-05-11 02:14:05 +08:00
|
|
|
| QueryStatus::Err(e) => div()
|
|
|
|
|
.size_full()
|
|
|
|
|
.child(format!("{:?}", e))
|
|
|
|
|
.into_any_element(),
|
|
|
|
|
| QueryStatus::Loading => div()
|
|
|
|
|
.size_full()
|
|
|
|
|
.child("loading pr content")
|
|
|
|
|
.into_any_element(),
|
2026-05-11 00:32:12 +08:00
|
|
|
},
|
2026-05-11 02:14:05 +08:00
|
|
|
| None => div().size_full().child("no pr selected").into_any_element(),
|
|
|
|
|
})
|
2026-05-11 00:32:12 +08:00
|
|
|
}
|
|
|
|
|
}
|