mirror of
https://github.com/aljazceru/notedeck.git
synced 2025-12-18 17:14:21 +01:00
bugfix: properly sub to new selected acc after removal of selected
Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
@@ -97,18 +97,33 @@ 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 pk != self.cache.fallback() {
|
||||
if let Some(key_store) = &self.storage_writer {
|
||||
if let Err(e) = key_store.remove_key(&removed.key) {
|
||||
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 {
|
||||
self.cache
|
||||
.get(pubkey)
|
||||
|
||||
@@ -50,20 +50,20 @@ impl AccountCache {
|
||||
self.accounts.entry(pk).insert(account)
|
||||
}
|
||||
|
||||
pub(super) fn remove(&mut self, pk: &Pubkey) -> Option<UserAccount> {
|
||||
pub(super) fn remove(&mut self, pk: &Pubkey) -> Option<AccountDeletionResponse> {
|
||||
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<Pubkey>,
|
||||
}
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user