BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / proposal_card.rs

proposal_card.rs

proposal_card.rs 7.1 KiB Download Raw
1use std::rc::Rc;
2
3use chrono::{DateTime, Utc};
4use gpui_kit::assets::IconName;
5use gpui_kit::component::{h_flex, v_flex};
6use gpui_kit::prelude::FluentBuilder as _;
7use gpui_kit::*;
8
9use crate::agent_api::{ProposalEntry, ProposalStatus, agent_label};
10use crate::theme;
11use crate::views::widgets::icon;
12
13const CARD_MARGIN_X: f32 = 20.;
14const CARD_MARGIN_BOTTOM: f32 = 8.;
15const CARD_PADDING: f32 = 12.;
16const CARD_RADIUS: f32 = 10.;
17const CARD_GAP: f32 = 8.;
18const BUTTON_HEIGHT: f32 = 28.;
19const BUTTON_RADIUS: f32 = 7.;
20const BODY_LINE_HEIGHT: f32 = 20.;
21const BODY_MAX_LINES: f32 = 4.;
22const QUOTE_BAR_WIDTH: f32 = 2.;
23
24pub type CardAction = Rc<dyn Fn(&mut Window, &mut App)>;
25
26#[derive(Clone)]
27pub struct CardActions {
28 pub send: CardAction,
29 pub edit: CardAction,
30 pub discard: CardAction,
31}
32
33fn open_session_available(session_id: &str, cx: &App) -> bool {
34 crate::agents::has_session(session_id, cx)
35}
36
37fn open_session(session_id: &str, _window: &mut Window, cx: &mut App) {
38 crate::agents::open_session(session_id, cx);
39}
40
41pub fn age_label(created_at: DateTime<Utc>, now: DateTime<Utc>) -> String {
42 let minutes = (now - created_at).num_minutes().max(0);
43 match minutes {
44 0 => "just now".to_owned(),
45 1..=59 => format!("{minutes} min ago"),
46 60..=1439 => format!("{} h ago", minutes / 60),
47 _ => format!("{} d ago", minutes / 1440),
48 }
49}
50
51fn card_button(
52 id: &'static str,
53 label: &'static str,
54 background: Hsla,
55 color: Hsla,
56 action: CardAction,
57) -> Stateful<Div> {
58 div()
59 .id(id)
60 .h(px(BUTTON_HEIGHT))
61 .px(px(12.))
62 .flex()
63 .items_center()
64 .rounded(px(BUTTON_RADIUS))
65 .cursor_pointer()
66 .text_size(px(12.))
67 .font_weight(FontWeight::SEMIBOLD)
68 .bg(background)
69 .text_color(color)
70 .hover(|button| button.opacity(0.85))
71 .child(label)
72 .on_click(move |_, window, cx| action(window, cx))
73}
74
75fn header_text(entry: &ProposalEntry, now: DateTime<Utc>) -> (String, Hsla, Option<String>) {
76 let agent = agent_label(entry.proposal.agent.as_deref());
77 match &entry.status {
78 ProposalStatus::Pending => (
79 format!("{agent} proposes a reply"),
80 theme::accent_text(),
81 Some(format!(" - {}", age_label(entry.proposal.created_at, now))),
82 ),
83 ProposalStatus::Stale { answered_at } => (
84 format!(
85 "{agent}'s reply is outdated - you answered at {}",
86 answered_at.format("%H:%M")
87 ),
88 theme::text_muted(),
89 None,
90 ),
91 ProposalStatus::Failed { reason } => (
92 format!("Could not send - {reason}"),
93 theme::red_soft(),
94 None,
95 ),
96 }
97}
98
99pub fn render(
100 entry: &ProposalEntry,
101 quote: Option<String>,
102 actions: CardActions,
103 now: DateTime<Utc>,
104 cx: &App,
105) -> Div {
106 let stale = matches!(entry.status, ProposalStatus::Stale { .. });
107 let failed = matches!(entry.status, ProposalStatus::Failed { .. });
108 let (title, title_color, age) = header_text(entry, now);
109 let close = actions.discard.clone();
110 let header = h_flex()
111 .w_full()
112 .items_center()
113 .gap(px(4.))
114 .child(
115 h_flex()
116 .flex_1()
117 .min_w_0()
118 .items_baseline()
119 .overflow_hidden()
120 .text_size(px(12.))
121 .child(
122 div()
123 .truncate()
124 .font_weight(FontWeight::SEMIBOLD)
125 .text_color(title_color)
126 .child(title),
127 )
128 .children(
129 age.map(|age| div().flex_none().text_color(theme::text_faint()).child(age)),
130 ),
131 )
132 .child(
133 div()
134 .id("proposal-close")
135 .size(px(BUTTON_HEIGHT))
136 .flex_none()
137 .flex()
138 .items_center()
139 .justify_center()
140 .rounded(px(BUTTON_RADIUS))
141 .cursor_pointer()
142 .hover(|button| button.bg(theme::surface_raised()))
143 .child(icon(IconName::Close, 14., theme::text_muted()))
144 .on_click(move |_, window, cx| close(window, cx)),
145 );
146 let quote = quote.map(|quote| {
147 div()
148 .w_full()
149 .truncate()
150 .pl(px(8.))
151 .border_l(px(QUOTE_BAR_WIDTH))
152 .border_color(theme::border_strong())
153 .text_size(px(12.))
154 .text_color(theme::text_muted())
155 .child(quote)
156 });
157 let body = div()
158 .w_full()
159 .max_h(px(BODY_LINE_HEIGHT * BODY_MAX_LINES))
160 .overflow_hidden()
161 .text_size(px(14.))
162 .line_height(px(BODY_LINE_HEIGHT))
163 .text_color(if stale {
164 theme::text_faint()
165 } else {
166 theme::text()
167 })
168 .child(entry.proposal.text.clone());
169 let session = entry
170 .proposal
171 .session_id
172 .clone()
173 .filter(|session_id| open_session_available(session_id, cx));
174 let buttons = h_flex()
175 .w_full()
176 .items_center()
177 .gap(px(8.))
178 .when(!stale, |row| {
179 row.child(card_button(
180 "proposal-send",
181 if failed { "Retry" } else { "Send" },
182 theme::accent(),
183 theme::on_accent(),
184 actions.send.clone(),
185 ))
186 })
187 .child(card_button(
188 "proposal-edit",
189 "Edit",
190 theme::surface_raised(),
191 theme::text(),
192 actions.edit.clone(),
193 ))
194 .child(card_button(
195 "proposal-discard",
196 "Discard",
197 theme::surface_raised(),
198 theme::text(),
199 actions.discard.clone(),
200 ))
201 .children(session.map(|session_id| {
202 div()
203 .id("proposal-open-session")
204 .ml_auto()
205 .cursor_pointer()
206 .text_size(px(12.))
207 .text_color(theme::accent_text())
208 .child("Open session")
209 .on_click(move |_, window, cx| open_session(&session_id, window, cx))
210 }));
211 v_flex()
212 .flex_none()
213 .mx(px(CARD_MARGIN_X))
214 .mb(px(CARD_MARGIN_BOTTOM))
215 .p(px(CARD_PADDING))
216 .gap(px(CARD_GAP))
217 .rounded(px(CARD_RADIUS))
218 .bg(theme::row_hover())
219 .child(header)
220 .children(quote)
221 .child(body)
222 .child(buttons)
223}
224
225#[cfg(test)]
226mod tests {
227 use chrono::{Duration, Utc};
228
229 use super::age_label;
230
231 #[test]
232 fn age_labels_follow_the_elapsed_time() {
233 let now = Utc::now();
234 let label = |minutes: i64| age_label(now - Duration::minutes(minutes), now);
235 assert_eq!(label(0), "just now");
236 assert_eq!(label(1), "1 min ago");
237 assert_eq!(label(59), "59 min ago");
238 assert_eq!(label(120), "2 h ago");
239 assert_eq!(label(3000), "2 d ago");
240 assert_eq!(age_label(now + Duration::minutes(5), now), "just now");
241 }
242}