Skip to content

Commit

Permalink
Check if accounts is empty to avoid errors from #166 (#167)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ail3ngrimaldi authored Nov 5, 2024
1 parent 64998e4 commit ebbd2e3
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 8 deletions.
9 changes: 7 additions & 2 deletions src/components/molecules/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Option<DropdownItem>>(|| {
Expand All @@ -116,7 +116,12 @@ pub fn Header() -> Element {

let on_handle_account = use_coroutine(move |mut rx: UnboundedReceiver<u8>| 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(),
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/use_notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 2 additions & 1 deletion src/locales/es-ES.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/is_signer_ready.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/pages/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
});
};

Expand Down

0 comments on commit ebbd2e3

Please sign in to comment.