| 1 | use std::collections::HashMap; |
| 2 | use std::time::Duration; |
| 3 | |
| 4 | use teams_core::AdaptiveCard; |
| 5 | |
| 6 | use gpui_kit::App; |
| 7 | |
| 8 | use crate::card_inputs::{FieldHandle, InputField}; |
| 9 | |
| 10 | const DIALOG_KEY: &str = "task-dialog"; |
| 11 | pub const DONE_HOLD: Duration = Duration::from_millis(1600); |
| 12 | pub const MAX_REASON_CHARS: usize = 160; |
| 13 | |
| 14 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 15 | pub enum ActionPhase { |
| 16 | Busy, |
| 17 | Done, |
| 18 | Failed(String), |
| 19 | } |
| 20 | |
| 21 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 22 | pub struct BotIdentity { |
| 23 | pub name: String, |
| 24 | pub icon_url: Option<String>, |
| 25 | } |
| 26 | |
| 27 | #[derive(Debug, Clone, PartialEq)] |
| 28 | pub struct TaskDialogState { |
| 29 | pub title: String, |
| 30 | pub card: AdaptiveCard, |
| 31 | pub scope: CardScope, |
| 32 | pub width: u32, |
| 33 | pub height: u32, |
| 34 | } |
| 35 | |
| 36 | #[derive(Debug, Clone, PartialEq)] |
| 37 | pub struct CardOverride { |
| 38 | pub basis: String, |
| 39 | pub cards: Vec<AdaptiveCard>, |
| 40 | } |
| 41 | |
| 42 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 43 | pub enum Surface { |
| 44 | Message, |
| 45 | Dialog, |
| 46 | } |
| 47 | |
| 48 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 49 | pub struct CardScope { |
| 50 | pub conversation_id: String, |
| 51 | pub message_id: String, |
| 52 | pub card_index: usize, |
| 53 | pub surface: Surface, |
| 54 | } |
| 55 | |
| 56 | impl CardScope { |
| 57 | pub fn message(conversation_id: &str, message_id: &str, card_index: usize) -> Self { |
| 58 | CardScope { |
| 59 | conversation_id: conversation_id.to_owned(), |
| 60 | message_id: message_id.to_owned(), |
| 61 | card_index, |
| 62 | surface: Surface::Message, |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | pub fn card_key(&self) -> String { |
| 67 | match self.surface { |
| 68 | Surface::Message => format!("{}-card-{}", self.message_id, self.card_index), |
| 69 | Surface::Dialog => Self::dialog_key(), |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | pub fn input_key(&self, input_id: &str) -> String { |
| 74 | format!("{}/{input_id}", self.card_key()) |
| 75 | } |
| 76 | |
| 77 | pub fn dialog_key() -> String { |
| 78 | DIALOG_KEY.to_owned() |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | #[derive(Default)] |
| 83 | pub struct CardState { |
| 84 | phases: HashMap<String, ActionPhase>, |
| 85 | visibility: HashMap<String, bool>, |
| 86 | open_cards: HashMap<String, usize>, |
| 87 | notes: HashMap<String, String>, |
| 88 | overrides: HashMap<(String, String), CardOverride>, |
| 89 | input_values: HashMap<String, String>, |
| 90 | input_errors: HashMap<String, String>, |
| 91 | input_fields: HashMap<String, FieldHandle>, |
| 92 | } |
| 93 | |
| 94 | impl CardState { |
| 95 | pub fn phase(&self, action_key: &str) -> Option<&ActionPhase> { |
| 96 | self.phases.get(action_key) |
| 97 | } |
| 98 | |
| 99 | pub fn is_busy(&self, action_key: &str) -> bool { |
| 100 | self.phases.get(action_key) == Some(&ActionPhase::Busy) |
| 101 | } |
| 102 | |
| 103 | pub fn set_phase(&mut self, action_key: &str, phase: ActionPhase) { |
| 104 | self.phases.insert(action_key.to_owned(), phase); |
| 105 | } |
| 106 | |
| 107 | pub fn clear_done(&mut self, action_key: &str) { |
| 108 | if self.phases.get(action_key) == Some(&ActionPhase::Done) { |
| 109 | self.phases.remove(action_key); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | pub fn clear_card(&mut self, card_key: &str) { |
| 114 | self.phases.retain(|key, _| !key.starts_with(card_key)); |
| 115 | self.visibility.retain(|key, _| !key.starts_with(card_key)); |
| 116 | self.open_cards.retain(|key, _| !key.starts_with(card_key)); |
| 117 | self.notes.remove(card_key); |
| 118 | self.input_values.retain(|key, _| !key.starts_with(card_key)); |
| 119 | self.input_errors.retain(|key, _| !key.starts_with(card_key)); |
| 120 | self.input_fields.retain(|key, _| !key.starts_with(card_key)); |
| 121 | } |
| 122 | |
| 123 | pub fn has_input(&self, input_key: &str) -> bool { |
| 124 | self.input_values.contains_key(input_key) |
| 125 | } |
| 126 | |
| 127 | pub fn seed_input(&mut self, input_key: String, value: String, field: Option<FieldHandle>) { |
| 128 | if let Some(field) = field { |
| 129 | self.input_fields.insert(input_key.clone(), field); |
| 130 | } |
| 131 | self.input_values.insert(input_key, value); |
| 132 | } |
| 133 | |
| 134 | pub fn input_value(&self, input_key: &str) -> &str { |
| 135 | self.input_values |
| 136 | .get(input_key) |
| 137 | .map_or("", String::as_str) |
| 138 | } |
| 139 | |
| 140 | pub fn input_text(&self, input_key: &str, cx: &App) -> String { |
| 141 | match self.input_fields.get(input_key) { |
| 142 | Some(handle) => handle.field.text(cx), |
| 143 | None => self.input_value(input_key).to_owned(), |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | pub fn input_field(&self, input_key: &str) -> Option<&InputField> { |
| 148 | self.input_fields.get(input_key).map(|handle| &handle.field) |
| 149 | } |
| 150 | |
| 151 | pub fn set_input_value(&mut self, input_key: &str, value: String) -> bool { |
| 152 | self.input_values.insert(input_key.to_owned(), value); |
| 153 | self.input_errors.remove(input_key).is_some() |
| 154 | } |
| 155 | |
| 156 | pub fn input_error(&self, input_key: &str) -> Option<&str> { |
| 157 | self.input_errors.get(input_key).map(String::as_str) |
| 158 | } |
| 159 | |
| 160 | pub fn set_input_error(&mut self, input_key: &str, message: String) { |
| 161 | self.input_errors.insert(input_key.to_owned(), message); |
| 162 | } |
| 163 | |
| 164 | pub fn clear_input_errors(&mut self, input_keys: &[String]) { |
| 165 | for input_key in input_keys { |
| 166 | self.input_errors.remove(input_key); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | pub fn is_visible(&self, element_key: &str, initial: bool) -> bool { |
| 171 | self.visibility.get(element_key).copied().unwrap_or(initial) |
| 172 | } |
| 173 | |
| 174 | pub fn toggle_visibility(&mut self, element_key: &str, initial: bool, forced: Option<bool>) { |
| 175 | let next = forced.unwrap_or(!self.is_visible(element_key, initial)); |
| 176 | self.visibility.insert(element_key.to_owned(), next); |
| 177 | } |
| 178 | |
| 179 | pub fn open_card(&self, actions_key: &str) -> Option<usize> { |
| 180 | self.open_cards.get(actions_key).copied() |
| 181 | } |
| 182 | |
| 183 | pub fn toggle_open_card(&mut self, actions_key: &str, index: usize) { |
| 184 | if self.open_cards.get(actions_key) == Some(&index) { |
| 185 | self.open_cards.remove(actions_key); |
| 186 | } else { |
| 187 | self.open_cards.insert(actions_key.to_owned(), index); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | pub fn note(&self, card_key: &str) -> Option<&str> { |
| 192 | self.notes.get(card_key).map(String::as_str) |
| 193 | } |
| 194 | |
| 195 | pub fn set_note(&mut self, card_key: &str, text: String) { |
| 196 | self.notes.insert(card_key.to_owned(), text); |
| 197 | } |
| 198 | |
| 199 | pub fn clear_note(&mut self, card_key: &str) { |
| 200 | self.notes.remove(card_key); |
| 201 | } |
| 202 | |
| 203 | pub fn set_override( |
| 204 | &mut self, |
| 205 | conversation_id: &str, |
| 206 | message_id: &str, |
| 207 | replaced: CardOverride, |
| 208 | ) { |
| 209 | self.overrides.insert( |
| 210 | (conversation_id.to_owned(), message_id.to_owned()), |
| 211 | replaced, |
| 212 | ); |
| 213 | } |
| 214 | |
| 215 | pub fn overrides_for(&self, conversation_id: &str) -> HashMap<String, CardOverride> { |
| 216 | self.overrides |
| 217 | .iter() |
| 218 | .filter(|((conversation, _), _)| conversation == conversation_id) |
| 219 | .map(|((_, message_id), replaced)| (message_id.clone(), replaced.clone())) |
| 220 | .collect() |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | pub fn short_reason(reason: &str) -> String { |
| 225 | reason.chars().take(MAX_REASON_CHARS).collect() |
| 226 | } |
| 227 | |
| 228 | #[cfg(test)] |
| 229 | mod tests { |
| 230 | use super::*; |
| 231 | |
| 232 | #[test] |
| 233 | fn done_clears_but_busy_and_failed_stay() { |
| 234 | let mut state = CardState::default(); |
| 235 | state.set_phase("a", ActionPhase::Done); |
| 236 | state.set_phase("b", ActionPhase::Busy); |
| 237 | state.set_phase("c", ActionPhase::Failed("nope".into())); |
| 238 | for key in ["a", "b", "c"] { |
| 239 | state.clear_done(key); |
| 240 | } |
| 241 | assert_eq!(state.phase("a"), None); |
| 242 | assert!(state.is_busy("b")); |
| 243 | assert_eq!(state.phase("c"), Some(&ActionPhase::Failed("nope".into()))); |
| 244 | } |
| 245 | |
| 246 | #[test] |
| 247 | fn visibility_toggles_from_the_initial_value_or_is_forced() { |
| 248 | let mut state = CardState::default(); |
| 249 | assert!(!state.is_visible("e", false)); |
| 250 | state.toggle_visibility("e", false, None); |
| 251 | assert!(state.is_visible("e", false)); |
| 252 | state.toggle_visibility("e", false, None); |
| 253 | assert!(!state.is_visible("e", false)); |
| 254 | state.toggle_visibility("e", false, Some(true)); |
| 255 | state.toggle_visibility("e", false, Some(true)); |
| 256 | assert!(state.is_visible("e", false)); |
| 257 | } |
| 258 | |
| 259 | #[test] |
| 260 | fn one_show_card_is_open_per_action_set() { |
| 261 | let mut state = CardState::default(); |
| 262 | state.toggle_open_card("set", 1); |
| 263 | assert_eq!(state.open_card("set"), Some(1)); |
| 264 | state.toggle_open_card("set", 2); |
| 265 | assert_eq!(state.open_card("set"), Some(2)); |
| 266 | state.toggle_open_card("set", 2); |
| 267 | assert_eq!(state.open_card("set"), None); |
| 268 | } |
| 269 | |
| 270 | #[test] |
| 271 | fn overrides_are_scoped_to_their_conversation() { |
| 272 | let mut state = CardState::default(); |
| 273 | let replaced = CardOverride { |
| 274 | basis: "[]".into(), |
| 275 | cards: Vec::new(), |
| 276 | }; |
| 277 | state.set_override("c1", "m1", replaced.clone()); |
| 278 | assert_eq!(state.overrides_for("c1").get("m1"), Some(&replaced)); |
| 279 | assert!(state.overrides_for("c2").is_empty()); |
| 280 | } |
| 281 | |
| 282 | #[test] |
| 283 | fn reasons_are_shortened() { |
| 284 | assert_eq!( |
| 285 | short_reason(&"x".repeat(500)).chars().count(), |
| 286 | MAX_REASON_CHARS |
| 287 | ); |
| 288 | } |
| 289 | |
| 290 | #[test] |
| 291 | fn card_keys_differ_by_surface_and_index() { |
| 292 | let message = CardScope::message("c", "m", 2); |
| 293 | assert_eq!(message.card_key(), "m-card-2"); |
| 294 | let dialog = CardScope { |
| 295 | surface: Surface::Dialog, |
| 296 | ..message |
| 297 | }; |
| 298 | assert_eq!(dialog.card_key(), "task-dialog"); |
| 299 | } |
| 300 | } |