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

fix: Ledger - cancel properly and don't allow user to close non cancellable sections #5353

Merged
merged 5 commits into from
May 15, 2024

Conversation

pete-watters
Copy link
Contributor

@pete-watters pete-watters commented May 9, 2024

Try out Leather build 910a39dExtension build, Test report, Storybook, Chromatic

This PR:

Summary by CodeRabbit

  • New Features

    • Enhanced cancel functionality for ledger actions, allowing users to cancel actions more intuitively based on device connection status.
  • Improvements

    • Updated header components for a more consistent user interface.
    • Improved logic for determining when actions can be canceled, providing a smoother user experience.
  • Bug Fixes

    • Fixed issues related to the rendering of header components and action cancellation logic within various ledger-related flows.
  • Refactor

    • Streamlined the logic for handling ledger actions, enhancing maintainability and readability of the codebase.

Copy link

coderabbitai bot commented May 9, 2024

Walkthrough

The recent updates primarily focus on enhancing the user experience during Ledger interactions by introducing a cancellable action mechanism. This involves importing and utilizing the useCancelLedgerAction function across various components, ensuring users can cancel actions under specific conditions. Additionally, several components have been updated to replace the Header component with DialogHeader to streamline dialog interactions and improve the handling of the onClose event.

Changes

File Path Change Summary
src/app/.../ledger-bitcoin-sign-tx-container.tsx Added useCancelLedgerAction and updated closeAction logic based on canCancelLedgerAction.
src/app/.../ledger-sign-jwt-container.tsx Changed import from Header to DialogHeader, modified header rendering and onClose logic.
src/app/.../ledger-request-bitcoin-keys.tsx Updated isActionCancellableByUser condition and modified parameter in onRequestKey function.
src/app/.../ledger-request-stacks-keys.tsx Replaced useActionCancellableByUser with useCancelLedgerAction for isActionCancellableByUser logic.
src/app/ui/.../headers/header.tsx Removed isWaitingOnPerformed condition.
src/app/.../ledger-stacks-sign-msg-container.tsx Replaced Header with DialogHeader, adjusted onClose behavior and dialog props.
src/app/.../ledger-sign-stacks-tx-container.tsx Added useCancelLedgerAction and updated closeAction logic.
src/app/.../use-ledger-navigate.ts Updated cancelLedgerAction to use baseUrl for navigation and added wentBack property to state.
src/app/.../generic-ledger-utils.ts Added useLocation, RouteUrls, useActionCancellableByUser, and useCancelLedgerAction imports and functions.
src/app/.../stacks-ledger-utils.ts Updated useActionCancellableByUser to include a check for ConnectLedgerError route.

🐰

In the land of code, a rabbit hops,

Making changes, never stops.

With DialogHeader and actions to cancel,

Enhancing flows with a touch so gentle.

Navigate with ease, no need to fret,

For user-friendly paths, we’re all set!

🐇✨


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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

@pete-watters
Copy link
Contributor Author

@coderabbitai review

Copy link

coderabbitai bot commented May 9, 2024

Actions Performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@pete-watters pete-watters changed the title fix: go back properly and don't allow user to close non cancellable l… fix: Ledger - cancel properly and don't allow user to close non cancellable sections May 9, 2024
@@ -8,11 +8,10 @@ import { ArrowLeftIcon, CloseIcon } from '@app/ui/icons';
import { BigTitleHeader } from './components/big-title-header';
import { HeaderActionButton } from './components/header-action-button';

type HeaderVariants = 'page' | 'home' | 'onboarding' | 'dialog' | 'bigTitle' | 'fund';
type HeaderVariants = 'page' | 'home' | 'onboarding' | 'bigTitle' | 'fund';
Copy link
Contributor Author

@pete-watters pete-watters May 9, 2024

Choose a reason for hiding this comment

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

I just made a new header for Dialog - DialogHeader to remove this extra variant here and isWaitingOnPerformedAction

}
interface RadixDialogProps extends DialogProps {
children: ReactNode;
footer?: ReactNode;
header: ReactElement<any, string | JSXElementConstructor<any>>;
header?: ReactElement<any, string | JSXElementConstructor<any>>;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried changing the type of this to ReactNode but I need these types as I am using cloneElement to clone the header passed to the Dialog and pass on the onClose action

const { chain } = useCurrentNetwork();

const [isLoading, setIsLoading] = useState(false);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added this code as if users:

