From a0a661786ce24f6ecb91139f37e8ed30eba35e8a Mon Sep 17 00:00:00 2001 From: Vincenzo Scamporlino Date: Wed, 25 Dec 2024 22:54:29 +0100 Subject: [PATCH 1/2] catalog-react: do not refetch if filters don't change Signed-off-by: Vincenzo Scamporlino --- .../src/hooks/useEntityListProvider.test.tsx | 18 +++++++++++++++--- .../src/hooks/useEntityListProvider.tsx | 7 ++++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/plugins/catalog-react/src/hooks/useEntityListProvider.test.tsx b/plugins/catalog-react/src/hooks/useEntityListProvider.test.tsx index 8701c56301..2a922cf417 100644 --- a/plugins/catalog-react/src/hooks/useEntityListProvider.test.tsx +++ b/plugins/catalog-react/src/hooks/useEntityListProvider.test.tsx @@ -555,7 +555,7 @@ describe('', () => { }); }); -describe('', () => { +describe(``, () => { const origReplaceState = window.history.replaceState; const pagination: EntityListPagination = { mode: 'offset' }; const limit = 20; @@ -688,6 +688,18 @@ describe('', () => { await waitFor(() => { expect(mockCatalogApi.queryEntities).toHaveBeenCalledTimes(2); }); + + act(() => + result.current.updateFilters({ + user: EntityUserFilter.owned(ownershipEntityRefs), + }), + ); + + await expect(() => + waitFor(() => { + expect(mockCatalogApi.queryEntities).toHaveBeenCalledTimes(3); + }), + ).rejects.toThrow(); }); it('fetch when limit change', async () => { @@ -723,7 +735,7 @@ describe('', () => { expect(result.current.backendEntities.length).toBe(2); expect(mockCatalogApi.queryEntities).toHaveBeenCalledTimes(1); - await act(async () => { + act(() => { result.current.updateFilters({ kind: new EntityKindFilter('api', 'API'), }); @@ -750,7 +762,7 @@ describe('', () => { expect(result.current.backendEntities.length).toBe(2); expect(mockCatalogApi.queryEntities).toHaveBeenCalledTimes(1); - await act(async () => { + act(() => { result.current.setOffset!(5); result.current.setOffset!(10); }); diff --git a/plugins/catalog-react/src/hooks/useEntityListProvider.tsx b/plugins/catalog-react/src/hooks/useEntityListProvider.tsx index b96357eb62..fc2dfe6a73 100644 --- a/plugins/catalog-react/src/hooks/useEntityListProvider.tsx +++ b/plugins/catalog-react/src/hooks/useEntityListProvider.tsx @@ -173,7 +173,7 @@ export const EntityListProvider = ( : 'none'; }; - const paginationMode: PaginationMode = getPaginationMode(); + const paginationMode = getPaginationMode(); const paginationLimit = typeof props.pagination === 'object' ? props.pagination.limit ?? 20 : 20; @@ -227,7 +227,7 @@ export const EntityListProvider = ( appliedFilters: {} as EntityFilters, entities: [], backendEntities: [], - pageInfo: paginationMode === 'cursor' ? {} : undefined, + pageInfo: {}, offset, limit, }; @@ -279,7 +279,8 @@ export const EntityListProvider = ( ); if ( - paginationMode === 'offset' || + (paginationMode === 'offset' && + (outputState.limit !== limit || outputState.offset !== offset)) || !isEqual(previousBackendFilter, backendFilter) ) { const response = await catalogApi.queryEntities({ From cbfc0a4eec81bb725ced4104594c99e18496494c Mon Sep 17 00:00:00 2001 From: Vincenzo Scamporlino Date: Wed, 25 Dec 2024 22:54:50 +0100 Subject: [PATCH 2/2] catalog-react: fix pagination changeset Signed-off-by: Vincenzo Scamporlino --- .changeset/nasty-pears-taste.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/nasty-pears-taste.md diff --git a/.changeset/nasty-pears-taste.md b/.changeset/nasty-pears-taste.md new file mode 100644 index 0000000000..667e85d9d4 --- /dev/null +++ b/.changeset/nasty-pears-taste.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-react': patch +--- + +Fixed an issue where the `` in `offset` mode would unnecessarily re-fetch data when the filter didn't change, causing a flicker effect.