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(suite/components): improve typing and performance of the VirtualizedList #16592

Merged
merged 4 commits into from
Jan 27, 2025

Conversation

vojtatranta
Copy link
Contributor

@vojtatranta vojtatranta commented Jan 24, 2025

TODO

Add tests for:

  • ✅ types
  • ✅ ref passing if possible

Description

VirtualizedList didn't have the proper generic types passed on due to forwardRef(). The forwardRef() is apparantly needed due to useShadow(). I tried to pass the ref manually, but the shad was broken.
However, with typecast, types work and shadow too. Also, I added few perf improvements and simplify

Changes

  • fixes typing of the VirtualizedList (the generic type hasn't been passed properly due to forwardRef())
  • adds memoization to the main VirtualizedList component, mantaining the type safety
  • separates the inner container to leverage the memoization better
  • mantains "scroll shadow" working around, at cost of type cast though

Screenshots:

  • well, there should be literally no visual change :))

Screenshot 2025-01-24 at 3 19 57 PM

@@ -90,7 +90,7 @@ export const SelectAssetModal = ({
badge,
contractAddress,
shouldTryToFetch,
}: AssetProps) => (
}) => (
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No need here anymore, due to generic types work properly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TODO: add type tests 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.

}

// NOTE: typecast here + memo() and forwardRef() because of passing the ref for useShadow()
export const VirtualizedList = memo(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunatelly, memo() and forwardRef() eat the generic. This can be bypassed with typecast, but that kills passing of the ref. I think it should be possible to create some type helper for it, altough I am not sure if it makes sense here to dive that deep.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pls advice, if you have any idea why direct passing of the ref prop doesn't work and I am forced to use the forwardRef again, thanks

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, no idea 🤷‍♂️

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 could have been, it, but it doesnt matter anymore.

onScroll?: (e: Event) => void;
onScrollEnd: () => void;
listHeight: number | string;
listMinHeight: number | string;
ref?: React.Ref<HTMLDivElement>;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunatelly, due to typecast, this needs to be here to prevent TS errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah, forwardRef will be deprecated https://react.dev/reference/react/forwardRef

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, this could have been better solution.

Copy link

github-actions bot commented Jan 24, 2025

🚀 Expo preview is ready!

  • Project → trezor-suite-preview
  • Platforms → android, ios
  • Scheme → trezorsuitelite
  • Runtime Version → 23
  • More info

Learn more about 𝝠 Expo Github Action

@vojtatranta vojtatranta force-pushed the refactor-virtualized-list branch 2 times, most recently from 9e94a54 to b51c7e1 Compare January 24, 2025 15:08
@vojtatranta vojtatranta marked this pull request as ready for review January 24, 2025 15:14
@vojtatranta vojtatranta force-pushed the refactor-virtualized-list branch from b51c7e1 to 1f876c0 Compare January 27, 2025 08:41
@vojtatranta vojtatranta force-pushed the refactor-virtualized-list branch from 1f876c0 to edacb7c Compare January 27, 2025 09:01
@@ -22,6 +24,7 @@
"@suite-common/suite-constants": "workspace:*",
"@suite-common/suite-utils": "workspace:*",
"@suite-common/validators": "workspace:*",
"@testing-library/jest-dom": "^6.6.3",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

for some reason, linter forced me to put it here

Copy link
Contributor

@jvaclavik jvaclavik left a comment

Choose a reason for hiding this comment

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

LGTM and I really like you added tests.

I'm not 100% sure about changes in the logic, but tested and it works fine 👍

}

// NOTE: typecast here + memo() and forwardRef() because of passing the ref for useShadow()
export const VirtualizedList = memo(
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, no idea 🤷‍♂️

@jvaclavik jvaclavik merged commit 307c075 into develop Jan 27, 2025
78 checks passed
@jvaclavik jvaclavik deleted the refactor-virtualized-list branch January 27, 2025 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: 🤝 Needs QA
Development

Successfully merging this pull request may close these issues.

2 participants