fix(chat): recent chats load in the background before they are opened
- After each sidebar sync the 15 newest chats without cached messages fetch their latest page, one after another - Hovering a chat row fetches that chat the same way - Each chat is warmed once per session; demo mode skips it Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -149,6 +149,7 @@ pub struct AppState {
149
149
pub typing: TypingState,
150
150
typing_timer_running: bool,
151
151
pub(crate) sidebar_load: crate::sidebar_load::SidebarLoad,
152
pub(crate) warm_chats: crate::warm_chats::WarmChats,
152
153
pub local_previews: HashMap<String, LocalPreview>,
153
154
pub quick_view: Option<QuickView>,
154
155
pub scheduled: Vec<ScheduledDraft>,
@@ -307,6 +308,7 @@ impl AppState {
307
308
typing: TypingState::default(),
308
309
typing_timer_running: false,
309
310
sidebar_load: Default::default(),
311
warm_chats: Default::default(),
310
312
local_previews: HashMap::new(),
311
313
quick_view: None,
312
314
scheduled: Vec::new(),
@@ -149,6 +149,7 @@ pub struct AppState {149
pub typing: TypingState,149
pub typing: TypingState,150
typing_timer_running: bool,150
typing_timer_running: bool,151
pub(crate) sidebar_load: crate::sidebar_load::SidebarLoad,151
pub(crate) sidebar_load: crate::sidebar_load::SidebarLoad,152
pub(crate) warm_chats: crate::warm_chats::WarmChats,152
pub local_previews: HashMap<String, LocalPreview>,153
pub local_previews: HashMap<String, LocalPreview>,153
pub quick_view: Option<QuickView>,154
pub quick_view: Option<QuickView>,154
pub scheduled: Vec<ScheduledDraft>,155
pub scheduled: Vec<ScheduledDraft>,@@ -307,6 +308,7 @@ impl AppState {307
typing: TypingState::default(),308
typing: TypingState::default(),308
typing_timer_running: false,309
typing_timer_running: false,309
sidebar_load: Default::default(),310
sidebar_load: Default::default(),311
warm_chats: Default::default(),310
local_previews: HashMap::new(),312
local_previews: HashMap::new(),311
quick_view: None,313
quick_view: None,312
scheduled: Vec::new(),314
scheduled: Vec::new(),@@ -58,6 +58,7 @@ pub mod task_dialog;
58
58
pub mod team_admin;
59
59
pub mod translation_actions;
60
60
pub mod typing;
61
mod warm_chats;
61
62
pub mod urgent_ack;
62
63
63
64
pub use app_ui_kit::render;
@@ -58,6 +58,7 @@ pub mod task_dialog;58
pub mod team_admin;58
pub mod team_admin;59
pub mod translation_actions;59
pub mod translation_actions;60
pub mod typing;60
pub mod typing;61
mod warm_chats;61
pub mod urgent_ack;62
pub mod urgent_ack;62
63
63
pub use app_ui_kit::render;64
pub use app_ui_kit::render;@@ -105,6 +105,7 @@ impl AppState {
105
105
.filter(|_| removed)
106
106
.and_then(|chat_id| self.next_chat_after(chat_id));
107
107
self.adopt_loaded_sidebar(loaded, cx);
108
self.warm_recent_chats();
108
109
if removed {
109
110
self.leave_removed_selection(next, cx);
110
111
}
@@ -105,6 +105,7 @@ impl AppState {105
.filter(|_| removed)105
.filter(|_| removed)106
.and_then(|chat_id| self.next_chat_after(chat_id));106
.and_then(|chat_id| self.next_chat_after(chat_id));107
self.adopt_loaded_sidebar(loaded, cx);107
self.adopt_loaded_sidebar(loaded, cx);108
self.warm_recent_chats();108
if removed {109
if removed {109
self.leave_removed_selection(next, cx);110
self.leave_removed_selection(next, cx);110
}111
}@@ -0,0 +1,91 @@
1
use std::collections::HashSet;
2
3
use crate::app_state::AppState;
4
use crate::runtime;
5
6
const RECENT_CHAT_COUNT: usize = 15;
7
8
#[derive(Default)]
9
pub struct WarmChats {
10
requested: HashSet<String>,
11
}
12
13
impl WarmChats {
14
fn take_new(&mut self, chat_ids: Vec<String>) -> Vec<String> {
15
chat_ids
16
.into_iter()
17
.filter(|chat_id| self.requested.insert(chat_id.clone()))
18
.collect()
19
}
20
}
21
22
pub fn recent_chat_ids(chats: &[store::ChatRecord], count: usize) -> Vec<String> {
23
let mut recent: Vec<_> = chats.iter().collect();
24
recent.sort_by(|left, right| right.last_message_at.cmp(&left.last_message_at));
25
recent
26
.into_iter()
27
.take(count)
28
.map(|chat| chat.id.clone())
29
.collect()
30
}
31
32
impl AppState {
33
pub(crate) fn warm_recent_chats(&mut self) {
34
let chat_ids = recent_chat_ids(&self.sidebar.chats, RECENT_CHAT_COUNT);
35
self.warm(chat_ids);
36
}
37
38
pub fn warm_chat(&mut self, chat_id: &str) {
39
self.warm(vec![chat_id.to_owned()]);
40
}
41
42
fn warm(&mut self, chat_ids: Vec<String>) {
43
let Some(engine) = self.engine.clone().filter(|_| !self.mode.demo) else {
44
return;
45
};
46
let chat_ids = self.warm_chats.take_new(chat_ids);
47
if chat_ids.is_empty() {
48
return;
49
}
50
let store = self.store.clone();
51
runtime::handle().spawn(async move {
52
for chat_id in chat_ids {
53
let cached = store
54
.messages(&chat_id, None, 1)
55
.is_ok_and(|messages| !messages.is_empty());
56
if !cached {
57
let _ = engine.fetch_newer(&chat_id).await;
58
}
59
}
60
});
61
}
62
}
63
64
#[cfg(test)]
65
mod tests {
66
use chrono::{TimeZone, Utc};
67
use store::ChatRecord;
68
69
use super::*;
70
71
fn chat(id: &str, hour: Option<u32>) -> ChatRecord {
72
ChatRecord {
73
id: id.to_owned(),
74
last_message_at: hour.map(|hour| Utc.with_ymd_and_hms(2026, 11, 11, hour, 0, 0).unwrap()),
75
..Default::default()
76
}
77
}
78
79
#[test]
80
fn the_newest_chats_are_warmed_first() {
81
let chats = [chat("old", Some(8)), chat("never", None), chat("new", Some(12)), chat("mid", Some(10))];
82
assert_eq!(recent_chat_ids(&chats, 2), ["new", "mid"]);
83
}
84
85
#[test]
86
fn a_chat_is_warmed_only_once() {
87
let mut warm = WarmChats::default();
88
assert_eq!(warm.take_new(vec!["a".into(), "b".into()]), ["a", "b"]);
89
assert_eq!(warm.take_new(vec!["b".into(), "c".into()]), ["c"]);
90
}
91
}
@@ -0,0 +1,91 @@1
use std::collections::HashSet;2
3
use crate::app_state::AppState;4
use crate::runtime;5
6
const RECENT_CHAT_COUNT: usize = 15;7
8
#[derive(Default)]9
pub struct WarmChats {10
requested: HashSet<String>,11
}12
13
impl WarmChats {14
fn take_new(&mut self, chat_ids: Vec<String>) -> Vec<String> {15
chat_ids16
.into_iter()17
.filter(|chat_id| self.requested.insert(chat_id.clone()))18
.collect()19
}20
}21
22
pub fn recent_chat_ids(chats: &[store::ChatRecord], count: usize) -> Vec<String> {23
let mut recent: Vec<_> = chats.iter().collect();24
recent.sort_by(|left, right| right.last_message_at.cmp(&left.last_message_at));25
recent26
.into_iter()27
.take(count)28
.map(|chat| chat.id.clone())29
.collect()30
}31
32
impl AppState {33
pub(crate) fn warm_recent_chats(&mut self) {34
let chat_ids = recent_chat_ids(&self.sidebar.chats, RECENT_CHAT_COUNT);35
self.warm(chat_ids);36
}37
38
pub fn warm_chat(&mut self, chat_id: &str) {39
self.warm(vec![chat_id.to_owned()]);40
}41
42
fn warm(&mut self, chat_ids: Vec<String>) {43
let Some(engine) = self.engine.clone().filter(|_| !self.mode.demo) else {44
return;45
};46
let chat_ids = self.warm_chats.take_new(chat_ids);47
if chat_ids.is_empty() {48
return;49
}50
let store = self.store.clone();51
runtime::handle().spawn(async move {52
for chat_id in chat_ids {53
let cached = store54
.messages(&chat_id, None, 1)55
.is_ok_and(|messages| !messages.is_empty());56
if !cached {57
let _ = engine.fetch_newer(&chat_id).await;58
}59
}60
});61
}62
}63
64
#[cfg(test)]65
mod tests {66
use chrono::{TimeZone, Utc};67
use store::ChatRecord;68
69
use super::*;70
71
fn chat(id: &str, hour: Option<u32>) -> ChatRecord {72
ChatRecord {73
id: id.to_owned(),74
last_message_at: hour.map(|hour| Utc.with_ymd_and_hms(2026, 11, 11, hour, 0, 0).unwrap()),75
..Default::default()76
}77
}78
79
#[test]80
fn the_newest_chats_are_warmed_first() {81
let chats = [chat("old", Some(8)), chat("never", None), chat("new", Some(12)), chat("mid", Some(10))];82
assert_eq!(recent_chat_ids(&chats, 2), ["new", "mid"]);83
}84
85
#[test]86
fn a_chat_is_warmed_only_once() {87
let mut warm = WarmChats::default();88
assert_eq!(warm.take_new(vec!["a".into(), "b".into()]), ["a", "b"]);89
assert_eq!(warm.take_new(vec!["b".into(), "c".into()]), ["c"]);90
}91
}@@ -1076,6 +1076,7 @@ impl SidebarView {
1076
1076
);
1077
1077
let state = self.state.clone();
1078
1078
let chat_id = item.id.clone();
1079
let (warm_state, warm_chat_id) = (self.state.clone(), item.id.clone());
1079
1080
let dragged = DraggedChat {
1080
1081
id: item.id.clone(),
1081
1082
title: item.title.clone(),
@@ -1144,6 +1145,11 @@ impl SidebarView {
1144
1145
.child(avatar)
1145
1146
.child(lines)
1146
1147
.child(more_button)
1148
.on_hover(move |hovered, _, cx| {
1149
if *hovered {
1150
warm_state.update(cx, |state, _| state.warm_chat(&warm_chat_id));
1151
}
1152
})
1147
1153
.on_click(move |_, _, cx| {
1148
1154
let selection = Selection::Chat(chat_id.clone());
1149
1155
state.update(cx, |state, cx| state.select(selection, cx));
@@ -1076,6 +1076,7 @@ impl SidebarView {1076
);1076
);1077
let state = self.state.clone();1077
let state = self.state.clone();1078
let chat_id = item.id.clone();1078
let chat_id = item.id.clone();1079
let (warm_state, warm_chat_id) = (self.state.clone(), item.id.clone());1079
let dragged = DraggedChat {1080
let dragged = DraggedChat {1080
id: item.id.clone(),1081
id: item.id.clone(),1081
title: item.title.clone(),1082
title: item.title.clone(),@@ -1144,6 +1145,11 @@ impl SidebarView {1144
.child(avatar)1145
.child(avatar)1145
.child(lines)1146
.child(lines)1146
.child(more_button)1147
.child(more_button)1148
.on_hover(move |hovered, _, cx| {1149
if *hovered {1150
warm_state.update(cx, |state, _| state.warm_chat(&warm_chat_id));1151
}1152
})1147
.on_click(move |_, _, cx| {1153
.on_click(move |_, _, cx| {1148
let selection = Selection::Chat(chat_id.clone());1154
let selection = Selection::Chat(chat_id.clone());1149
state.update(cx, |state, cx| state.select(selection, cx));1155
state.update(cx, |state, cx| state.select(selection, cx));