| 1 | use std::future::Future; |
| 2 | |
| 3 | use chrono::{Duration, Local, Offset, Utc}; |
| 4 | use gpui_kit::*; |
| 5 | use store::ChatRecord; |
| 6 | |
| 7 | use crate::app_state::{AppState, Selection, chat_title}; |
| 8 | use crate::notice::{NoticeAction, short_error, truncated}; |
| 9 | use crate::sidebar_model::{SectionInput, build_sections, next_chat_id}; |
| 10 | |
| 11 | pub const TITLE_LIMIT: usize = 40; |
| 12 | |
| 13 | impl AppState { |
| 14 | pub fn keeps_unread(&self, chat_id: &str) -> bool { |
| 15 | self.keep_unread.as_deref() == Some(chat_id) |
| 16 | } |
| 17 | |
| 18 | pub fn mark_chat_unread(&mut self, chat_id: &str, cx: &mut Context<Self>) { |
| 19 | let Some(chat) = self.chat_record(chat_id) else { |
| 20 | return; |
| 21 | }; |
| 22 | let Some(last_message_at) = chat.last_message_at else { |
| 23 | return; |
| 24 | }; |
| 25 | let Some(engine_or_demo) = self.chat_action_engine(cx) else { |
| 26 | return; |
| 27 | }; |
| 28 | let last_read_at = last_message_at - Duration::milliseconds(1); |
| 29 | let _ = self.store.mark_chat_unread(chat_id, last_read_at); |
| 30 | if self.selection == Some(Selection::Chat(chat_id.to_owned())) { |
| 31 | self.keep_unread = Some(chat_id.to_owned()); |
| 32 | } |
| 33 | self.reload_sidebar(cx); |
| 34 | let Some(engine) = engine_or_demo else { |
| 35 | return; |
| 36 | }; |
| 37 | let owned_id = chat_id.to_owned(); |
| 38 | let revert_id = owned_id.clone(); |
| 39 | let restored_read_at = chat.last_read_at.unwrap_or(last_message_at); |
| 40 | self.run_chat_action( |
| 41 | "Mark as unread", |
| 42 | async move { engine.mark_unread(&owned_id).await }, |
| 43 | move |state, cx| { |
| 44 | let _ = state.store.mark_chat_read(&revert_id, restored_read_at); |
| 45 | if state.keep_unread.as_deref() == Some(revert_id.as_str()) { |
| 46 | state.keep_unread = None; |
| 47 | } |
| 48 | state.reload_sidebar(cx); |
| 49 | }, |
| 50 | |_, _| {}, |
| 51 | cx, |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | pub fn set_chat_muted(&mut self, chat_id: &str, muted: bool, cx: &mut Context<Self>) { |
| 56 | if self.chat_record(chat_id).is_none() { |
| 57 | return; |
| 58 | } |
| 59 | let Some(engine_or_demo) = self.chat_action_engine(cx) else { |
| 60 | return; |
| 61 | }; |
| 62 | let _ = self.store.set_chat_muted(chat_id, muted); |
| 63 | self.reload_sidebar(cx); |
| 64 | let Some(engine) = engine_or_demo else { |
| 65 | return; |
| 66 | }; |
| 67 | let owned_id = chat_id.to_owned(); |
| 68 | let revert_id = owned_id.clone(); |
| 69 | self.run_chat_action( |
| 70 | if muted { "Mute" } else { "Unmute" }, |
| 71 | async move { engine.set_chat_muted(&owned_id, muted).await }, |
| 72 | move |state, cx| { |
| 73 | let _ = state.store.set_chat_muted(&revert_id, !muted); |
| 74 | state.reload_sidebar(cx); |
| 75 | }, |
| 76 | |_, _| {}, |
| 77 | cx, |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | pub fn hide_chat(&mut self, chat_id: &str, cx: &mut Context<Self>) { |
| 82 | let Some(chat) = self.chat_record(chat_id) else { |
| 83 | return; |
| 84 | }; |
| 85 | let Some(engine_or_demo) = self.chat_action_engine(cx) else { |
| 86 | return; |
| 87 | }; |
| 88 | self.remove_chat_locally(chat_id, cx); |
| 89 | let Some(engine) = engine_or_demo else { |
| 90 | return; |
| 91 | }; |
| 92 | let owned_id = chat_id.to_owned(); |
| 93 | let undo_id = owned_id.clone(); |
| 94 | let text = format!("\"{}\" hidden", truncated(&chat_title(&chat), TITLE_LIMIT)); |
| 95 | self.run_chat_action( |
| 96 | "Hide", |
| 97 | async move { engine.hide_chat(&owned_id).await }, |
| 98 | move |state, cx| state.restore_chat_locally(&chat, cx), |
| 99 | move |state, cx| { |
| 100 | let undo = |
| 101 | NoticeAction::new("Undo", move |state, cx| state.unhide_chat(&undo_id, cx)); |
| 102 | state.raise_notice(text, Some(undo), cx); |
| 103 | }, |
| 104 | cx, |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | pub fn unhide_chat(&mut self, chat_id: &str, cx: &mut Context<Self>) { |
| 109 | let Some(engine) = self.chat_action_engine(cx).flatten() else { |
| 110 | return; |
| 111 | }; |
| 112 | let owned_id = chat_id.to_owned(); |
| 113 | self.run_chat_action( |
| 114 | "Undo", |
| 115 | async move { engine.unhide_chat(&owned_id).await }, |
| 116 | |_, _| {}, |
| 117 | |_, _| {}, |
| 118 | cx, |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | pub fn leave_chat(&mut self, chat_id: &str, cx: &mut Context<Self>) { |
| 123 | let Some(chat) = self.chat_record(chat_id) else { |
| 124 | return; |
| 125 | }; |
| 126 | let Some(engine_or_demo) = self.chat_action_engine(cx) else { |
| 127 | return; |
| 128 | }; |
| 129 | self.remove_chat_locally(chat_id, cx); |
| 130 | let Some(engine) = engine_or_demo else { |
| 131 | return; |
| 132 | }; |
| 133 | let owned_id = chat_id.to_owned(); |
| 134 | self.run_chat_action( |
| 135 | "Leave chat", |
| 136 | async move { engine.leave_chat(&owned_id).await }, |
| 137 | move |state, cx| state.restore_chat_locally(&chat, cx), |
| 138 | |_, _| {}, |
| 139 | cx, |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | fn chat_record(&self, chat_id: &str) -> Option<ChatRecord> { |
| 144 | self.sidebar |
| 145 | .chats |
| 146 | .iter() |
| 147 | .find(|chat| chat.id == chat_id) |
| 148 | .cloned() |
| 149 | } |
| 150 | |
| 151 | /// `None` stops the action. `Some(None)` is demo mode, which only changes the local store. |
| 152 | fn chat_action_engine( |
| 153 | &mut self, |
| 154 | cx: &mut Context<Self>, |
| 155 | ) -> Option<Option<std::sync::Arc<crate::backend::Engine>>> { |
| 156 | if self.mode.read_only { |
| 157 | self.raise_notice("Read-only mode: chat not changed".to_owned(), None, cx); |
| 158 | return None; |
| 159 | } |
| 160 | match self.engine.clone() { |
| 161 | Some(engine) => Some(Some(engine)), |
| 162 | None if self.mode.demo => Some(None), |
| 163 | None => None, |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | fn remove_chat_locally(&mut self, chat_id: &str, cx: &mut Context<Self>) { |
| 168 | let was_selected = self.selection == Some(Selection::Chat(chat_id.to_owned())); |
| 169 | let next = was_selected |
| 170 | .then(|| self.next_chat_after(chat_id)) |
| 171 | .flatten(); |
| 172 | let _ = self.store.remove_chats(&[chat_id.to_owned()]); |
| 173 | self.reload_sidebar(cx); |
| 174 | if was_selected { |
| 175 | match next { |
| 176 | Some(next_id) => self.select(Selection::Chat(next_id), cx), |
| 177 | None => { |
| 178 | self.selection = None; |
| 179 | self.keep_unread = None; |
| 180 | cx.emit(crate::app_state::AppEvent::Selection); |
| 181 | cx.notify(); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | fn restore_chat_locally(&mut self, chat: &ChatRecord, cx: &mut Context<Self>) { |
| 188 | let _ = self.store.upsert_chats(std::slice::from_ref(chat)); |
| 189 | let _ = self.store.set_chat_muted(&chat.id, chat.muted); |
| 190 | self.reload_sidebar(cx); |
| 191 | } |
| 192 | |
| 193 | fn next_chat_after(&self, chat_id: &str) -> Option<String> { |
| 194 | let input = SectionInput { |
| 195 | chats: &self.sidebar.chats, |
| 196 | directory: &self.directory, |
| 197 | collapsed: &self.collapsed, |
| 198 | now: Utc::now(), |
| 199 | offset: Local::now().offset().fix(), |
| 200 | }; |
| 201 | next_chat_id(&build_sections(&input), chat_id) |
| 202 | } |
| 203 | |
| 204 | fn run_chat_action( |
| 205 | &mut self, |
| 206 | label: &'static str, |
| 207 | call: impl Future<Output = teams_core::Result<()>> + Send + 'static, |
| 208 | revert: impl FnOnce(&mut Self, &mut Context<Self>) + 'static, |
| 209 | on_success: impl FnOnce(&mut Self, &mut Context<Self>) + 'static, |
| 210 | cx: &mut Context<Self>, |
| 211 | ) { |
| 212 | let receiver = crate::runtime::spawn(call); |
| 213 | cx.spawn(async move |this, cx| { |
| 214 | let failure = match receiver.await { |
| 215 | Ok(Ok(())) => None, |
| 216 | Ok(Err(error)) => Some(short_error(&error)), |
| 217 | Err(_) => Some("cancelled".to_owned()), |
| 218 | }; |
| 219 | this.update(cx, |state, cx| match failure { |
| 220 | None => on_success(state, cx), |
| 221 | Some(reason) => { |
| 222 | revert(state, cx); |
| 223 | state.raise_notice(format!("{label} failed: {reason}"), None, cx); |
| 224 | } |
| 225 | }) |
| 226 | .ok(); |
| 227 | }) |
| 228 | .detach(); |
| 229 | } |
| 230 | } |