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: sync chains with url #398

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

feat: sync chains with url #398

wants to merge 7 commits into from

Conversation

Xaroz
Copy link
Contributor

@Xaroz Xaroz commented Jan 23, 2025

  • Sync chains select with URL so that there is persistence
  • Sync token with URL for persistence
  • Added util functions to handle special cases so that app does not crash

fixes #397

@Xaroz Xaroz requested review from jmrossy and nambrot as code owners January 23, 2025 19:38
Copy link

vercel bot commented Jan 23, 2025

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

Name Status Preview Comments Updated (UTC)
hyperlane-warp-template ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 26, 2025 7:40pm
5 Skipped Deployments
Name Status Preview Comments Updated (UTC)
injective-bridge ⬜️ Ignored (Inspect) Jan 26, 2025 7:40pm
nautilus-bridge ⬜️ Ignored (Inspect) Jan 26, 2025 7:40pm
nexus-bridge ⬜️ Ignored (Inspect) Jan 26, 2025 7:40pm
renzo-bridge ⬜️ Ignored (Inspect) Visit Preview Jan 26, 2025 7:40pm
trump-bridge ⬜️ Ignored (Inspect) Visit Preview Jan 26, 2025 7:40pm

return useMemo(() => {
const firstToken = warpCore.tokens[0];
const connectedToken = firstToken.connections?.[0];

if (!isReady) return null;
Copy link
Contributor Author

@Xaroz Xaroz Jan 23, 2025

Choose a reason for hiding this comment

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

router needs to be ready before we can actually check if any of the URL queries are available, if this is not done, it will override the URL query using the fallback

Copy link
Collaborator

Choose a reason for hiding this comment

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

Just curious, why does it need to be ready? Does the URL not have the params in it regardless of the router status?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also what happens if you return the default values here instead of null (i.e. the ones that we use when the param isn't set)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed dependency from next router and instead default to browser native URL handling

Copy link

vercel bot commented Jan 23, 2025

Deployment failed with the following error:

Preview deployments are disabled for this project.

src/features/tokens/TokenSelectField.tsx Outdated Show resolved Hide resolved
src/features/tokens/TokenSelectField.tsx Outdated Show resolved Hide resolved
src/features/tokens/TokenSelectField.tsx Outdated Show resolved Hide resolved
src/features/tokens/TokenSelectField.tsx Outdated Show resolved Hide resolved
return useMemo(() => {
const firstToken = warpCore.tokens[0];
const connectedToken = firstToken.connections?.[0];

if (!isReady) return null;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just curious, why does it need to be ready? Does the URL not have the params in it regardless of the router status?

Copy link
Collaborator

@jmrossy jmrossy left a comment

Choose a reason for hiding this comment

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

Your general approach to solving the problem here is good but there IMO there are some subtle code smells here (see comments). These aren't obvious issues, just subtleties related to code quality and organization that may possibly hurt maintainability in the long run. But don't be discouraged. You'll naturally get better at nuances like naming, organization, and consistency with more experience.

@@ -13,3 +13,9 @@ export const APP_URL = 'hyperlane-warp-template.vercel.app';
export const BRAND_COLOR = Color.primary['500'];
export const BACKGROUND_COLOR = Color.primary['500'];
export const BACKGROUND_IMAGE = 'url(/backgrounds/main.svg)';

export enum WARP_QUERY_PARAMS {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Move this somewhere else. These are consts that we expect people to change to customize the app. Consts for core functionality should probably be somewhere else

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved under a new file inside const called core.ts, I was debating to put it in common.ts too, but felt like it didn't really match what the enum meant to do

/**
* Check if given chainName has valid chain metadata and return chainName if chain is valid
*/
export function getValidChain(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Your former isValidChain was more clear IMO

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason for this change is that I want to make sure to get the chainName or undefined if the chain does not exist, instead of a boolean, but I am happy to make adjustments if needed

handleSwapChain,
}: {
disabled?: boolean;
handleSwapChain(origin: string, destination: string): void;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't see this syntax often. To clarify, is this equivalent to:
handleSwapChain: (origin: string, destination: string)=> void?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also nit but I think onSwapChain would be more consistent with the nomenclature we use elsewhere

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Kinda derped here, fixed

Comment on lines +165 to +166
updateQueryParam(WARP_QUERY_PARAMS.ORIGIN, origin);
updateQueryParam(WARP_QUERY_PARAMS.DESTINATION, destination);
Copy link
Collaborator

Choose a reason for hiding this comment

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

For the TokenSelectField, you handle the query param setting in the component. But for these others, you're doing it in TransferTokenForm. Let's be consistent to keep code cohesion high

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved the logic inside TransferTokenForm for consistency

const handleChange = (chainName: string, fieldName: string) => {
if (fieldName === 'origin') handleGetTokensRoute(chainName, values.destination);
else handleGetTokensRoute(values.origin, chainName);
updateQueryParam(fieldName, chainName);
Copy link
Collaborator

Choose a reason for hiding this comment

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

You've already decided to make a separate WARP_QUERY_PARAMS enum which defines the param name, but here you're assuming that fieldName === WARP_QUERY_PARAMS. If you can assume they're always the same, remove WARP_QUERY_PARAMS. If you can't, use WARP_QUERY_PARAMS

Copy link
Contributor Author

@Xaroz Xaroz Jan 26, 2025

Choose a reason for hiding this comment

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

done, did a check for fieldName === WARP_QUERY_PARAMS

src/features/transfer/TransferTokenForm.tsx Outdated Show resolved Hide resolved
@@ -449,17 +492,36 @@ function WarningBanners() {

function useFormInitialValues(): TransferFormValues {
const warpCore = useWarpCore();
const parameters = new URLSearchParams(window.location.search);
Copy link
Collaborator

@jmrossy jmrossy Jan 25, 2025

Choose a reason for hiding this comment

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

Consider maybe moving the interactions with URLSearchParams to your query params file since you're already doing that with the update function and it's better to be consistent. When you introduce a layer of abstraction, it's better to always use it as much as possible instead of creating two paths to the same thing.

Copy link
Contributor Author

@Xaroz Xaroz Jan 26, 2025

Choose a reason for hiding this comment

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

created a function called getQueryParams for this, also use this function in the updateQueryParams function

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.

Persist chain/token in url params in Warp UI
2 participants