diff --git a/webapp/packages/core-blocks/src/ResourcesHooks/useOffsetPagination.ts b/webapp/packages/core-blocks/src/ResourcesHooks/useOffsetPagination.ts index fd41e8c75a..3be5e5f09b 100644 --- a/webapp/packages/core-blocks/src/ResourcesHooks/useOffsetPagination.ts +++ b/webapp/packages/core-blocks/src/ResourcesHooks/useOffsetPagination.ts @@ -59,7 +59,7 @@ export function useOffsetPagination { this.offsetPagination.setPage( isPageListKey - ? CachedResourceOffsetPageListKey(offset, limit).setParent(parentKey || CachedResourceOffsetPageTargetKey(nodeId)) - : CachedResourceOffsetPageKey(offset, limit).setParent(parentKey || CachedResourceOffsetPageTargetKey(nodeId)), + ? CachedResourceOffsetPageListKey(offset, keys.length).setParent(parentKey || CachedResourceOffsetPageTargetKey(nodeId)) + : CachedResourceOffsetPageKey(offset, keys.length).setParent(parentKey || CachedResourceOffsetPageTargetKey(nodeId)), keys, keys.length === limit, ); diff --git a/webapp/packages/core-resource/src/Resource/CachedResourceOffsetPageKeys.test.ts b/webapp/packages/core-resource/src/Resource/CachedResourceOffsetPageKeys.test.ts index 7ae7721459..4c9a932d61 100644 --- a/webapp/packages/core-resource/src/Resource/CachedResourceOffsetPageKeys.test.ts +++ b/webapp/packages/core-resource/src/Resource/CachedResourceOffsetPageKeys.test.ts @@ -7,7 +7,7 @@ */ import { describe, expect, test } from '@jest/globals'; -import { expandOffsetPageRange } from './CachedResourceOffsetPageKeys.js'; +import { expandOffsetPageRange, getNextPageOffset, type ICachedResourceOffsetPage } from './CachedResourceOffsetPageKeys.js'; import type { IResourceOffsetPage } from './OffsetPagination/IResourceOffsetPage.js'; import { ResourceOffsetPage } from './OffsetPagination/ResourceOffsetPage.js'; @@ -108,6 +108,49 @@ describe('CachedResourceOffsetPageKeys', () => { expect(pages).toStrictEqual(initialPages); }); }); + + describe('getNextPageOffset', () => { + test('should return next page offset', () => { + const randomPage = getRandomPage(0, 100, false); + const pageInfo: ICachedResourceOffsetPage = { + pages: [randomPage], + }; + expect(getNextPageOffset(pageInfo)).toBe(100); + }); + test('should return next page offset with multiple pages', () => { + const pages = []; + for (let i = 0; i < 10; i++) { + pages.push(getRandomPage(i * 100, 100, false)); + } + const pageInfo: ICachedResourceOffsetPage = { + pages, + }; + expect(getNextPageOffset(pageInfo)).toBe(1000); + }); + test('should return next page offset with multiple pages with gaps', () => { + const pages = []; + for (let i = 0; i < 10; i++) { + pages.push(getRandomPage(i * 100, 100, false)); + } + pages.push(getRandomPage(11 * 100, 100, false)); + const pageInfo: ICachedResourceOffsetPage = { + pages, + }; + expect(getNextPageOffset(pageInfo)).toBe(1000); + }); + test('should return next page offset with end', () => { + const pages = []; + for (let i = 0; i < 10; i++) { + pages.push(getRandomPage(i * 100, 100, false)); + } + pages.push(getRandomPage(10 * 100, 20, false)); + const pageInfo: ICachedResourceOffsetPage = { + end: 1020, + pages, + }; + expect(getNextPageOffset(pageInfo)).toBe(1020); + }); + }); }); function getRandomPage(offset: number, limit: number, outdate: boolean): IResourceOffsetPage { diff --git a/webapp/packages/core-resource/src/Resource/CachedResourceOffsetPageKeys.ts b/webapp/packages/core-resource/src/Resource/CachedResourceOffsetPageKeys.ts index b08998962a..aecedd20a9 100644 --- a/webapp/packages/core-resource/src/Resource/CachedResourceOffsetPageKeys.ts +++ b/webapp/packages/core-resource/src/Resource/CachedResourceOffsetPageKeys.ts @@ -60,7 +60,7 @@ export function getNextPageOffset(info: ICachedResourceOffsetPage): number { lastPage = page; } - return lastPage?.to ?? CACHED_RESOURCE_DEFAULT_PAGE_OFFSET; + return Math.min(info.end ?? Number.MAX_SAFE_INTEGER, lastPage?.to ?? CACHED_RESOURCE_DEFAULT_PAGE_OFFSET); } export function isOffsetPageOutdated(pages: IResourceOffsetPage[], info: IOffsetPageInfo): boolean { diff --git a/webapp/packages/core-resource/src/Resource/ResourceOffsetPagination.ts b/webapp/packages/core-resource/src/Resource/ResourceOffsetPagination.ts index c73e28a7fd..df0979bb7b 100644 --- a/webapp/packages/core-resource/src/Resource/ResourceOffsetPagination.ts +++ b/webapp/packages/core-resource/src/Resource/ResourceOffsetPagination.ts @@ -41,17 +41,17 @@ export class ResourceOffsetPagination>, items: any[], hasNextPage: boolean) { const offset = key.options.offset; - const limit = offset + key.options.limit; + const pageEnd = offset + items.length; this.metadata.update(key as TKey, metadata => { let end = metadata.offsetPage?.end; if (hasNextPage) { - if (end !== undefined && end <= limit) { + if (end !== undefined && end <= pageEnd) { end = undefined; } } else { - end = limit; + end = pageEnd; } if (!metadata.offsetPage) {