diff --git a/packages/twenty-front/src/modules/action-menu/actions/record-actions/multiple-records/hooks/useDeleteMultipleRecordsAction.tsx b/packages/twenty-front/src/modules/action-menu/actions/record-actions/multiple-records/hooks/useDeleteMultipleRecordsAction.tsx index 5e517ace1346..7e2c5a405c30 100644 --- a/packages/twenty-front/src/modules/action-menu/actions/record-actions/multiple-records/hooks/useDeleteMultipleRecordsAction.tsx +++ b/packages/twenty-front/src/modules/action-menu/actions/record-actions/multiple-records/hooks/useDeleteMultipleRecordsAction.tsx @@ -13,13 +13,14 @@ import { useFavorites } from '@/favorites/hooks/useFavorites'; import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; import { DELETE_MAX_COUNT } from '@/object-record/constants/DeleteMaxCount'; import { useDeleteManyRecords } from '@/object-record/hooks/useDeleteManyRecords'; -import { useFetchAllRecordIds } from '@/object-record/hooks/useFetchAllRecordIds'; import { useRecordTable } from '@/object-record/record-table/hooks/useRecordTable'; import { ConfirmationModal } from '@/ui/layout/modal/components/ConfirmationModal'; import { useRightDrawer } from '@/ui/layout/right-drawer/hooks/useRightDrawer'; import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2'; import { useCallback, useContext, useState } from 'react'; import { IconTrash, isDefined } from 'twenty-ui'; +import { useLazyFetchAllRecords } from '@/object-record/hooks/useLazyFetchAllRecords'; +import { DEFAULT_QUERY_PAGE_SIZE } from '@/object-record/constants/DefaultQueryPageSize'; export const useDeleteMultipleRecordsAction = ({ objectMetadataItem, @@ -60,15 +61,18 @@ export const useDeleteMultipleRecordsAction = ({ objectMetadataItem, ); - const { fetchAllRecordIds } = useFetchAllRecordIds({ + const { fetchAllRecords: fetchAllRecordIds } = useLazyFetchAllRecords({ objectNameSingular: objectMetadataItem.nameSingular, filter: graphqlFilter, + limit: DEFAULT_QUERY_PAGE_SIZE, + recordGqlFields: { id: true }, }); const { closeRightDrawer } = useRightDrawer(); const handleDeleteClick = useCallback(async () => { - const recordIdsToDelete = await fetchAllRecordIds(); + const recordsToDelete = await fetchAllRecordIds(); + const recordIdsToDelete = recordsToDelete.map((record) => record.id); resetTableRowSelection(); diff --git a/packages/twenty-front/src/modules/object-record/hooks/__tests__/useFetchAllRecordIds.test.tsx b/packages/twenty-front/src/modules/object-record/hooks/__tests__/useFetchAllRecordIds.test.tsx deleted file mode 100644 index 80b57d7dc5bd..000000000000 --- a/packages/twenty-front/src/modules/object-record/hooks/__tests__/useFetchAllRecordIds.test.tsx +++ /dev/null @@ -1,96 +0,0 @@ -import { act, renderHook } from '@testing-library/react'; -import { useEffect } from 'react'; -import { useRecoilState } from 'recoil'; - -import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState'; -import { - mockPageSize, - peopleMockWithIdsOnly, - query, - responseFirstRequest, - responseSecondRequest, - responseThirdRequest, - variablesFirstRequest, - variablesSecondRequest, - variablesThirdRequest, -} from '@/object-record/hooks/__mocks__/useFetchAllRecordIds'; -import { useFetchAllRecordIds } from '@/object-record/hooks/useFetchAllRecordIds'; -import { getJestMetadataAndApolloMocksWrapper } from '~/testing/jest/getJestMetadataAndApolloMocksWrapper'; -import { generatedMockObjectMetadataItems } from '~/testing/mock-data/generatedMockObjectMetadataItems'; - -const mocks = [ - { - delay: 100, - request: { - query, - variables: variablesFirstRequest, - }, - result: jest.fn(() => ({ - data: responseFirstRequest, - })), - }, - { - delay: 100, - request: { - query, - variables: variablesSecondRequest, - }, - result: jest.fn(() => ({ - data: responseSecondRequest, - })), - }, - { - delay: 100, - request: { - query, - variables: variablesThirdRequest, - }, - result: jest.fn(() => ({ - data: responseThirdRequest, - })), - }, -]; - -const Wrapper = getJestMetadataAndApolloMocksWrapper({ - apolloMocks: mocks, -}); - -describe('useFetchAllRecordIds', () => { - it('fetches all record ids with fetch more synchronous loop', async () => { - const { result } = renderHook( - () => { - const [, setObjectMetadataItems] = useRecoilState( - objectMetadataItemsState, - ); - - useEffect(() => { - setObjectMetadataItems(generatedMockObjectMetadataItems); - }, [setObjectMetadataItems]); - - return useFetchAllRecordIds({ - objectNameSingular: 'person', - pageSize: mockPageSize, - }); - }, - { - wrapper: Wrapper, - }, - ); - - const { fetchAllRecordIds } = result.current; - - let recordIds: string[] = []; - - await act(async () => { - recordIds = await fetchAllRecordIds(); - }); - - expect(mocks[0].result).toHaveBeenCalled(); - expect(mocks[1].result).toHaveBeenCalled(); - expect(mocks[2].result).toHaveBeenCalled(); - - expect(recordIds).toEqual( - peopleMockWithIdsOnly.edges.map((edge) => edge.node.id).slice(0, 6), - ); - }); -}); diff --git a/packages/twenty-front/src/modules/object-record/hooks/__tests__/useLazyFetchAllRecords.test.tsx b/packages/twenty-front/src/modules/object-record/hooks/__tests__/useLazyFetchAllRecords.test.tsx new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/twenty-front/src/modules/object-record/hooks/useFetchAllRecordIds.ts b/packages/twenty-front/src/modules/object-record/hooks/useFetchAllRecordIds.ts deleted file mode 100644 index 715cdc5af15d..000000000000 --- a/packages/twenty-front/src/modules/object-record/hooks/useFetchAllRecordIds.ts +++ /dev/null @@ -1,88 +0,0 @@ -import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; -import { DEFAULT_QUERY_PAGE_SIZE } from '@/object-record/constants/DefaultQueryPageSize'; -import { UseFindManyRecordsParams } from '@/object-record/hooks/useFetchMoreRecordsWithPagination'; -import { useLazyFindManyRecords } from '@/object-record/hooks/useLazyFindManyRecords'; -import { useCallback } from 'react'; -import { isDefined } from '~/utils/isDefined'; - -type UseLazyFetchAllRecordIdsParams = Omit< - UseFindManyRecordsParams, - 'skip' -> & { pageSize?: number }; - -export const useFetchAllRecordIds = ({ - objectNameSingular, - filter, - orderBy, - pageSize = DEFAULT_QUERY_PAGE_SIZE, -}: UseLazyFetchAllRecordIdsParams) => { - const { fetchMore, findManyRecords } = useLazyFindManyRecords({ - objectNameSingular, - filter, - orderBy, - limit: pageSize, - recordGqlFields: { id: true }, - }); - - const { objectMetadataItem } = useObjectMetadataItem({ - objectNameSingular, - }); - - const fetchAllRecordIds = useCallback(async () => { - if (!isDefined(findManyRecords)) { - return []; - } - - const findManyRecordsDataResult = await findManyRecords(); - - const firstQueryResult = - findManyRecordsDataResult?.data?.[objectMetadataItem.namePlural]; - - const totalCount = firstQueryResult?.totalCount ?? 0; - - const recordsCount = firstQueryResult?.edges.length ?? 0; - - const recordIdSet = new Set( - firstQueryResult?.edges?.map((edge) => edge.node.id) ?? [], - ); - - const remainingCount = totalCount - recordsCount; - - const remainingPages = Math.ceil(remainingCount / pageSize); - - let lastCursor = firstQueryResult?.pageInfo.endCursor ?? null; - - for (let pageIndex = 0; pageIndex < remainingPages; pageIndex++) { - if (lastCursor === null) { - break; - } - - const rawResult = await fetchMore?.({ - variables: { - lastCursor: lastCursor, - limit: pageSize, - }, - }); - - const fetchMoreResult = rawResult?.data?.[objectMetadataItem.namePlural]; - - for (const edge of fetchMoreResult.edges) { - recordIdSet.add(edge.node.id); - } - - if (fetchMoreResult.pageInfo.hasNextPage === false) { - break; - } - - lastCursor = fetchMoreResult.pageInfo.endCursor ?? null; - } - - const recordIds = Array.from(recordIdSet); - - return recordIds; - }, [fetchMore, findManyRecords, objectMetadataItem.namePlural, pageSize]); - - return { - fetchAllRecordIds, - }; -}; diff --git a/packages/twenty-front/src/modules/object-record/hooks/useLazyFetchAllRecords.ts b/packages/twenty-front/src/modules/object-record/hooks/useLazyFetchAllRecords.ts index 4bb2754fb1ed..e522cfed7c09 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useLazyFetchAllRecords.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useLazyFetchAllRecords.ts @@ -62,6 +62,7 @@ export const useLazyFetchAllRecords = ({ const recordsCount = firstQueryResult?.edges.length ?? 0; const records = firstQueryResult?.edges?.map((edge) => edge.node) ?? []; + setProgress({ exportedRecordCount: recordsCount, totalRecordCount: totalCount, @@ -97,7 +98,6 @@ export const useLazyFetchAllRecords = ({ const fetchMoreResult = rawResult?.data?.[objectMetadataItem.namePlural]; for (const edge of fetchMoreResult.edges) { - console.log(edge.node); records.push(edge.node); }