  • start signing this message
  • hit X to cancel and go back
  • when they go back to the correct place now signBip322MessageIsLoading stays stuck as loading so they cannot retry

Copy link

@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

Out of diff range and nitpick comments (4)
src/app/ui/components/containers/headers/components/header-action-button.tsx (1)

7-7: Ensure onAction is properly documented in the interface.

Adding a brief comment describing the purpose and usage of onAction would improve code readability and maintainability.

src/app/features/ledger/flows/stacks-message-signing/ledger-stacks-sign-msg-container.tsx (3)

Line range hint 7-7: Remove unused import get from 'lodash.get'.

- import get from 'lodash.get';

Line range hint 53-53: The variable navigate is declared but never used. Consider removing it if it's not needed.

- const navigate = useNavigate();

Line range hint 55-55: The variable location is declared but never used. Consider removing it if it's not needed.

- const location = useLocation();
Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 5d9895f and f28b981.
Files selected for processing (35)
  • src/app/components/broadcast-error-dialog/broadcast-error-dialog.tsx (2 hunks)
  • src/app/features/dialogs/edit-nonce-dialog/edit-nonce-dialog.tsx (2 hunks)
  • src/app/features/dialogs/high-fee-dialog/high-fee-dialog.tsx (2 hunks)
  • src/app/features/dialogs/increase-fee-dialog/increase-btc-fee-dialog.tsx (2 hunks)
  • src/app/features/dialogs/increase-fee-dialog/increase-fee-sent-dialog.tsx (2 hunks)
  • src/app/features/dialogs/increase-fee-dialog/increase-stx-fee-dialog.tsx (2 hunks)
  • src/app/features/dialogs/switch-account-dialog/components/account-list-unavailable.tsx (1 hunks)
  • src/app/features/dialogs/switch-account-dialog/switch-account-dialog.tsx (3 hunks)
  • src/app/features/ledger/flows/bitcoin-tx-signing/ledger-bitcoin-sign-tx-container.tsx (3 hunks)
  • src/app/features/ledger/flows/jwt-signing/ledger-sign-jwt-container.tsx (2 hunks)
  • src/app/features/ledger/flows/request-bitcoin-keys/ledger-request-bitcoin-keys.tsx (1 hunks)
  • src/app/features/ledger/flows/request-stacks-keys/ledger-request-stacks-keys.tsx (1 hunks)
  • src/app/features/ledger/flows/stacks-message-signing/ledger-stacks-sign-msg-container.tsx (2 hunks)
  • src/app/features/ledger/flows/stacks-tx-signing/ledger-sign-stacks-tx-container.tsx (3 hunks)
  • src/app/features/ledger/generic-flows/request-keys/request-keys-flow.tsx (1 hunks)
  • src/app/features/ledger/generic-flows/tx-signing/tx-signing-flow.tsx (1 hunks)
  • src/app/features/ledger/generic-steps/connect-device/connect-ledger-error.layout.tsx (1 hunks)
  • src/app/features/ledger/generic-steps/connect-device/connect-ledger-start.tsx (2 hunks)
  • src/app/features/ledger/generic-steps/unsupported-browser/unsupported-browser.layout.tsx (2 hunks)
  • src/app/features/ledger/hooks/use-ledger-navigate.ts (1 hunks)
  • src/app/features/ledger/utils/stacks-ledger-utils.ts (1 hunks)
  • src/app/features/retrieve-taproot-to-native-segwit/components/retrieve-taproot-to-native-segwit.layout.tsx (2 hunks)
  • src/app/features/settings/network/network.tsx (2 hunks)
  • src/app/features/settings/sign-out/sign-out.tsx (2 hunks)
  • src/app/features/settings/theme/theme-dialog.tsx (2 hunks)
  • src/app/pages/rpc-sign-bip322-message/rpc-sign-bip322-message.tsx (2 hunks)
  • src/app/pages/send/ordinal-inscription/send-inscription-choose-fee.tsx (2 hunks)
  • src/app/pages/send/ordinal-inscription/send-inscription-form.tsx (2 hunks)
  • src/app/pages/send/ordinal-inscription/send-inscription-review.tsx (2 hunks)
  • src/app/pages/send/ordinal-inscription/sent-inscription-summary.tsx (2 hunks)
  • src/app/pages/send/send-crypto-asset-form/components/recipient-accounts-dialog/recipient-accounts-dialog.tsx (2 hunks)
  • src/app/ui/components/containers/dialog/dialog.tsx (1 hunks)
  • src/app/ui/components/containers/headers/components/header-action-button.tsx (1 hunks)
  • src/app/ui/components/containers/headers/dialog-header.tsx (1 hunks)
  • src/app/ui/components/containers/headers/header.tsx (4 hunks)
Files skipped from review due to trivial changes (9)
  • src/app/features/dialogs/high-fee-dialog/high-fee-dialog.tsx
  • src/app/features/dialogs/increase-fee-dialog/increase-btc-fee-dialog.tsx
  • src/app/features/dialogs/increase-fee-dialog/increase-fee-sent-dialog.tsx
  • src/app/features/dialogs/switch-account-dialog/components/account-list-unavailable.tsx
  • src/app/features/ledger/flows/jwt-signing/ledger-sign-jwt-container.tsx
  • src/app/features/settings/theme/theme-dialog.tsx
  • src/app/pages/send/ordinal-inscription/send-inscription-choose-fee.tsx
  • src/app/pages/send/send-crypto-asset-form/components/recipient-accounts-dialog/recipient-accounts-dialog.tsx
  • src/app/ui/components/containers/dialog/dialog.tsx
Additional Context Used
GitHub Check Runs (1)
typecheck failure (5)

src/app/features/ledger/flows/stacks-message-signing/ledger-stacks-sign-msg-container.tsx: [failure] 7-7:
'get' is declared but its value is never read.


src/app/features/ledger/flows/stacks-message-signing/ledger-stacks-sign-msg-container.tsx: [failure] 53-53:
'navigate' is declared but its value is never read.


src/app/features/ledger/flows/stacks-message-signing/ledger-stacks-sign-msg-container.tsx: [failure] 55-55:
'location' is declared but its value is never read.

Additional comments not posted (39)
src/app/ui/components/containers/headers/components/header-action-button.tsx (1)

15-16: Good use of hover styles to enhance user interaction.

src/app/features/ledger/generic-flows/tx-signing/tx-signing-flow.tsx (2)

5-5: Correct update of the import path for DialogHeader.


11-17: Ensure closeAction is properly handled when undefined.

src/app/features/ledger/generic-flows/request-keys/request-keys-flow.tsx (2)

5-5: Correct update of the import path for DialogHeader.


14-25: Proper handling of action cancellability based on isActionCancellableByUser.

src/app/ui/components/containers/headers/dialog-header.tsx (2)

11-12: Proper addition of optional props to enhance component flexibility.


15-40: Effective use of conditional rendering for title and onClose props in DialogHeader.

src/app/features/ledger/generic-steps/unsupported-browser/unsupported-browser.layout.tsx (1)

7-7: Correct update of the import path for DialogHeader.

src/app/components/broadcast-error-dialog/broadcast-error-dialog.tsx (1)

10-10: Correct update of the import path for DialogHeader.

src/app/features/ledger/generic-steps/connect-device/connect-ledger-start.tsx (1)

9-9: Correct update of the import path for DialogHeader.

src/app/features/dialogs/edit-nonce-dialog/edit-nonce-dialog.tsx (2)

12-12: Correct update of the import path for DialogHeader.


51-51: Effective use of the title prop to provide context in the DialogHeader.

src/app/features/retrieve-taproot-to-native-segwit/components/retrieve-taproot-to-native-segwit.layout.tsx (2)

8-8: Updated import to use DialogHeader aligns with the project-wide renaming effort.


24-24: Correct usage of DialogHeader in the Dialog component. This change ensures consistency in dialog headers across the application.

src/app/features/ledger/generic-steps/connect-device/connect-ledger-error.layout.tsx (1)

41-43: Responsive design adjustments to LedgerTitle are appropriate for improving user experience across different devices.

src/app/features/settings/network/network.tsx (2)

15-15: Updated import to use DialogHeader aligns with the project-wide renaming effort.


46-46: Proper use of DialogHeader with a title in the Dialog component enhances clarity and consistency in UI.

src/app/pages/rpc-sign-bip322-message/rpc-sign-bip322-message.tsx (2)

1-2: Importing useState and useEffect is essential for managing component state and side effects, which is seen in the use of these hooks further down in the file.


41-50: The logic to manage the loading state based on navigation changes is sound. It ensures that the loading indicator is reset when the user navigates back, allowing them to retry the operation.

src/app/features/ledger/flows/request-bitcoin-keys/ledger-request-bitcoin-keys.tsx (1)

70-70: Adjusting the condition for action cancellability based on the connection status is a prudent enhancement. It prevents user actions during critical operations, enhancing the robustness of the flow.

src/app/features/ledger/flows/request-stacks-keys/ledger-request-stacks-keys.tsx (1)

82-82: Consistent with the previous file, adjusting the condition for action cancellability based on the connection status is a good practice. It ensures that user actions are blocked during sensitive operations.

src/app/features/dialogs/switch-account-dialog/switch-account-dialog.tsx (2)

12-14: Updated import to use DialogHeader aligns with the project-wide renaming effort.


56-56: Proper use of DialogHeader with a title in the Dialog component enhances clarity and consistency in UI, especially in a critical feature like account switching.

src/app/pages/send/ordinal-inscription/send-inscription-form.tsx (2)

15-15: Updated import statement to use DialogHeader reflects the renaming of the component for consistency across the application.


45-45: Usage of DialogHeader in the Dialog component aligns with the new component structure and enhances readability and maintainability of the dialog-related code.

src/app/features/settings/sign-out/sign-out.tsx (2)

10-10: Updated import statement to use DialogHeader reflects the renaming of the component for consistency across the application.


31-31: Usage of DialogHeader in the Dialog component aligns with the new component structure and enhances readability and maintainability of the dialog-related code.

src/app/pages/send/ordinal-inscription/sent-inscription-summary.tsx (2)

19-19: Updated import statement to use DialogHeader reflects the renaming of the component for consistency across the application.


63-63: Usage of DialogHeader in the Dialog component aligns with the new component structure and enhances readability and maintainability of the dialog-related code.

src/app/features/ledger/utils/stacks-ledger-utils.ts (1)

91-93: The addition of useActionCancellableByUser function enhances the application's ability to determine if an action can be cancelled based on the current route, improving user experience by providing appropriate UI feedback and options.

src/app/pages/send/ordinal-inscription/send-inscription-review.tsx (2)

19-19: Updated import statement to use DialogHeader reflects the renaming of the component for consistency across the application.


79-79: Usage of DialogHeader in the Dialog component aligns with the new component structure and enhances readability and maintainability of the dialog-related code.

src/app/features/ledger/hooks/use-ledger-navigate.ts (1)

144-150: Modification of the cancelLedgerAction function to use baseUrl for determining the navigation destination enhances the predictability and robustness of the navigation logic, aligning with the PR's objectives to fix navigation issues.

src/app/features/ledger/flows/bitcoin-tx-signing/ledger-bitcoin-sign-tx-container.tsx (1)

29-29: The use of useActionCancellableByUser in the Bitcoin transaction signing flow allows the application to dynamically determine if the current action can be cancelled, enhancing user experience by providing appropriate options based on the context.

src/app/features/ledger/flows/stacks-tx-signing/ledger-sign-stacks-tx-container.tsx (1)

21-21: The use of useActionCancellableByUser in the Stacks transaction signing flow allows the application to dynamically determine if the current action can be cancelled, enhancing user experience by providing appropriate options based on the context.

src/app/features/ledger/flows/stacks-message-signing/ledger-stacks-sign-msg-container.tsx (2)

24-24: Updated import to use DialogHeader instead of Header. Ensure all references to the old Header component are updated across the project.


156-156: The onClose logic now conditionally allows the dialog to be closed based on canClose. Ensure this change aligns with the intended user flow, especially in non-cancellable ledger actions.

Verification successful

The search results from the script provide a comprehensive overview of how the onClose property is used across various ledger-related components in the application. The specific line from the file ledger-stacks-sign-msg-container.tsx shows that the onClose callback is conditionally set based on the canClose variable, which implies that the dialog's ability to close is contingent on certain conditions being met.

From the output, it's evident that similar conditional logic for onClose is also used in other ledger-related components, such as:

