Skip to content

Commit

Permalink
update(Attachments): Improve attachments code, remove duplicates (#1831)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgmarchi authored Feb 8, 2024
1 parent d0299fa commit 81b5618
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 78 deletions.
4 changes: 4 additions & 0 deletions common/src/state/action.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::path::PathBuf;
use std::{collections::HashMap, rc::Weak};

use derive_more::Display;
Expand Down Expand Up @@ -179,6 +180,9 @@ pub enum Action<'a> {
/// Sets a files attached to send
#[display(fmt = "SetChatAttachments")]
SetChatAttachments(Uuid, Vec<Location>),
/// Similar to SetChatAttachments, but appends to the existing attachments
#[display(fmt = "AppendChatAttachments")]
AppendChatAttachments(Uuid, Vec<PathBuf>),
/// Clear attachments on chat
#[display(fmt = "ClearChatAttachments")]
ClearChatAttachments(Uuid),
Expand Down
18 changes: 18 additions & 0 deletions common/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,24 @@ impl State {
Action::SetChatAttachments(chat_id, value) => {
self.set_chat_attachments(&chat_id, value)
}
Action::AppendChatAttachments(chat_id, value) => {
if value.is_empty() {
return;
}
let new_files: Vec<Location> = value
.iter()
.map(|path| Location::Disk { path: path.clone() })
.collect();
let mut current_files: Vec<_> = self
.get_active_chat()
.map(|f| f.files_attached_to_send)
.unwrap_or_default()
.drain(..)
.filter(|x| !new_files.contains(x))
.collect();
current_files.extend(new_files);
self.set_chat_attachments(&chat_id, current_files)
}
Action::ClearChatAttachments(chat_id) => self.clear_chat_attachments(&chat_id),
Action::AddReaction(_, _, emoji) => {
self.ui.emojis.increment_emoji(emoji);
Expand Down
43 changes: 6 additions & 37 deletions ui/src/layouts/chats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ use dioxus::prelude::*;
use dioxus_desktop::{use_window, wry::webview::FileDropEvent, DesktopContext};
use dioxus_html::input_data::keyboard_types::Code;
use dioxus_html::input_data::keyboard_types::Modifiers;
use uuid::Uuid;
use warp::raygun::Location;

type UseEvalFn = Rc<dyn Fn(&str) -> Result<UseEval, EvalError>>;

Expand Down Expand Up @@ -120,25 +118,10 @@ pub fn ChatLayout(cx: Scope) -> Element {
})
.await
.expect("Should succeed");
if !files_local_path.is_empty() {
let new_files: Vec<Location> = files_local_path
.iter()
.map(|path| Location::Disk { path: path.clone() })
.collect();
let mut current_files: Vec<_> = state
.read()
.get_active_chat()
.map(|f| f.files_attached_to_send)
.unwrap_or_default()
.drain(..)
.filter(|x| !new_files.contains(x))
.collect();
current_files.extend(new_files);
let active_chat_id = state.read().get_active_chat().map(|f| f.id).unwrap_or(Uuid::nil());
state
.write()
.mutate(Action::SetChatAttachments(active_chat_id, current_files));
}
let active_chat_id = state.read().get_active_chat().map(|f| f.id).unwrap_or_default();
state
.write()
.mutate(Action::AppendChatAttachments(active_chat_id, files_local_path));
}
});
}
Expand Down Expand Up @@ -177,28 +160,14 @@ async fn drop_and_attach_files(
state: UseSharedState<State>,
) {
let new_files = drag_and_drop_function(eval, window, drag_event).await;
let new_files: Vec<Location> = new_files
.iter()
.map(|path| Location::Disk { path: path.clone() })
.collect();

let mut current_files: Vec<_> = state
.read()
.get_active_chat()
.map(|f| f.files_attached_to_send)
.unwrap_or_default()
.drain(..)
.filter(|x| !new_files.contains(x))
.collect();
current_files.extend(new_files);
let chat_uuid = state
.read()
.get_active_chat()
.map(|f| f.id)
.unwrap_or(Uuid::nil());
.unwrap_or_default();
state
.write()
.mutate(Action::SetChatAttachments(chat_uuid, current_files));
.mutate(Action::AppendChatAttachments(chat_uuid, new_files));
}

