use std::collections::HashMap; use std::rc::Rc; use gpui_kit::*; use store::MessageRecord; use super::ConversationView; use gpui_kit::component::WindowExt as _; use gpui_kit::component::button::{Button, ButtonVariants as _}; use gpui_kit::component::dialog::DialogFooter; use crate::app_state::{AppState, Selection, selection_title}; use crate::channel_menu::post_pin_dialog; use crate::i18n::tr; use crate::post_follow::post_followed; use crate::rows::{Row, reply_excerpt}; use crate::runtime; use crate::views::message_actions::Action; use crate::views::pin_banner::{PinBannerActions, PinBannerData, cycle_pin, render_pin_banner}; pub(super) struct PinPreview { author: Option, text: String, } impl PinPreview { fn of(record: &MessageRecord) -> Self { PinPreview { author: record.sender_name.clone(), text: reply_excerpt(record), } } } fn confirm_post_pin( state: &Entity, channel_id: &str, message_id: &str, pin: bool, window: &mut Window, cx: &mut App, ) { let dialog = post_pin_dialog(pin); let (state, channel_id, message_id) = (state.clone(), channel_id.to_owned(), message_id.to_owned()); window.open_alert_dialog(cx, move |alert, _, _| { let (state, channel_id, message_id) = (state.clone(), channel_id.clone(), message_id.clone()); let cancel = Button::new("post-pin-cancel") .label(tr("common.cancel")) .on_click(|_, window, cx| window.close_dialog(cx)); let confirm = Button::new("post-pin-confirm") .label(dialog.confirm) .primary() .on_click(move |_, window, cx| { state.update(cx, |state, cx| { state.set_pinned(&channel_id, &message_id, pin, cx) }); window.close_dialog(cx); }); alert .title(dialog.title) .description(dialog.body) .footer(DialogFooter::new().child(cancel).child(confirm)) .on_ok(|_, _, _| false) }); } impl ConversationView { pub(super) fn chat_id(&self) -> Option { match self.current.as_ref()?.selection { Selection::Chat(ref chat_id) => Some(chat_id.clone()), Selection::Channel(_) => None, } } pub(super) fn pin_conversation_id(&self) -> Option { self.current .as_ref() .map(|current| current.selection.conversation_id().to_owned()) } pub(super) fn refresh_pin_previews(&mut self, cx: &mut Context) { self.pin_previews.clear(); self.pin_fetches.clear(); self.pin_index = 0; let Some(conversation_id) = self.pin_conversation_id() else { return; }; self.update_pin_previews(&conversation_id, cx); cx.notify(); } pub(super) fn on_pins_changed(&mut self, conversation_id: &str, cx: &mut Context) { if self.pin_conversation_id().as_deref() != Some(conversation_id) { return; } self.pin_index = 0; self.update_pin_previews(conversation_id, cx); cx.notify(); } fn update_pin_previews(&mut self, conversation_id: &str, cx: &mut Context) { let state = self.app.read(cx); let ids: Vec = state .pinned_messages(conversation_id) .iter() .map(|pin| pin.message_id.clone()) .collect(); let records = state .store .messages_by_id(conversation_id, &ids) .unwrap_or_default(); let mut previews: HashMap = records .into_iter() .map(|(message_id, record)| (message_id, PinPreview::of(&record))) .collect(); for id in &ids { if !previews.contains_key(id) && let Some(preview) = self.pin_previews.remove(id) { previews.insert(id.clone(), preview); } } self.pin_previews = previews; let count = ids.len(); if self.pin_index >= count { self.pin_index = 0; } for id in ids { if !self.pin_previews.contains_key(&id) { self.fetch_pin_preview(conversation_id, id, cx); } } } fn fetch_pin_preview( &mut self, conversation_id: &str, message_id: String, cx: &mut Context, ) { let Some(engine) = self.app.read(cx).engine.clone() else { return; }; if self.chat_id().is_none() || !self.pin_fetches.insert(message_id.clone()) { return; } let conversation_id = conversation_id.to_owned(); let task_conversation_id = conversation_id.clone(); let task_message_id = message_id.clone(); let receiver = runtime::spawn(async move { engine .fetch_chat_message(&task_conversation_id, &task_message_id) .await }); cx.spawn(async move |this, cx| { let Ok(Ok(Some(record))) = receiver.await else { return; }; this.update(cx, |this, cx| { if this.conversation_id().as_deref() == Some(conversation_id.as_str()) { this.pin_previews .insert(message_id, PinPreview::of(&record)); cx.notify(); } }) .ok(); }) .detach(); } fn pin_action( &self, view: WeakEntity, run: fn(&mut Self, &mut Window, &mut Context), ) -> Action { Rc::new(move |window, cx| { view.update(cx, |this, cx| run(this, window, cx)).ok(); }) } pub(super) fn render_pin_banner(&self, cx: &mut Context) -> Option { let conversation_id = self.chat_id()?; let pins = self.app.read(cx).pinned_messages(&conversation_id); let pin = pins.get(self.pin_index)?; let preview = self.pin_previews.get(&pin.message_id); let data = PinBannerData { author: preview.and_then(|preview| preview.author.clone()), preview: preview.map(|preview| preview.text.clone()), position: self.pin_index, total: pins.len(), }; let view = cx.weak_entity(); let actions = PinBannerActions { open: self.pin_action(view.clone(), |this, _, cx| this.open_shown_pin(cx)), previous: self.pin_action(view.clone(), |this, _, cx| this.step_pin(-1, cx)), next: self.pin_action(view.clone(), |this, _, cx| this.step_pin(1, cx)), unpin: self.pin_action(view, |this, _, cx| this.unpin_shown_pin(cx)), }; Some(render_pin_banner(data, actions)) } fn shown_pin_id(&self, cx: &App) -> Option { let conversation_id = self.pin_conversation_id()?; self.app .read(cx) .pinned_messages(&conversation_id) .get(self.pin_index) .map(|pin| pin.message_id.clone()) } fn open_shown_pin(&mut self, cx: &mut Context) { let (Some(conversation_id), Some(message_id)) = (self.pin_conversation_id(), self.shown_pin_id(cx)) else { return; }; self.jump_to_message(&conversation_id, &message_id, cx); } fn step_pin(&mut self, delta: isize, cx: &mut Context) { let Some(conversation_id) = self.pin_conversation_id() else { return; }; let total = self.app.read(cx).pinned_messages(&conversation_id).len(); self.pin_index = cycle_pin(self.pin_index, total, delta); cx.notify(); } fn unpin_shown_pin(&mut self, cx: &mut Context) { let (Some(conversation_id), Some(message_id)) = (self.pin_conversation_id(), self.shown_pin_id(cx)) else { return; }; self.app.update(cx, |state, cx| { state.set_pinned(&conversation_id, &message_id, false, cx) }); } pub(super) fn forward_message(&mut self, message_id: &str, cx: &mut Context) { if let Some(conversation_id) = self.conversation_id() { self.app.update(cx, |state, cx| { state.request_forward(&conversation_id, message_id, cx) }); } } pub(super) fn copy_link(&mut self, message_id: &str, cx: &mut Context) { if let Some(conversation_id) = self.conversation_id() { self.app.update(cx, |state, cx| { state.copy_message_link(&conversation_id, message_id, cx) }); } } pub(super) fn share_to_outlook(&mut self, message_id: &str, cx: &mut Context) { let Some(current) = self.current.as_ref() else { return; }; let conversation_id = current.selection.conversation_id().to_owned(); let title = selection_title(&self.app.read(cx).sidebar, ¤t.selection); self.app.update(cx, |state, cx| { state.share_to_outlook(&conversation_id, message_id, &title, cx) }); } pub(super) fn toggle_saved(&mut self, message_id: &str, cx: &mut Context) { if let Some(conversation_id) = self.conversation_id() { self.app.update(cx, |state, cx| { state.toggle_saved(&conversation_id, message_id, cx) }); } } pub(super) fn toggle_pinned( &mut self, message_id: &str, window: &mut Window, cx: &mut Context, ) { let Some(conversation_id) = self.pin_conversation_id() else { return; }; if self.chat_id().is_some() { self.app.update(cx, |state, cx| { state.toggle_pinned(&conversation_id, message_id, cx) }); return; } let pin = !self.app.read(cx).is_pinned(&conversation_id, message_id); confirm_post_pin(&self.app, &conversation_id, message_id, pin, window, cx); } /// `Some(followed)` for a channel root post, `None` for everything else. pub(super) fn post_follow(&self, message_id: &str, cx: &App) -> Option { let Selection::Channel(ref channel_id) = self.current.as_ref()?.selection else { return None; }; let participated = self.rows.borrow().iter().find_map(|row| match row { Row::Post(post) if post.root.key == message_id => { Some(post.messages().any(|message| message.own)) } _ => None, })?; let state = self.app.read(cx).post_follow_state(channel_id, message_id); Some(post_followed(state, participated)) } pub(super) fn request_post_follow(&mut self, message_id: &str, cx: &mut Context) { if let Some(Selection::Channel(channel_id)) = self .current .as_ref() .map(|current| current.selection.clone()) && self.post_follow(message_id, cx).is_some() { self.app.update(cx, |state, cx| { state.request_post_follow_state(&channel_id, message_id, cx) }); } } pub(super) fn toggle_post_follow(&mut self, message_id: &str, cx: &mut Context) { let Some(followed) = self.post_follow(message_id, cx) else { return; }; let Some(channel_id) = self.pin_conversation_id() else { return; }; self.app.update(cx, |state, cx| { state.set_post_followed(&channel_id, message_id, !followed, cx) }); } pub(super) fn mark_unread_from(&mut self, message_id: &str, cx: &mut Context) { if let Some(chat_id) = self.chat_id() { self.app.update(cx, |state, cx| { state.mark_unread_from_message(&chat_id, message_id, cx) }); } } }