-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enforce primary account always at the top of account selections
- Loading branch information
1 parent
40b441f
commit a99cb24
Showing
7 changed files
with
65 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './sortAccountsByPolisAccount'; |
26 changes: 26 additions & 0 deletions
26
src/extension/utils/sortAccountsByPolisAccount/sortAccountsByPolisAccount.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
9 changes: 9 additions & 0 deletions
9
src/extension/utils/sortAccountsByPolisAccount/types/IOptions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type { default as IOptions } from './IOptions'; |