// Like ui::src:layout::storage::drag_and_drop_function
Expand Down
44 changes: 5 additions & 39 deletions ui/src/layouts/chats/presentation/chatbar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,24 +334,9 @@ pub fn get_chatbar<'a>(cx: &'a Scoped<'a, ChatProps>) -> Element<'a> {
&& keyboard_data.modifiers() == Modifiers::CONTROL && *enable_paste_shortcut.read()
{
let files_local_path = get_files_path_from_clipboard().unwrap_or_default();
if !files_local_path.is_empty() {
let new_files: Vec<Location> = files_local_path
.iter()
.map(|path| Location::Disk { path: path.clone() })
.collect();
let mut current_files: Vec<_> = state
.read()
.get_active_chat()
.map(|f| f.files_attached_to_send)
.unwrap_or_default()
.drain(..)
.filter(|x| !new_files.contains(x))
.collect();
current_files.extend(new_files);
state
.write()
.mutate(Action::SetChatAttachments(active_chat_id, current_files));
}
.mutate(Action::AppendChatAttachments(active_chat_id, files_local_path));
}
}
},
Expand Down Expand Up @@ -534,13 +519,9 @@ pub fn get_chatbar<'a>(cx: &'a Scoped<'a, ChatProps>) -> Element<'a> {
.set_directory(dirs::home_dir().unwrap_or_default())
.pick_files()
{
let new_files: Vec<Location> = new_files.iter()
.map(|path| Location::Disk { path: path.clone() })
.collect();
let mut current_files: Vec<_> = state.read().get_active_chat().map(|f| f.files_attached_to_send)
.unwrap_or_default().drain(..).filter(|x| !new_files.contains(x)).collect();
current_files.extend(new_files);
state.write().mutate(Action::SetChatAttachments(active_chat_id, current_files));
state
.write()
.mutate(Action::AppendChatAttachments(active_chat_id, new_files));
update_send();
}
},
Expand All @@ -561,24 +542,9 @@ pub fn get_chatbar<'a>(cx: &'a Scoped<'a, ChatProps>) -> Element<'a> {
if state.read().ui.metadata.focused && *enable_paste_shortcut.read() {
rsx!(shortcuts::paste_file_shortcut::PasteFilesShortcut {
on_paste: move |files_local_path: Vec<PathBuf>| {
if !files_local_path.is_empty() {
let new_files: Vec<Location> = files_local_path
.iter()
.map(|path| Location::Disk { path: path.clone() })
.collect();
let mut current_files: Vec<_> = state
.read()
.get_active_chat()
.map(|f| f.files_attached_to_send)
.unwrap_or_default()
.drain(..)
.filter(|x| !new_files.contains(x))
.collect();
current_files.extend(new_files);
state
.write()
.mutate(Action::SetChatAttachments(active_chat_id, current_files));
}
.mutate(Action::AppendChatAttachments(active_chat_id, files_local_path));
}})
}
SendFilesLayoutModal {
Expand Down
4 changes: 2 additions & 2 deletions ui/src/layouts/chats/presentation/messages/coroutines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ pub fn handle_msg_scroll(
.active_chat
.messages
.bottom()
.unwrap_or(Uuid::nil());
.unwrap_or_default();
let top_msg_id: Uuid = chat_data
.read()
.active_chat
.messages
.top()
.unwrap_or(Uuid::nil());
.unwrap_or_default();

log::trace!(
"top msg is: {}, bottom msg is: {}",
Expand Down

0 comments on commit 81b5618

Please sign in to comment.