  • ledger-sign-jwt-container.tsx uses a similar conditional approach.
  • request-keys-flow.tsx also conditions the onClose based on isActionCancellableByUser.

This pattern suggests that the application consistently handles the ability to close dialogs based on specific conditions, which likely aligns with the intended user flow for handling non-cancellable ledger actions. Therefore, the implementation in ledger-stacks-sign-msg-container.tsx appears to be consistent with other parts of the application.

Final Assessment:

  • The conditional onClose implementation in ledger-stacks-sign-msg-container.tsx aligns with similar patterns found in other ledger-related components, suggesting a consistent approach across the application.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the new onClose logic aligns with user flow requirements.

# Test: Search for the usage of `onClose` in ledger related components. Expect: Consistent handling of non-cancellable actions.
rg --type tsx $'onClose'

Length of output: 51



Script:

#!/bin/bash
# Description: Re-verify that the new onClose logic aligns with user flow requirements.

# Test: Search for the usage of `onClose` in ledger related components. Expect: Consistent handling of non-cancellable actions.
rg --glob '*.tsx' 'onClose'

Length of output: 12803

src/app/features/dialogs/increase-fee-dialog/increase-stx-fee-dialog.tsx (2)

27-27: Updated import to use DialogHeader instead of Header. Ensure all references to the old Header component are updated across the project.


106-106: The Dialog component now includes a title in the DialogHeader. Ensure this title is appropriate and consistent with UI guidelines.

const backLocation = location.pathname.match(RouteUrls.SendOrdinalInscription)
? RouteUrls.Home
: '..';
// Use baseUrl to determine where to go on close
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the main fix for 5143.

I tried making changes to our routing to remove /* and restructure things but had no luck and hit old bugs from the past.

For Ledger, we have a base route that we then append dialog routes to.

Here, when the user rejects we take the base url from location and send them back there.

  • So if they hit: /sign-bip322-message
  • Then go to /sign-bip322-message/connect-your-ledger
  • Then /sign-bip322-message/awaiting-approval
  • On close we now go back to /sign-bip322-message

@@ -140,27 +140,20 @@ function LedgerSignStacksMsg({ account, unsignedMessage }: LedgerSignMsgProps) {
}
}

const allowUserToGoBack = get(location.state, 'goBack');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed this allowUserToGoBack as I can't find where 'goBack' is set in the state and I don't think it's doing anything

@@ -88,8 +88,9 @@ export function signStacksTransactionWithSignature(transaction: string, signatur
export function useActionCancellableByUser() {
const { pathname } = useLocation();
return (
pathname.includes(RouteUrls.DeviceBusy) ||
pathname.includes(RouteUrls.ConnectLedger) ||
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made a change here to allow users to close the dialog if they are on:

  • ConnectLedger
  • ConnectLedgerError

I stopped allowing close if they are on the DeviceBusy route as this is when we are loading their keys so I don't think they should be able to close it

@pete-watters pete-watters force-pushed the fix/5143/ledger-routing branch from f28b981 to 85328a9 Compare May 9, 2024 06:05
@pete-watters pete-watters self-assigned this May 9, 2024
@pete-watters
Copy link
Contributor Author

pete-watters commented May 9, 2024

@kyranjamie : Opening this as a DRAFT as I made some more changes I am not sure you will agree are improvements. I can pick out the change to fix this specific bug as needed. I was using this as an exercise to help me get up to speed with our Ledger code.

When working on changing the dialogs/containers, we agreed to remove the text that was saying Ledger device in use but kept a disabled X. With my dialog changes, users could still click outside of the dialog and have it closed which is not good.

I have made changes so that when we are awaiting specific actions, the Dialog is not closable and we don't show a disabled X. This will stop the dialog from being able to be closed while certain things are happening:

Area.mp4

It's not perfect though as users can still close from this interim screen before accounts are loaded:
Screenshot 2024-05-09 at 09 52 21

If they do that, it continues to work but the UI flashes until the accounts are loaded trying to show please-wait from the get-started route

Copy link
Collaborator

@kyranjamie kyranjamie left a comment

Choose a reason for hiding this comment

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

Thanks Pete. Will QA later, I've lost my USB-C cable

@@ -148,11 +150,9 @@ function LedgerSignStacksTxContainer() {
hasUserSkippedBuggyAppWarning,
};

const canClose = !awaitingDeviceConnection && canUserCancelAction;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any way we can avoid the repetition of this expression?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I will refactor it 👍

Copy link
Contributor Author

@pete-watters pete-watters May 14, 2024

Choose a reason for hiding this comment

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

@kyranjamie: I did a refactor to improve this here.

I added some more notes here.

I'm going to do some more testing but let me know if you think the code looks OK or if I should keep going with the refactor

@pete-watters
Copy link
Contributor Author

Thanks Pete. Will QA later, I've lost my USB-C cable

Thanks, no rush, just wanted to share WIP so far

@pete-watters pete-watters force-pushed the fix/5143/ledger-routing branch from 53a3b71 to e2c7e19 Compare May 13, 2024 09:30
@markmhendrickson markmhendrickson linked an issue May 13, 2024 that may be closed by this pull request
@pete-watters pete-watters force-pushed the fix/5143/ledger-routing branch 3 times, most recently from d848d81 to 01dbb33 Compare May 14, 2024 08:57
@@ -120,3 +122,19 @@ export function checkLockedDeviceError(e: Error) {
e?.message === LedgerConnectionErrors.DeviceLocked
);
}

Copy link
Contributor Author

@pete-watters pete-watters May 14, 2024

Choose a reason for hiding this comment

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

I moved this logic to generic as it's for both Stacks and BTC.

I noticed that we often return awaitingDeviceConnection from the ledger hooks and then we need to feed it into my new hook useCancelLedgerAction.

For exampleuseLedgerSignTx returns awaitingDeviceConnection which gets passed into useCancelLedgerAction.

I experimented with instead pushing that logic into the hooks so that they return canCancelLedgerAction instead of awaitingDeviceConnection but going that route and I will need to also update BaseLedgerOperationContext which could be going a bit too far:

export interface BaseLedgerOperationContext {
  latestDeviceResponse: LatestDeviceResponse;
  awaitingDeviceConnection: boolean;
}

@pete-watters pete-watters force-pushed the fix/5143/ledger-routing branch from 01dbb33 to ff5aba0 Compare May 15, 2024 05:27
@pete-watters pete-watters marked this pull request as ready for review May 15, 2024 05:42
@pete-watters pete-watters requested a review from kyranjamie May 15, 2024 05:42
Copy link

@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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between f28b981 and ff5aba0.
Files selected for processing (36)
  • src/app/components/broadcast-error-dialog/broadcast-error-dialog.tsx (2 hunks)
  • src/app/features/dialogs/edit-nonce-dialog/edit-nonce-dialog.tsx (2 hunks)
  • src/app/features/dialogs/high-fee-dialog/high-fee-dialog.tsx (2 hunks)
  • src/app/features/dialogs/increase-fee-dialog/increase-btc-fee-dialog.tsx (2 hunks)
  • src/app/features/dialogs/increase-fee-dialog/increase-fee-sent-dialog.tsx (2 hunks)
  • src/app/features/dialogs/increase-fee-dialog/increase-stx-fee-dialog.tsx (2 hunks)
  • src/app/features/dialogs/switch-account-dialog/components/account-list-unavailable.tsx (1 hunks)
  • src/app/features/dialogs/switch-account-dialog/switch-account-dialog.tsx (3 hunks)
  • src/app/features/ledger/flows/bitcoin-tx-signing/ledger-bitcoin-sign-tx-container.tsx (2 hunks)
  • src/app/features/ledger/flows/jwt-signing/ledger-sign-jwt-container.tsx (3 hunks)
  • src/app/features/ledger/flows/request-bitcoin-keys/ledger-request-bitcoin-keys.tsx (3 hunks)
  • src/app/features/ledger/flows/request-stacks-keys/ledger-request-stacks-keys.tsx (3 hunks)
  • src/app/features/ledger/flows/stacks-message-signing/ledger-stacks-sign-msg-container.tsx (3 hunks)
  • src/app/features/ledger/flows/stacks-tx-signing/ledger-sign-stacks-tx-container.tsx (2 hunks)
  • src/app/features/ledger/generic-flows/request-keys/request-keys-flow.tsx (1 hunks)
  • src/app/features/ledger/generic-flows/tx-signing/tx-signing-flow.tsx (1 hunks)
  • src/app/features/ledger/generic-steps/connect-device/connect-ledger-error.layout.tsx (1 hunks)
  • src/app/features/ledger/generic-steps/connect-device/connect-ledger-start.tsx (2 hunks)
  • src/app/features/ledger/generic-steps/unsupported-browser/unsupported-browser.layout.tsx (2 hunks)
  • src/app/features/ledger/hooks/use-ledger-navigate.ts (1 hunks)
  • src/app/features/ledger/utils/generic-ledger-utils.ts (2 hunks)
  • src/app/features/ledger/utils/stacks-ledger-utils.ts (3 hunks)
  • src/app/features/retrieve-taproot-to-native-segwit/components/retrieve-taproot-to-native-segwit.layout.tsx (2 hunks)
  • src/app/features/settings/network/network.tsx (2 hunks)
  • src/app/features/settings/sign-out/sign-out.tsx (2 hunks)
  • src/app/features/settings/theme/theme-dialog.tsx (2 hunks)
  • src/app/pages/rpc-sign-bip322-message/rpc-sign-bip322-message.tsx (2 hunks)
  • src/app/pages/send/ordinal-inscription/send-inscription-choose-fee.tsx (2 hunks)
  • src/app/pages/send/ordinal-inscription/send-inscription-form.tsx (2 hunks)
  • src/app/pages/send/ordinal-inscription/send-inscription-review.tsx (2 hunks)
  • src/app/pages/send/ordinal-inscription/sent-inscription-summary.tsx (2 hunks)
  • src/app/pages/send/send-crypto-asset-form/components/recipient-accounts-dialog/recipient-accounts-dialog.tsx (2 hunks)
  • src/app/ui/components/containers/dialog/dialog.tsx (1 hunks)
  • src/app/ui/components/containers/headers/components/header-action-button.tsx (1 hunks)
  • src/app/ui/components/containers/headers/dialog-header.tsx (1 hunks)
  • src/app/ui/components/containers/headers/header.tsx (4 hunks)
Files skipped from review due to trivial changes (1)
  • src/app/features/dialogs/increase-fee-dialog/increase-btc-fee-dialog.tsx
Files skipped from review as they are similar to previous changes (34)
  • src/app/components/broadcast-error-dialog/broadcast-error-dialog.tsx
  • src/app/features/dialogs/edit-nonce-dialog/edit-nonce-dialog.tsx
  • src/app/features/dialogs/high-fee-dialog/high-fee-dialog.tsx
  • src/app/features/dialogs/increase-fee-dialog/increase-fee-sent-dialog.tsx
  • src/app/features/dialogs/increase-fee-dialog/increase-stx-fee-dialog.tsx
  • src/app/features/dialogs/switch-account-dialog/components/account-list-unavailable.tsx
  • src/app/features/dialogs/switch-account-dialog/switch-account-dialog.tsx
  • src/app/features/ledger/flows/bitcoin-tx-signing/ledger-bitcoin-sign-tx-container.tsx
  • src/app/features/ledger/flows/jwt-signing/ledger-sign-jwt-container.tsx
  • src/app/features/ledger/flows/request-bitcoin-keys/ledger-request-bitcoin-keys.tsx
  • src/app/features/ledger/flows/request-stacks-keys/ledger-request-stacks-keys.tsx
  • src/app/features/ledger/flows/stacks-message-signing/ledger-stacks-sign-msg-container.tsx
  • src/app/features/ledger/flows/stacks-tx-signing/ledger-sign-stacks-tx-container.tsx
  • src/app/features/ledger/generic-flows/request-keys/request-keys-flow.tsx
  • src/app/features/ledger/generic-flows/tx-signing/tx-signing-flow.tsx
  • src/app/features/ledger/generic-steps/connect-device/connect-ledger-error.layout.tsx
  • src/app/features/ledger/generic-steps/connect-device/connect-ledger-start.tsx
  • src/app/features/ledger/generic-steps/unsupported-browser/unsupported-browser.layout.tsx
  • src/app/features/ledger/hooks/use-ledger-navigate.ts
  • src/app/features/ledger/utils/stacks-ledger-utils.ts
  • src/app/features/retrieve-taproot-to-native-segwit/components/retrieve-taproot-to-native-segwit.layout.tsx
  • src/app/features/settings/network/network.tsx
  • src/app/features/settings/sign-out/sign-out.tsx
  • src/app/features/settings/theme/theme-dialog.tsx
  • src/app/pages/rpc-sign-bip322-message/rpc-sign-bip322-message.tsx
  • src/app/pages/send/ordinal-inscription/send-inscription-choose-fee.tsx
  • src/app/pages/send/ordinal-inscription/send-inscription-form.tsx
  • src/app/pages/send/ordinal-inscription/send-inscription-review.tsx
  • src/app/pages/send/ordinal-inscription/sent-inscription-summary.tsx
  • src/app/pages/send/send-crypto-asset-form/components/recipient-accounts-dialog/recipient-accounts-dialog.tsx
  • src/app/ui/components/containers/dialog/dialog.tsx
  • src/app/ui/components/containers/headers/components/header-action-button.tsx
  • src/app/ui/components/containers/headers/dialog-header.tsx
  • src/app/ui/components/containers/headers/header.tsx
Additional comments not posted (1)
src/app/features/ledger/utils/generic-ledger-utils.ts (1)

2-2: The import of useLocation is correct and necessary for the new hooks.

src/app/features/ledger/utils/generic-ledger-utils.ts Outdated Show resolved Hide resolved
src/app/features/ledger/utils/generic-ledger-utils.ts Outdated Show resolved Hide resolved
@pete-watters pete-watters force-pushed the fix/5143/ledger-routing branch from ff5aba0 to 649a1ec Compare May 15, 2024 05:55
Copy link

@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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between ff5aba0 and 772dea7.
Files selected for processing (9)
  • src/app/features/ledger/flows/bitcoin-tx-signing/ledger-bitcoin-sign-tx-container.tsx (2 hunks)
  • src/app/features/ledger/flows/jwt-signing/ledger-sign-jwt-container.tsx (3 hunks)
  • src/app/features/ledger/flows/request-bitcoin-keys/ledger-request-bitcoin-keys.tsx (3 hunks)
  • src/app/features/ledger/flows/request-stacks-keys/ledger-request-stacks-keys.tsx (3 hunks)
  • src/app/features/ledger/flows/stacks-message-signing/ledger-stacks-sign-msg-container.tsx (3 hunks)
  • src/app/features/ledger/flows/stacks-tx-signing/ledger-sign-stacks-tx-container.tsx (2 hunks)
  • src/app/features/ledger/hooks/use-ledger-navigate.ts (2 hunks)
  • src/app/features/ledger/utils/generic-ledger-utils.ts (2 hunks)
  • src/app/features/ledger/utils/stacks-ledger-utils.ts (3 hunks)
Files skipped from review as they are similar to previous changes (9)
  • src/app/features/ledger/flows/bitcoin-tx-signing/ledger-bitcoin-sign-tx-container.tsx
  • src/app/features/ledger/flows/jwt-signing/ledger-sign-jwt-container.tsx
  • src/app/features/ledger/flows/request-bitcoin-keys/ledger-request-bitcoin-keys.tsx
  • src/app/features/ledger/flows/request-stacks-keys/ledger-request-stacks-keys.tsx
  • src/app/features/ledger/flows/stacks-message-signing/ledger-stacks-sign-msg-container.tsx
  • src/app/features/ledger/flows/stacks-tx-signing/ledger-sign-stacks-tx-container.tsx
  • src/app/features/ledger/hooks/use-ledger-navigate.ts
  • src/app/features/ledger/utils/generic-ledger-utils.ts
  • src/app/features/ledger/utils/stacks-ledger-utils.ts

Copy link

@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

Out of diff range and nitpick comments (1)
src/app/features/ledger/flows/request-bitcoin-keys/request-bitcoin-keys.utils.ts (1)

Line range hint 81-86: Ensure consistent handling of defaultNumberOfKeysToPullFromLedgerDevice.

The loop structure is correct, but consider extracting the repeated logic into a separate function to adhere to the DRY principle.

- for (
-   let accountIndex = 0;
-   accountIndex < defaultNumberOfKeysToPullFromLedgerDevice;
-   accountIndex++
- ) {
-   onRequestKey?.(accountIndex + defaultNumberOfKeysToPullFromLedgerDevice);
-   const { path, policy } = await getTaprootExtendedPublicKey({
-     bitcoinApp,
-     fingerprint,
-     network,
-     accountIndex,
-   });
-   keys.push({ id: createWalletIdDecoratedPath(path, 'default'), path, policy, targetId });
- }

+ async function addKeys(getExtendedPublicKey, startIndex = 0) {
+   for (
+     let accountIndex = 0;
+     accountIndex < defaultNumberOfKeysToPullFromLedgerDevice;
+     accountIndex++
+   ) {
+     onRequestKey?.(accountIndex + startIndex);
+     const { path, policy } = await getExtendedPublicKey({
+       bitcoinApp,
+       fingerprint,
+       network,
+       accountIndex,
+     });
+     keys.push({ id: createWalletIdDecoratedPath(path, 'default'), path, policy, targetId });
+   }
+ }
+ await addKeys(getNativeSegwitExtendedPublicKey);
+ await addKeys(getTaprootExtendedPublicKey, defaultNumberOfKeysToPullFromLedgerDevice);
Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 772dea7 and 910a39d.
Files selected for processing (2)
  • src/app/features/ledger/flows/request-bitcoin-keys/ledger-request-bitcoin-keys.tsx (3 hunks)
  • src/app/features/ledger/flows/request-bitcoin-keys/request-bitcoin-keys.utils.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/app/features/ledger/flows/request-bitcoin-keys/ledger-request-bitcoin-keys.tsx

Comment on lines +75 to 80

for (
let accountIndex = 0;
accountIndex < defaultNumberOfKeysToPullFromLedgerDevice;
accountIndex++
) {
Copy link

Choose a reason for hiding this comment

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

Ensure consistent handling of defaultNumberOfKeysToPullFromLedgerDevice.

The loop structure is correct, but consider extracting the repeated logic into a separate function to adhere to the DRY principle.

- for (
-   let accountIndex = 0;
-   accountIndex < defaultNumberOfKeysToPullFromLedgerDevice;
-   accountIndex++
- ) {
-   onRequestKey?.(accountIndex);
-   const { path, policy } = await getNativeSegwitExtendedPublicKey({
-     bitcoinApp,
-     fingerprint,
-     network,
-     accountIndex,
-   });
-   keys.push({ id: createWalletIdDecoratedPath(path, 'default'), path, policy, targetId });
- }

+ async function addKeys(getExtendedPublicKey, startIndex = 0) {
+   for (
+     let accountIndex = 0;
+     accountIndex < defaultNumberOfKeysToPullFromLedgerDevice;
+     accountIndex++
+   ) {
+     onRequestKey?.(accountIndex + startIndex);
+     const { path, policy } = await getExtendedPublicKey({
+       bitcoinApp,
+       fingerprint,
+       network,
+       accountIndex,
+     });
+     keys.push({ id: createWalletIdDecoratedPath(path, 'default'), path, policy, targetId });
+   }
+ }
+ await addKeys(getNativeSegwitExtendedPublicKey);
+ await addKeys(getTaprootExtendedPublicKey, defaultNumberOfKeysToPullFromLedgerDevice);

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
for (
let accountIndex = 0;
accountIndex < defaultNumberOfKeysToPullFromLedgerDevice;
accountIndex++
) {
async function addKeys(getExtendedPublicKey, startIndex = 0) {
for (
let accountIndex = 0;
accountIndex < defaultNumberOfKeysToPullFromLedgerDevice;
accountIndex++
) {
onRequestKey?.(accountIndex + startIndex);
const { path, policy } = await getExtendedPublicKey({
bitcoinApp,
fingerprint,
network,
accountIndex,
});
keys.push({ id: createWalletIdDecoratedPath(path, 'default'), path, policy, targetId });
}
}
await addKeys(getNativeSegwitExtendedPublicKey);
await addKeys(getTaprootExtendedPublicKey, defaultNumberOfKeysToPullFromLedgerDevice);

@pete-watters pete-watters added this pull request to the merge queue May 15, 2024
Merged via the queue into dev with commit 9f73b24 May 15, 2024
28 checks passed
@pete-watters pete-watters deleted the fix/5143/ledger-routing branch May 15, 2024 14:11
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.

Ledger routing issue when rejecting
4 participants