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: support onCancel #1175

Merged
merged 1 commit into from May 25, 2021
Merged

feat: support onCancel #1175

merged 1 commit into from May 25, 2021

Conversation

fbwoolf
Copy link
Contributor

@fbwoolf fbwoolf commented May 17, 2021

Try out this version of the Stacks Wallet - download extension builds.

Description

This PR adds the ability for the developer to use an onCancel callback function from connect when a user closes/cancels a transaction window.

This is PR is in Draft status bc the callback is working, but I'm not sure what we actually want to do with the transaction during this process or what (if any) data we would want to return to the developer in the onCancel callback?

For details refer to issue #959

connect PR: hirosystems/connect#122

Type of Change

  • New feature
  • Bug fix
  • API reference/documentation update
  • Other

Does this introduce a breaking change?

No.

Are documentation updates required?

No.

Testing information

TBD.

Checklist

  • Code is commented where needed
  • Unit test coverage for new or modified code paths
  • Changelog is updated
  • Tag 1 of @hstove or @kyranjamie or @aulneau for review

@changeset-bot
Copy link

changeset-bot bot commented May 17, 2021

🦋 Changeset detected

Latest commit: a616e7f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@stacks/wallet-web Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented May 17, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/blockstack/stacks-wallet-web/Gh7Q9M1SdedhXRfA7PVyED9cDwvB
✅ Preview: https://stacks-wallet-web-git-feat-cancel-transaction-blockstack.vercel.app

Copy link
Contributor

@aulneau aulneau left a comment

Choose a reason for hiding this comment

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

Generally I think this is really fantastic! Just left a couple comments and then will do a follow up review after you've made updates

src/store/transaction.ts Outdated Show resolved Hide resolved
src/common/hooks/use-tx-state.ts Outdated Show resolved Hide resolved
@fbwoolf fbwoolf changed the title feat: add ability to cancel transactions feat: fire event when user cancels tx May 17, 2021
@fbwoolf fbwoolf linked an issue May 17, 2021 that may be closed by this pull request
src/pages/transaction/index.tsx Outdated Show resolved Hide resolved
@vercel vercel bot temporarily deployed to Preview May 17, 2021 20:06 Inactive
// export interface TxResult extends SponsoredFinishedTxPayload {
// txId?: string;
// }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removing this bc I think it was just left by mistake?

@fbwoolf
Copy link
Contributor Author

fbwoolf commented May 17, 2021

I addressed code comments and will put up the related PR in connect now.

@vercel vercel bot temporarily deployed to Preview May 17, 2021 20:19 Inactive
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.

Set up connect and the extension this morning and ran through these changes.

I think it'd be better to reject the promise when the user cancels, rather than altering the resolve data. This is a small change, and just requires rejecting in the inpage.ts file

  transactionRequest: async transactionRequest => {
    const event = new CustomEvent<TransactionRequestEventDetails>(DomEventName.transactionRequest, {
      detail: { transactionRequest },
    });
    document.dispatchEvent(event);
    return new Promise((resolve, reject) => {
      const handleMessage = (event: MessageEvent<TransactionResponseMessage>) => {
        console.log('message in inpage.ts', event);
        if (!isValidEvent(event, Methods.transactionResponse)) return;
        if (event.data.payload?.transactionRequest !== transactionRequest) return;
        window.removeEventListener('message', handleMessage);
        if (event.data.payload.transactionResponse.cancel) {
          reject(event.data.payload.transactionResponse);
          return;
        }
        resolve(event.data.payload.transactionResponse);
      };
      window.addEventListener('message', handleMessage);
    });
  },

Also there are two scenarios I noticed where the callback isn't fired:

  • Logged out state where you need to enter your password
  • When signed in, but there are async actions running and you close before they finish

We need to find an earlier way to set up the close listener, even before the transaction page.

@fbwoolf fbwoolf changed the title feat: fire event when user cancels tx feat: support onCancel May 18, 2021
@vercel vercel bot temporarily deployed to Preview May 19, 2021 00:56 Inactive
package.json Outdated Show resolved Hide resolved
window.addEventListener('beforeunload', handleUnmount);
return () => window.removeEventListener('beforeunload', handleUnmount);
}, [handleUnmount]);

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 only component I found during auth that doesn't use the PopupContainer so not great to have duplicate code here. Can this be wrapped in the PopupContainer? Why is this a Screen?

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.

👍🏼 from me, can make other changes later. Per comment, as some of these decisions are temporary, a comment wouldn't go a miss.

src/components/popup/container.tsx Show resolved Hide resolved
@fbwoolf fbwoolf marked this pull request as ready for review May 19, 2021 13:56
@fbwoolf
Copy link
Contributor Author

fbwoolf commented May 19, 2021

Set up connect and the extension this morning and ran through these changes.

I think it'd be better to reject the promise when the user cancels, rather than altering the resolve data. This is a small change, and just requires rejecting in the inpage.ts file

  transactionRequest: async transactionRequest => {
    const event = new CustomEvent<TransactionRequestEventDetails>(DomEventName.transactionRequest, {
      detail: { transactionRequest },
    });
    document.dispatchEvent(event);
    return new Promise((resolve, reject) => {
      const handleMessage = (event: MessageEvent<TransactionResponseMessage>) => {
        console.log('message in inpage.ts', event);
        if (!isValidEvent(event, Methods.transactionResponse)) return;
        if (event.data.payload?.transactionRequest !== transactionRequest) return;
        window.removeEventListener('message', handleMessage);
        if (event.data.payload.transactionResponse.cancel) {
          reject(event.data.payload.transactionResponse);
          return;
        }
        resolve(event.data.payload.transactionResponse);
      };
      window.addEventListener('message', handleMessage);
    });
  },

Also there are two scenarios I noticed where the callback isn't fired:

  • Logged out state where you need to enter your password
  • When signed in, but there are async actions running and you close before they finish

We need to find an earlier way to set up the close listener, even before the transaction page.

Just circled back and remembered this, maybe I should use this approach at least for now and change the conditional check in connect to just catching the failed promise?

@vercel vercel bot temporarily deployed to Preview May 19, 2021 17:34 Inactive
@vercel vercel bot temporarily deployed to Preview May 19, 2021 17:43 Inactive
@vercel vercel bot temporarily deployed to Preview May 19, 2021 18:01 Inactive
@fbwoolf
Copy link
Contributor Author

fbwoolf commented May 20, 2021

Tests are going to fail here until it points at the changes in connect PR.

@vercel vercel bot temporarily deployed to Preview May 24, 2021 18:10 Inactive
@vercel vercel bot temporarily deployed to Preview May 25, 2021 18:03 Inactive
@vercel vercel bot temporarily deployed to Preview May 25, 2021 18:24 Inactive
@vercel vercel bot temporarily deployed to Preview May 25, 2021 20:13 Inactive
@aulneau aulneau merged commit 09c9b85 into main May 25, 2021
@kyranjamie kyranjamie deleted the feat/cancel-transaction branch June 14, 2021 12:27
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.

Ability to detect if a users cancels / closes a transaction window
3 participants