BlueGit

ict rusty-teams

Files

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

local_previews.rs

local_previews.rs 2.5 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 self.set_local_previews(previews, cx);
31 }
32
33 pub fn set_local_previews(
34 &mut self,
35 previews: HashMap<String, LocalPreview>,
36 cx: &mut Context<Self>,
37 ) {
38 if previews != self.local_previews {
39 self.local_previews = previews;
40 cx.emit(AppEvent::LocalPreviews);
41 }
42 }
43}
44
45#[cfg(test)]
46mod tests {
47 use chrono::Utc;
48 use store::{DraftRecord, OutboxRecord, OutboxState, OutboxTarget};
49
50 use super::*;
51
52 fn failed_row(conversation_id: &str) -> OutboxRecord {
53 OutboxRecord {
54 id: "row".to_owned(),
55 conversation_id: conversation_id.to_owned(),
56 target: OutboxTarget::Flat,
57 thread_root_id: None,
58 payload: "{}".to_owned(),
59 images: Vec::new(),
60 state: OutboxState::Failed,
61 last_error: None,
62 created_at: Utc::now(),
63 }
64 }
65
66 #[test]
67 fn a_failed_send_beats_the_draft_in_the_same_chat() {
68 let store = Store::open_in_memory().unwrap();
69 for (conversation_id, preview) in [("a", "typing"), ("b", "also typing")] {
70 store
71 .save_draft(&DraftRecord {
72 conversation_id: conversation_id.to_owned(),
73 payload: "{}".to_owned(),
74 preview: preview.to_owned(),
75 images: Vec::new(),
76 updated_at: Utc::now(),
77 })
78 .unwrap();
79 }
80 store.put_outbox(&failed_row("a")).unwrap();
81 let previews = load_local_previews(&store);
82 assert_eq!(previews["a"], LocalPreview::NotSent);
83 assert_eq!(previews["b"], LocalPreview::Draft("also typing".to_owned()));
84 }
85}