BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / conversation / pins.rs

pins.rs

pins.rs 9.7 KiB Download Raw
1use std::collections::HashMap;
2use std::rc::Rc;
3
4use gpui_kit::*;
5use store::MessageRecord;
6
7use super::ConversationView;
8use gpui_kit::component::WindowExt as _;
9use gpui_kit::component::button::{Button, ButtonVariants as _};
10use gpui_kit::component::dialog::DialogFooter;
11
12use crate::app_state::{AppState, Selection};
13use crate::channel_menu::post_pin_dialog;
14use crate::rows::reply_excerpt;
15use crate::runtime;
16use crate::views::message_actions::Action;
17use crate::views::pin_banner::{PinBannerActions, PinBannerData, cycle_pin, render_pin_banner};
18
19pub(super) struct PinPreview {
20 author: Option<String>,
21 text: String,
22}
23
24impl PinPreview {
25 fn of(record: &MessageRecord) -> Self {
26 PinPreview {
27 author: record.sender_name.clone(),
28 text: reply_excerpt(record),
29 }
30 }
31}
32
33fn confirm_post_pin(
34 state: &Entity<AppState>,
35 channel_id: &str,
36 message_id: &str,
37 pin: bool,
38 window: &mut Window,
39 cx: &mut App,
40) {
41 let dialog = post_pin_dialog(pin);
42 let (state, channel_id, message_id) =
43 (state.clone(), channel_id.to_owned(), message_id.to_owned());
44 window.open_alert_dialog(cx, move |alert, _, _| {
45 let (state, channel_id, message_id) =
46 (state.clone(), channel_id.clone(), message_id.clone());
47 let cancel = Button::new("post-pin-cancel")
48 .label("Cancel")
49 .on_click(|_, window, cx| window.close_dialog(cx));
50 let confirm = Button::new("post-pin-confirm")
51 .label(dialog.confirm)
52 .primary()
53 .on_click(move |_, window, cx| {
54 state.update(cx, |state, cx| {
55 state.set_pinned(&channel_id, &message_id, pin, cx)
56 });
57 window.close_dialog(cx);
58 });
59 alert
60 .title(dialog.title)
61 .description(dialog.body)
62 .footer(DialogFooter::new().child(cancel).child(confirm))
63 .on_ok(|_, _, _| false)
64 });
65}
66
67impl ConversationView {
68 pub(super) fn chat_id(&self) -> Option<String> {
69 match self.current.as_ref()?.selection {
70 Selection::Chat(ref chat_id) => Some(chat_id.clone()),
71 Selection::Channel(_) => None,
72 }
73 }
74
75 pub(super) fn pin_conversation_id(&self) -> Option<String> {
76 self.current
77 .as_ref()
78 .map(|current| current.selection.conversation_id().to_owned())
79 }
80
81 pub(super) fn refresh_pin_previews(&mut self, cx: &mut Context<Self>) {
82 self.pin_previews.clear();
83 self.pin_fetches.clear();
84 self.pin_index = 0;
85 let Some(conversation_id) = self.pin_conversation_id() else {
86 return;
87 };
88 self.update_pin_previews(&conversation_id, cx);
89 cx.notify();
90 }
91
92 pub(super) fn on_pins_changed(&mut self, conversation_id: &str, cx: &mut Context<Self>) {
93 if self.pin_conversation_id().as_deref() != Some(conversation_id) {
94 return;
95 }
96 self.pin_index = 0;
97 self.update_pin_previews(conversation_id, cx);
98 cx.notify();
99 }
100
101 fn update_pin_previews(&mut self, conversation_id: &str, cx: &mut Context<Self>) {
102 let state = self.app.read(cx);
103 let ids: Vec<String> = state
104 .pinned_messages(conversation_id)
105 .iter()
106 .map(|pin| pin.message_id.clone())
107 .collect();
108 let records = state
109 .store
110 .messages_by_id(conversation_id, &ids)
111 .unwrap_or_default();
112 let mut previews: HashMap<String, PinPreview> = records
113 .into_iter()
114 .map(|(message_id, record)| (message_id, PinPreview::of(&record)))
115 .collect();
116 for id in &ids {
117 if !previews.contains_key(id)
118 && let Some(preview) = self.pin_previews.remove(id)
119 {
120 previews.insert(id.clone(), preview);
121 }
122 }
123 self.pin_previews = previews;
124 let count = ids.len();
125 if self.pin_index >= count {
126 self.pin_index = 0;
127 }
128 for id in ids {
129 if !self.pin_previews.contains_key(&id) {
130 self.fetch_pin_preview(conversation_id, id, cx);
131 }
132 }
133 }
134
135 fn fetch_pin_preview(
136 &mut self,
137 conversation_id: &str,
138 message_id: String,
139 cx: &mut Context<Self>,
140 ) {
141 let Some(engine) = self.app.read(cx).engine.clone() else {
142 return;
143 };
144 if self.chat_id().is_none() || !self.pin_fetches.insert(message_id.clone()) {
145 return;
146 }
147 let conversation_id = conversation_id.to_owned();
148 let task_conversation_id = conversation_id.clone();
149 let task_message_id = message_id.clone();
150 let receiver = runtime::spawn(async move {
151 engine
152 .fetch_chat_message(&task_conversation_id, &task_message_id)
153 .await
154 });
155 cx.spawn(async move |this, cx| {
156 let Ok(Ok(Some(record))) = receiver.await else {
157 return;
158 };
159 this.update(cx, |this, cx| {
160 if this.conversation_id().as_deref() == Some(conversation_id.as_str()) {
161 this.pin_previews
162 .insert(message_id, PinPreview::of(&record));
163 cx.notify();
164 }
165 })
166 .ok();
167 })
168 .detach();
169 }
170
171 fn pin_action(
172 &self,
173 view: WeakEntity<Self>,
174 run: fn(&mut Self, &mut Window, &mut Context<Self>),
175 ) -> Action {
176 Rc::new(move |window, cx| {
177 view.update(cx, |this, cx| run(this, window, cx)).ok();
178 })
179 }
180
181 pub(super) fn render_pin_banner(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
182 let conversation_id = self.chat_id()?;
183 let pins = self.app.read(cx).pinned_messages(&conversation_id);
184 let pin = pins.get(self.pin_index)?;
185 let preview = self.pin_previews.get(&pin.message_id);
186 let data = PinBannerData {
187 author: preview.and_then(|preview| preview.author.clone()),
188 preview: preview.map(|preview| preview.text.clone()),
189 position: self.pin_index,
190 total: pins.len(),
191 };
192 let view = cx.weak_entity();
193 let actions = PinBannerActions {
194 open: self.pin_action(view.clone(), |this, _, cx| this.open_shown_pin(cx)),
195 previous: self.pin_action(view.clone(), |this, _, cx| this.step_pin(-1, cx)),
196 next: self.pin_action(view.clone(), |this, _, cx| this.step_pin(1, cx)),
197 unpin: self.pin_action(view, |this, _, cx| this.unpin_shown_pin(cx)),
198 };
199 Some(render_pin_banner(data, actions))
200 }
201
202 fn shown_pin_id(&self, cx: &App) -> Option<String> {
203 let conversation_id = self.pin_conversation_id()?;
204 self.app
205 .read(cx)
206 .pinned_messages(&conversation_id)
207 .get(self.pin_index)
208 .map(|pin| pin.message_id.clone())
209 }
210
211 fn open_shown_pin(&mut self, cx: &mut Context<Self>) {
212 let (Some(conversation_id), Some(message_id)) =
213 (self.pin_conversation_id(), self.shown_pin_id(cx))
214 else {
215 return;
216 };
217 self.jump_to_message(&conversation_id, &message_id, cx);
218 }
219
220 fn step_pin(&mut self, delta: isize, cx: &mut Context<Self>) {
221 let Some(conversation_id) = self.pin_conversation_id() else {
222 return;
223 };
224 let total = self.app.read(cx).pinned_messages(&conversation_id).len();
225 self.pin_index = cycle_pin(self.pin_index, total, delta);
226 cx.notify();
227 }
228
229 fn unpin_shown_pin(&mut self, cx: &mut Context<Self>) {
230 let (Some(conversation_id), Some(message_id)) =
231 (self.pin_conversation_id(), self.shown_pin_id(cx))
232 else {
233 return;
234 };
235 self.app.update(cx, |state, cx| {
236 state.set_pinned(&conversation_id, &message_id, false, cx)
237 });
238 }
239
240 pub(super) fn forward_message(&mut self, message_id: &str, cx: &mut Context<Self>) {
241 if let Some(conversation_id) = self.conversation_id() {
242 self.app.update(cx, |state, cx| {
243 state.request_forward(&conversation_id, message_id, cx)
244 });
245 }
246 }
247
248 pub(super) fn copy_link(&mut self, message_id: &str, cx: &mut Context<Self>) {
249 if let Some(conversation_id) = self.conversation_id() {
250 self.app.update(cx, |state, cx| {
251 state.copy_message_link(&conversation_id, message_id, cx)
252 });
253 }
254 }
255
256 pub(super) fn toggle_saved(&mut self, message_id: &str, cx: &mut Context<Self>) {
257 if let Some(conversation_id) = self.conversation_id() {
258 self.app.update(cx, |state, cx| {
259 state.toggle_saved(&conversation_id, message_id, cx)
260 });
261 }
262 }
263
264 pub(super) fn toggle_pinned(
265 &mut self,
266 message_id: &str,
267 window: &mut Window,
268 cx: &mut Context<Self>,
269 ) {
270 let Some(conversation_id) = self.pin_conversation_id() else {
271 return;
272 };
273 if self.chat_id().is_some() {
274 self.app.update(cx, |state, cx| {
275 state.toggle_pinned(&conversation_id, message_id, cx)
276 });
277 return;
278 }
279 let pin = !self.app.read(cx).is_pinned(&conversation_id, message_id);
280 confirm_post_pin(&self.app, &conversation_id, message_id, pin, window, cx);
281 }
282
283 pub(super) fn mark_unread_from(&mut self, message_id: &str, cx: &mut Context<Self>) {
284 if let Some(chat_id) = self.chat_id() {
285 self.app.update(cx, |state, cx| {
286 state.mark_unread_from_message(&chat_id, message_id, cx)
287 });
288 }
289 }
290}