| 1 | use gpui_kit::Context; |
| 2 | |
| 3 | use crate::app_state::AppState; |
| 4 | use crate::emoticons; |
| 5 | |
| 6 | pub fn quick_reaction_glyphs(reaction_ids: &[String], skin_tone: u8) -> Vec<String> { |
| 7 | reaction_ids |
| 8 | .iter() |
| 9 | .filter_map(|reaction_id| chatsvc::reaction_glyph_for_key(reaction_id)) |
| 10 | .map(|glyph| emoticons::with_tone(&glyph, skin_tone)) |
| 11 | .collect() |
| 12 | } |
| 13 | |
| 14 | pub fn reaction_id_for_glyph(glyph: &str) -> Option<String> { |
| 15 | chatsvc::reaction_key(&emoticons::without_tone(glyph)) |
| 16 | } |
| 17 | |
| 18 | pub fn slots_for(reaction_ids: &[String]) -> Vec<Option<String>> { |
| 19 | let length = reaction_ids.len().max(teams_core::DEFAULT_REACTION_SLOTS); |
| 20 | (0..length) |
| 21 | .map(|index| reaction_ids.get(index).cloned()) |
| 22 | .collect() |
| 23 | } |
| 24 | |
| 25 | /// Teams' replace rule: an id already in another slot leaves that slot empty. |
| 26 | pub fn place_in_slot(slots: &mut [Option<String>], index: usize, reaction_id: String) { |
| 27 | for slot in slots.iter_mut() { |
| 28 | if slot.as_deref() == Some(reaction_id.as_str()) { |
| 29 | *slot = None; |
| 30 | } |
| 31 | } |
| 32 | if let Some(slot) = slots.get_mut(index) { |
| 33 | *slot = Some(reaction_id); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | pub fn filled(slots: &[Option<String>]) -> Vec<String> { |
| 38 | slots.iter().flatten().cloned().collect() |
| 39 | } |
| 40 | |
| 41 | impl AppState { |
| 42 | pub fn default_reactions(&self) -> Vec<String> { |
| 43 | teams_core::default_reactions(&self.store) |
| 44 | } |
| 45 | |
| 46 | pub fn quick_reactions(&self) -> Vec<String> { |
| 47 | quick_reaction_glyphs( |
| 48 | &self.default_reactions(), |
| 49 | teams_core::skin_tone(&self.store), |
| 50 | ) |
| 51 | } |
| 52 | |
| 53 | pub fn set_default_reactions(&mut self, reaction_ids: Vec<String>, cx: &mut Context<Self>) { |
| 54 | let Some(engine_or_demo) = self.chat_action_engine(cx) else { |
| 55 | return; |
| 56 | }; |
| 57 | let previous = self.default_reactions(); |
| 58 | let _ = teams_core::store_account_default_reactions(&self.store, Some(&reaction_ids)); |
| 59 | cx.notify(); |
| 60 | let Some(engine) = engine_or_demo else { |
| 61 | return; |
| 62 | }; |
| 63 | self.run_chat_action( |
| 64 | "Customising default reactions", |
| 65 | async move { engine.set_default_reactions(&reaction_ids).await }, |
| 66 | move |state, cx| { |
| 67 | let _ = teams_core::store_account_default_reactions(&state.store, Some(&previous)); |
| 68 | cx.notify(); |
| 69 | }, |
| 70 | |_, _| {}, |
| 71 | cx, |
| 72 | ); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | #[cfg(test)] |
| 77 | mod tests { |
| 78 | use super::*; |
| 79 | |
| 80 | fn ids(values: &[&str]) -> Vec<String> { |
| 81 | values.iter().map(|value| (*value).to_owned()).collect() |
| 82 | } |
| 83 | |
| 84 | #[test] |
| 85 | fn teams_defaults_show_as_glyphs_and_like_takes_the_tone() { |
| 86 | let defaults = ids(&teams_core::TEAMS_DEFAULT_REACTIONS); |
| 87 | assert_eq!( |
| 88 | quick_reaction_glyphs(&defaults, 0), |
| 89 | ["\u{1F44D}", "\u{2764}", "\u{1F606}", "\u{1F62E}"] |
| 90 | ); |
| 91 | assert_eq!(quick_reaction_glyphs(&defaults, 2)[0], "\u{1F44D}\u{1F3FC}"); |
| 92 | assert_eq!( |
| 93 | chatsvc::reaction_key(&quick_reaction_glyphs(&defaults, 2)[0]).as_deref(), |
| 94 | Some("yes-tone2") |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn unknown_ids_are_left_out() { |
| 100 | assert_eq!( |
| 101 | quick_reaction_glyphs(&ids(&["heart", "custom-gone"]), 0), |
| 102 | ["\u{2764}"] |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | #[test] |
| 107 | fn picked_glyphs_store_the_untoned_id() { |
| 108 | assert_eq!( |
| 109 | reaction_id_for_glyph("\u{1F44D}\u{1F3FC}").as_deref(), |
| 110 | Some("like") |
| 111 | ); |
| 112 | assert_eq!( |
| 113 | reaction_id_for_glyph("\u{2764}\u{FE0F}").as_deref(), |
| 114 | Some("heart") |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | #[test] |
| 119 | fn there_are_always_five_slots_and_a_repeat_moves() { |
| 120 | let mut slots = slots_for(&ids(&["like", "heart"])); |
| 121 | assert_eq!(slots.len(), 5); |
| 122 | place_in_slot(&mut slots, 3, "like".to_owned()); |
| 123 | assert_eq!(filled(&slots), ["heart", "like"]); |
| 124 | assert_eq!(slots[0], None); |
| 125 | assert_eq!(slots_for(&ids(&["a", "b", "c", "d", "e", "f"])).len(), 6); |
| 126 | } |
| 127 | } |