BlueGit

ict rusty-teams

Files

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

section_actions.rs

ae3149e9 History
section_actions.rs 6.4 KiB Download Raw
1use std::collections::VecDeque;
2
3use gpui_kit::*;
4use teams_core::FolderChange;
5
6use crate::app_state::{AppEvent, AppState};
7use crate::data::{Directory, PENDING_FOLDER_PREFIX};
8
9pub const READ_ONLY_SECTIONS: &str = "Read-only mode: sections not changed";
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct QueuedFolderChange {
13 pub change: FolderChange,
14 pub label: &'static str,
15 pub created_id: String,
16}
17
18#[derive(Debug, Default)]
19pub struct SectionChanges {
20 queue: VecDeque<QueuedFolderChange>,
21 created: u64,
22}
23
24impl SectionChanges {
25 pub fn push(&mut self, change: FolderChange, label: &'static str) -> QueuedFolderChange {
26 self.created += 1;
27 let queued = QueuedFolderChange {
28 change,
29 label,
30 created_id: format!("{PENDING_FOLDER_PREFIX}{}", self.created),
31 };
32 self.queue.push_back(queued.clone());
33 queued
34 }
35
36 pub fn in_flight(&self) -> Option<&QueuedFolderChange> {
37 self.queue.front()
38 }
39
40 pub fn finish(&mut self) -> Option<&QueuedFolderChange> {
41 self.queue.pop_front();
42 self.queue.front()
43 }
44
45 pub fn clear(&mut self) {
46 self.queue.clear();
47 }
48
49 pub fn reapply(&self, directory: &mut Directory) {
50 for queued in &self.queue {
51 directory.apply_folder_change(&queued.change, &queued.created_id);
52 }
53 }
54}
55
56impl AppState {
57 pub fn create_section(&mut self, name: &str, cx: &mut Context<Self>) {
58 let change = FolderChange::Create {
59 name: name.to_owned(),
60 };
61 self.change_sections(change, "New section", cx);
62 }
63
64 pub fn rename_section(&mut self, folder_id: &str, name: &str, cx: &mut Context<Self>) {
65 let change = FolderChange::Rename {
66 folder_id: folder_id.to_owned(),
67 name: name.to_owned(),
68 };
69 self.change_sections(change, "Rename section", cx);
70 }
71
72 pub fn delete_section(&mut self, folder_id: &str, cx: &mut Context<Self>) {
73 let change = FolderChange::Delete {
74 folder_id: folder_id.to_owned(),
75 };
76 self.change_sections(change, "Delete section", cx);
77 }
78
79 pub fn move_section(
80 &mut self,
81 folder_id: &str,
82 target_folder_id: &str,
83 cx: &mut Context<Self>,
84 ) {
85 let change = FolderChange::Move {
86 folder_id: folder_id.to_owned(),
87 target_folder_id: target_folder_id.to_owned(),
88 };
89 self.change_sections(change, "Move section", cx);
90 }
91
92 fn change_sections(
93 &mut self,
94 change: FolderChange,
95 label: &'static str,
96 cx: &mut Context<Self>,
97 ) {
98 if self.mode.read_only {
99 self.raise_notice(READ_ONLY_SECTIONS.to_owned(), None, cx);
100 return;
101 }
102 if self.engine.is_none() {
103 if self.mode.demo {
104 let demo_id = uuid::Uuid::new_v4().to_string();
105 self.directory.apply_folder_change(&change, &demo_id);
106 self.notify_sections(cx);
107 }
108 return;
109 }
110 let starts_now = self.section_changes.in_flight().is_none();
111 let queued = self.section_changes.push(change, label);
112 self.directory
113 .apply_folder_change(&queued.change, &queued.created_id);
114 self.notify_sections(cx);
115 if starts_now {
116 self.run_next_section_change(cx);
117 }
118 }
119
120 fn run_next_section_change(&mut self, cx: &mut Context<Self>) {
121 let Some(queued) = self.section_changes.in_flight().cloned() else {
122 return;
123 };
124 let Some(engine) = self.engine.clone() else {
125 self.section_changes.clear();
126 return;
127 };
128 self.run_chat_action(
129 queued.label,
130 async move { engine.change_folders(queued.change).await },
131 Self::finish_section_change,
132 Self::finish_section_change,
133 cx,
134 );
135 }
136
137 fn finish_section_change(&mut self, cx: &mut Context<Self>) {
138 let has_next = self.section_changes.finish().is_some();
139 self.reload_directory();
140 self.notify_sections(cx);
141 if has_next {
142 self.run_next_section_change(cx);
143 }
144 }
145
146 pub(crate) fn reapply_section_changes(&mut self) {
147 self.section_changes.reapply(&mut self.directory);
148 }
149
150 fn notify_sections(&mut self, cx: &mut Context<Self>) {
151 cx.emit(AppEvent::Sidebar);
152 cx.notify();
153 }
154}
155
156#[cfg(test)]
157mod tests {
158 use super::{Directory, FolderChange, SectionChanges};
159 use crate::data::{FolderInfo, FolderKind};
160
161 fn create(name: &str) -> FolderChange {
162 FolderChange::Create { name: name.into() }
163 }
164
165 #[test]
166 fn folder_changes_run_one_at_a_time_in_order() {
167 let mut changes = SectionChanges::default();
168 assert!(changes.in_flight().is_none());
169 changes.push(create("a"), "New section");
170 changes.push(create("b"), "New section");
171 assert_eq!(changes.in_flight().unwrap().change, create("a"));
172 assert_eq!(changes.finish().unwrap().change, create("b"));
173 assert!(changes.finish().is_none());
174 assert!(changes.in_flight().is_none());
175 }
176
177 #[test]
178 fn same_name_creates_get_distinct_pending_ids() {
179 let mut changes = SectionChanges::default();
180 changes.push(create("Todo"), "New section");
181 changes.push(create("Todo"), "New section");
182 let mut directory = Directory::default();
183 changes.reapply(&mut directory);
184 assert_eq!(directory.folders.len(), 2);
185 assert_ne!(directory.folders[0].id, directory.folders[1].id);
186 }
187
188 #[test]
189 fn pending_create_survives_a_directory_reload() {
190 let mut changes = SectionChanges::default();
191 changes.push(create("Todo"), "New section");
192 let reloaded = || Directory {
193 folders: vec![FolderInfo {
194 id: "recent".into(),
195 name: String::new(),
196 kind: FolderKind::Recent,
197 expanded: None,
198 conversation_ids: Vec::new(),
199 }],
200 ..Default::default()
201 };
202 let mut directory = reloaded();
203 changes.reapply(&mut directory);
204 assert_eq!(directory.folders[0].name, "Todo");
205 changes.finish();
206 let mut directory = reloaded();
207 changes.reapply(&mut directory);
208 assert_eq!(directory.folders.len(), 1);
209 }
210}