Skip to content

Commit d189f4d

Browse files
fix(predict): prevent pay token alert flicker
1 parent e249678 commit d189f4d

4 files changed

Lines changed: 52 additions & 5 deletions

File tree

app/components/UI/Predict/views/PredictBuyWithAnyToken/PredictBuyWithAnyToken.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ const PredictBuyWithAnyToken = (props: PredictBuyPreviewProps) => {
257257
isInsufficientBalance,
258258
isConfirming,
259259
isPayFeesLoading,
260+
isPaySystemSettling,
260261
blockingPayAlertMessage,
261262
outcomeTokenPrice: outcomeToken?.price,
262263
isSheetMode,

app/components/UI/Predict/views/PredictBuyWithAnyToken/hooks/usePredictBuyConditions.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,13 @@ export const usePredictBuyConditions = ({
150150
return null;
151151
}
152152

153-
return `${selectedPaymentTokenKey}:${totalPayForPredictBalance}`;
154-
}, [selectedPaymentTokenKey, totalPayForPredictBalance]);
153+
const amountValue = totalPayForPredictBalance || currentValue;
154+
const roundedQuoteAmount = new BigNumber(amountValue)
155+
.decimalPlaces(2, BigNumber.ROUND_UP)
156+
.toString(10);
157+
158+
return `${selectedPaymentTokenKey}:${roundedQuoteAmount}`;
159+
}, [currentValue, selectedPaymentTokenKey, totalPayForPredictBalance]);
155160

156161
// Tracks the token/amount pair for which a full quote-loading cycle has
157162
// completed. Keeping the amount in the key makes same-token amount changes

app/components/UI/Predict/views/PredictBuyWithAnyToken/hooks/usePredictBuyError.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ const defaultParams = {
106106
isBelowMinimum: false,
107107
isInsufficientBalance: false,
108108
isPayFeesLoading: false,
109+
isPaySystemSettling: false,
109110
blockingPayAlertMessage: null as string | null,
110111
// Inline banner UX is sheet-mode-only; default these tests to sheet mode so
111112
// banner / errorMessage suppression behavior is exercised. The dedicated
@@ -236,6 +237,28 @@ describe('usePredictBuyError', () => {
236237
expect(result.current.errorMessageSource).toBe('blocking_pay_alert');
237238
});
238239

240+
it('suppresses pay token balance alert message while pay system is settling', () => {
241+
mockActiveOrder = { error: 'order failed' };
242+
mockIsPredictBalanceSelected = false;
243+
mockGetPlaceOrderErrorOutcome.mockReturnValue({
244+
status: 'error',
245+
error: 'Order placement failed',
246+
});
247+
248+
const { result } = renderHook(() =>
249+
usePredictBuyError({
250+
...defaultParams,
251+
isPaySystemSettling: true,
252+
blockingPayAlertMessage: 'Insufficient payment token balance',
253+
}),
254+
);
255+
256+
expect(result.current.errorMessage).toBeUndefined();
257+
expect(result.current.buyErrorBanner).toEqual(
258+
expect.objectContaining({ variant: 'order_failed' }),
259+
);
260+
});
261+
239262
it('returns undefined when activeOrder has no error', () => {
240263
mockActiveOrder = {};
241264

app/components/UI/Predict/views/PredictBuyWithAnyToken/hooks/usePredictBuyError.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ interface UsePredictBuyInfoParams {
4646
isBelowMinimum: boolean;
4747
isInsufficientBalance: boolean;
4848
isPayFeesLoading: boolean;
49+
isPaySystemSettling: boolean;
4950
blockingPayAlertMessage: string | null;
5051
outcomeTokenPrice?: number;
5152
// Inline banner UX (price_changed / order_failed) only exists inside the
@@ -63,6 +64,7 @@ export const usePredictBuyError = ({
6364
isBelowMinimum,
6465
isInsufficientBalance,
6566
isPayFeesLoading,
67+
isPaySystemSettling,
6668
blockingPayAlertMessage,
6769
outcomeTokenPrice,
6870
isSheetMode = false,
@@ -96,6 +98,7 @@ export const usePredictBuyError = ({
9698
!isPlacingOrder &&
9799
!isConfirming &&
98100
!isPredictBalanceSelected &&
101+
!isPaySystemSettling &&
99102
!!blockingPayAlertMessage
100103
) {
101104
return {
@@ -141,6 +144,7 @@ export const usePredictBuyError = ({
141144
isConfirming,
142145
preview,
143146
isPayFeesLoading,
147+
isPaySystemSettling,
144148
isPredictBalanceSelected,
145149
blockingPayAlertMessage,
146150
activeOrder?.error,
@@ -194,7 +198,11 @@ export const usePredictBuyError = ({
194198
const bannerWouldSuppress =
195199
isSheetMode &&
196200
activeOrder?.error &&
197-
!(blockingPayAlertMessage && !isPredictBalanceSelected);
201+
!(
202+
blockingPayAlertMessage &&
203+
!isPredictBalanceSelected &&
204+
!isPaySystemSettling
205+
);
198206
if (bannerWouldSuppress) {
199207
return undefined;
200208
}
@@ -214,6 +222,7 @@ export const usePredictBuyError = ({
214222
activeOrder?.error,
215223
blockingPayAlertMessage,
216224
isPredictBalanceSelected,
225+
isPaySystemSettling,
217226
isSheetMode,
218227
]);
219228

@@ -233,7 +242,11 @@ export const usePredictBuyError = ({
233242
return null;
234243
}
235244

236-
if (blockingPayAlertMessage && !isPredictBalanceSelected) {
245+
if (
246+
blockingPayAlertMessage &&
247+
!isPredictBalanceSelected &&
248+
!isPaySystemSettling
249+
) {
237250
return null;
238251
}
239252

@@ -274,6 +287,7 @@ export const usePredictBuyError = ({
274287
isConfirming,
275288
blockingPayAlertMessage,
276289
isPredictBalanceSelected,
290+
isPaySystemSettling,
277291
isSheetMode,
278292
]);
279293

@@ -298,7 +312,11 @@ export const usePredictBuyError = ({
298312
!isSheetMode ||
299313
isPlacingOrder ||
300314
isConfirming ||
301-
Boolean(blockingPayAlertMessage && !isPredictBalanceSelected);
315+
Boolean(
316+
blockingPayAlertMessage &&
317+
!isPredictBalanceSelected &&
318+
!isPaySystemSettling,
319+
);
302320

303321
const buyErrorBanner =
304322
currentBuyErrorBanner ??

0 commit comments

Comments
 (0)