Skip to content

fix: token selector related issues#867

Merged
kushagrasarathe merged 4 commits intopeanut-wallet-devfrom
fix/tkn-slctr-bugs
May 16, 2025
Merged

fix: token selector related issues#867
kushagrasarathe merged 4 commits intopeanut-wallet-devfrom
fix/tkn-slctr-bugs

Conversation

@kushagrasarathe
Copy link
Copy Markdown
Contributor

  • contributes to TASK-11332
  • also fixes TASK-11255
  • do no default to peanut wallet token/chain on first load
  • check if token is supported by squid
  • add fallback for logos

@vercel
Copy link
Copy Markdown

vercel bot commented May 16, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
peanut-ui ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 16, 2025 6:48pm

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented May 16, 2025

## Walkthrough

This change refactors several components in the Token Selector feature to improve image error handling and token support indication. It introduces state variables to track image load failures, adds conditional rendering for fallback avatars, and updates default token data initialization to use an empty object instead of a specific token. The token selection and display logic is streamlined for clarity.

## Changes

| Files                                                                                      | Change Summary                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
|--------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| src/components/Global/TokenSelector/Components/NetworkButton.tsx,<br>src/components/Global/TokenSelector/Components/NetworkListItem.tsx | Refactored both components to introduce state variables for tracking image load errors. Updated rendering logic to display fallback avatars if network icon images fail to load, using `useState` and `onError` handlers.                                                                                                                                                                                                                                                                                                                                                                                  |
| src/components/Global/TokenSelector/Components/TokenListItem.tsx                            | Enhanced the component to accept a new `isSquidSupported` prop (defaulting to true) that controls interactivity and styling. Added state for tracking token and chain image load errors, with improved fallback rendering using avatars. Chevron icon rendering is now conditional based on token support or popularity.                                                                                                                     |
| src/components/Global/TokenSelector/TokenSelector.tsx                                      | Added state variables for image error tracking in token and chain logos. Simplified logic for token selection when changing chains, defaulting to clearing selection. Updated rendering to pass Squid support status to token list items and improved image fallback handling. Removed unused imports and streamlined control flow for token and chain display.                                                                                                                        |
| src/context/tokenSelector.context.tsx                                                      | Replaced the use of `peanutDefaultTokenData` with an `emptyTokenData` object for default token data initialization and context reset, ensuring no default token is selected when not using Peanut Wallet and no last used token exists.                                                                                                                                                                                                                                                                                                                           |

## Possibly related PRs

