BlueGit

ict rusty-teams

Files

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

saved_panel.rs

saved_panel.rs 8.3 KiB Download Raw
1use chrono::{DateTime, Local, Offset};
2use gpui_kit::assets::IconName;
3use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex};
4use gpui_kit::*;
5use teams_core::SavedMessage;
6
7use super::avatar::person_avatar;
8use super::scroll_edge::with_top_edge;
9use super::sidebar::SIDEBAR_SCROLL_DIVIDER;
10use super::widgets::icon;
11use crate::app_state::{AppState, selection_for, selection_title};
12use crate::format;
13use crate::theme;
14
15const AVATAR_SIZE: f32 = 32.;
16const BUTTON_SIZE: f32 = 24.;
17const PREVIEW_LINES: usize = 2;
18const EMPTY_TEXT: &str = "No saved messages. Use Save message in the ... menu of a message.";
19const UNKNOWN_AUTHOR: &str = "Unknown";
20
21pub enum SavedPanelEvent {
22 Open {
23 conversation_id: String,
24 message_id: String,
25 },
26}
27
28pub struct SavedPanel {
29 app: Entity<AppState>,
30 scroll: ScrollHandle,
31 _observation: Subscription,
32}
33
34impl EventEmitter<SavedPanelEvent> for SavedPanel {}
35
36pub fn count_label(count: usize) -> String {
37 match count {
38 0 => String::new(),
39 count => count.to_string(),
40 }
41}
42
43impl SavedPanel {
44 pub fn new(app: Entity<AppState>, cx: &mut Context<Self>) -> Self {
45 let observation = cx.observe(&app, |_, _, cx| cx.notify());
46 SavedPanel {
47 app,
48 scroll: ScrollHandle::new(),
49 _observation: observation,
50 }
51 }
52
53 fn header(&self, count: usize) -> Div {
54 h_flex()
55 .h(px(52.))
56 .flex_none()
57 .pl(px(16.))
58 .pr(px(12.))
59 .gap(px(8.))
60 .items_center()
61 .child(
62 div()
63 .text_size(px(16.))
64 .font_weight(FontWeight::BOLD)
65 .text_color(theme::text())
66 .child("Saved"),
67 )
68 .child(
69 div()
70 .text_size(px(12.))
71 .text_color(theme::text_muted())
72 .child(count_label(count)),
73 )
74 }
75
76 fn row(
77 &self,
78 item: &SavedMessage,
79 index: usize,
80 title: String,
81 panel: Entity<SavedPanel>,
82 cx: &App,
83 ) -> AnyElement {
84 let app = self.app.clone();
85 let state = self.app.read(cx);
86 let now = Local::now();
87 let author = item.author_name.as_deref().unwrap_or(UNKNOWN_AUTHOR);
88 let group = SharedString::from(format!("saved-row-group-{index}"));
89 let (conversation_id, message_id) = (item.conversation_id.clone(), item.message_id.clone());
90 let unsave_item = item.clone();
91 h_flex()
92 .id(ElementId::Name(format!("saved-row-{index}").into()))
93 .group(group.clone())
94 .relative()
95 .mx(px(6.))
96 .p(px(10.))
97 .gap(px(12.))
98 .items_start()
99 .rounded(px(8.))
100 .cursor_pointer()
101 .hover(|row| row.bg(theme::row_hover()))
102 .child(div().flex_none().size(px(AVATAR_SIZE)).child(person_avatar(
103 &state.directory,
104 item.author_id.as_deref(),
105 author,
106 AVATAR_SIZE,
107 )))
108 .child(
109 v_flex()
110 .flex_1()
111 .min_w_0()
112 .gap(px(4.))
113 .child(
114 h_flex()
115 .gap(px(6.))
116 .items_baseline()
117 .child(
118 div()
119 .flex_none()
120 .text_size(px(13.))
121 .font_weight(FontWeight::SEMIBOLD)
122 .text_color(theme::text())
123 .child(author.to_owned()),
124 )
125 .child(
126 div()
127 .flex_1()
128 .min_w_0()
129 .truncate()
130 .text_size(px(12.))
131 .text_color(theme::text_muted())
132 .child(title),
133 )
134 .child(
135 div()
136 .flex_none()
137 .text_size(px(11.))
138 .text_color(theme::text_muted())
139 .child(saved_time(item.saved_at, now)),
140 ),
141 )
142 .child(
143 div()
144 .line_clamp(PREVIEW_LINES)
145 .text_size(px(13.))
146 .line_height(relative(1.4))
147 .text_color(theme::text_soft())
148 .child(item.preview.clone()),
149 ),
150 )
151 .child(
152 div()
153 .id(ElementId::Name(format!("saved-unsave-{index}").into()))
154 .absolute()
155 .right(px(6.))
156 .bottom(px(6.))
157 .size(px(BUTTON_SIZE))
158 .flex()
159 .items_center()
160 .justify_center()
161 .rounded(px(6.))
162 .bg(theme::surface_raised())
163 .cursor_pointer()
164 .opacity(0.)
165 .group_hover(group, |button| button.opacity(1.))
166 .hover(|button| button.bg(theme::border_strong()))
167 .child(icon(IconName::BookmarkOff, 14., theme::text_muted()))
168 .tooltip(|window, cx| Tooltip::new("Unsave").build(window, cx))
169 .on_click(move |_, _, cx| {
170 cx.stop_propagation();
171 let entry = unsave_item.clone();
172 app.update(cx, |state, cx| state.set_saved(entry, false, cx));
173 }),
174 )
175 .on_click(move |_, _, cx| {
176 let event = SavedPanelEvent::Open {
177 conversation_id: conversation_id.clone(),
178 message_id: message_id.clone(),
179 };
180 panel.update(cx, |_, cx| cx.emit(event));
181 })
182 .into_any_element()
183 }
184}
185
186fn saved_time(saved_at: DateTime<chrono::Utc>, now: DateTime<Local>) -> String {
187 format::list_time_label(saved_at, now.date_naive(), now.offset().fix())
188}
189
190impl Render for SavedPanel {
191 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
192 let panel = cx.entity();
193 let state = self.app.read(cx);
194 let items = state.saved.items().to_vec();
195 let mut rows: Vec<AnyElement> = items
196 .iter()
197 .enumerate()
198 .map(|(index, item)| {
199 let title = selection_for(&state.sidebar, &item.conversation_id)
200 .map(|selection| selection_title(&state.sidebar, &selection))
201 .or_else(|| item.topic.clone())
202 .unwrap_or_default();
203 self.row(item, index, title, panel.clone(), cx)
204 })
205 .collect();
206 if rows.is_empty() {
207 rows.push(
208 div()
209 .py(px(32.))
210 .px(px(24.))
211 .flex()
212 .justify_center()
213 .text_center()
214 .text_size(px(13.))
215 .text_color(theme::text_muted())
216 .child(EMPTY_TEXT)
217 .into_any_element(),
218 );
219 }
220 v_flex()
221 .flex_1()
222 .min_h_0()
223 .child(self.header(items.len()))
224 .child(with_top_edge(
225 v_flex()
226 .id("saved-list")
227 .flex_1()
228 .min_h_0()
229 .pb(px(12.))
230 .overflow_y_scroll()
231 .track_scroll(&self.scroll)
232 .children(rows),
233 &self.scroll,
234 SIDEBAR_SCROLL_DIVIDER,
235 ))
236 }
237}
238
239#[cfg(test)]
240mod tests {
241 use super::count_label;
242
243 #[test]
244 fn the_header_count_is_hidden_when_nothing_is_saved() {
245 assert_eq!(count_label(0), "");
246 assert_eq!(count_label(2), "2");
247 }
248}