summaryrefslogtreecommitdiffstats
path: root/src/open_chats.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/open_chats.rs')
-rw-r--r--src/open_chats.rs88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/open_chats.rs b/src/open_chats.rs
new file mode 100644
index 0000000..ed89537
--- /dev/null
+++ b/src/open_chats.rs
@@ -0,0 +1,88 @@
+use filamento::chat::ChatStoreFields;
+use indexmap::IndexMap;
+use jid::BareJID;
+use reactive_stores::{ArcStore, Store};
+use tracing::debug;
+use leptos::prelude::*;
+
+use crate::chat::MacawChat;
+
+#[derive(Store, Default)]
+pub struct OpenChatsPanel {
+ // jid must be a chat in the chats map
+ chat_view: Option<BareJID>,
+ #[store(key: BareJID = |(jid, _)| jid.clone())]
+ chats: IndexMap<BareJID, MacawChat>,
+}
+
+pub fn open_chat(open_chats: Store<OpenChatsPanel>, chat: MacawChat) {
+ if let Some(jid) = &*open_chats.chat_view().read() {
+ if let Some((index, _jid, entry)) = open_chats.chats().write().shift_remove_full(jid) {
+ let new_jid = <ArcStore<filamento::chat::Chat> as Clone>::clone(&chat.chat)
+ .correspondent()
+ .read()
+ .clone();
+ open_chats
+ .chats()
+ .write()
+ .insert_before(index, new_jid.clone(), chat);
+ *open_chats.chat_view().write() = Some(new_jid);
+ } else {
+ let new_jid = <ArcStore<filamento::chat::Chat> as Clone>::clone(&chat.chat)
+ .correspondent()
+ .read()
+ .clone();
+ open_chats.chats().write().insert(new_jid.clone(), chat);
+ *open_chats.chat_view().write() = Some(new_jid);
+ }
+ } else {
+ let new_jid = <ArcStore<filamento::chat::Chat> as Clone>::clone(&chat.chat)
+ .correspondent()
+ .read()
+ .clone();
+ open_chats.chats().write().insert(new_jid.clone(), chat);
+ *open_chats.chat_view().write() = Some(new_jid);
+ }
+}
+
+impl OpenChatsPanel {
+ pub fn open(&mut self, chat: MacawChat) {
+ if let Some(jid) = &mut self.chat_view {
+ debug!("a chat was already open");
+ if let Some((index, _jid, entry)) = self.chats.shift_remove_full(jid) {
+ let new_jid = <ArcStore<filamento::chat::Chat> as Clone>::clone(&chat.chat)
+ .correspondent()
+ .read()
+ .clone();
+ self.chats.insert_before(index, new_jid.clone(), chat);
+ *&mut self.chat_view = Some(new_jid);
+ } else {
+ let new_jid = <ArcStore<filamento::chat::Chat> as Clone>::clone(&chat.chat)
+ .correspondent()
+ .read()
+ .clone();
+ self.chats.insert(new_jid.clone(), chat);
+ *&mut self.chat_view = Some(new_jid);
+ }
+ } else {
+ let new_jid = <ArcStore<filamento::chat::Chat> as Clone>::clone(&chat.chat)
+ .correspondent()
+ .read()
+ .clone();
+ self.chats.insert(new_jid.clone(), chat);
+ *&mut self.chat_view = Some(new_jid);
+ }
+ debug!("opened chat");
+ }
+
+ // TODO:
+ // pub fn open_in_new_tab_unfocused(&mut self) {
+
+ // }
+
+ // pub fn open_in_new_tab_focus(&mut self) {
+
+ // }
+}
+
+