2026-05-11 02:14:05 +08:00
|
|
|
use gpui::{
|
|
|
|
|
AppContext, InteractiveElement, IntoElement, ParentElement, StatefulInteractiveElement, Styled,
|
|
|
|
|
div, prelude::FluentBuilder,
|
|
|
|
|
};
|
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(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let author_pill = div()
|
|
|
|
|
.px_2()
|
|
|
|
|
.border_1()
|
|
|
|
|
.border_color(theme.colors.border)
|
|
|
|
|
.rounded_full()
|
|
|
|
|
.bg(theme.colors.surface_elevated)
|
|
|
|
|
.child(text("kennethnym").text_xs());
|
|
|
|
|
|
|
|
|
|
let row = div()
|
|
|
|
|
.flex()
|
|
|
|
|
.flex_row()
|
|
|
|
|
.gap_2()
|
|
|
|
|
.child(status_pill)
|
|
|
|
|
.child(author_pill);
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
.w_full()
|
|
|
|
|
.px_3p5()
|
|
|
|
|
.py_3()
|
|
|
|
|
.border_b_1()
|
|
|
|
|
.border_color(theme.colors.border)
|
|
|
|
|
.child(text(pr.title.clone()).w_full().text_xl().mb_2())
|
|
|
|
|
.child(row),
|
|
|
|
|
)
|
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
|
|
|
}
|
|
|
|
|
}
|