Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reset button for going back to request link token on pay #410

Merged
merged 3 commits into from
Oct 3, 2024

Conversation

jjramirezn
Copy link
Contributor

@jjramirezn jjramirezn commented Oct 3, 2024

On the pay view for the request flow, when the selected token-chain pair is different from the combination on the request link, the token selector has a "X" which can be used to select the pair from the request link

Summary by CodeRabbit

  • New Features

    • Introduced an optional reset functionality for the token selector component.
    • Enhanced the InitialView component to manage selected chain and token states effectively, improving cross-chain transaction handling.
  • Bug Fixes

    • Improved fee estimation logic to ensure it accounts for cross-chain scenarios.
  • Documentation

    • Updated context management to include new state variables related to cross-chain functionality.

Copy link

vercel bot commented Oct 3, 2024

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 Oct 3, 2024 3:19pm

Copy link
Contributor

coderabbitai bot commented Oct 3, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes introduce a new optional onReset property to the IAdvancedTokenSelectorButtonProps and TokenSelectorProps interfaces, allowing the AdvancedTokenSelectorButton and TokenSelector components to handle reset actions. The button's rendering logic is updated to conditionally display icons based on the type prop, and its height is increased. Additionally, the InitialView component is modified to manage selected chain and token states, incorporating the new isXChain variable for cross-chain functionality. The context is also updated to include the isXChain state.

Changes

File Change Summary
src/components/Global/TokenSelector/Components/AdvancedButton.tsx Added onReset property to IAdvancedTokenSelectorButtonProps, updated props destructuring, modified rendering logic to conditionally display icons based on type, increased button height from h-14 to h-18.
src/components/Global/TokenSelector/TokenSelector.consts.ts Added onReset method signature to TokenSelectorProps and TokenSelectorXChainProps interfaces.
src/components/Global/TokenSelector/TokenSelector.tsx Updated TokenSelector to accept onReset prop, added isXChain to context extraction, and passed onReset to AdvancedTokenSelectorButton.
src/components/Request/Pay/Views/Initial.view.tsx Added state variables setSelectedChainID, setSelectedTokenAddress, and setIsXChain, modified useEffect to incorporate isXChain, updated handleOnNext to include resetTokenAndChain function.
src/context/tokenSelector.context.tsx Added isXChain state variable and setter function to context, updated context value object to include isXChain and setIsXChain.

Possibly related PRs

  • fix: prefered token in localStorage doesn't match any currently fetched tokens #395: The changes in the TokenSelector.tsx component include modifications to the AdvancedTokenSelectorButton, which is directly related to the updates made in the main PR regarding the onReset prop and its handling in the TokenSelector component.
  • fix: remaining ux issues #401: This PR addresses modifications in the TokenSelector components, which are relevant to the changes made in the main PR that introduced the onReset functionality, enhancing user interaction with token selection.
  • fix(request): two bugs when preparing pay transaction #404: The modifications in this PR regarding token address comparisons and the interaction between the token selector and request component are directly related to the changes made in the main PR, which also involves the TokenSelector and its props.

Suggested labels

Being worked on

Suggested reviewers

  • Hugo0

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between c02059a and 69c4e8c.