- [peanutprotocol/peanut-ui#756](https://github.com/peanutprotocol/peanut-ui/pull/756): Both PRs change the initial token data setup in `tokenSelector.context.tsx`, focusing on default token selection logic.
- [peanutprotocol/peanut-ui#395](https://github.com/peanutprotocol/peanut-ui/pull/395): Both PRs modify `TokenSelector.tsx` to improve token selection logic and state management, but address different aspects of token selection and image handling.
- [peanutprotocol/peanut-ui#825](https://github.com/peanutprotocol/peanut-ui/pull/825): Both PRs refactor the `NetworkButton` component with similar image error handling and rendering improvements.

## Suggested labels

`Being worked on`

## Suggested reviewers

- Hugo0

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 02d1459 and dec1734.

📒 Files selected for processing (1)
  • src/components/Global/TokenSelector/TokenSelector.tsx (8 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/components/Global/TokenSelector/TokenSelector.tsx
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Deploy-Preview
✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@notion-workspace
Copy link
Copy Markdown

@notion-workspace
Copy link
Copy Markdown

@coderabbitai coderabbitai bot added the Being worked on Being worked on label May 16, 2025
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (4)
src/components/Global/TokenSelector/TokenSelector.tsx (4)

187-194: Duplicate clearing logic – simplify handleChainSelectFromList

Both branches assign setSelectedTokenAddress(''); the if/else distinction is no longer needed.

if (isExternalWalletConnected) {
-  setSelectedTokenAddress('') // clear token when chain changes with external wallet
-} else {
-  setSelectedTokenAddress('') // clear selected token when changing network in popular view
+  // clear selected token whenever the chain changes
+  setSelectedTokenAddress('')
 }

Removing the redundant path makes the intent clearer and avoids future divergence.


458-470: Costly per-render look-ups in large token lists

find + some are executed for every balance rendered. When the list is large this becomes O(n²).
Caching the Set of supported addresses for each chain outside the map (e.g., in useMemo) avoids repeated scans.

Example optimisation (sketch):

const squidSupportedByChain = useMemo(() => {
  const map = new Map<string, Set<string>>()
  Object.entries(supportedSquidChainsAndTokens).forEach(([chainId, data]) =>
    map.set(chainId, new Set(data.tokens.map(t => t.address.toLowerCase()))),
  )
  return map
}, [supportedSquidChainsAndTokens])

Then inside the map:

const isTokenSupportedBySquid =
  squidSupportedByChain.get(String(balance.chainId))?.has(balance.address.toLowerCase()) ?? false

518-526: Button logo fallback misses chain badge & alt text context

  1. When the token image fails, the chain badge is dropped completely. Consider rendering the badge even when the main image is replaced by the generic currency icon or avatar.
  2. The generic currency icon lacks descriptive alt text, affecting accessibility.

599-627: AvatarWithBadge reuse: consider componentising the fallback

The default-token block repeats the same “image with badge or fallback avatar/icon” logic already seen elsewhere. Extracting this into a small <TokenLogo> component (with internal onError handling and optional badge) would:

  • Eliminate duplication
  • Automatically reset error flags when the src prop changes (state lives inside the component)
  • Keep this parent component leaner

Let me know if you’d like a quick proof-of-concept diff.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b0ca3fc and 02d1459.

📒 Files selected for processing (5)
  • src/components/Global/TokenSelector/Components/NetworkButton.tsx (2 hunks)
  • src/components/Global/TokenSelector/Components/NetworkListItem.tsx (3 hunks)
  • src/components/Global/TokenSelector/Components/TokenListItem.tsx (5 hunks)
  • src/components/Global/TokenSelector/TokenSelector.tsx (8 hunks)
  • src/context/tokenSelector.context.tsx (3 hunks)
🧰 Additional context used
🧠 Learnings (2)
src/context/tokenSelector.context.tsx (2)
Learnt from: jjramirezn
PR: peanutprotocol/peanut-ui#413
File: src/context/tokenSelector.context.tsx:118-123
Timestamp: 2024-10-08T20:13:42.967Z
Learning: In the `TokenContextProvider` component within `src/context/tokenSelector.context.tsx`, in the TypeScript React application, when data changes and before calling `fetchAndSetTokenPrice`, it is necessary to reset `selectedTokenData`, `selectedTokenPrice`, `selectedTokenDecimals`, and `inputDenomination` to discard stale data.
Learnt from: jjramirezn
PR: peanutprotocol/peanut-ui#631
File: src/components/Create/Create.tsx:108-112
Timestamp: 2025-01-16T13:14:40.363Z
Learning: In the Peanut UI codebase, the `resetTokenContextProvider` function from `tokenSelectorContext` is a stable function reference that doesn't change, so it doesn't need to be included in useEffect dependencies.
src/components/Global/TokenSelector/TokenSelector.tsx (1)
Learnt from: jjramirezn
PR: peanutprotocol/peanut-ui#551
File: src/components/Request/Create/Views/Initial.view.tsx:151-156
Timestamp: 2024-12-02T17:19:18.532Z
Learning: In the `InitialView` component at `src/components/Request/Create/Views/Initial.view.tsx`, when setting the default chain and token in the `useEffect` triggered by `isPeanutWallet`, it's acceptable to omit the setters from the dependency array and not include additional error handling for invalid defaults.
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Deploy-Preview
🔇 Additional comments (13)
src/context/tokenSelector.context.tsx (3)

64-68: Good addition of emptyTokenData for better token state initialization.

The new emptyTokenData object provides a proper empty state for tokens, which aligns with the PR objective of removing default token selection on first load.


81-82: Improved token initialization logic.

Replacing the default Peanut wallet token with an empty token state when no wallet is connected and no last used token exists creates a cleaner user experience. Users will now need to explicitly select a token rather than having one pre-selected.


113-113: Consistent application of empty token data in the reset function.

This change ensures that the same empty token data logic is applied when resetting the token context, maintaining consistency with the initialization logic.

src/components/Global/TokenSelector/Components/NetworkListItem.tsx (2)

2-2: Added state for image error handling.

Good addition of the React useState hook and the iconError state variable to track image loading failures.

Also applies to: 28-28


54-65: Implemented fallback for network icons.

The conditional rendering now properly handles image loading failures by displaying an AvatarWithBadge component when the network icon fails to load, which prevents broken images from appearing in the UI.

src/components/Global/TokenSelector/Components/NetworkButton.tsx (2)

4-4: Added state for chain image error handling.

Good implementation of the React useState hook and the chainImageError state variable to track chain icon loading failures.

Also applies to: 22-24


25-57: Implemented fallback for chain icons.

The component now properly handles chain icon loading failures by displaying an AvatarWithBadge component when the icon fails to load. This improves the user experience by providing a consistent visual representation even when network or CDN issues occur.

src/components/Global/TokenSelector/Components/TokenListItem.tsx (6)

18-18: Added support for flagging Squid-unsupported tokens.

The new isSquidSupported prop allows the component to visually indicate and disable tokens that aren't supported by Squid, addressing the PR objective of implementing Squid support verification.

Also applies to: 28-28


32-33: Added state variables for token and chain image error handling.

Good addition of separate state variables to track loading failures for both token and chain images.


51-58: Improved styling and interaction for unsupported tokens.

The component now visually indicates unsupported tokens with reduced opacity and a "not-allowed" cursor, and prevents interaction by conditionally applying the click handler based on the isSquidSupported prop.


71-85: Enhanced token image error handling.

The rendering logic for the token icon now includes proper error handling, falling back to the AvatarWithBadge component when the image fails to load.


86-100: Improved chain logo error handling.

The chain logo is now only rendered if it exists and hasn't encountered loading errors, with proper error state tracking.


126-129: Conditionally rendering chevron icon based on token support.

The chevron icon is now only displayed for tokens that are either supported by Squid or marked as popular, providing a visual indication of which tokens can be selected.

@kushagrasarathe kushagrasarathe changed the title Fix/tkn slctr bugs fix: token selector related issues May 16, 2025
@kushagrasarathe kushagrasarathe added the enhancement New feature or request label May 16, 2025
@kushagrasarathe kushagrasarathe merged commit 4f4a058 into peanut-wallet-dev May 16, 2025
5 checks passed
@Hugo0 Hugo0 deleted the fix/tkn-slctr-bugs branch July 3, 2025 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Being worked on Being worked on enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant