diff --git a/src/renderer/entities/wallet/model/wallet-model.ts b/src/renderer/entities/wallet/model/wallet-model.ts index d7c0840763..50bdfd9190 100644 --- a/src/renderer/entities/wallet/model/wallet-model.ts +++ b/src/renderer/entities/wallet/model/wallet-model.ts @@ -90,18 +90,20 @@ type CreateResult = { accounts: Account[]; external: boolean; }; -const walletCreatedFx = createEffect(async ({ wallet, accounts }: CreateParams): Promise => { - const dbWallet = await storageService.wallets.create({ ...wallet, isActive: false }); +const walletCreatedFx = createEffect( + async ({ wallet, accounts, external }: CreateParams): Promise => { + const dbWallet = await storageService.wallets.create({ ...wallet, isActive: false }); - if (!dbWallet) return undefined; + if (!dbWallet) return undefined; - const accountsPayload = accounts.map((account) => ({ ...account, walletId: dbWallet.id })); - const dbAccounts = await storageService.accounts.createAll(accountsPayload); + const accountsPayload = accounts.map((account) => ({ ...account, walletId: dbWallet.id })); + const dbAccounts = await storageService.accounts.createAll(accountsPayload); - if (!dbAccounts) return undefined; + if (!dbAccounts) return undefined; - return { wallet: dbWallet, accounts: dbAccounts, external: false }; -}); + return { wallet: dbWallet, accounts: dbAccounts, external }; + }, +); const multishardCreatedFx = createEffect( async ({ diff --git a/src/renderer/features/wallets/WalletSelect/lib/wallet-select-utils.ts b/src/renderer/features/wallets/WalletSelect/lib/wallet-select-utils.ts index 756aee33bf..d2c4efff5e 100644 --- a/src/renderer/features/wallets/WalletSelect/lib/wallet-select-utils.ts +++ b/src/renderer/features/wallets/WalletSelect/lib/wallet-select-utils.ts @@ -2,11 +2,7 @@ import { type Wallet, type WalletFamily, WalletType } from '@/shared/core'; import { includes } from '@/shared/lib/utils'; import { walletUtils } from '@/entities/wallet'; -export const walletSelectUtils = { - getWalletByGroups, -}; - -function getWalletByGroups(wallets: Wallet[], query = ''): Record { +const getWalletByGroups = (wallets: Wallet[], query = ''): Record => { const accumulator: Record = { [WalletType.POLKADOT_VAULT]: [], [WalletType.MULTISIG]: [], @@ -32,4 +28,13 @@ function getWalletByGroups(wallets: Wallet[], query = ''): Record { + return getWalletByGroups(wallets)[WalletType.POLKADOT_VAULT].at(0) ?? null; +}; + +export const walletSelectUtils = { + getWalletByGroups, + getFirstWallet, +}; diff --git a/src/renderer/features/wallets/WalletSelect/model/wallet-select-model.ts b/src/renderer/features/wallets/WalletSelect/model/wallet-select-model.ts index 5a7878d5ee..5785874833 100644 --- a/src/renderer/features/wallets/WalletSelect/model/wallet-select-model.ts +++ b/src/renderer/features/wallets/WalletSelect/model/wallet-select-model.ts @@ -1,9 +1,9 @@ import { default as BigNumber } from 'bignumber.js'; import { attach, combine, createApi, createEvent, createStore, sample } from 'effector'; -import { once, previous } from 'patronum'; +import { once } from 'patronum'; import { type Account, type ID, type Wallet } from '@/shared/core'; -import { dictionary, getRoundedValue, totalAmount } from '@/shared/lib/utils'; +import { dictionary, getRoundedValue, nonNullable, totalAmount } from '@/shared/lib/utils'; import { balanceModel } from '@/entities/balance'; import { networkModel } from '@/entities/network'; import { currencyModel, priceProviderModel } from '@/entities/price'; @@ -25,18 +25,6 @@ const callbacksApi = createApi($callbacks, { const $walletId = createStore(null); const $filterQuery = createStore(''); -const $isWalletsRemoved = combine( - { - prevWallets: previous(walletModel.$wallets), - wallets: walletModel.$wallets, - }, - ({ prevWallets, wallets }) => { - if (!prevWallets) return false; - - return prevWallets.length > wallets.length; - }, -); - const $walletForDetails = combine( { walletId: $walletId, @@ -100,32 +88,21 @@ sample({ clock: queryChanged, target: $filterQuery }); sample({ clock: walletIdSet, target: $walletId }); -sample({ - clock: $isWalletsRemoved, - source: walletModel.$wallets, - filter: (wallets, isWalletsRemoved) => { - if (!isWalletsRemoved || wallets.length === 0) return false; - - return wallets.every((wallet) => !wallet.isActive); - }, - fn: (wallets) => { - const groups = walletSelectUtils.getWalletByGroups(wallets); +const select = sample({ + clock: walletModel.$wallets, + filter: (wallets) => wallets.every((wallet) => !wallet.isActive), + fn: (wallets) => walletSelectUtils.getFirstWallet(wallets)?.id ?? null, +}); - return Object.values(groups).flat()[0].id; - }, +sample({ + clock: select.filter({ fn: nonNullable }), target: walletModel.events.selectWallet, }); sample({ clock: walletModel.events.walletCreatedDone, - source: walletModel.$wallets, - filter: (wallets, { wallet, external }) => { - const foundWallet = wallets.find((w) => w.id === wallet.id); - if (!foundWallet) return false; - - return !walletUtils.isProxied(foundWallet) && !walletUtils.isMultisig(foundWallet) && !external; - }, - fn: (_, { wallet }) => wallet.id, + filter: ({ external }) => !external, + fn: ({ wallet }) => wallet.id, target: walletModel.events.selectWallet, }); diff --git a/src/renderer/widgets/CreateWallet/model/flow-model.ts b/src/renderer/widgets/CreateWallet/model/flow-model.ts index 12dccae01b..32b3cc5b99 100644 --- a/src/renderer/widgets/CreateWallet/model/flow-model.ts +++ b/src/renderer/widgets/CreateWallet/model/flow-model.ts @@ -264,14 +264,6 @@ sample({ target: $error, }); -sample({ - clock: walletModel.events.walletCreatedDone, - filter: ({ wallet, external }) => wallet.type === WalletType.MULTISIG && !external, - fn: ({ wallet }) => wallet.id, - // wallet selection shouldn't be here, but here we are - target: [walletModel.events.selectWallet, walletProviderModel.events.completed], -}); - // Submit sample({