Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow deleting last wallet #122

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions lib/state/appStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,15 @@ export const useAppStore = create<AppState>()((set, get) => {
const removeCurrentWallet = () => {
const wallets = [...get().wallets];
if (wallets.length <= 1) {
// cannot delete last wallet
// set to initial wallet status
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this check is needed or if we can actually just delete the whole if?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't we need this check? We only do the below if it is the last wallet right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the bottom is still does:

    get().setSelectedWalletId(0);
    set({
      wallets: wallets.filter((_, i) => i !== selectedWalletId),
    });

But I think there is actually a bug in the for loop - it will always not work if you try delete the wallet at the last index

secureStorage.removeItem(hasOnboardedKey);
secureStorage.setItem(selectedWalletIdKey, "0");
secureStorage.setItem(getWalletKey(0), JSON.stringify({}));
set({
nwcClient: undefined,
selectedWalletId: 0,
wallets: [{}],
});
return;
}
const selectedWalletId = get().selectedWalletId;
Expand Down Expand Up @@ -191,17 +199,22 @@ export const useAppStore = create<AppState>()((set, get) => {
for (let i = 0; i < get().addressBookEntries.length; i++) {
secureStorage.removeItem(getAddressBookEntryKey(i));
}
// clear selected wallet ID
secureStorage.removeItem(selectedWalletIdKey);

// clear fiat currency
secureStorage.removeItem(fiatCurrencyKey);

// clear onboarding status
secureStorage.removeItem(hasOnboardedKey);

// set to initial wallet status
secureStorage.setItem(selectedWalletIdKey, "0");
secureStorage.setItem(getWalletKey(0), JSON.stringify({}));

set({
nwcClient: undefined,
fiatCurrency: undefined,
selectedWalletId: undefined,
wallets: [],
selectedWalletId: 0,
wallets: [{}],
addressBookEntries: [],
});
},
Expand Down
7 changes: 6 additions & 1 deletion pages/settings/wallets/EditWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ export function EditWallet() {
{
text: "Confirm",
onPress: () => {
router.back();
if (useAppStore.getState().wallets.length == 1) {
router.dismissAll();
router.replace("/onboarding");
} else {
router.back();
}
useAppStore.getState().removeCurrentWallet();
},
},
Expand Down
Loading