-
Notifications
You must be signed in to change notification settings - Fork 19
tenders lazy-loading #601
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
base: staged
Are you sure you want to change the base?
tenders lazy-loading #601
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,17 +1,19 @@ | ||||||||||||
| 'use client'; | ||||||||||||
|
|
||||||||||||
| import React, { Suspense } from 'react'; | ||||||||||||
| import Link from 'next/link'; | ||||||||||||
| import { useState } from 'react'; | ||||||||||||
|
||||||||||||
| import React, { Suspense } from 'react'; | |
| import Link from 'next/link'; | |
| import { useState } from 'react'; | |
| import { Suspense, useState } from 'react'; | |
| import Link from 'next/link'; |
Copilot
AI
Apr 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the // Removed stray comment note—this kind of meta-comment adds noise and isn’t useful for future readers.
Copilot
AI
Apr 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the commented-out Link import and the note about removing it. Commented-out code and meta-notes like this tend to rot and make the file harder to scan.
Copilot
AI
Apr 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<Suspense> won’t provide lazy-loading here because TendersList is imported synchronously and doesn’t suspend. If the intent is lazy-loading, switch TendersList to a React.lazy(() => import('./TendersList'))/Next dynamic() import; otherwise drop the Suspense wrapper.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import React, { | ||
| type ComponentType, | ||
| lazy, | ||
| type LazyExoticComponent, | ||
| Suspense, | ||
| } from 'react'; | ||
|
|
||
| export interface DynamicLazyOptions { | ||
| fallback?: React.ReactNode; | ||
| } | ||
|
|
||
| export interface DynamicLazyComponent<P> { | ||
| (props: P): JSX.Element; | ||
| preload: () => Promise<void>; | ||
| } | ||
|
|
||
| export function DynamicLazy<P extends React.JSX.IntrinsicAttributes>( | ||
| loader: () => Promise<{ default: React.ComponentType<P> }>, | ||
| options?: DynamicLazyOptions | ||
| ): DynamicLazyComponent<P> { | ||
| let LoadedComponent: LazyExoticComponent<ComponentType<P>> | null = null; | ||
| let loadPromise: Promise<void> | null = null; | ||
|
|
||
| function preload(): Promise<void> { | ||
| if (!loadPromise) { | ||
| loadPromise = loader().then((mod) => { | ||
| LoadedComponent = lazy(() => Promise.resolve(mod)); | ||
| }); | ||
| } | ||
| return loadPromise ?? Promise.resolve(); | ||
| } | ||
|
|
||
| const LazyComponent = (props: P) => { | ||
| if (!LoadedComponent) { | ||
| LoadedComponent = lazy(loader); | ||
| } | ||
| const Component = LoadedComponent; | ||
| return ( | ||
| <Suspense fallback={options?.fallback ?? null}> | ||
| <Component {...(props as React.PropsWithRef<P>)} /> | ||
| </Suspense> | ||
| ); | ||
| }; | ||
| (LazyComponent as DynamicLazyComponent<P>).preload = preload; | ||
| return LazyComponent as DynamicLazyComponent<P>; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import React, { useCallback, useEffect, useRef, useState } from 'react'; | ||
|
|
||
| import { useIntersectionObserver } from './useIntersectionObserver'; | ||
| import { useIdleCallback } from './useIdleCallback'; | ||
|
|
||
| export type LazyLoadTrigger = 'viewport' | 'interaction' | 'immediate'; | ||
|
|
||
| export interface LazyLoadBoundaryProps { | ||
| trigger?: LazyLoadTrigger; | ||
| fallback?: React.ReactNode; | ||
| children: React.ReactNode; | ||
| rootMargin?: string; | ||
| threshold?: number; | ||
| preloadOnHover?: boolean; | ||
| preloadOnIdle?: boolean; | ||
| keepMounted?: boolean; | ||
| onPreload?: () => void; | ||
| } | ||
|
|
||
| export const LazyLoadBoundary: React.FC<LazyLoadBoundaryProps> = ({ | ||
| trigger = 'viewport', | ||
| fallback = null, | ||
| children, | ||
| rootMargin = '0px', | ||
| threshold = 0, | ||
| preloadOnHover = false, | ||
| preloadOnIdle = false, | ||
| keepMounted = false, | ||
| onPreload, | ||
| }) => { | ||
| const [mounted, setMounted] = useState(trigger === 'immediate'); | ||
| const [hasInteracted, setHasInteracted] = useState(false); | ||
| const [ref, inView] = useIntersectionObserver({ rootMargin, threshold }); | ||
| const hasMountedRef = useRef<boolean>(false); | ||
|
|
||
| // Preload on hover | ||
| const handleMouseEnter = useCallback(() => { | ||
| if (preloadOnHover && onPreload) onPreload(); | ||
| if (trigger === 'interaction') setHasInteracted(true); | ||
| }, [preloadOnHover, onPreload, trigger]); | ||
|
|
||
| // Preload on idle | ||
| useIdleCallback(() => { | ||
| if (preloadOnIdle && onPreload) onPreload(); | ||
| }, [preloadOnIdle, onPreload]); | ||
|
|
||
| // Mount logic | ||
| useEffect(() => { | ||
| if (trigger === 'immediate') { | ||
| setMounted(true); | ||
| } else if (trigger === 'viewport' && inView) { | ||
| setMounted(true); | ||
| } else if (trigger === 'interaction' && hasInteracted) { | ||
| setMounted(true); | ||
| } | ||
| }, [trigger, inView, hasInteracted]); | ||
|
|
||
| // Keep mounted | ||
| useEffect(() => { | ||
| if (mounted && keepMounted) { | ||
| hasMountedRef.current = true; | ||
| } | ||
| }, [mounted, keepMounted]); | ||
|
|
||
| // Clean up interaction state if not keepMounted | ||
| useEffect(() => { | ||
| return () => { | ||
| if (!keepMounted) { | ||
| setMounted(false); | ||
| setHasInteracted(false); | ||
| } | ||
| }; | ||
| }, [keepMounted]); | ||
|
|
||
| // Render logic | ||
| if (mounted || hasMountedRef.current) { | ||
| return ( | ||
| <div | ||
| ref={ | ||
| trigger === 'viewport' | ||
| ? (ref as React.RefObject<HTMLDivElement>) | ||
| : undefined | ||
| } | ||
| > | ||
| {children} | ||
| </div> | ||
| ); | ||
| } | ||
| return ( | ||
| <div | ||
| ref={ | ||
| trigger === 'viewport' | ||
| ? (ref as React.RefObject<HTMLDivElement>) | ||
| : undefined | ||
| } | ||
| onClick={ | ||
| trigger === 'interaction' ? () => setHasInteracted(true) : undefined | ||
| } | ||
| onMouseEnter={handleMouseEnter} | ||
| style={{ width: '100%', height: '100%' }} | ||
| > | ||
| {fallback} | ||
| </div> | ||
| ); | ||
| }; |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,57 @@ | ||||
| import React from 'react'; | ||||
|
|
||||
| import type { CarouselProps } from '~/components/carousels'; | ||||
|
|
||||
| // import type { CarouselProps } from '~/components/carousels'; | ||||
|
||||
| // import type { CarouselProps } from '~/components/carousels'; |
Copilot
AI
Apr 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GalleryBelowFold is declared but never used, which will trigger @typescript-eslint/no-unused-vars warnings. Either use it in an exported example component or remove the unused constant (and any now-unused types/imports).
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { useEffect } from 'react'; | ||
|
|
||
| interface WindowWithIdleCallback extends Window { | ||
| requestIdleCallback: (callback: IdleRequestCallback) => number; | ||
| cancelIdleCallback: (id: number) => void; | ||
| setTimeout: (callback: () => void, ms: number) => number; | ||
| clearTimeout: (id: number) => void; | ||
| } | ||
|
|
||
| export function useIdleCallback( | ||
| cb: () => void, | ||
| _deps: React.DependencyList = [] | ||
| ) { | ||
|
Comment on lines
+10
to
+13
|
||
| useEffect(() => { | ||
| let id: number | null = null; | ||
| if ('requestIdleCallback' in window) { | ||
| id = (window as WindowWithIdleCallback).requestIdleCallback(cb); | ||
| } else { | ||
| id = (window as WindowWithIdleCallback).setTimeout(cb, 200); | ||
| } | ||
| return () => { | ||
| if (id !== null) { | ||
| if ('cancelIdleCallback' in window) { | ||
| (window as WindowWithIdleCallback).cancelIdleCallback(id); | ||
| } else { | ||
| clearTimeout(id); | ||
| } | ||
| } | ||
| }; | ||
| }, [cb]); | ||
| } | ||
|
Comment on lines
+27
to
+31
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,41 @@ | ||||||||||||||||||||||||||||||||||
| import { useEffect, useRef, useState } from 'react'; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| export interface UseIntersectionObserverOptions { | ||||||||||||||||||||||||||||||||||
| root?: Element | null; | ||||||||||||||||||||||||||||||||||
| rootMargin?: string; | ||||||||||||||||||||||||||||||||||
| threshold?: number | number[]; | ||||||||||||||||||||||||||||||||||
| freezeOnceVisible?: boolean; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| export function useIntersectionObserver<T extends Element>( | ||||||||||||||||||||||||||||||||||
| options: UseIntersectionObserverOptions = {} | ||||||||||||||||||||||||||||||||||
| ): [React.RefObject<T>, boolean] { | ||||||||||||||||||||||||||||||||||
| const { | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+10
to
+13
|
||||||||||||||||||||||||||||||||||
| root = null, | ||||||||||||||||||||||||||||||||||
| rootMargin = '0px', | ||||||||||||||||||||||||||||||||||
| threshold = 0, | ||||||||||||||||||||||||||||||||||
| freezeOnceVisible = true, | ||||||||||||||||||||||||||||||||||
| } = options; | ||||||||||||||||||||||||||||||||||
| const ref = useRef<T>(null); | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+12
to
+19
|
||||||||||||||||||||||||||||||||||
| ): [React.RefObject<T>, boolean] { | |
| const { | |
| root = null, | |
| rootMargin = '0px', | |
| threshold = 0, | |
| freezeOnceVisible = true, | |
| } = options; | |
| const ref = useRef<T>(null); | |
| ): [React.RefObject<T | null>, boolean] { | |
| const { | |
| root = null, | |
| rootMargin = '0px', | |
| threshold = 0, | |
| freezeOnceVisible = true, | |
| } = options; | |
| const ref = useRef<T | null>(null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export default TendersList;appears beforeTendersListis declared, which is a compile-time error. If you want a default export, export the function as default (e.g.,export default function TendersList(...)) or move the default export to after the function declaration.