Skip to content

Commit

Permalink
feat(chat): Add a notification if whether download is success or fail…
Browse files Browse the repository at this point in the history
…ed (#1492)

Co-authored-by: Flemmli97 <Flemmli97@users.noreply.github.com>
Co-authored-by: Darius Clark <dariusc93@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 9, 2023
1 parent cae2123 commit 42615d4
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 9 deletions.
2 changes: 2 additions & 0 deletions common/locales/en-US/main.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ files = Files
.no-files-available = No files available.
.share-files = Share Files
.go-to-files = Go to Files
.download-failed = Failed to download file { $file }
.download-success = Downloaded file { $file }
settings = Settings
.settings = Settings
Expand Down
36 changes: 35 additions & 1 deletion ui/src/layouts/chats/presentation/messages/coroutines.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::time::Duration;

use common::{
state::{Action, State},
language::get_local_text_with_args,
state::{Action, State, ToastNotification},
warp_runner::{FetchMessagesConfig, FetchMessagesResponse, RayGunCmd, WarpCmd},
WARP_CMD_CH,
};
Expand Down Expand Up @@ -397,6 +398,17 @@ pub fn handle_warp_commands(
}))
{
log::error!("failed to send warp command: {}", e);
state.write().mutate(Action::AddToastNotification(
ToastNotification::init(
"".into(),
get_local_text_with_args(
"files.download-failed",
vec![("file", file.name())],
),
None,
2,
),
));
if let Some(conv) = pending_downloads.write().get_mut(&conv_id) {
conv.remove(&file);
}
Expand All @@ -409,8 +421,30 @@ pub fn handle_warp_commands(
while let Some(p) = stream.next().await {
log::debug!("{p:?}");
}
state.write().mutate(Action::AddToastNotification(
ToastNotification::init(
"".into(),
get_local_text_with_args(
"files.download-success",
vec![("file", file.name())],
),
None,
2,
),
));
}
Err(e) => {
state.write().mutate(Action::AddToastNotification(
ToastNotification::init(
"".into(),
get_local_text_with_args(
"files.download-failed",
vec![("file", file.name())],
),
None,
2,
),
));
log::error!("failed to download attachment: {}", e);
}
}
Expand Down
2 changes: 1 addition & 1 deletion ui/src/layouts/storage/files_layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn FilesLayout(cx: Scope<'_>) -> Element<'_> {

functions::use_allow_block_folder_nav(cx, &files_in_queue_to_upload);

let ch: &Coroutine<ChanCmd> = functions::init_coroutine(cx, storage_controller);
let ch: &Coroutine<ChanCmd> = functions::init_coroutine(cx, storage_controller, state);

use_future(cx, (), |_| {
to_owned![files_been_uploaded, files_in_queue_to_upload];
Expand Down
49 changes: 43 additions & 6 deletions ui/src/layouts/storage/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,10 @@ pub enum ChanCmd {
pub fn init_coroutine<'a>(
cx: &'a ScopeState,
controller: &'a UseRef<StorageController>,
state: &'a UseSharedState<State>,
) -> &'a Coroutine<ChanCmd> {
let ch = use_coroutine(cx, |mut rx: UnboundedReceiver<ChanCmd>| {
to_owned![controller];
to_owned![controller, state];
async move {
let warp_cmd_tx = WARP_CMD_CH.tx.clone();
while let Some(cmd) = rx.next().await {
Expand Down Expand Up @@ -339,20 +340,56 @@ pub fn init_coroutine<'a>(

if let Err(e) = warp_cmd_tx.send(WarpCmd::Constellation(
ConstellationCmd::DownloadFile {
file_name,
file_name: file_name.clone(),
local_path_to_save_file,
rsp: tx,
},
)) {
state.write().mutate(Action::AddToastNotification(
ToastNotification::init(
"".into(),
get_local_text_with_args(
"files.download-failed",
vec![("file", file_name)],
),
None,
2,
),
));
log::error!("failed to download file {}", e);
continue;
}

let rsp = rx.await.expect("command canceled");

if let Err(error) = rsp {
log::error!("failed to download file: {}", error);
continue;
match rsp {
Ok(_) => {
state.write().mutate(Action::AddToastNotification(
ToastNotification::init(
"".into(),
get_local_text_with_args(
"files.download-success",
vec![("file", file_name)],
),
None,
2,
),
));
}
Err(error) => {
state.write().mutate(Action::AddToastNotification(
ToastNotification::init(
"".into(),
get_local_text_with_args(
"files.download-failed",
vec![("file", file_name)],
),
None,
2,
),
));
log::error!("failed to download file: {}", error);
continue;
}
}
}
ChanCmd::RenameItem { old_name, new_name } => {
Expand Down
2 changes: 1 addition & 1 deletion ui/src/layouts/storage/send_files_layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn SendFilesLayout<'a>(cx: Scope<'a, SendFilesProps<'a>>) -> Element<'a> {
let send_files_from_storage_state = cx.props.send_files_from_storage_state.clone();
let storage_controller = StorageController::new(cx, state);
let first_render = use_ref(cx, || true);
let ch: &Coroutine<ChanCmd> = functions::init_coroutine(cx, storage_controller);
let ch: &Coroutine<ChanCmd> = functions::init_coroutine(cx, storage_controller, state);
let in_files = send_files_start_location.eq(&SendFilesStartLocation::Storage);
functions::get_items_from_current_directory(cx, ch);

Expand Down

0 comments on commit 42615d4

Please sign in to comment.