From ebbd2e358e1275c0c286619a3099abc58ad12de0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ail=C3=A9n=20Grimaldi?= <63558201+ail3ngrimaldi@users.noreply.github.com> Date: Tue, 5 Nov 2024 18:06:25 -0300 Subject: [PATCH] Check if accounts is empty to avoid errors from #166 (#167) * Add if statements to check if the accounts object is empty and avoid out of bounds error * prevent hook error by changing handle_warning fn --- src/components/molecules/header.rs | 9 +++++++-- src/hooks/use_notification.rs | 4 ++-- src/locales/en-US.json | 3 ++- src/locales/es-ES.json | 3 ++- src/middlewares/is_signer_ready.rs | 2 +- src/pages/account.rs | 2 +- 6 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/components/molecules/header.rs b/src/components/molecules/header.rs index 7a66dab..e7fa954 100644 --- a/src/components/molecules/header.rs +++ b/src/components/molecules/header.rs @@ -93,7 +93,7 @@ pub fn Header() -> Element { Ok::<(), String>(()) } - .unwrap_or_else(move |e: String| notification.handle_warning(&e)) + .unwrap_or_else(move |e: String| notification.handle_warning(&translate!(i18, "warnings.title"), &e)) }); }; let mut dropdown_value = use_signal::>(|| { @@ -116,7 +116,12 @@ pub fn Header() -> Element { let on_handle_account = use_coroutine(move |mut rx: UnboundedReceiver| async move { while let Some(event) = rx.next().await { - let account = &accounts.get()[event as usize]; + let account_list = accounts.get(); + + let Some(account) = account_list.get(event as usize) else { + // return; + return notification.handle_warning(&translate!(i18, "warnings.title"), &translate!(i18, "warnings.middleware.not_account")); + }; let Ok(serialized_session) = serde_json::to_string(&UserSession { name: account.name(), diff --git a/src/hooks/use_notification.rs b/src/hooks/use_notification.rs index adf40f3..6bbf75e 100644 --- a/src/hooks/use_notification.rs +++ b/src/hooks/use_notification.rs @@ -70,9 +70,9 @@ impl UseNotificationState { }, }); } - pub fn handle_warning(&mut self, body: &str) { + pub fn handle_warning(&mut self, title: &str, body: &str) { self.handle_notification(NotificationItem { - title: translate!(use_i18(), "warnings.title"), + title: String::from(title), body: String::from(body), variant: NotificationVariant::Warning, show: true, diff --git a/src/locales/en-US.json b/src/locales/en-US.json index 0832336..bc8c88c 100644 --- a/src/locales/en-US.json +++ b/src/locales/en-US.json @@ -478,7 +478,8 @@ "middleware": { "has_dao": "We're sorry! 😞 You cannot create more organizations associated with this account.", "chain_unavailable": "At this time, it is not possible to complete this action.", - "signer_not_found": "We're sorry! 😞 You cannot perform this action, you must authenticate first." + "signer_not_found": "We're sorry! 😞 You cannot perform this action, you must authenticate first.", + "not_account": "You are not connected to an account. Connect to access more features and easily manage your assets." } }, "utils": { diff --git a/src/locales/es-ES.json b/src/locales/es-ES.json index b5bff4b..bab2e32 100644 --- a/src/locales/es-ES.json +++ b/src/locales/es-ES.json @@ -492,7 +492,8 @@ "middleware": { "has_dao": "¡Lo sentimos! 😞 No puedes crear más organizaciones asociadas a esta cuenta.", "chain_unavailable": "En este momento no es posible completar está acción.", - "signer_not_found": "¡Lo sentimos! 😞 No puedes realizar esta acción, primero debes autenticarte." + "signer_not_found": "¡Lo sentimos! 😞 No puedes realizar esta acción, primero debes autenticarte.", + "not_account": "No estás conectado a una cuenta. Conéctate para acceder a más funciones y gestionar tus activos de manera sencilla." } }, "errors": { diff --git a/src/middlewares/is_signer_ready.rs b/src/middlewares/is_signer_ready.rs index 11b6971..9893cc4 100644 --- a/src/middlewares/is_signer_ready.rs +++ b/src/middlewares/is_signer_ready.rs @@ -7,7 +7,7 @@ pub fn is_signer_ready( ) -> impl FnOnce() -> Result<(), &'static str> { move || { if accounts.get_account().is_none() { - notification.handle_warning(&translate!(i18, "warnings.middleware.signer_not_found")); + notification.handle_warning(&translate!(i18, "warnings.title"), &translate!(i18, "warnings.middleware.signer_not_found")); Err("Failed to get account to sign") } else { log::debug!("Signer is ready"); diff --git a/src/pages/account.rs b/src/pages/account.rs index c852d95..19f62d9 100644 --- a/src/pages/account.rs +++ b/src/pages/account.rs @@ -99,7 +99,7 @@ pub fn Account() -> Element { Ok::<(), String>(()) } - .unwrap_or_else(move |e: String| notification.handle_warning(&e)) + .unwrap_or_else(move |e: String| notification.handle_warning(&translate!(i18, "warnings.title"), &e)) }); };