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

refactor: swaps routes, closes #4317 #5177

Merged
merged 2 commits into from
Apr 9, 2024
Merged

refactor: swaps routes, closes #4317 #5177

merged 2 commits into from
Apr 9, 2024

Conversation

fbwoolf
Copy link
Contributor

@fbwoolf fbwoolf commented Apr 8, 2024

Try out Leather build 65b7a38Extension build, Test report, Storybook, Chromatic

This PR refactors the swaps routes so that now a user can see swap/:base/:quote as part of the url path. The form/path is loaded with swap/STX/:quote. Also, a user can now type in the route swap/STX/ALEX and the swap form will automatically select those tokens to swap.

To select an asset is: /swap/:base/:quote/select-base or /swap/:base/:quote/select-quote
To review the swap is: /swap/:base/:quote/review

I also did a bunch of renaming here to refactor away from using to and from, instead we are now using base and quote in the codebase naming convention for swaps.

Screenshot 2024-04-08 at 8 44 18 AM

@fbwoolf fbwoolf linked an issue Apr 8, 2024 that may be closed by this pull request
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.

Nice improvement @fbwoolf !

Notice the default state is :quote. Could this instead be blank?

image

@fbwoolf
Copy link
Contributor Author

fbwoolf commented Apr 9, 2024

Notice the default state is :quote. Could this instead be blank?

Let me check this, I bet it is possible.

@@ -69,9 +69,11 @@ export function Container() {
const displayHeader = !isLandingPage(pathname) && !isNoHeaderPopup(pathname);
const isSessionLocked = getIsSessionLocked(pathname);

// TODO: Refactor? This is very hard to manage with dynamic routes. Temporarily
Copy link
Contributor

@pete-watters pete-watters Apr 9, 2024

Choose a reason for hiding this comment

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

I agree, this isn't great. Before we were storing the whole header including callback in the state.

Maybe we could try using the location state to store where it should go back to?

For some of the other dynamic routes I was using match to get to more specific cases.

If we use includes here it also means if you reach the /swap/review page then you go all the way back to the homepage on back and not to the previous page where you are selecting what to swap.

In those cases I think it should go back navigate(-1)

Copy link
Collaborator

@kyranjamie kyranjamie Apr 9, 2024

Choose a reason for hiding this comment

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

Rather than trying to put all the go back logic in the container—which think we all agree isn't a scalable solution—can't we instead find a way to define this in each component tree where there is a back button?

What if the header back button simply publishes a "go back" event, e.g. with PubSub pattern, and components, via a hook listen for this event?

function useHandleGoBack (handler) {
  // listen for go back logic, then fire handler()
}
export function Swap() {
  // nav passed as handler to avoid useNavigate if not needed
  useHandleGoBack(navigate => navigate(RouteUrls.Home))
  // would be nice to make a friendly API for this fn, perhaps could take a callback _or_ a RouteUrl for simple cases
  useHandleGoBack(RouteUrls.Home)

Perhaps not perfect. We'd need to be careful not to have double handlers, and ensure we define goback where necessary, but this demonstrates a way we can completely decouple the logic.

Any other ideas?

Copy link
Collaborator

@kyranjamie kyranjamie Apr 9, 2024

Choose a reason for hiding this comment

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

Opened issue for this here #5181

Copy link
Contributor

Choose a reason for hiding this comment

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

Another idea I have is to have route props that specify it. So we can pass a call back either when loading the component in the routes file, or have it as part of the location state. I'll have a think about it.

Getting the headers out of state was a good start but this logic isn't great so I am open to a full overhaul. If you check in route-helpers.ts, I've had to use routes to apply other logic so it could be good to also improve that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you make an issue to refactor this based on your understanding for what you wrote here and your implementation? I will likely not tackle this here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nvm, my comments hadn't refreshed, sorry that last comment was outdated.

Copy link
Contributor

@pete-watters pete-watters left a comment

Choose a reason for hiding this comment

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

Nice work @fbwoolf . I added some suggestions but the only real thing to fix is the initial :quote in the URL.

I noticed also that on the swap/review screen onBack goes all the way to the home page.

Lets think about how we can improve the handling of onBack in general as I agree the code in container.tsx isn't great and won't scale well

@fbwoolf fbwoolf added this pull request to the merge queue Apr 9, 2024
Merged via the queue into dev with commit d927ec0 Apr 9, 2024
29 checks passed
@fbwoolf fbwoolf deleted the refactor/swap-routes branch April 9, 2024 18:28
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.

Store swap asset info in routing
3 participants