BlueGit

ict rusty-teams

Files

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

meeting_proposal_card.rs

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