Skip to content

Render an empty div for game cards until they inter the viewport #2460

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

Merged
merged 4 commits into from
Feb 19, 2023

Conversation

arielj
Copy link
Collaborator

@arielj arielj commented Feb 17, 2023

This is an alternative to #2447

Instead of pagination, it renders all the library elements but for the ones that are outside of the viewport it renders an empty div until the element has to be displayed. Then it uses the IntersectionObserver to actually render the real content.

I'll add some comments in the PR.

There are still improvements that can be made (because of how react works, all hooks has to be defined before rendering the empty div, so there's extra unnecessary work there, also cards are re-rendered multiple times) but I'll leave those things for another PR.

You can see the empty divs using devtools and how they are updated when scrolling:
image


Use the following Checklist if you have changed something on the Backend or Frontend:

  • Tested the feature and it's working on a current and clean install.
  • Tested the main App features and they are still working on a current and clean install. (Login, Install, Play, Uninstall, Move games, etc.)
  • Created / Updated Tests (If necessary)
  • Created / Updated documentation (If necessary)

@arielj arielj added the pr:ready-for-review Feature-complete, ready for the grind! :P label Feb 17, 2023
@arielj arielj requested review from a team, flavioislima, CommandMC, Nocccer, imLinguin and redromnon and removed request for a team February 17, 2023 03:41
}

.gameCard.gamepad {
aspect-ratio: 3/4;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

these are the current ratios, but I'm hardcoding them so the empty divs use the correct space

@@ -63,6 +63,24 @@ const GameCard = ({
isRecent = false,
gameInfo: gameInfoFromProps
}: Card) => {
const [visible, setVisible] = useState(false)

useEffect(() => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

start as invisible, make visible when the visible-cards event is fired and this card is in the list of visible cards

@@ -344,7 +372,7 @@ const GameCard = ({
/>
)}
<ContextMenu items={items}>
<div className={wrapperClasses}>
<div className={wrapperClasses} data-app-name={appName}>
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

using this data attribute for the intersection observer

@arielj arielj added pr:wip WIP, don't merge. and removed pr:ready-for-review Feature-complete, ready for the grind! :P labels Feb 17, 2023
const { t } = useTranslation()
const [gameCards, setGameCards] = useState<JSX.Element[]>([])
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this useState and the useEffect changing it was causing extra re-renders

I moved this back to the return jsx instead of using a useState+useEffect

@arielj arielj added pr:ready-for-review Feature-complete, ready for the grind! :P and removed pr:wip WIP, don't merge. labels Feb 17, 2023
@flavioislima flavioislima added the pr:testing This PR is in testing, don't merge. label Feb 17, 2023
Copy link
Member

@flavioislima flavioislima left a comment

Choose a reason for hiding this comment

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

Lgtm

@arielj arielj merged commit 7096260 into main Feb 19, 2023
@arielj arielj deleted the defer-card-render branch February 19, 2023 23:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr:ready-for-review Feature-complete, ready for the grind! :P pr:testing This PR is in testing, don't merge.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants