BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / composer / drafts.rs

drafts.rs

drafts.rs 4.4 KiB Download Raw
1use std::time::Duration;
2
3use gpui_kit::component::input::InputContent;
4use gpui_kit::*;
5use store::{AttachmentImage, DraftRecord};
6use teams_core::Draft;
7
8use super::{Composer, Outgoing};
9use crate::notice::truncated;
10use crate::stored_outgoing::{decode, encode};
11
12const DRAFT_SAVE_DELAY: Duration = Duration::from_millis(500);
13const DRAFT_PREVIEW_CHARS: usize = 200;
14const IMAGE_ONLY_PREVIEW: &str = "Image";
15
16pub(super) type ImageFingerprint = (String, Vec<(String, usize, Option<u32>, Option<u32>)>);
17
18fn fingerprint(conversation_id: &str, images: &[AttachmentImage]) -> ImageFingerprint {
19 let images = images
20 .iter()
21 .map(|image| {
22 (
23 image.name.clone(),
24 image.bytes.len(),
25 image.width,
26 image.height,
27 )
28 })
29 .collect();
30 (conversation_id.to_owned(), images)
31}
32
33fn draft_preview(outgoing: &Outgoing) -> String {
34 let text = outgoing
35 .text()
36 .split_whitespace()
37 .collect::<Vec<_>>()
38 .join(" ");
39 if text.is_empty() {
40 return IMAGE_ONLY_PREVIEW.to_owned();
41 }
42 truncated(&text, DRAFT_PREVIEW_CHARS)
43}
44
45impl Composer {
46 pub(super) fn schedule_draft_save(&mut self, cx: &mut Context<Self>) {
47 self.draft_save = Some(cx.spawn(async move |this, cx| {
48 cx.background_executor().timer(DRAFT_SAVE_DELAY).await;
49 this.update(cx, |composer, cx| composer.save_draft_now(cx))
50 .ok();
51 }));
52 }
53
54 fn storable_conversation(&self) -> Option<String> {
55 self.conversation_id
56 .clone()
57 .filter(|conversation_id| !conversation_id.is_empty())
58 .filter(|_| self.editing.is_none() && !self.inline)
59 }
60
61 fn storable_draft(&self, cx: &App) -> Option<Outgoing> {
62 let mut outgoing = self.compose(cx);
63 outgoing.files.clear();
64 (!outgoing.draft.is_blank() || !outgoing.images.is_empty()).then_some(outgoing)
65 }
66
67 pub fn save_draft_now(&mut self, cx: &mut Context<Self>) {
68 let Some(conversation_id) = self.storable_conversation() else {
69 return;
70 };
71 let store = self.app.read(cx).store.clone();
72 let stored = self
73 .storable_draft(cx)
74 .and_then(|outgoing| Some((draft_preview(&outgoing), encode(&outgoing)?)));
75 match stored {
76 Some((preview, stored)) => {
77 let current = fingerprint(&conversation_id, &stored.images);
78 let unchanged = self.saved_images.as_ref() == Some(&current);
79 let record = DraftRecord {
80 conversation_id,
81 payload: stored.payload,
82 preview,
83 images: stored.images,
84 updated_at: chrono::Utc::now(),
85 };
86 let saved = if unchanged {
87 store.save_draft_text(&record)
88 } else {
89 store.save_draft(&record)
90 };
91 self.saved_images = saved.is_ok().then_some(current);
92 }
93 None => {
94 self.saved_images = None;
95 store.delete_draft(&conversation_id).ok();
96 }
97 }
98 self.app
99 .update(cx, |state, cx| state.refresh_local_previews(cx));
100 }
101
102 pub(super) fn clear_stored_draft(&mut self, cx: &mut Context<Self>) {
103 self.draft_save = None;
104 self.saved_images = None;
105 let Some(conversation_id) = self.storable_conversation() else {
106 return;
107 };
108 self.app.read(cx).store.delete_draft(&conversation_id).ok();
109 self.app
110 .update(cx, |state, cx| state.refresh_local_previews(cx));
111 }
112
113 pub(super) fn load_stored_draft(
114 &mut self,
115 conversation_id: &str,
116 window: &mut Window,
117 cx: &mut Context<Self>,
118 ) {
119 let stored = Some(conversation_id)
120 .filter(|conversation_id| !conversation_id.is_empty() && !self.inline)
121 .and_then(|conversation_id| self.app.read(cx).store.draft(conversation_id).ok())
122 .flatten()
123 .and_then(|record| decode(&record.payload, &record.images));
124 match stored {
125 Some(outgoing) => self.restore(&outgoing, window, cx),
126 None => {
127 self.mention_inputs.clear();
128 self.load_draft(Draft::default(), InputContent::new(""), window, cx);
129 }
130 }
131 self.draft_save = None;
132 }
133}