feat: add redirect logic on landing page#1323
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
WalkthroughRefactors exchange-rate CTA navigation: replaces inline multi-branch logic in two components with a shared utility getExchangeRateWidgetRedirectRoute, integrates wallet balance formatting and auth checks in LandingPage NoFees component, and adds a new routing utility that computes redirect routes based on currency pairs and balance. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Pre-merge checks and finishing touches❌ Failed checks (2 warnings)
✅ Passed checks (1 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
♻️ Duplicate comments (1)
src/components/LandingPage/noFees.tsx (1)
27-27: Inefficient balance conversion chain.Same issue as in the profile page: converting
bigint → string → numberviaparseFloat(printableUsdc(...))is inefficient.Apply the same refactor suggested for
src/app/(mobile-ui)/profile/exchange-rate/page.tsxat line 17: create a dedicated conversion utility to convert bigint directly to number without the intermediate string formatting.
🧹 Nitpick comments (4)
src/utils/exchangeRateWidget.utils.ts (2)
11-41: Refactor to use if-else-if chain and remove redundant assignments.The current implementation has several issues:
- All conditions are evaluated sequentially even though they're mutually exclusive
- Cases 1 and 3 redundantly reassign
route = '/add-money'when it's already the default- The logic would be clearer and more efficient with an if-else-if chain
Apply this diff to improve the control flow:
// Case 1: source currency is not usd and destination currency is usd -> redirect to add-money/sourceCurrencyCountry page if (sourceCurrency !== 'USD' && destinationCurrency === 'USD') { countryPath = countryCurrencyMappings.find((currency) => currency.currencyCode === sourceCurrency)?.path - route = '/add-money' - } - - // Case 2: source currency is usd and destination currency is not usd -> redirect to withdraw/destinationCurrencyCountry page - if (sourceCurrency === 'USD' && destinationCurrency !== 'USD') { + } else if (sourceCurrency === 'USD' && destinationCurrency !== 'USD') { + // Case 2: source currency is usd and destination currency is not usd -> redirect to withdraw/destinationCurrencyCountry page // if there is no balance, redirect to add-money if (userBalance <= 0) { countryPath = countryCurrencyMappings.find((currency) => currency.currencyCode === sourceCurrency)?.path - route = '/add-money' } else { countryPath = countryCurrencyMappings.find( (currency) => currency.currencyCode === destinationCurrency )?.path route = '/withdraw' } - } - - // Case 3: source currency is not usd and destination currency is not usd -> redirect to add-money/sourceCurrencyCountry page - if (sourceCurrency !== 'USD' && destinationCurrency !== 'USD') { + } else if (sourceCurrency !== 'USD' && destinationCurrency !== 'USD') { + // Case 3: source currency is not usd and destination currency is not usd -> redirect to add-money/sourceCurrencyCountry page countryPath = countryCurrencyMappings.find((currency) => currency.currencyCode === sourceCurrency)?.path - route = '/add-money' - } - - // Case 4: source currency is usd and destination currency is usd - if (sourceCurrency === 'USD' && destinationCurrency === 'USD') { + } else { + // Case 4: source currency is usd and destination currency is usd countryPath = countryCurrencyMappings.find((currency) => currency.currencyCode === 'USD')?.path route = userBalance <= 0 ? '/add-money' : '/withdraw' }
43-49: Simplify route construction and fix falsy check.The current code has unnecessary intermediate variables and doesn't properly handle the empty string case for
countryPath.Apply this diff to simplify:
- if (!countryPath) { - const redirectRoute = `${route}?currencyCode=EUR` - return redirectRoute - } else { - const redirectRoute = `${route}/${countryPath}` - return redirectRoute - } + return countryPath ? `${route}/${countryPath}` : `${route}?currencyCode=EUR`src/app/(mobile-ui)/profile/exchange-rate/page.tsx (1)
17-17: Inefficient balance conversion chain.Converting
bigint → string → numberviaparseFloat(printableUsdc(...))is inefficient and can introduce precision issues. TheprintableUsdcfunction formats the number to 2 decimal places as a string, thenparseFloatconverts it back to a number.Consider creating a dedicated conversion utility or directly converting bigint to number:
const formattedBalance = Number(formatUnits(balance ?? 0n, PEANUT_WALLET_TOKEN_DECIMALS))Or add a new utility in
balance.utils.ts:export const balanceToNumber = (balance: bigint): number => { return Number(formatUnits(balance, PEANUT_WALLET_TOKEN_DECIMALS)) }Then use:
const formattedBalance = balanceToNumber(balance ?? 0n)src/components/LandingPage/noFees.tsx (1)
43-47: Add fetchBalance to the dependency array.The
fetchBalancefunction should be included in the dependency array to satisfy React's exhaustive-deps rule, even though it's likely stable from the hook.Apply this diff:
useEffect(() => { if (user) { fetchBalance() } - }, [user]) + }, [user, fetchBalance])
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/app/(mobile-ui)/profile/exchange-rate/page.tsx(2 hunks)src/components/LandingPage/noFees.tsx(3 hunks)src/utils/exchangeRateWidget.utils.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (3)
src/app/(mobile-ui)/profile/exchange-rate/page.tsx (2)
src/utils/balance.utils.ts (1)
printableUsdc(32-38)src/utils/exchangeRateWidget.utils.ts (1)
getExchangeRateWidgetRedirectRoute(3-50)
src/components/LandingPage/noFees.tsx (4)
src/hooks/wallet/useWallet.ts (1)
useWallet(14-98)src/context/authContext.tsx (1)
useAuth(191-197)src/utils/balance.utils.ts (1)
printableUsdc(32-38)src/utils/exchangeRateWidget.utils.ts (1)
getExchangeRateWidgetRedirectRoute(3-50)
src/utils/exchangeRateWidget.utils.ts (1)
src/constants/countryCurrencyMapping.ts (1)
countryCurrencyMappings(10-48)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Deploy-Preview
🔇 Additional comments (1)
src/components/LandingPage/noFees.tsx (1)
22-31: LGTM! Auth check prevents unauthorized access.The authentication check before routing is a good practice, ensuring unauthenticated users are redirected to setup.
No description provided.