BlueGit

ict rusty-teams

Files

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

meeting_proposal_card.rs

meeting_proposal_card.rs 4.4 KiB Download Raw
1use std::rc::Rc;
2
3use chrono::{DateTime, Local, Utc};
4use gpui_kit::component::{h_flex, v_flex};
5use gpui_kit::*;
6use mcp::MeetingProposal;
7
8use super::proposal_card::{CardAction, age_label, card_button};
9use crate::agent_api::{self, meetings};
10use crate::theme;
11
12pub const MEETING_PROPOSAL_CARD: &str = "calendar/meeting-proposal";
13const CARD_MARGIN: f32 = 12.;
14const CARD_PADDING: f32 = 12.;
15const CARD_GAP: f32 = 6.;
16const CARD_RADIUS: f32 = 10.;
17const DESCRIPTION_LINES: usize = 3;
18
19pub fn when_label(start: DateTime<Utc>, end: DateTime<Utc>) -> String {
20 let (start, end) = (start.with_timezone(&Local), end.with_timezone(&Local));
21 let clock = crate::format::clock_format();
22 let day = |time: DateTime<Local>| crate::format::weekday_date_label(time.date_naive());
23 let end_text = if end.date_naive() == start.date_naive() {
24 end.format(clock).to_string()
25 } else {
26 format!("{} {}", day(end), end.format(clock))
27 };
28 format!("{} {} - {end_text}", day(start), start.format(clock))
29}
30
31fn description_excerpt(description: &str) -> Option<String> {
32 let lines: Vec<&str> = description
33 .lines()
34 .map(str::trim)
35 .filter(|line| !line.is_empty())
36 .collect();
37 if lines.is_empty() {
38 return None;
39 }
40 let mut excerpt = lines[..lines.len().min(DESCRIPTION_LINES)].join("\n");
41 if lines.len() > DESCRIPTION_LINES {
42 excerpt.push_str(" ...");
43 }
44 Some(excerpt)
45}
46
47fn card(proposal: &MeetingProposal, now: DateTime<Utc>) -> Div {
48 let agent = agent_api::agent_label(proposal.agent.as_deref());
49 let draft = &proposal.draft;
50 let (create_id, discard_id) = (proposal.id.clone(), proposal.id.clone());
51 let create: CardAction = Rc::new(move |_, cx| meetings::create_meeting(&create_id, cx));
52 let discard: CardAction = Rc::new(move |_, cx| meetings::discard_meeting(&discard_id, cx));
53 v_flex()
54 .flex_none()
55 .mx(px(CARD_MARGIN))
56 .mt(px(CARD_MARGIN))
57 .p(px(CARD_PADDING))
58 .gap(px(CARD_GAP))
59 .rounded(px(CARD_RADIUS))
60 .bg(theme::row_hover())
61 .debug_selector(|| MEETING_PROPOSAL_CARD.into())
62 .child(
63 div()
64 .text_size(px(12.))
65 .text_color(theme::accent_text())
66 .child(format!(
67 "{agent} proposes a meeting - {}",
68 age_label(proposal.created_at, now)
69 )),
70 )
71 .child(
72 div()
73 .text_size(px(14.))
74 .font_weight(FontWeight::SEMIBOLD)
75 .child(draft.subject.clone()),
76 )
77 .child(
78 div()
79 .text_size(px(13.))
80 .child(when_label(draft.start, draft.end)),
81 )
82 .child(
83 div()
84 .text_size(px(12.))
85 .text_color(theme::text_muted())
86 .child(format!("Invites {}", draft.attendees.join(", "))),
87 )
88 .children(description_excerpt(&draft.description).map(|excerpt| {
89 div()
90 .text_size(px(12.))
91 .text_color(theme::text_muted())
92 .whitespace_normal()
93 .child(excerpt)
94 }))
95 .child(
96 h_flex()
97 .pt(px(2.))
98 .gap(px(8.))
99 .child(card_button(
100 "meeting-proposal-create",
101 "Create",
102 theme::accent(),
103 theme::on_accent(),
104 create,
105 ))
106 .child(card_button(
107 "meeting-proposal-discard",
108 "Discard",
109 theme::surface_raised(),
110 theme::text(),
111 discard,
112 )),
113 )
114}
115
116pub fn meeting_proposal_cards(cx: &App) -> Vec<AnyElement> {
117 let now = Utc::now();
118 meetings::pending_meetings(cx)
119 .iter()
120 .map(|proposal| {
121 card(proposal, now)
122 .id(SharedString::from(proposal.id.clone()))
123 .into_any_element()
124 })
125 .collect()
126}
127
128#[cfg(test)]
129mod tests {
130 use super::description_excerpt;
131
132 #[test]
133 fn long_descriptions_show_three_lines() {
134 assert_eq!(description_excerpt(" \n"), None);
135 assert_eq!(
136 description_excerpt("a\n\nb\nc\nd").as_deref(),
137 Some("a\nb\nc ...")
138 );
139 }
140}