Skip to content
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

feat: record group fetch more #8868

Merged
merged 8 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export const useFetchMoreRecordsWithPagination = <
const pageInfo =
fetchMoreResult?.[objectMetadataItem.namePlural]?.pageInfo;

if (isDefined(data?.[objectMetadataItem.namePlural])) {
if (isDefined(pageInfo)) {
set(
cursorFamilyState(queryIdentifier),
pageInfo.endCursor ?? '',
Expand Down Expand Up @@ -201,7 +201,6 @@ export const useFetchMoreRecordsWithPagination = <
fetchMore,
filter,
orderBy,
data,
onCompleted,
handleFindManyRecordsError,
queryIdentifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const useLazyFindManyRecords = <T extends ObjectRecord = ObjectRecord>({
onError: handleFindManyRecordsError,
});

const { fetchMoreRecords, totalCount, records } =
const { fetchMoreRecords, totalCount, records, hasNextPage } =
useFetchMoreRecordsWithPagination<T>({
objectNameSingular,
filter,
Expand Down Expand Up @@ -108,8 +108,9 @@ export const useLazyFindManyRecords = <T extends ObjectRecord = ObjectRecord>({
loading,
error,
fetchMore,
fetchMoreRecordsWithPagination: fetchMoreRecords,
fetchMoreRecords,
queryStateIdentifier: queryIdentifier,
findManyRecords: findManyRecordsLazy,
hasNextPage,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { recordGroupDefinitionFamilyState } from '@/object-record/record-group/s
import { recordGroupIdsComponentState } from '@/object-record/record-group/states/recordGroupIdsComponentState';
import { RecordGroupDefinition } from '@/object-record/record-group/types/RecordGroupDefinition';
import { RecordGroupSort } from '@/object-record/record-group/types/RecordGroupSort';
import { sortedInsert } from '@/object-record/record-group/utils/sortedInsert';
import { recordGroupSortedInsert } from '@/object-record/record-group/utils/recordGroupSortedInsert';
import { recordIndexRecordGroupSortComponentState } from '@/object-record/record-index/states/recordIndexRecordGroupSortComponentState';

import { createComponentSelectorV2 } from '@/ui/utilities/state/component-state/utils/createComponentSelectorV2';
Expand Down Expand Up @@ -54,7 +54,7 @@ export const visibleRecordGroupIdsComponentSelector = createComponentSelectorV2<
isDefined(recordGroupDefinition) &&
recordGroupDefinition.isVisible
) {
sortedInsert(result, recordGroupDefinition, comparator);
recordGroupSortedInsert(result, recordGroupDefinition, comparator);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// export const recordGroupSortedInsert = <T>(
// array: T[],
// item: T,
// comparator: (a: T, b: T) => number,
// ) => {
// let low = 0;
// let high = array.length;

import { expect } from '@storybook/test';

// while (low < high) {
// const mid = Math.floor((low + high) / 2);

// if (comparator(item, array[mid]) < 0) {
// high = mid;
// } else {
// low = mid + 1;
// }
// }

// array.splice(low, 0, item);
// };

import { recordGroupSortedInsert } from '../recordGroupSortedInsert';

describe('recordGroupSortedInsert', () => {
it('should insert an item into an empty array', () => {
const array: number[] = [];
const item = 1;
const comparator = (a: number, b: number) => a - b;

recordGroupSortedInsert(array, item, comparator);

expect(array).toEqual([1]);
});

it('should insert an item at the beginning of the array', () => {
const array = [2, 3, 4];
const item = 1;
const comparator = (a: number, b: number) => a - b;

recordGroupSortedInsert(array, item, comparator);

expect(array).toEqual([1, 2, 3, 4]);
});

it('should insert an item at the end of the array', () => {
const array = [1, 2, 3];
const item = 4;
const comparator = (a: number, b: number) => a - b;

recordGroupSortedInsert(array, item, comparator);

expect(array).toEqual([1, 2, 3, 4]);
});

it('should insert an item in the middle of the array', () => {
const array = [1, 3, 4];
const item = 2;
const comparator = (a: number, b: number) => a - b;

recordGroupSortedInsert(array, item, comparator);

expect(array).toEqual([1, 2, 3, 4]);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const sortedInsert = <T>(
export const recordGroupSortedInsert = <T>(
array: T[],
item: T,
comparator: (a: T, b: T) => number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadata
import { useObjectNameSingularFromPlural } from '@/object-metadata/hooks/useObjectNameSingularFromPlural';
import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords';
import { RecordIndexRootPropsContext } from '@/object-record/record-index/contexts/RecordIndexRootPropsContext';
import { useFindManyParams } from '@/object-record/record-index/hooks/useLoadRecordIndexTable';
import { useFindManyRecordIndexTableParams } from '@/object-record/record-index/hooks/useFindManyRecordIndexTableParams';
import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2';
import { useSetRecoilComponentStateV2 } from '@/ui/utilities/state/component-state/hooks/useSetRecoilComponentStateV2';
import { useContext, useEffect } from 'react';
Expand All @@ -31,7 +31,7 @@ export const RecordIndexContainerContextStoreNumberOfSelectedRecordsEffect =
objectNameSingular,
});

const findManyRecordsParams = useFindManyParams(
const findManyRecordsParams = useFindManyRecordIndexTableParams(
objectMetadataItem?.nameSingular ?? '',
objectMetadataItem?.namePlural ?? '',
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useLazyFindManyRecords } from '@/object-record/hooks/useLazyFindManyRec
import { EXPORT_TABLE_DATA_DEFAULT_PAGE_SIZE } from '@/object-record/object-options-dropdown/constants/ExportTableDataDefaultPageSize';
import { useObjectOptionsForBoard } from '@/object-record/object-options-dropdown/hooks/useObjectOptionsForBoard';
import { recordGroupFieldMetadataComponentState } from '@/object-record/record-group/states/recordGroupFieldMetadataComponentState';
import { useFindManyParams } from '@/object-record/record-index/hooks/useLoadRecordIndexTable';
import { useFindManyRecordIndexTableParams } from '@/object-record/record-index/hooks/useFindManyRecordIndexTableParams';
import { visibleTableColumnsComponentSelector } from '@/object-record/record-table/states/selectors/visibleTableColumnsComponentSelector';
import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2';
import { ViewType } from '@/views/types/ViewType';
Expand Down Expand Up @@ -94,29 +94,24 @@ export const useExportFetchRecords = ({
objectMetadataItem,
);

const findManyRecordsParams = useFindManyParams(
const findManyRecordsParams = useFindManyRecordIndexTableParams(
objectMetadataItem.nameSingular,
recordIndexId,
);

const {
findManyRecords,
totalCount,
records,
fetchMoreRecordsWithPagination,
loading,
} = useLazyFindManyRecords({
...findManyRecordsParams,
filter: queryFilter,
limit: pageSize,
});
const { findManyRecords, totalCount, records, fetchMoreRecords, loading } =
useLazyFindManyRecords({
...findManyRecordsParams,
filter: queryFilter,
limit: pageSize,
});

useEffect(() => {
const fetchNextPage = async () => {
setInflight(true);
setPreviousRecordCount(records.length);

await fetchMoreRecordsWithPagination();
await fetchMoreRecords();

setPageCount((state) => state + 1);
setProgress({
Expand Down Expand Up @@ -166,7 +161,7 @@ export const useExportFetchRecords = ({
}
}, [
delayMs,
fetchMoreRecordsWithPagination,
fetchMoreRecords,
inflight,
isDownloading,
pageCount,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
import { useRecoilValue } from 'recoil';

import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState';
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords';
import { turnSortsIntoOrderBy } from '@/object-record/object-sort-dropdown/utils/turnSortsIntoOrderBy';
import { computeViewRecordGqlOperationFilter } from '@/object-record/record-filter/utils/computeViewRecordGqlOperationFilter';
import { useCurrentRecordGroupDefinition } from '@/object-record/record-group/hooks/useCurrentRecordGroupDefinition';
import { useRecordTableRecordGqlFields } from '@/object-record/record-index/hooks/useRecordTableRecordGqlFields';
import { useRecordTable } from '@/object-record/record-table/hooks/useRecordTable';
import { tableFiltersComponentState } from '@/object-record/record-table/states/tableFiltersComponentState';
import { tableSortsComponentState } from '@/object-record/record-table/states/tableSortsComponentState';
import { tableViewFilterGroupsComponentState } from '@/object-record/record-table/states/tableViewFilterGroupsComponentState';
import { SIGN_IN_BACKGROUND_MOCK_COMPANIES } from '@/sign-in-background-mock/constants/SignInBackgroundMockCompanies';
import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2';
import { isNull } from '@sniptt/guards';
import { useMemo } from 'react';
import { isDefined } from 'twenty-ui';
import { WorkspaceActivationStatus } from '~/generated/graphql';

export const useFindManyParams = (
export const useFindManyRecordIndexTableParams = (
objectNameSingular: string,
recordTableId?: string,
) => {
Expand Down Expand Up @@ -84,51 +74,7 @@ export const useFindManyParams = (
...recordGroupFilter,
},
orderBy,
};
};

export const useLoadRecordIndexTable = (objectNameSingular: string) => {
const { objectMetadataItem } = useObjectMetadataItem({
objectNameSingular,
});

const { setRecordTableData, setIsRecordTableInitialLoading } =
useRecordTable();
const currentWorkspace = useRecoilValue(currentWorkspaceState);
const currentWorkspaceMember = useRecoilValue(currentWorkspaceMemberState);
const params = useFindManyParams(objectNameSingular);

const recordGqlFields = useRecordTableRecordGqlFields({ objectMetadataItem });

const {
records,
loading,
totalCount,
fetchMoreRecords,
queryStateIdentifier,
hasNextPage,
} = useFindManyRecords({
...params,
recordGqlFields,
onCompleted: () => {
setIsRecordTableInitialLoading(false);
},
onError: () => {
setIsRecordTableInitialLoading(false);
},
skip: isNull(currentWorkspaceMember),
});

return {
records:
currentWorkspace?.activationStatus === WorkspaceActivationStatus.Active
? records
: SIGN_IN_BACKGROUND_MOCK_COMPANIES,
totalCount: totalCount,
loading,
fetchMoreRecords,
queryStateIdentifier,
setRecordTableData,
hasNextPage,
// If we have a current record group definition, we only want to fetch 8 records by page
...(currentRecordGroupDefinition ? { limit: 8 } : {}),
};
};
Loading
Loading