From d4082eb818613d348da4771ea18c96fc218bb1d4 Mon Sep 17 00:00:00 2001 From: kernelkind Date: Thu, 17 Jul 2025 20:31:36 -0400 Subject: [PATCH] bugfix: properly sub to new selected acc after removal of selected Signed-off-by: kernelkind --- crates/notedeck/src/account/accounts.rs | 27 +++++++++++++++++++------ crates/notedeck/src/account/cache.rs | 21 +++++++++++++++---- crates/notedeck_columns/src/nav.rs | 5 ++++- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/crates/notedeck/src/account/accounts.rs b/crates/notedeck/src/account/accounts.rs index 6874fd7..faca5ef 100644 --- a/crates/notedeck/src/account/accounts.rs +++ b/crates/notedeck/src/account/accounts.rs @@ -97,16 +97,31 @@ impl Accounts { } } - pub fn remove_account(&mut self, pk: &Pubkey) { - let Some(removed) = self.cache.remove(pk) else { - return; + pub fn remove_account( + &mut self, + pk: &Pubkey, + ndb: &mut Ndb, + pool: &mut RelayPool, + ctx: &egui::Context, + ) -> bool { + let Some(resp) = self.cache.remove(pk) else { + return false; }; - if let Some(key_store) = &self.storage_writer { - if let Err(e) = key_store.remove_key(&removed.key) { - tracing::error!("Could not remove account {pk}: {e}"); + if pk != self.cache.fallback() { + if let Some(key_store) = &self.storage_writer { + if let Err(e) = key_store.remove_key(&resp.deleted) { + tracing::error!("Could not remove account {pk}: {e}"); + } } } + + if let Some(swap_to) = resp.swap_to { + let txn = Transaction::new(ndb).expect("txn"); + self.select_account_internal(&swap_to, ndb, &txn, pool, ctx); + } + + true } pub fn contains_full_kp(&self, pubkey: &enostr::Pubkey) -> bool { diff --git a/crates/notedeck/src/account/cache.rs b/crates/notedeck/src/account/cache.rs index 6806bab..541b3f9 100644 --- a/crates/notedeck/src/account/cache.rs +++ b/crates/notedeck/src/account/cache.rs @@ -50,20 +50,20 @@ impl AccountCache { self.accounts.entry(pk).insert(account) } - pub(super) fn remove(&mut self, pk: &Pubkey) -> Option { + pub(super) fn remove(&mut self, pk: &Pubkey) -> Option { if *pk == self.fallback && self.accounts.len() == 1 { // no point in removing it since it'll just get re-added anyway return None; } - let removed = self.accounts.remove(pk); + let removed = self.accounts.remove(pk)?; if self.accounts.is_empty() { self.accounts .insert(self.fallback, self.fallback_account.clone()); } - if removed.is_some() && self.selected == *pk { + if self.selected == *pk { // TODO(kernelkind): choose next better let (next, _) = self .accounts @@ -71,9 +71,17 @@ impl AccountCache { .next() .expect("accounts can never be empty"); self.selected = *next; + + return Some(AccountDeletionResponse { + deleted: removed.key, + swap_to: Some(*next), + }); } - removed + Some(AccountDeletionResponse { + deleted: removed.key, + swap_to: None, + }) } /// guarenteed that all selected exist in accounts @@ -111,3 +119,8 @@ impl<'a> IntoIterator for &'a AccountCache { self.accounts.iter() } } + +pub struct AccountDeletionResponse { + pub deleted: enostr::Keypair, + pub swap_to: Option, +} diff --git a/crates/notedeck_columns/src/nav.rs b/crates/notedeck_columns/src/nav.rs index 6879eb8..8e72dda 100644 --- a/crates/notedeck_columns/src/nav.rs +++ b/crates/notedeck_columns/src/nav.rs @@ -94,7 +94,10 @@ impl SwitchingAction { .router_mut() .go_back(); } - AccountsAction::Remove(to_remove) => ctx.accounts.remove_account(to_remove), + AccountsAction::Remove(to_remove) => { + ctx.accounts + .remove_account(to_remove, ctx.ndb, ctx.pool, ui_ctx); + } }, SwitchingAction::Columns(columns_action) => match *columns_action { ColumnsAction::Remove(index) => {