Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update(Attachments): Improve attachments code, remove duplicates #1831

Merged
merged 6 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
40 changes: 5 additions & 35 deletions ui/src/layouts/chats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ 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 +119,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(Uuid::nil());
lgmarchi marked this conversation as resolved.
Show resolved Hide resolved
state
.write()
.mutate(Action::AppendChatAttachments(active_chat_id, files_local_path));
}
});
}
Expand Down Expand Up @@ -177,28 +161,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());
lgmarchi marked this conversation as resolved.
Show resolved Hide resolved
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
Loading