use crate::{
components::{
AvatarColor, Badge, BadgeSize, BadgeVariant, Button, ButtonSize, ButtonVariant, FieldSize,
Input, InputState,
},
icons::CarbonIcon,
t3::craft::{self, AvatarTone, rem, sans},
theme::{LayerLevel, color},
tokens::{ColorRole, FontFamilyKey},
typography::font,
};
use gpui_kit::{
AnyElement, App, AppContext as _, ElementId, InteractiveElement as _, IntoElement,
ParentElement, RenderOnce, SharedString, Styled, Window, div, prelude::FluentBuilder as _, px,
relative,
};
use std::rc::Rc;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ReviewComment {
pub author: SharedString,
pub time: SharedString,
pub body: SharedString,
pub color: AvatarColor,
}
impl ReviewComment {
pub fn new(
author: impl Into<SharedString>,
time: impl Into<SharedString>,
body: impl Into<SharedString>,
) -> Self {
Self {
author: author.into(),
time: time.into(),
body: body.into(),
color: AvatarColor::Blue,
}
}
pub fn color(mut self, color: AvatarColor) -> Self {
self.color = color;
self
}
}
#[derive(Clone, Debug)]
pub struct ReviewThreadTexts {
pub reply: SharedString,
pub reply_label: SharedString,
pub resolve: SharedString,
pub unresolve: SharedString,
pub add_to_chat: SharedString,
pub pending: SharedString,
}
impl Default for ReviewThreadTexts {
fn default() -> Self {
Self {
reply: "Reply\u{2026}".into(),
reply_label: "Reply".into(),
resolve: "Resolve conversation".into(),
unresolve: "Unresolve".into(),
add_to_chat: "Add to chat".into(),
pending: "Pending".into(),
}
}
}
type Handler = Rc<dyn Fn(&mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct ReviewThreadCard {
id: ElementId,
comments: Vec<ReviewComment>,
resolved: bool,
pending: bool,
in_diff: bool,
on_resolve: Option<Handler>,
on_add_to_chat: Option<Handler>,
texts: ReviewThreadTexts,
}
impl ReviewThreadCard {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
comments: Vec::new(),
resolved: false,
pending: false,
in_diff: false,
on_resolve: None,
on_add_to_chat: None,
texts: ReviewThreadTexts::default(),
}
}
pub fn comments(mut self, comments: impl IntoIterator<Item = ReviewComment>) -> Self {
self.comments = comments.into_iter().collect();
self
}
pub fn resolved(mut self, resolved: bool) -> Self {
self.resolved = resolved;
self
}
pub fn pending(mut self, pending: bool) -> Self {
self.pending = pending;
self
}
pub fn in_diff(mut self, in_diff: bool) -> Self {
self.in_diff = in_diff;
self
}
pub fn on_resolve(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_resolve = Some(Rc::new(f));
self
}
pub fn on_add_to_chat(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_add_to_chat = Some(Rc::new(f));
self
}
pub fn texts(mut self, texts: ReviewThreadTexts) -> Self {
self.texts = texts;
self
}
}
fn comment(c: &ReviewComment, pending_tag: Option<SharedString>, in_diff: bool) -> AnyElement {
div()
.flex()
.gap(rem(12.))
.p(rem(12.))
.border_b_1()
.border_color(color(ColorRole::BorderSubtle01))
.child(craft::initials(
craft::two_letters(&c.author),
AvatarTone::Avatar(c.color),
24.,
))
.child(
div()
.flex_1()
.min_w_0()
.child(
div()
.font(font(FontFamilyKey::Sans, 400))
.text_size(rem(12.))
.when(!in_diff, |this| {
this.line_height(relative(craft::BODY_LINE))
})
.flex()
.items_center()
.gap(rem(8.))
.text_color(color(ColorRole::TextSecondary))
.child(
div()
.text_color(color(ColorRole::TextPrimary))
.child(c.author.clone()),
)
.child(c.time.clone())
.when_some(pending_tag, |this, tag| {
this.child(
Badge::new(tag)
.variant(BadgeVariant::WarmGray)
.size(BadgeSize::Sm),
)
}),
)
.child(
sans(div(), 14., 400)
.mt(rem(4.))
.line_height(relative(1.42857))
.text_color(color(ColorRole::TextPrimary))
.child(c.body.clone()),
),
)
.into_any_element()
}
impl RenderOnce for ReviewThreadCard {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let reply = window
.use_keyed_state((id.clone(), "reply"), cx, |window, cx| {
cx.new(|cx| InputState::new(window, cx))
})
.read(cx)
.clone();
let on_resolve = self.on_resolve.clone();
let on_chat = self.on_add_to_chat.clone();
let comments = self.comments.iter().enumerate().map(|(i, c)| {
comment(
c,
(self.pending && i == 0).then(|| self.texts.pending.clone()),
self.in_diff,
)
});
let foot = div()
.flex()
.flex_wrap()
.items_center()
.gap(rem(4.))
.pt(rem(8.))
.pb(rem(8.))
.pr(rem(4.))
.pl(rem(12.))
.child(
div()
.flex_grow_1()
.flex_shrink_1()
.flex_basis(rem(192.))
.min_w_0()
.child(
Input::new((id.clone(), "reply-field"), &reply)
.label(self.texts.reply_label.clone())
.hide_label(true)
.placeholder(self.texts.reply.clone())
.size(FieldSize::Sm)
.layer(LayerLevel::Two),
),
)
.child(
Button::new(
(id.clone(), "resolve"),
if self.resolved {
self.texts.unresolve.clone()
} else {
self.texts.resolve.clone()
},
)
.variant(ButtonVariant::Ghost)
.size(ButtonSize::Sm)
.when_some(on_resolve, |b, f| b.on_click(move |_, w, cx| f(w, cx))),
)
.child(
Button::new((id.clone(), "chat"), self.texts.add_to_chat.clone())
.variant(ButtonVariant::Ghost)
.size(ButtonSize::Sm)
.icon(CarbonIcon::Chat)
.when_some(on_chat, |b, f| b.on_click(move |_, w, cx| f(w, cx))),
);
div()
.font(font(FontFamilyKey::Sans, 400))
.when(!self.in_diff, |this| {
this.text_size(rem(14.))
.line_height(relative(craft::BODY_LINE))
})
.id(id)
.relative()
.flex()
.flex_col()
.w_full()
.max_w(rem(576.))
.bg(color(ColorRole::Layer02))
.when(self.resolved, |this| this.opacity(0.7))
.children(comments)
.child(foot)
.child(
div()
.absolute()
.top_0()
.left_0()
.right_0()
.bottom_0()
.border_1()
.border_color(color(ColorRole::BorderSubtle01)),
)
.when(self.pending, |this| {
this.child(
div()
.absolute()
.top_0()
.left_0()
.bottom_0()
.w(px(3.))
.bg(color(ColorRole::SupportWarning)),
)
})
}
}