Skip to content

Commit 92a7516

Browse files
authored
Merge pull request #116 from spacemeshos/fix-auto-selecting-account-to-spawn
Fix: autoselect account to spawn properly
2 parents 50fa0f3 + 977660f commit 92a7516

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

src/components/sendTx/SpawnAnotherAccount.tsx

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useState } from 'react';
1+
import { useCallback, useEffect, useState } from 'react';
22
import {
33
UseFormRegister,
44
UseFormSetValue,
@@ -40,13 +40,29 @@ function SpawnAnotherAccount({
4040
}: SpawnAnotherAccount) {
4141
const genesisID = useCurrentGenesisID();
4242
const { isSpawnedAccount } = useAccountData();
43-
const isSpawned = (acc: AccountWithAddress<AnySpawnArguments>) =>
44-
pipe(
45-
genesisID,
46-
O.mapWithDefault(false, (genesis) =>
47-
isSpawnedAccount(genesis, acc.address)
48-
)
49-
);
43+
const isSpawned = useCallback(
44+
(acc: AccountWithAddress<AnySpawnArguments>) =>
45+
pipe(
46+
genesisID,
47+
O.mapWithDefault(false, (genesis) =>
48+
isSpawnedAccount(genesis, acc.address)
49+
)
50+
),
51+
[genesisID, isSpawnedAccount]
52+
);
53+
54+
useEffect(() => {
55+
if (accounts.length > 0) {
56+
const firstUnspawned = accounts.find((x) => !isSpawned(x));
57+
if (firstUnspawned) {
58+
setSelectedAddress(firstUnspawned.address);
59+
setValue(
60+
'templateAddress',
61+
firstUnspawned.templateAddress as StdTemplateKeys
62+
);
63+
}
64+
}
65+
}, [accounts, isSpawned, setValue]);
5066

5167
const [selectedAddress, setSelectedAddress] = useState(
5268
accounts.find((x) => !isSpawned(x))?.address

0 commit comments

Comments
 (0)