Skip to content

Commit

Permalink
fix: display selected signatory wallet (#2638)
Browse files Browse the repository at this point in the history
  • Loading branch information
sokolova-an authored Nov 13, 2024
1 parent 0c1aa31 commit fadf2ca
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 27 deletions.
1 change: 1 addition & 0 deletions src/renderer/widgets/CreateWallet/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ export interface SignatoryInfo {
index: number;
name: string;
address: string;
walletId: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@ describe('widgets/CreateWallet/model/form-model', () => {

await allSettled(signatoryModel.events.changeSignatory, {
scope,
params: { index: 0, name: signerWallet.name, address: toAddress(signerWallet.accounts[0].accountId) },
params: {
index: 0,
name: signerWallet.name,
address: toAddress(signerWallet.accounts[0].accountId),
walletId: '1',
},
});
await allSettled(signatoryModel.events.changeSignatory, {
scope,
params: { index: 1, name: 'Alice', address: '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY' },
params: { index: 1, name: 'Alice', address: '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY', walletId: '1' },
});
await allSettled(flowModel.events.signerSelected, { scope, params: signerWallet.accounts[0] });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ describe('widgets/CreateWallet/model/form-model', () => {

await allSettled(signatoryModel.events.changeSignatory, {
scope,
params: { index: 0, name: 'test', address: toAddress(signerWallet.accounts[0].accountId) },
params: { index: 0, name: 'test', address: toAddress(signerWallet.accounts[0].accountId), walletId: '1' },
});
await allSettled(signatoryModel.events.changeSignatory, {
scope,
params: { index: 1, name: 'Alice', address: toAddress(signatoryWallet.accounts[0].accountId) },
params: { index: 1, name: 'Alice', address: toAddress(signatoryWallet.accounts[0].accountId), walletId: '1' },
});

await allSettled(formModel.$createMultisigForm.fields.threshold.onChange, { scope, params: 2 });
Expand Down Expand Up @@ -112,11 +112,11 @@ describe('widgets/CreateWallet/model/form-model', () => {
await allSettled(formModel.$createMultisigForm.fields.chain.onChange, { scope, params: testChain });
await allSettled(signatoryModel.events.changeSignatory, {
scope,
params: { index: 0, name: 'test', address: toAddress(signerWallet.accounts[0].accountId) },
params: { index: 0, name: 'test', address: toAddress(signerWallet.accounts[0].accountId), walletId: '1' },
});
await allSettled(signatoryModel.events.changeSignatory, {
scope,
params: { index: 1, name: 'Alice', address: toAddress(signatoryWallet.accounts[0].accountId) },
params: { index: 1, name: 'Alice', address: toAddress(signatoryWallet.accounts[0].accountId), walletId: '1' },
});
await allSettled(formModel.$createMultisigForm.fields.threshold.onChange, { scope, params: 2 });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ describe('widgets/CreateWallet/model/signatory-model', () => {

await allSettled(signatoryModel.events.addSignatory, {
scope,
params: { name: 'Alice', address: toAddress(signerWallet.accounts[0].accountId) },
params: { name: 'Alice', address: toAddress(signerWallet.accounts[0].accountId), walletId: '1' },
});

await allSettled(signatoryModel.events.addSignatory, {
scope,
params: { name: 'test', address: toAddress(signerWallet.accounts[0].accountId) },
params: { name: 'test', address: toAddress(signerWallet.accounts[0].accountId), walletId: '1' },
});

expect(scope.getState(signatoryModel.$signatories).length).toEqual(2);
Expand All @@ -40,7 +40,7 @@ describe('widgets/CreateWallet/model/signatory-model', () => {

await allSettled(signatoryModel.events.changeSignatory, {
scope,
params: { index: 0, name: 'test', address: toAddress(signerWallet.accounts[0].accountId) },
params: { index: 0, name: 'test', address: toAddress(signerWallet.accounts[0].accountId), walletId: '1' },
});

expect(scope.getState(signatoryModel.$signatories).length).toEqual(1);
Expand All @@ -60,14 +60,14 @@ describe('widgets/CreateWallet/model/signatory-model', () => {

await allSettled(signatoryModel.events.changeSignatory, {
scope,
params: { index: 1, name: 'Alice', address: toAddress(signatoryWallet.accounts[0].accountId) },
params: { index: 1, name: 'Alice', address: toAddress(signatoryWallet.accounts[0].accountId), walletId: '1' },
});

expect(scope.getState(signatoryModel.$ownedSignatoriesWallets)?.length).toEqual(0);

await allSettled(signatoryModel.events.changeSignatory, {
scope,
params: { index: 0, name: 'test', address: toAddress(signerWallet.accounts[0].accountId) },
params: { index: 0, name: 'test', address: toAddress(signerWallet.accounts[0].accountId), walletId: '1' },
});
expect(scope.getState(signatoryModel.$ownedSignatoriesWallets)?.length).toEqual(1);
});
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/widgets/CreateWallet/model/flow-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const $signer = restore<Account | null>(signerSelected, null).reset(flowFinished
const $signerWallet = combine({ signer: $signer, wallets: walletModel.$wallets }, ({ signer, wallets }) => {
const res = walletUtils.getWalletFilteredAccounts(wallets, {
accountFn: (a) => a.accountId === signer?.accountId,
walletFn: (w) => walletUtils.isValidSignatory(w),
walletFn: (w) => walletUtils.isValidSignatory(w) && w.id === signer?.walletId,
});

return res;
Expand Down
12 changes: 6 additions & 6 deletions src/renderer/widgets/CreateWallet/model/signatory-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const changeSignatory = createEvent<SignatoryInfo>();
const deleteSignatory = createEvent<number>();
const getSignatoriesBalance = createEvent<Wallet[]>();

const $signatories = createStore<Omit<SignatoryInfo, 'index'>[]>([{ name: '', address: '' }]);
const $signatories = createStore<Omit<SignatoryInfo, 'index'>[]>([{ name: '', address: '', walletId: '' }]);
const $hasDuplicateSignatories = combine($signatories, (signatories) => {
const existingKeys: Set<Address> = new Set();

Expand Down Expand Up @@ -58,9 +58,9 @@ sample({
sample({
clock: addSignatory,
source: $signatories,
fn: (signatories, { name, address }) => {
fn: (signatories, { name, address, walletId }) => {
return produce(signatories, (draft) => {
draft.push({ name, address });
draft.push({ name, address, walletId });
});
},
target: $signatories,
Expand All @@ -69,12 +69,12 @@ sample({
sample({
clock: changeSignatory,
source: $signatories,
fn: (signatories, { index, name, address }) => {
fn: (signatories, { index, name, address, walletId }) => {
return produce(signatories, (draft) => {
if (index >= draft.length) {
draft.push({ name, address });
draft.push({ name, address, walletId });
} else {
draft[index] = { name, address };
draft[index] = { name, address, walletId };
}
});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const SelectSignatories = () => {
const signatories = useUnit(signatoryModel.$signatories);

const onAddSignatoryClick = () => {
signatoryModel.events.addSignatory({ name: '', address: '' });
signatoryModel.events.addSignatory({ name: '', address: '', walletId: '' });
};

return (
Expand All @@ -25,6 +25,7 @@ export const SelectSignatories = () => {
isOwnAccount={index === 0}
signatoryName={value.name}
signatoryAddress={value.address}
selectedWallet={value.walletId}
onDelete={signatoryModel.events.deleteSignatory}
/>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { performSearch, toAccountId, toAddress, validateAddress } from '@/shared
import { CaptionText, Combobox, IconButton, Identicon, Input } from '@/shared/ui';
import { type ComboboxOption } from '@/shared/ui/types';
import { contactModel } from '@/entities/contact';
import { AddressWithName, WalletIcon, walletModel, walletUtils } from '@/entities/wallet';
import { AddressWithName, WalletIcon, accountUtils, walletModel, walletUtils } from '@/entities/wallet';
import { filterModel } from '@/features/contacts';
import { walletSelectUtils } from '@/features/wallets/WalletSelect/lib/wallet-select-utils';
import { GroupLabels } from '@/features/wallets/WalletSelect/ui/WalletGroup';
Expand All @@ -19,6 +19,7 @@ interface Props {
signatoryName: string;
signatoryAddress: string;
signatoryIndex: number;
selectedWallet: string;
isOwnAccount?: boolean;
onDelete?: (index: number) => void;
}
Expand All @@ -29,6 +30,7 @@ export const Signatory = ({
isOwnAccount = false,
signatoryName,
signatoryAddress,
selectedWallet,
}: Props) => {
const { t } = useI18n();
const [query, setQuery] = useState('');
Expand All @@ -54,12 +56,17 @@ export const Signatory = ({

const ownAccountName =
walletUtils.getWalletsFilteredAccounts(wallets, {
walletFn: (w) => !walletUtils.isWatchOnly(w) && !walletUtils.isMultisig(w),
accountFn: (a) => toAccountId(signatoryAddress) === a.accountId,
walletFn: (w) =>
!walletUtils.isWatchOnly(w) &&
!walletUtils.isMultisig(w) &&
(!selectedWallet || w.id.toString() === selectedWallet),
accountFn: (a) =>
toAccountId(signatoryAddress) === a.accountId && accountUtils.isChainIdMatch(a, chain.value.chainId),
})?.[0]?.name || '';

const contactAccountName =
contacts.filter((contact) => toAccountId(contact.address) === toAccountId(signatoryAddress))?.[0]?.name || '';

const displayName = useMemo(() => {
const hasDuplicateName = !!ownAccountName && !!contactAccountName;
const shouldForceOwnAccountName = hasDuplicateName && isOwnAccount;
Expand Down Expand Up @@ -95,7 +102,7 @@ export const Signatory = ({
return {
value: address,
element: <AddressWithName name={account.name} address={address} />,
id: account.accountId,
id: account.walletId.toString(),
};
}),
);
Expand Down Expand Up @@ -153,6 +160,7 @@ export const Signatory = ({
index: signatoryIndex,
name: newName,
address: signatoryAddress,
walletId: selectedWallet,
});
};

Expand All @@ -162,12 +170,13 @@ export const Signatory = ({
}
}, [displayName]);

const onAddressChange = (newAddress: string) => {
const validatedAddress = validateAddress(newAddress) ? newAddress : '';
const onAddressChange = (data: ComboboxOption) => {
const validatedAddress = validateAddress(data.value) ? data.value : '';
const fixedAddress = toAddress(validatedAddress, { prefix: chain.value.addressPrefix });

signatoryModel.events.changeSignatory({
index: signatoryIndex,
walletId: data.id,
name: signatoryName,
address: fixedAddress,
});
Expand Down Expand Up @@ -210,8 +219,8 @@ export const Signatory = ({
query={query}
value={toAddress(signatoryAddress, { prefix: chain.value.addressPrefix })}
prefixElement={prefixElement}
onChange={({ value }) => {
onAddressChange(value);
onChange={(data) => {
onAddressChange(data);
}}
onInput={handleQueryChange}
/>
Expand Down

0 comments on commit fadf2ca

Please sign in to comment.