Skip to content

Commit

Permalink
feat: enforce primary account always at the top of account selections
Browse files Browse the repository at this point in the history
  • Loading branch information
kieranroneill committed Sep 22, 2024
1 parent 40b441f commit a99cb24
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/extension/components/AccountSelect/AccountSelectModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import type { TAccountSelectModalProps } from './types';
import calculateIconSize from '@extension/utils/calculateIconSize';
import convertPublicKeyToAVMAddress from '@extension/utils/convertPublicKeyToAVMAddress';
import ellipseAddress from '@extension/utils/ellipseAddress';
import sortAccountsByPolisAccount from '@extension/utils/sortAccountsByPolisAccount';
import upsertItemsById from '@extension/utils/upsertItemsById';

const AccountSelectModal: FC<TAccountSelectModalProps> = ({
Expand Down Expand Up @@ -138,7 +139,14 @@ const AccountSelectModal: FC<TAccountSelectModalProps> = ({
);
}

return accounts.map((account, index) => {
return (
systemInfo?.polisAccountID
? sortAccountsByPolisAccount({
accounts,
polisAccountID: systemInfo.polisAccountID,
})
: accounts
).map((account, index) => {
const address = convertPublicKeyToAVMAddress(account.publicKey);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
useSelectAccounts,
useSelectEvents,
useSelectNetworks,
useSelectSystemInfo,
} from '@extension/selectors';

// types
Expand All @@ -25,12 +26,14 @@ import type { IUseEnableModalState } from './types';
// utils
import availableAccountsForNetwork from '@extension/utils/availableAccountsForNetwork';
import selectDefaultNetwork from '@extension/utils/selectDefaultNetwork';
import sortAccountsByPolisAccount from '@extension/utils/sortAccountsByPolisAccount';

export default function useEnableModal(): IUseEnableModalState {
// selectors
const accounts = useSelectAccounts();
const events = useSelectEvents();
const networks = useSelectNetworks();
const systemInfo = useSelectSystemInfo();
// state
const [availableAccounts, setAvailableAccounts] = useState<
IAccountWithExtendedProps[] | null
Expand All @@ -56,7 +59,12 @@ export default function useEnableModal(): IUseEnableModalState {
if (event && network) {
setAvailableAccounts(
availableAccountsForNetwork({
accounts,
accounts: systemInfo?.polisAccountID
? sortAccountsByPolisAccount({
accounts,
polisAccountID: systemInfo?.polisAccountID,
})
: accounts,
network,
})
);
Expand Down
13 changes: 10 additions & 3 deletions src/extension/modals/ManageSessionModal/ManageSessionModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import type { IProps } from './types';
import availableAccountsForNetwork from '@extension/utils/availableAccountsForNetwork';
import convertPublicKeyToAVMAddress from '@extension/utils/convertPublicKeyToAVMAddress';
import ellipseAddress from '@extension/utils/ellipseAddress';
import sortAccountsByPolisAccount from '@extension/utils/sortAccountsByPolisAccount';

const ManageSessionModal: FC<IProps> = ({ onClose, session }) => {
const _context = 'manage-sessions-modal';
Expand Down Expand Up @@ -160,9 +161,15 @@ const ManageSessionModal: FC<IProps> = ({ onClose, session }) => {
));
}

accountNodes = availableAccountsForNetwork({ accounts, network }).reduce<
ReactNode[]
>((acc, account, currentIndex, availableAccounts) => {
accountNodes = availableAccountsForNetwork({
accounts: systemInfo?.polisAccountID
? sortAccountsByPolisAccount({
accounts,
polisAccountID: systemInfo.polisAccountID,
})
: accounts,
network,
}).reduce<ReactNode[]>((acc, account, currentIndex, availableAccounts) => {
const address = convertPublicKeyToAVMAddress(account.publicKey);

return [
Expand Down
1 change: 1 addition & 0 deletions src/extension/utils/sortAccountsByPolisAccount/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './sortAccountsByPolisAccount';
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// types
import type { IAccount } from '@extension/types';
import type { IOptions } from './types';

// utils
import serialize from '@extension/utils/serialize';

/**
* Convenience function that "sorts" a list of accounts by moving the polis account to the front. This uses a splice
* rather than a sort for efficiency reasons. If no polis account is found, the accounts remain unchanged.
* @param {IOptions<Type extends IAccount>} options - a list of accounts and the polis account ID.
* @returns {Type extends IAccount} the list of accounts with the polis account at the front.
*/
export default function sortAccountsByPolisAccount<Type extends IAccount>({
accounts,
polisAccountID,
}: IOptions<Type>): Type[] {
const _accounts = serialize(accounts);
const index = accounts.findIndex(({ id }) => id === polisAccountID);

if (index < 0) {
return _accounts;
}

return [..._accounts.splice(index, 1), ..._accounts];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// types
import type { IAccount } from '@extension/types';

interface IOptions<Type extends IAccount> {
accounts: Type[];
polisAccountID: string;
}

export default IOptions;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { default as IOptions } from './IOptions';

0 comments on commit a99cb24

Please sign in to comment.