@@ -6,6 +6,7 @@ import { getImagesGalery } from '@/src/widgets/messages' import { useMediaQuery } from '@mui/material' import { useSession } from 'next-auth/react' import { useRef, useState } from 'react' +import { Limit, LimitSize } from '../types' export function useImageBotPagination(deviceType: Device) { const refScrollMobile = useRef(null) @@ -13,8 +14,6 @@ export function useImageBotPagination(deviceType: Device) { const mobileScrollContainer = useRef(null) const offset = useRef(0) - const query = useMediaQuery('(min-height: 600px)') - const [messages, setMessages] = useState([]) const [loading, setLoading] = useState(false) @@ -23,6 +22,24 @@ export function useImageBotPagination(deviceType: Device) { const { data } = useSession() + const limits: Record = { + small: { + active: useMediaQuery('(max-height: 600px)'), + limit: 20, + firstLimit: 30, + }, + medium: { + active: useMediaQuery('(min-height: 600px) and (max-height: 900px)'), + limit: 30, + firstLimit: 50, + }, + large: { + active: useMediaQuery('(min-height: 900px)'), + limit: 45, + firstLimit: 70, + }, + } + const fetchMessages = async (count?: number) => { if (!data) return @@ -51,9 +68,11 @@ export function useImageBotPagination(deviceType: Device) { if (!entries[0].isIntersecting) return if (deviceType === 'desktop') { - if (query && offset.current === 0) return fetchMessages(20) + const active = Object.values(limits).find((item) => item.active) + + if (offset.current > 0) return fetchMessages(active?.limit) - fetchMessages() + fetchMessages(active?.firstLimit) } if (!mobileScrollContainer.current) return @@ -0,0 +1 @@ +export * from './limits' \ No newline at end of file @@ -0,0 +1,7 @@ +export type LimitSize = 'small' | 'medium' | 'large' + +export interface Limit { + active: boolean + limit: number + firstLimit: number +} \ No newline at end of file @@ -1 +1,2 @@ -export * from './model' \ No newline at end of file +export * from './model' +export * from './types' \ No newline at end of file @@ -79,7 +79,7 @@ const Images: React.FC = ({ deviceType, deviceOs }) => { onObserverMounted, setLoading, setMessages, - fetchMessages, + fetchMessages, loading, messages, } = useImageBotPagination(deviceType) @@ -170,7 +170,8 @@ const Images: React.FC = ({ deviceType, deviceOs }) => {