-
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 all commits
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,38 @@ | ||
| 'use client'; | ||
|
|
||
| import React from 'react'; | ||
| import Link from 'next/link'; | ||
| import { useState } from 'react'; | ||
| import { FaPlus } from 'react-icons/fa'; | ||
|
|
||
| import { Button } from '~/components/buttons'; | ||
| import { DynamicLazy } from '~/components/lazy/DynamicLazy'; | ||
| import { | ||
|
Comment on lines
7
to
10
|
||
| deleteTenderAction, | ||
| type TenderWithStatus, | ||
| } from '~/server/actions/tenders'; | ||
| import type { TendersTranslations } from '~/i18n/translate/tenders'; | ||
|
|
||
| import { TendersList } from './TendersList'; | ||
| type LazyTendersListProps = React.JSX.IntrinsicAttributes & { | ||
| tenders: TenderWithStatus[]; | ||
| locale: string; | ||
| canManage: boolean; | ||
| text: TendersTranslations; | ||
| isArchived: boolean; | ||
| onDelete?: (id: number) => Promise<void>; | ||
| deletingId?: number | null; | ||
| }; | ||
|
|
||
| const LazyTendersList = DynamicLazy<LazyTendersListProps>( | ||
| () => import('./TendersList').then((mod) => ({ default: mod.default })), | ||
| { | ||
| fallback: ( | ||
| <div className="rounded-lg border border-neutral-200 bg-neutral-50 p-8 text-center text-neutral-600"> | ||
| Loading tenders... | ||
| </div> | ||
| ), | ||
| } | ||
| ); | ||
|
|
||
| interface TendersPageClientProps { | ||
| allTenders: TenderWithStatus[]; | ||
|
|
@@ -41,7 +62,6 @@ export function TendersPageClient({ | |
| // Handle delete - updates state for both tabs | ||
| const handleDelete = async (id: number) => { | ||
| if (!confirm(text.confirmDelete)) return; | ||
|
|
||
| setDeletingId(id); | ||
| try { | ||
| const result = await deleteTenderAction(id); | ||
|
|
@@ -78,6 +98,8 @@ export function TendersPageClient({ | |
| <div className="mb-6 flex gap-2"> | ||
| <button | ||
| onClick={() => setActiveTab('live')} | ||
| onMouseEnter={() => void LazyTendersList.preload()} | ||
| onFocus={() => void LazyTendersList.preload()} | ||
| className={`rounded-full px-4 py-2 text-sm font-medium transition-colors ${ | ||
| activeTab === 'live' | ||
| ? 'text-white bg-primary-700' | ||
|
|
@@ -88,6 +110,8 @@ export function TendersPageClient({ | |
| </button> | ||
| <button | ||
| onClick={() => setActiveTab('archived')} | ||
| onMouseEnter={() => void LazyTendersList.preload()} | ||
| onFocus={() => void LazyTendersList.preload()} | ||
| className={`rounded-full px-4 py-2 text-sm font-medium transition-colors ${ | ||
| activeTab === 'archived' | ||
| ? 'text-white bg-primary-700' | ||
|
|
@@ -99,7 +123,7 @@ export function TendersPageClient({ | |
| </div> | ||
|
|
||
| {/* Tenders List */} | ||
| <TendersList | ||
| <LazyTendersList | ||
| tenders={displayedTenders} | ||
| locale={locale} | ||
| canManage={canManage} | ||
|
|
||
| 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.