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: optimize Banner Performance with Lazy Loading #3351

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions components/campaigns/AnnouncementHero.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ export default function AnnouncementHero({ className = '', small = false }: IAnn
)}
<div className='relative flex w-5/6 flex-col items-center justify-center gap-2'>
<div className='relative flex min-h-72 w-full items-center justify-center overflow-hidden lg:h-[17rem] lg:w-[38rem]'>
{visibleBanners.map((banner, index) => (
{visibleBanners.map((banner, index) => {
// Only render active banner and immediate neighbors
const isVisible = Math.abs(index - (activeIndex % numberOfVisibleBanners)) <= 1;
if (!isVisible) return null;
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve code readability with a dedicated visibility check function

The visibility calculation logic could be more maintainable and self-documenting.

Consider extracting the visibility logic:

+ const isBannerVisible = (index: number, activeIndex: number, total: number) => {
+   const normalizedIndex = activeIndex % total;
+   return Math.abs(index - normalizedIndex) <= 1;
+ };

  {visibleBanners.map((banner, index) => {
-   const isVisible = Math.abs(index - (activeIndex % numberOfVisibleBanners)) <= 1;
+   const isVisible = isBannerVisible(index, activeIndex, numberOfVisibleBanners);
    if (!isVisible) return null;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Only render active banner and immediate neighbors
const isVisible = Math.abs(index - (activeIndex % numberOfVisibleBanners)) <= 1;
if (!isVisible) return null;
const isBannerVisible = (index: number, activeIndex: number, total: number) => {
const normalizedIndex = activeIndex % total;
return Math.abs(index - normalizedIndex) <= 1;
};
// Only render active banner and immediate neighbors
const isVisible = isBannerVisible(index, activeIndex, numberOfVisibleBanners);
if (!isVisible) return null;

return(
<Banner
key={index}
title={banner.title}
Expand All @@ -76,7 +80,8 @@ export default function AnnouncementHero({ className = '', small = false }: IAnn
className={className}
small={small}
/>
))}
);
})}
Manancode marked this conversation as resolved.
Show resolved Hide resolved
</div>
<div className='m-auto flex justify-center'>
{visibleBanners.map((banner, index) => (
Expand Down
Loading