📒 Files selected for processing (1)
  • src/components/Global/TokenSelector/Components/AdvancedButton.tsx (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/components/Global/TokenSelector/Components/AdvancedButton.tsx

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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.

Copy link
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: 0

🧹 Outside diff range and nitpick comments (4)
src/components/Global/TokenSelector/TokenSelector.consts.ts (1)

Line range hint 24-24: Remove redundant onReset property from TokenSelectorXChainProps

The onReset property is already inherited from TokenSelectorProps, making this declaration redundant. To improve code maintainability and adhere to the DRY principle, consider removing this line.

Apply this diff to remove the redundant property:

export interface TokenSelectorXChainProps extends TokenSelectorProps {
    data?: CombinedType[]
    tokenSymbol?: string
    tokenAddress?: string
    chainName?: string
    tokenLogoUrl?: string
    chainLogoUrl?: string
    tokenAmount?: string
    isLoading?: boolean
    routeError?: boolean
    routeFound?: boolean
-   onReset?: () => void
    isStatic?: boolean
}
src/context/tokenSelector.context.tsx (1)

25-26: LGTM! Consider adding JSDoc comments for new properties.

The additions of isXChain and setIsXChain to the context object are well-implemented and consistent with the existing pattern.

To improve code documentation, consider adding JSDoc comments for these new properties, explaining their purpose and usage. For example:

/** Indicates whether the current operation is cross-chain */
isXChain: false as boolean,
/** Sets the cross-chain status */
setIsXChain: (value: boolean) => {},
src/components/Global/TokenSelector/Components/AdvancedButton.tsx (1)

136-161: LGTM with a minor suggestion: Conditional rendering for reset functionality

The conditional rendering based on the type prop is well-implemented and aligns with the PR objectives. The event propagation stopping is correct to prevent opening the modal when clicking the reset button.

Consider using the optional chaining operator for the onReset function call to improve readability:

- onReset && onReset()
+ onReset?.()

This change makes the code more concise while maintaining the same functionality.

🧰 Tools
🪛 Biome

[error] 151-151: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

src/components/Global/TokenSelector/TokenSelector.tsx (1)

21-25: LGTM! Consider adding JSDoc for the new onReset prop.

The addition of the onReset prop aligns well with the PR objective of adding a reset button functionality. The prop is correctly typed and integrated into the component's signature.

Consider adding JSDoc comments to document the purpose and expected type of the onReset prop. This will improve code readability and maintainability. For example:

/**
 * @param {Object} props - The component props
 * @param {string} props.classNameButton - CSS class for the button
 * @param {boolean} [props.shouldBeConnected=true] - Whether the user should be connected
 * @param {() => void} props.onReset - Callback function to reset the token selection
 */
const TokenSelector = ({
    classNameButton,
    shouldBeConnected = true,
    onReset,
}: _consts.TokenSelectorProps) => {
    // ...
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between ba36e3c and 62387f9.

📒 Files selected for processing (5)
  • src/components/Global/TokenSelector/Components/AdvancedButton.tsx (3 hunks)
  • src/components/Global/TokenSelector/TokenSelector.consts.ts (1 hunks)
  • src/components/Global/TokenSelector/TokenSelector.tsx (3 hunks)
  • src/components/Request/Pay/Views/Initial.view.tsx (4 hunks)
  • src/context/tokenSelector.context.tsx (3 hunks)
🧰 Additional context used
🪛 Biome
src/components/Global/TokenSelector/Components/AdvancedButton.tsx

[error] 151-151: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

🔇 Additional comments (16)
src/components/Global/TokenSelector/TokenSelector.consts.ts (1)

10-10: LGTM: Addition of onReset method to TokenSelectorProps

The addition of the optional onReset method to the TokenSelectorProps interface aligns well with the PR objectives. It provides a clear way to implement the reset functionality for the token selector component.

src/context/tokenSelector.context.tsx (3)

41-41: LGTM! State variable added correctly.

The addition of the isXChain state variable using the useState hook is implemented correctly and consistently with other state variables in the component.


25-26: Summary: New cross-chain state added to support reset functionality.

The changes introduce a new isXChain state to the tokenSelectorContext. This addition aligns with the PR objectives of enhancing the pay view within the request flow, particularly for managing the reset button functionality.

Key points:

  1. A new isXChain boolean state and its setter are added to the context.
  2. The TokenContextProvider component initializes this state.
  3. The new state is properly exposed through the context provider.

These changes lay the groundwork for implementing the reset button functionality described in the PR objectives. Ensure that this new state is utilized effectively in the components responsible for the token selector interface and the reset button implementation.

Also applies to: 41-41, 145-146


145-146: LGTM! Verify usage of new context values.

The isXChain and setIsXChain properties have been correctly added to the Provider's value prop, maintaining consistency with the context object and state variable declarations.

To ensure these new context values are being used correctly throughout the application, run the following script:

src/components/Global/TokenSelector/Components/AdvancedButton.tsx (4)

23-23: LGTM: Addition of onReset prop

The new onReset prop is correctly typed and made optional, which aligns with the PR objectives and maintains backward compatibility.


39-39: LGTM: Destructuring of onReset prop

The onReset prop is correctly added to the component's destructured props, maintaining code readability.


Line range hint 91-93: LGTM: Styling and structure improvements

The changes to the component's height, hover effect, and overall layout are well-implemented. These improvements enhance the component's usability and visual feedback while accommodating the new reset functionality.


Line range hint 1-164: Overall assessment: Well-implemented feature addition

The changes in this file successfully implement the reset functionality for the token selector as described in the PR objectives. The code is well-structured, and the new features are integrated seamlessly with the existing component. The conditional rendering and event handling for the reset functionality are implemented correctly, with only minor suggestions for improvement.

Great job on this implementation!

🧰 Tools
🪛 Biome

[error] 151-151: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

src/components/Global/TokenSelector/TokenSelector.tsx (3)

Line range hint 1-368: Overall changes align well with PR objectives.

The modifications to the TokenSelector component are focused and well-implemented. They successfully introduce the reset functionality and accommodate cross-chain transactions as described in the PR objectives. The changes are minimal and don't introduce any apparent regressions or unintended modifications to the existing functionality.

Key improvements:

  1. Addition of the onReset prop to enable reset functionality.
  2. Integration of isXChain to handle different transaction types.
  3. Updates to the AdvancedTokenSelectorButton to support these new features.

These changes enhance the component's flexibility and user experience, particularly in scenarios involving cross-chain transactions or when users need to reset their token selection.


36-42: LGTM! Verify the usage of isXChain throughout the component.

The addition of isXChain to the context extraction is a good change that aligns with the PR objective. It will likely be used to determine the type of transaction and adjust the component's behavior accordingly.

To ensure that isXChain is being used correctly throughout the component, please run the following verification script:

#!/bin/bash
# Description: Verify the usage of isXChain in the TokenSelector component

# Search for usages of isXChain within the file
echo "Usages of isXChain in TokenSelector.tsx:"
rg --type typescript "isXChain" src/components/Global/TokenSelector/TokenSelector.tsx

# Check if isXChain is being set correctly in the context
echo "\nContext setup for isXChain:"
rg --type typescript "isXChain.*=|isXChain:.*," -A 5 -B 5 src/context

This script will help us verify that isXChain is being used appropriately and that it's correctly set up in the context.


153-154: LGTM! Verify the AdvancedTokenSelectorButton implementation.

The changes to the AdvancedTokenSelectorButton props are well-implemented and align with the PR objectives. The conditional type prop and the addition of the onReset prop are good improvements.

To ensure that the AdvancedTokenSelectorButton component correctly handles these new props, please run the following verification script:

This script will help us verify that the AdvancedTokenSelectorButton component is correctly implemented to handle the new type and onReset props.

src/components/Request/Pay/Views/Initial.view.tsx (5)

33-38: New context variables are correctly integrated

The newly added context variables setSelectedChainID, setSelectedTokenAddress, and setIsXChain are properly destructured from tokenSelectorContext and prepared for use within the component.


98-104: Correctly determining cross-chain transactions

The calculation of isXChain accurately reflects whether the selected chain or token differs from the request's chain ID or token address. The state updater setIsXChain is appropriately called with the result.


108-108: Efficient conditional rendering based on connection and token price status

The added early return statement ensures that the fee estimation function estimateTxFee is only called when the user is connected and, if it's a cross-chain transaction, after the token price fetching is complete. This prevents unnecessary execution and potential errors.


232-236: Reset function effectively restores default token and chain

The resetTokenAndChain function correctly resets the selectedChainID and selectedTokenAddress to match those in requestLinkData, allowing users to easily revert to the requested token and chain.


317-320: TokenSelector component enhancement supports reset functionality

Passing the resetTokenAndChain function via the onReset prop to the TokenSelector component integrates the reset functionality, improving user experience by allowing them to quickly revert to the original request parameters.

Copy link
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: 2

🧹 Outside diff range and nitpick comments (4)
src/components/Global/TokenSelector/Components/AdvancedButton.tsx (1)

136-161: LGTM: Conditional rendering and reset functionality added.

The changes implement the reset button functionality as per the PR objectives. The conditional rendering based on the type prop is well-implemented.

Consider using an optional chain for the onReset function call to improve code safety:

- onReset && onReset()
+ onReset?.()

This change ensures that onReset is called only if it's defined, without the need for an explicit check.

🧰 Tools
🪛 Biome

[error] 151-151: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

src/components/Global/TokenSelector/TokenSelector.tsx (3)

21-25: LGTM! Consider adding JSDoc for the new onReset prop.

The addition of the onReset prop aligns well with the PR objective of adding a reset button functionality. To improve code maintainability, consider adding JSDoc comments to describe the purpose and expected type of the onReset prop.

You could add a JSDoc comment like this:

/**
 * @param {Object} props - The component props
 * @param {string} [props.classNameButton] - Optional CSS class for the button
 * @param {boolean} [props.shouldBeConnected=true] - Whether the user should be connected
 * @param {() => void} [props.onReset] - Callback function to reset the token selection
 */

36-42: LGTM! Consider grouping related context variables.

The addition of isXChain to the context extraction is appropriate for determining the transaction type. To improve code readability, consider grouping related context variables together.

You could reorder the destructured variables like this:

const {
    selectedChainID,
    setSelectedChainID,
    selectedTokenAddress,
    setSelectedTokenAddress,
    isXChain,
} = useContext(context.tokenSelectorContext)

This groups the chain-related, token-related, and transaction type variables together.


153-154: LGTM! Consider using a constant for the 'type' prop value.

The addition of the type and onReset props to the AdvancedTokenSelectorButton component is appropriate. The conditional setting of the type prop based on isXChain aligns well with the different behavior for cross-chain transactions.

To improve code clarity and maintainability, consider using a constant for the 'type' prop value:

const TRANSACTION_TYPES = {
  XCHAIN: 'xchain',
  SEND: 'send',
} as const;

// In the JSX
type={isXChain ? TRANSACTION_TYPES.XCHAIN : TRANSACTION_TYPES.SEND}

This approach makes it easier to manage and update the possible transaction types in the future.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 62387f9 and c02059a.

📒 Files selected for processing (5)
  • src/components/Global/TokenSelector/Components/AdvancedButton.tsx (3 hunks)
  • src/components/Global/TokenSelector/TokenSelector.consts.ts (1 hunks)
  • src/components/Global/TokenSelector/TokenSelector.tsx (3 hunks)
  • src/components/Request/Pay/Views/Initial.view.tsx (5 hunks)
  • src/context/tokenSelector.context.tsx (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/components/Global/TokenSelector/TokenSelector.consts.ts
  • src/context/tokenSelector.context.tsx
🧰 Additional context used
🪛 Biome
src/components/Global/TokenSelector/Components/AdvancedButton.tsx

[error] 151-151: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

🔇 Additional comments (9)
src/components/Global/TokenSelector/Components/AdvancedButton.tsx (3)

23-23: LGTM: Interface update for reset functionality.

The addition of the optional onReset property to IAdvancedTokenSelectorButtonProps is consistent with the PR objectives and correctly typed.


39-39: LGTM: Props destructuring updated.

The addition of onReset to the destructured props is consistent with the interface change and allows the component to access the reset functionality.


Line range hint 91-91: LGTM: Component structure updated for better usability.

The increased height (h-18) and layout adjustments improve the component's visual appearance and accommodate the new reset functionality effectively.

src/components/Request/Pay/Views/Initial.view.tsx (6)

17-17: Consistent use of error constants

The constant ERR_NO_ROUTE is defined for the error message. Ensure that this constant is used consistently throughout the code wherever this specific error message is needed to maintain consistency and ease future changes.


35-40: Verify that context variables are properly initialized

You are destructuring setSelectedChainID, setSelectedTokenAddress, isTokenPriceFetchingComplete, and setIsXChain from context.tokenSelectorContext. Please verify that these variables are correctly provided by the context to prevent potential undefined errors at runtime.


100-105: Logic for determining cross-chain transactions is correct

The calculation of isXChain correctly determines if the selected chain ID or token address differs from the request link's chain ID or token address. This ensures that cross-chain logic is appropriately handled.


110-110: Conditionally delay fee estimation based on token price fetching

The updated condition in the useEffect hook correctly delays the fee estimation when the token price fetching is not complete in cross-chain scenarios. This prevents unnecessary fee estimations and potential errors.


234-237: Ensure reset functionality updates the state correctly

The resetTokenAndChain function resets the selected chain ID and token address to those from requestLinkData. Verify that this function properly updates the state and that any dependent components re-render as expected.


319-322: Verify that TokenSelector component supports the onReset prop

You have added the onReset prop to the TokenSelector component. Please confirm that the TokenSelector component is designed to accept this prop and that it handles the reset functionality correctly.

Run the following script to check if the TokenSelector component accepts the onReset prop:

✅ Verification successful

Confirmation: TokenSelector Supports the onReset Prop

The TokenSelector component in both TokenSelector.tsx and TokenSelectorXChain.tsx accepts the onReset prop and handles the reset functionality correctly.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify if `TokenSelector` component supports `onReset` prop

# Find the `TokenSelector` component files
fd 'TokenSelector' --type file --extension tsx

# Search for the `onReset` prop in the component files
fd 'TokenSelector' --type file --extension tsx | xargs rg 'onReset'

Length of output: 579

Make the selector and the reset button accessible by tab and provide
aria-label
@Hugo0
Copy link
Contributor

Hugo0 commented Oct 3, 2024

image
FYI the pay button should be disabled if it doesn't find a route - currently not the case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants