| 1 | use std::collections::HashMap; |
| 2 | |
| 3 | use gpui_kit::*; |
| 4 | use store::{ChatRecord, MessageImportance, Sidebar, Store}; |
| 5 | |
| 6 | use crate::app_state::{AppState, Selection}; |
| 7 | use crate::backend::Engine; |
| 8 | use crate::data; |
| 9 | |
| 10 | #[derive(Default)] |
| 11 | pub struct SidebarLoad { |
| 12 | running: bool, |
| 13 | requested_again: bool, |
| 14 | local_changes: u64, |
| 15 | } |
| 16 | |
| 17 | impl SidebarLoad { |
| 18 | pub fn local_change(&mut self) { |
| 19 | self.local_changes += 1; |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | #[derive(Default)] |
| 24 | pub struct Unread { |
| 25 | pub counts: HashMap<String, u32>, |
| 26 | pub importance: HashMap<String, MessageImportance>, |
| 27 | } |
| 28 | |
| 29 | pub struct LoadedSidebar { |
| 30 | pub sidebar: Sidebar, |
| 31 | pub unread: Unread, |
| 32 | } |
| 33 | |
| 34 | pub fn unread_of(engine: Option<&Engine>, chats: &[ChatRecord]) -> Unread { |
| 35 | match engine { |
| 36 | Some(engine) => Unread { |
| 37 | counts: data::unread_counts(engine, chats), |
| 38 | importance: data::unread_importance(engine, chats), |
| 39 | }, |
| 40 | None => Unread::default(), |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | fn load(store: &Store, engine: Option<&Engine>) -> Option<LoadedSidebar> { |
| 45 | let sidebar = store.sidebar().ok()?; |
| 46 | let unread = unread_of(engine, &sidebar.chats); |
| 47 | Some(LoadedSidebar { sidebar, unread }) |
| 48 | } |
| 49 | |
| 50 | impl AppState { |
| 51 | pub fn reload_sidebar_after_sync(&mut self, cx: &mut Context<Self>) { |
| 52 | if self.sidebar_load.running { |
| 53 | self.sidebar_load.requested_again = true; |
| 54 | return; |
| 55 | } |
| 56 | self.sidebar_load.running = true; |
| 57 | cx.spawn(async move |this, cx| { |
| 58 | loop { |
| 59 | let Ok((store, engine, local_changes)) = this.update(cx, |state, _| { |
| 60 | state.sidebar_load.requested_again = false; |
| 61 | ( |
| 62 | state.store.clone(), |
| 63 | state.engine.clone(), |
| 64 | state.sidebar_load.local_changes, |
| 65 | ) |
| 66 | }) else { |
| 67 | return; |
| 68 | }; |
| 69 | let loaded = cx |
| 70 | .background_spawn(async move { load(&store, engine.as_deref()) }) |
| 71 | .await; |
| 72 | let again = this.update(cx, |state, cx| { |
| 73 | let stale = state.sidebar_load.local_changes != local_changes; |
| 74 | if let Some(loaded) = loaded.filter(|_| !stale) { |
| 75 | state.adopt_synced_sidebar(loaded, cx); |
| 76 | } |
| 77 | let again = state.sidebar_load.requested_again || stale; |
| 78 | state.sidebar_load.running = again; |
| 79 | again |
| 80 | }); |
| 81 | if !matches!(again, Ok(true)) { |
| 82 | return; |
| 83 | } |
| 84 | } |
| 85 | }) |
| 86 | .detach(); |
| 87 | } |
| 88 | |
| 89 | fn adopt_synced_sidebar(&mut self, loaded: LoadedSidebar, cx: &mut Context<Self>) { |
| 90 | let selected = match &self.selection { |
| 91 | Some(Selection::Chat(chat_id)) => Some(chat_id.clone()), |
| 92 | _ => None, |
| 93 | }; |
| 94 | let is_listed = |
| 95 | |chats: &[ChatRecord], chat_id: &str| chats.iter().any(|chat| chat.id == chat_id); |
| 96 | let was_listed = selected |
| 97 | .as_deref() |
| 98 | .is_some_and(|chat_id| is_listed(&self.sidebar.chats, chat_id)); |
| 99 | let removed = was_listed |
| 100 | && selected |
| 101 | .as_deref() |
| 102 | .is_some_and(|chat_id| !is_listed(&loaded.sidebar.chats, chat_id)); |
| 103 | let next = selected |
| 104 | .as_deref() |
| 105 | .filter(|_| removed) |
| 106 | .and_then(|chat_id| self.next_chat_after(chat_id)); |
| 107 | self.adopt_loaded_sidebar(loaded, cx); |
| 108 | self.warm_recent_chats(); |
| 109 | if removed { |
| 110 | self.leave_removed_selection(next, cx); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | #[cfg(test)] |
| 116 | mod tests { |
| 117 | use std::sync::Arc; |
| 118 | |
| 119 | use gpui_kit::{AppContext, TestAppContext}; |
| 120 | use store::{ChatRecord, Store}; |
| 121 | use teams_core::CoreEvent; |
| 122 | |
| 123 | use crate::app_state::{AppState, Mode, Selection}; |
| 124 | use crate::backend::BackendEvent; |
| 125 | |
| 126 | fn chat(id: &str) -> ChatRecord { |
| 127 | ChatRecord { |
| 128 | id: id.into(), |
| 129 | kind: "group".into(), |
| 130 | title: id.into(), |
| 131 | ..Default::default() |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | fn listed(app: &gpui_kit::Entity<AppState>, cx: &mut TestAppContext) -> Vec<String> { |
| 136 | cx.update(|cx| { |
| 137 | app.read(cx) |
| 138 | .sidebar |
| 139 | .chats |
| 140 | .iter() |
| 141 | .map(|chat| chat.id.clone()) |
| 142 | .collect() |
| 143 | }) |
| 144 | } |
| 145 | |
| 146 | fn sidebar_changed(app: &gpui_kit::Entity<AppState>, cx: &mut TestAppContext) { |
| 147 | cx.update(|cx| { |
| 148 | app.update(cx, |state, cx| { |
| 149 | state.apply(BackendEvent::Core(CoreEvent::SidebarChanged), cx) |
| 150 | }) |
| 151 | }); |
| 152 | } |
| 153 | |
| 154 | #[gpui_kit::test] |
| 155 | fn a_synced_sidebar_loads_in_the_background_and_bursts_load_once_more(cx: &mut TestAppContext) { |
| 156 | cx.update(gpui_kit::init); |
| 157 | let store = Arc::new(Store::open_in_memory().unwrap()); |
| 158 | let app = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default()))); |
| 159 | store.upsert_chats(&[chat("first")]).unwrap(); |
| 160 | sidebar_changed(&app, cx); |
| 161 | assert!(listed(&app, cx).is_empty()); |
| 162 | store.upsert_chats(&[chat("second")]).unwrap(); |
| 163 | sidebar_changed(&app, cx); |
| 164 | cx.run_until_parked(); |
| 165 | let mut chats = listed(&app, cx); |
| 166 | chats.sort(); |
| 167 | assert_eq!(chats, vec!["first".to_owned(), "second".to_owned()]); |
| 168 | } |
| 169 | |
| 170 | #[gpui_kit::test] |
| 171 | fn a_selected_chat_gone_after_sync_moves_the_selection_on(cx: &mut TestAppContext) { |
| 172 | cx.update(gpui_kit::init); |
| 173 | let store = Arc::new(Store::open_in_memory().unwrap()); |
| 174 | store.upsert_chats(&[chat("kept"), chat("gone")]).unwrap(); |
| 175 | let app = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default()))); |
| 176 | cx.update(|cx| { |
| 177 | app.update(cx, |state, cx| { |
| 178 | state.reload_sidebar(cx); |
| 179 | state.selection = Some(Selection::Chat("gone".into())); |
| 180 | }) |
| 181 | }); |
| 182 | store.remove_chats(&["gone".to_owned()]).unwrap(); |
| 183 | sidebar_changed(&app, cx); |
| 184 | cx.run_until_parked(); |
| 185 | let selection = cx.update(|cx| app.read(cx).selection.clone()); |
| 186 | assert_eq!(selection, Some(Selection::Chat("kept".into()))); |
| 187 | } |
| 188 | } |