BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / local_previews.rs

local_previews.rs

local_previews.rs 2.3 KiB Download Raw
1use std::collections::HashMap;
2
3use gpui_kit::Context;
4use store::Store;
5
6use crate::app_state::{AppEvent, AppState};
7
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum LocalPreview {
10 Draft(String),
11 NotSent,
12}
13
14pub fn load_local_previews(store: &Store) -> HashMap<String, LocalPreview> {
15 let mut previews: HashMap<String, LocalPreview> = store
16 .draft_previews()
17 .unwrap_or_default()
18 .into_iter()
19 .map(|(conversation_id, text)| (conversation_id, LocalPreview::Draft(text)))
20 .collect();
21 for conversation_id in store.failed_outbox_conversations().unwrap_or_default() {
22 previews.insert(conversation_id, LocalPreview::NotSent);
23 }
24 previews
25}
26
27impl AppState {
28 pub fn refresh_local_previews(&mut self, cx: &mut Context<Self>) {
29 let previews = load_local_previews(&self.store);
30 if previews != self.local_previews {
31 self.local_previews = previews;
32 cx.emit(AppEvent::LocalPreviews);
33 }
34 }
35}
36
37#[cfg(test)]
38mod tests {
39 use chrono::Utc;
40 use store::{DraftRecord, OutboxRecord, OutboxState, OutboxTarget};
41
42 use super::*;
43
44 fn failed_row(conversation_id: &str) -> OutboxRecord {
45 OutboxRecord {
46 id: "row".to_owned(),
47 conversation_id: conversation_id.to_owned(),
48 target: OutboxTarget::Flat,
49 thread_root_id: None,
50 payload: "{}".to_owned(),
51 images: Vec::new(),
52 state: OutboxState::Failed,
53 last_error: None,
54 created_at: Utc::now(),
55 }
56 }
57
58 #[test]
59 fn a_failed_send_beats_the_draft_in_the_same_chat() {
60 let store = Store::open_in_memory().unwrap();
61 for (conversation_id, preview) in [("a", "typing"), ("b", "also typing")] {
62 store
63 .save_draft(&DraftRecord {
64 conversation_id: conversation_id.to_owned(),
65 payload: "{}".to_owned(),
66 preview: preview.to_owned(),
67 images: Vec::new(),
68 updated_at: Utc::now(),
69 })
70 .unwrap();
71 }
72 store.put_outbox(&failed_row("a")).unwrap();
73 let previews = load_local_previews(&store);
74 assert_eq!(previews["a"], LocalPreview::NotSent);
75 assert_eq!(previews["b"], LocalPreview::Draft("also typing".to_owned()));
76 }
77}