| 1 | use chrono::{DateTime, Utc}; |
| 2 | use gpui_kit::*; |
| 3 | use teams_core::{JumpPlan, WindowDirection, WindowPage, jump_plan}; |
| 4 | |
| 5 | use super::ConversationView; |
| 6 | use crate::notice::short_error; |
| 7 | use crate::runtime; |
| 8 | |
| 9 | pub(super) struct DetachedWindow { |
| 10 | pub(super) anchor_id: String, |
| 11 | pub(super) oldest: DateTime<Utc>, |
| 12 | pub(super) newest: DateTime<Utc>, |
| 13 | pub(super) older_cursor: Option<String>, |
| 14 | pub(super) newer_cursor: Option<String>, |
| 15 | pub(super) loading: bool, |
| 16 | } |
| 17 | |
| 18 | impl ConversationView { |
| 19 | pub(super) fn is_detached(&self) -> bool { |
| 20 | self.current |
| 21 | .as_ref() |
| 22 | .is_some_and(|current| current.detached.is_some()) |
| 23 | } |
| 24 | |
| 25 | pub(super) fn jump_plan_for( |
| 26 | &self, |
| 27 | chat_id: &str, |
| 28 | target_created_at: Option<DateTime<Utc>>, |
| 29 | cx: &App, |
| 30 | ) -> JumpPlan { |
| 31 | let oldest_loaded = self |
| 32 | .app |
| 33 | .read(cx) |
| 34 | .store |
| 35 | .sync_state(chat_id) |
| 36 | .ok() |
| 37 | .flatten() |
| 38 | .and_then(|state| state.oldest_loaded); |
| 39 | let window = self |
| 40 | .current |
| 41 | .as_ref() |
| 42 | .and_then(|current| current.detached.as_ref()) |
| 43 | .map(|detached| (detached.oldest, detached.newest)); |
| 44 | jump_plan(window, target_created_at, oldest_loaded) |
| 45 | } |
| 46 | |
| 47 | pub(super) fn open_detached( |
| 48 | &mut self, |
| 49 | chat_id: String, |
| 50 | message_id: String, |
| 51 | cx: &mut Context<Self>, |
| 52 | ) { |
| 53 | let Some(engine) = self.app.read(cx).engine.clone() else { |
| 54 | return; |
| 55 | }; |
| 56 | let Some(current) = self.current.as_mut() else { |
| 57 | return; |
| 58 | }; |
| 59 | if current.window_request.as_deref() == Some(message_id.as_str()) { |
| 60 | return; |
| 61 | } |
| 62 | current.window_request = Some(message_id.clone()); |
| 63 | cx.notify(); |
| 64 | let task_chat_id = chat_id.clone(); |
| 65 | let task_message_id = message_id.clone(); |
| 66 | let receiver = |
| 67 | runtime::spawn( |
| 68 | async move { engine.load_window(&task_chat_id, &task_message_id).await }, |
| 69 | ); |
| 70 | cx.spawn(async move |this, cx| { |
| 71 | let result = receiver.await; |
| 72 | this.update(cx, |this, cx| { |
| 73 | this.finish_open_detached(&chat_id, message_id, result, cx) |
| 74 | }) |
| 75 | .ok(); |
| 76 | }) |
| 77 | .detach(); |
| 78 | } |
| 79 | |
| 80 | fn finish_open_detached( |
| 81 | &mut self, |
| 82 | chat_id: &str, |
| 83 | message_id: String, |
| 84 | result: Result<teams_core::Result<WindowPage>, tokio::sync::oneshot::error::RecvError>, |
| 85 | cx: &mut Context<Self>, |
| 86 | ) { |
| 87 | if self.chat_id().as_deref() != Some(chat_id) { |
| 88 | return; |
| 89 | } |
| 90 | let Some(current) = self |
| 91 | .current |
| 92 | .as_mut() |
| 93 | .filter(|current| current.window_request.as_deref() == Some(message_id.as_str())) |
| 94 | else { |
| 95 | return; |
| 96 | }; |
| 97 | current.window_request = None; |
| 98 | cx.notify(); |
| 99 | let page = match result { |
| 100 | Ok(Ok(page)) => page, |
| 101 | Ok(Err(error)) => { |
| 102 | self.notice = Some(format!( |
| 103 | "Could not load the message: {}", |
| 104 | short_error(&error) |
| 105 | )); |
| 106 | cx.notify(); |
| 107 | return; |
| 108 | } |
| 109 | Err(_) => return, |
| 110 | }; |
| 111 | let (Some(oldest), Some(newest)) = (page.oldest, page.newest) else { |
| 112 | self.notice = Some("Could not find the message".to_owned()); |
| 113 | cx.notify(); |
| 114 | return; |
| 115 | }; |
| 116 | let Some(current) = self.current.as_mut() else { |
| 117 | return; |
| 118 | }; |
| 119 | current.first_unread = None; |
| 120 | current.detached = Some(DetachedWindow { |
| 121 | anchor_id: message_id.clone(), |
| 122 | oldest, |
| 123 | newest, |
| 124 | older_cursor: page.older_cursor, |
| 125 | newer_cursor: page.newer_cursor, |
| 126 | loading: false, |
| 127 | }); |
| 128 | self.reload_pending(cx); |
| 129 | self.rebuild(true, cx); |
| 130 | self.reveal_row(message_id.clone(), message_id, cx); |
| 131 | } |
| 132 | |
| 133 | pub(super) fn leave_detached(&mut self, cx: &mut Context<Self>) { |
| 134 | let Some(current) = self.current.as_mut() else { |
| 135 | return; |
| 136 | }; |
| 137 | if current.detached.take().is_none() { |
| 138 | return; |
| 139 | } |
| 140 | self.rebuild(true, cx); |
| 141 | } |
| 142 | |
| 143 | pub(super) fn request_window_page( |
| 144 | &mut self, |
| 145 | direction: WindowDirection, |
| 146 | cx: &mut Context<Self>, |
| 147 | ) { |
| 148 | let Some(engine) = self.app.read(cx).engine.clone() else { |
| 149 | return; |
| 150 | }; |
| 151 | let Some(chat_id) = self.chat_id() else { |
| 152 | return; |
| 153 | }; |
| 154 | let Some(detached) = self |
| 155 | .current |
| 156 | .as_mut() |
| 157 | .and_then(|current| current.detached.as_mut()) |
| 158 | else { |
| 159 | return; |
| 160 | }; |
| 161 | let cursor = match direction { |
| 162 | WindowDirection::Backward => detached.older_cursor.clone(), |
| 163 | WindowDirection::Forward => detached.newer_cursor.clone(), |
| 164 | WindowDirection::Bidirectional => None, |
| 165 | }; |
| 166 | let Some(cursor) = cursor.filter(|_| !detached.loading) else { |
| 167 | return; |
| 168 | }; |
| 169 | detached.loading = true; |
| 170 | let anchor_id = detached.anchor_id.clone(); |
| 171 | let task_chat_id = chat_id.clone(); |
| 172 | let receiver = runtime::spawn(async move { |
| 173 | engine |
| 174 | .load_window_page(&task_chat_id, &cursor, direction) |
| 175 | .await |
| 176 | }); |
| 177 | cx.spawn(async move |this, cx| { |
| 178 | let result = receiver.await; |
| 179 | this.update(cx, |this, cx| { |
| 180 | this.finish_window_page(&chat_id, &anchor_id, direction, result, cx) |
| 181 | }) |
| 182 | .ok(); |
| 183 | }) |
| 184 | .detach(); |
| 185 | } |
| 186 | |
| 187 | fn finish_window_page( |
| 188 | &mut self, |
| 189 | chat_id: &str, |
| 190 | anchor_id: &str, |
| 191 | direction: WindowDirection, |
| 192 | result: Result<teams_core::Result<WindowPage>, tokio::sync::oneshot::error::RecvError>, |
| 193 | cx: &mut Context<Self>, |
| 194 | ) { |
| 195 | if self.chat_id().as_deref() != Some(chat_id) { |
| 196 | return; |
| 197 | } |
| 198 | let Some(detached) = self |
| 199 | .current |
| 200 | .as_mut() |
| 201 | .and_then(|current| current.detached.as_mut()) |
| 202 | .filter(|detached| detached.anchor_id == anchor_id) |
| 203 | else { |
| 204 | return; |
| 205 | }; |
| 206 | detached.loading = false; |
| 207 | let page = match result { |
| 208 | Ok(Ok(page)) => page, |
| 209 | failure => { |
| 210 | match direction { |
| 211 | WindowDirection::Backward => detached.older_cursor = None, |
| 212 | _ => detached.newer_cursor = None, |
| 213 | } |
| 214 | if let Ok(Err(error)) = failure { |
| 215 | self.notice = Some(format!("Could not load messages: {}", short_error(&error))); |
| 216 | } |
| 217 | self.rebuild(false, cx); |
| 218 | return; |
| 219 | } |
| 220 | }; |
| 221 | match direction { |
| 222 | WindowDirection::Backward => { |
| 223 | detached.older_cursor = page.older_cursor; |
| 224 | detached.oldest = page |
| 225 | .oldest |
| 226 | .map_or(detached.oldest, |oldest| oldest.min(detached.oldest)); |
| 227 | } |
| 228 | _ => { |
| 229 | detached.newer_cursor = page.newer_cursor; |
| 230 | detached.newest = page |
| 231 | .newest |
| 232 | .map_or(detached.newest, |newest| newest.max(detached.newest)); |
| 233 | } |
| 234 | } |
| 235 | let reached_latest = direction == WindowDirection::Forward |
| 236 | && (detached.newer_cursor.is_none() || self.window_reaches_newest_cached(chat_id, cx)); |
| 237 | if reached_latest { |
| 238 | self.leave_detached(cx); |
| 239 | } else { |
| 240 | self.rebuild(false, cx); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | fn window_reaches_newest_cached(&self, chat_id: &str, cx: &App) -> bool { |
| 245 | let Some(newest) = self |
| 246 | .current |
| 247 | .as_ref() |
| 248 | .and_then(|current| current.detached.as_ref()) |
| 249 | .map(|detached| detached.newest) |
| 250 | else { |
| 251 | return false; |
| 252 | }; |
| 253 | self.app |
| 254 | .read(cx) |
| 255 | .store |
| 256 | .messages(chat_id, None, 1) |
| 257 | .ok() |
| 258 | .and_then(|latest| latest.last().map(|record| record.created_at)) |
| 259 | .is_none_or(|latest| newest >= latest) |
| 260 | } |
| 261 | } |