Skip to content

Commit a679e8e

Browse files
fix: cp-7.60.0 show all blocking alerts if keyboard not visible (#22837)
## **Description** Ensure all blocking alerts are visible in MetaMask Pay if keyboard is not visible. ## **Changelog** CHANGELOG entry: null ## **Related issues** Fixes: #22756 ## **Manual testing steps** ## **Screenshots/Recordings** ### **Before** ### **After** ## **Pre-merge author checklist** - [x] I’ve followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.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. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Refactors alert filtering to surface blocking and pending alerts appropriately (respecting keyboard visibility and input-change rules) and updates tests accordingly. > > - **Hooks** (`useTransactionCustomAmountAlerts`): > - Filter confirmation alerts to only blocking, then exclude alerts based on: > - No input change for `ON_CHANGE_ALERTS`. > - Keyboard visible for non-`KEYBOARD_ALERTS`. > - Keyboard visible for `PENDING_AMOUNT_ALERTS`. > - Combine `pendingTokenAlerts` with filtered confirmation alerts and select the first alert for display. > - **Tests** (`useTransactionCustomAmountAlerts.test.ts`): > - Add test ensuring pending alert is returned when applicable. > - Add test ensuring pending alerts are suppressed when keyboard is visible. > - Adjust test names to clarify keyboard and on-change behaviors. > - Verify non-blocking alerts are ignored. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 5bb7b96. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 80584b2 commit a679e8e

File tree

2 files changed

+57
-24
lines changed

2 files changed

+57
-24
lines changed

app/components/Views/confirmations/hooks/transactions/useTransactionCustomAmountAlerts.test.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,21 @@ describe('useTransactionCustomAmountAlerts', () => {
116116
expect(result.current.alertMessage).toBeUndefined();
117117
});
118118

119-
it('does not return alert message if on change alert and input not changed', () => {
119+
it('returns pending alert', () => {
120+
const PENDING_ALERT_MOCK = {
121+
...ALERT_MOCK,
122+
key: AlertKeys.SignedOrSubmitted,
123+
};
124+
125+
usePendingAmountAlertsMock.mockReturnValue([PENDING_ALERT_MOCK]);
126+
127+
const { result } = runHook();
128+
129+
expect(result.current.alertTitle).toBe(PENDING_ALERT_MOCK.title);
130+
expect(result.current.alertMessage).toBe(PENDING_ALERT_MOCK.message);
131+
});
132+
133+
it('does not return alert if on change alert and input not changed', () => {
120134
useAlertsMock.mockReturnValue({
121135
alerts: [
122136
{
@@ -131,7 +145,7 @@ describe('useTransactionCustomAmountAlerts', () => {
131145
expect(result.current.alertMessage).toBeUndefined();
132146
});
133147

134-
it('does not return non-keyboard alert if keyboard visible', () => {
148+
it('does not return alert if keyboard visible and not keyboard alert', () => {
135149
useAlertsMock.mockReturnValue({
136150
alerts: [
137151
{
@@ -160,4 +174,22 @@ describe('useTransactionCustomAmountAlerts', () => {
160174

161175
expect(result.current.alertMessage).toBeUndefined();
162176
});
177+
178+
it('does not return alert if keyboard visible and pending alert', () => {
179+
useAlertsMock.mockReturnValue({
180+
alerts: [
181+
{
182+
...ALERT_MOCK,
183+
key: AlertKeys.InsufficientPayTokenBalance,
184+
},
185+
],
186+
} as AlertsContextParams);
187+
188+
const { result } = runHook({
189+
isInputChanged: true,
190+
isKeyboardVisible: true,
191+
});
192+
193+
expect(result.current.alertMessage).toBeUndefined();
194+
});
163195
});

app/components/Views/confirmations/hooks/transactions/useTransactionCustomAmountAlerts.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,33 @@ export function useTransactionCustomAmountAlerts({
3838
const { alerts: confirmationAlerts } = useAlerts();
3939
const pendingTokenAlerts = usePendingAmountAlerts({ pendingTokenAmount });
4040

41-
const uniqueAlerts = useMemo(
42-
() =>
43-
confirmationAlerts.filter(
44-
(a_) => !PENDING_AMOUNT_ALERTS.includes(a_.key as AlertKeys),
45-
),
46-
[confirmationAlerts],
47-
);
41+
const filteredAlerts = useMemo(() => {
42+
const blockingAlerts = confirmationAlerts.filter((a) => a.isBlocking);
4843

49-
const allAlerts = useMemo(
50-
() => [...pendingTokenAlerts, ...uniqueAlerts],
51-
[uniqueAlerts, pendingTokenAlerts],
52-
);
44+
return blockingAlerts.filter((a) => {
45+
const isIgnoredAsNoInput =
46+
!isInputChanged && ON_CHANGE_ALERTS.includes(a.key as AlertKeys);
47+
48+
const isIgnoredAsKeyboardVisible =
49+
isKeyboardVisible && !KEYBOARD_ALERTS.includes(a.key as AlertKeys);
50+
51+
const isIgnoredAsPending =
52+
isKeyboardVisible && PENDING_AMOUNT_ALERTS.includes(a.key as AlertKeys);
53+
54+
return (
55+
!isIgnoredAsNoInput &&
56+
!isIgnoredAsKeyboardVisible &&
57+
!isIgnoredAsPending
58+
);
59+
});
60+
}, [confirmationAlerts, isInputChanged, isKeyboardVisible]);
5361

54-
const filteredAlerts = useMemo(
55-
() =>
56-
allAlerts.filter(
57-
(a) =>
58-
a.isBlocking &&
59-
(!isKeyboardVisible ||
60-
KEYBOARD_ALERTS.includes(a.key as AlertKeys)) &&
61-
(isInputChanged || !ON_CHANGE_ALERTS.includes(a.key as AlertKeys)),
62-
),
63-
[allAlerts, isInputChanged, isKeyboardVisible],
62+
const alerts = useMemo(
63+
() => [...pendingTokenAlerts, ...filteredAlerts],
64+
[filteredAlerts, pendingTokenAlerts],
6465
);
6566

66-
const firstAlert = filteredAlerts?.[0];
67+
const firstAlert = alerts?.[0];
6768

6869
if (!firstAlert) {
6970
return {};

0 commit comments

Comments
 (0)