Skip to content

Commit

Permalink
refactor: move getInternalAccounts from selectors.js to `accounts…
Browse files Browse the repository at this point in the history
….ts` (#27645)

<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.


## **Description**

<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?


[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/27645?quickstart=1)

## **Related issues**

Fixes:

## **Manual testing steps**

1. Go to this page...
2.
3.

## **Screenshots/Recordings**


### **Before**


### **After**


## **Pre-merge author checklist**

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
 -->
  • Loading branch information
davidmurdoch authored Nov 4, 2024
1 parent 7a8da64 commit 77b77a8
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ const InteractiveReplacementTokenPage: React.FC = () => {

const filteredAccounts = custodianAccounts.filter(
(account: TokenAccount) =>
// @ts-expect-error metaMaskAccounts isn't a real type
metaMaskAccounts[account.address.toLowerCase()],
);

Expand All @@ -163,6 +164,7 @@ const InteractiveReplacementTokenPage: React.FC = () => {
name: account.name,
labels: account.labels,
balance:
// @ts-expect-error metaMaskAccounts isn't a real type
metaMaskAccounts[account.address.toLowerCase()]?.balance || 0,
}),
);
Expand Down
2 changes: 1 addition & 1 deletion ui/pages/notifications-settings/notifications-settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default function NotificationsSettings() {
const isUpdatingMetamaskNotifications = useSelector(
getIsUpdatingMetamaskNotifications,
);
const accounts: AccountType[] = useSelector(getInternalAccounts);
const accounts = useSelector(getInternalAccounts) as AccountType[];

// States
const [loadingAllowNotifications, setLoadingAllowNotifications] =
Expand Down
9 changes: 9 additions & 0 deletions ui/selectors/accounts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
hasCreatedBtcMainnetAccount,
hasCreatedBtcTestnetAccount,
getSelectedInternalAccount,
getInternalAccounts,
} from './accounts';

const MOCK_STATE: AccountsState = {
Expand All @@ -27,6 +28,14 @@ const MOCK_STATE: AccountsState = {
};

describe('Accounts Selectors', () => {
describe('#getInternalAccounts', () => {
it('returns a list of internal accounts', () => {
expect(getInternalAccounts(mockState as AccountsState)).toStrictEqual(
Object.values(mockState.metamask.internalAccounts.accounts),
);
});
});

describe('#getSelectedInternalAccount', () => {
it('returns selected internalAccount', () => {
expect(
Expand Down
5 changes: 4 additions & 1 deletion ui/selectors/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
isBtcMainnetAddress,
isBtcTestnetAddress,
} from '../../shared/lib/multichain';
import { getInternalAccounts } from './selectors';

export type AccountsState = {
metamask: AccountsControllerState;
Expand All @@ -20,6 +19,10 @@ function isBtcAccount(account: InternalAccount) {
return Boolean(account && account.type === P2wpkh);
}

export function getInternalAccounts(state: AccountsState) {
return Object.values(state.metamask.internalAccounts.accounts);
}

export function getSelectedInternalAccount(state: AccountsState) {
const accountId = state.metamask.internalAccounts.selectedAccount;
return state.metamask.internalAccounts.accounts[accountId];
Expand Down
6 changes: 1 addition & 5 deletions ui/selectors/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ import {
getOrderedConnectedAccountsForConnectedDapp,
getSubjectMetadata,
} from './permissions';
import { getSelectedInternalAccount } from './accounts';
import { getSelectedInternalAccount, getInternalAccounts } from './accounts';
import { createDeepEqualSelector } from './util';
import { getMultichainBalances, getMultichainNetwork } from './multichain';

Expand Down Expand Up @@ -371,10 +371,6 @@ export function getSelectedInternalAccountWithBalance(state) {
return selectedAccountWithBalance;
}

export function getInternalAccounts(state) {
return Object.values(state.metamask.internalAccounts.accounts);
}

export function getInternalAccount(state, accountId) {
return state.metamask.internalAccounts.accounts[accountId];
}
Expand Down
8 changes: 0 additions & 8 deletions ui/selectors/selectors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,6 @@ describe('Selectors', () => {
});
});

describe('#getInternalAccounts', () => {
it('returns a list of internal accounts', () => {
expect(selectors.getInternalAccounts(mockState)).toStrictEqual(
Object.values(mockState.metamask.internalAccounts.accounts),
);
});
});

describe('#getInternalAccount', () => {
it("returns undefined if the account doesn't exist", () => {
expect(
Expand Down
3 changes: 2 additions & 1 deletion ui/selectors/snaps/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createSelector } from 'reselect';
import { AccountsControllerState } from '@metamask/accounts-controller';
import { getAccountName, getInternalAccounts } from '../selectors';
import { getAccountName } from '../selectors';
import { getInternalAccounts } from '../accounts';
import { createDeepEqualSelector } from '../util';

/**
Expand Down

0 comments on commit 77b77a8

Please sign in to comment.