Skip to content

Commit

Permalink
tweak: autoselect proper keys on adding more keys for MultiSig / Vesting
Browse files Browse the repository at this point in the history
  • Loading branch information
brusherru committed Oct 9, 2024
1 parent 5cd6294 commit a1b85ca
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/components/CreateAccountModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ function CreateAccountModal({
() => findUnusedKey(keys, usedPublicKeys),
[keys, usedPublicKeys]
);
const unusedKeys = useMemo(
() => keys.filter((key) => !usedPublicKeys.has(key.publicKey)),
[keys, usedPublicKeys]
);

const isKeyUsed = (() => {
if (selectedTemplate === StdPublicKeys.SingleSig) {
Expand Down Expand Up @@ -300,6 +304,7 @@ function CreateAccountModal({
errors={errors}
isSubmitted={isSubmitted}
hasCreateOption
autoSelectKeys={unusedKeys}
/>
</>
);
Expand Down Expand Up @@ -343,6 +348,7 @@ function CreateAccountModal({
isSubmitted={isSubmitted}
values={multiKeyValues}
hasCreateOption
autoSelectKeys={unusedKeys}
/>
</>
);
Expand Down
21 changes: 15 additions & 6 deletions src/components/FormMultiKeySelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ type Props<T extends FieldValues, FieldName extends ArrayPath<T>> = {
isSubmitted?: boolean;
values?: string[] | null;
hasCreateOption?: boolean;
autoSelectKeys?: null | SafeKey[];
};

function FormMultiKeySelect<
T extends FieldValues,
FieldName extends ArrayPath<T>
Expand All @@ -48,6 +48,7 @@ function FormMultiKeySelect<
isSubmitted = false,
values = null,
hasCreateOption = false,
autoSelectKeys = null,
}: Props<T, FieldName>): JSX.Element {
const { fields, append, remove } = useFieldArray({
control,
Expand All @@ -56,13 +57,12 @@ function FormMultiKeySelect<
const addEmptyField = useCallback(
() =>
append(
(keys[fields.length]?.publicKey ||
`0x${String(fields.length).padStart(2, '0')}`) as FieldArray<
`0x${String(fields.length).padStart(2, '0')}` as FieldArray<
T,
FieldName
>
),
[append, fields.length, keys]
[append, fields.length]
);

useEffect(() => {
Expand All @@ -76,8 +76,17 @@ function FormMultiKeySelect<
}, [values, append, remove, keys]);

const getSelectValue = useCallback(
(index: number) => values?.[index] || keys[index]?.publicKey,
[keys, values]
(index: number) => {
const nextRecommendedKey = autoSelectKeys
? autoSelectKeys[index]?.publicKey
: keys[index]?.publicKey;
const defaultKey = hasCreateOption
? CREATE_NEW_KEY_LITERAL
: `0x${String(index).padStart(2, '0')}`;
const nextKey = nextRecommendedKey ?? defaultKey;
return values?.[index] || nextKey;
},
[autoSelectKeys, hasCreateOption, keys, values]
);

const rootError = errors[fieldName]?.message;
Expand Down

0 comments on commit a1b85ca

Please sign in to comment.