Improve Jotai work (#18205)
This commit is contained in:
+7
-5
@@ -31,8 +31,8 @@ import { hasNextPageFamilyState } from '@/object-record/states/hasNextPageFamily
|
||||
import { isFetchingMoreRecordsFamilyState } from '@/object-record/states/isFetchingMoreRecordsFamilyState';
|
||||
import { useFamilyRecoilValueV2 } from '@/ui/utilities/state/jotai/hooks/useFamilyRecoilValueV2';
|
||||
import { useSetFamilyRecoilStateV2 } from '@/ui/utilities/state/jotai/hooks/useSetFamilyRecoilStateV2';
|
||||
import { jotaiStore } from '@/ui/utilities/state/jotai/jotaiStore';
|
||||
import { capitalize, isDefined } from 'twenty-shared/utils';
|
||||
import { useStore } from 'jotai';
|
||||
|
||||
export type UseFindManyRecordsParams<T> = ObjectMetadataItemIdentifier &
|
||||
RecordGqlOperationVariables & {
|
||||
@@ -81,6 +81,7 @@ export const useFetchMoreRecordsWithPagination = <
|
||||
objectMetadataItem,
|
||||
onCompleted,
|
||||
}: UseFindManyRecordsStateParams<T>) => {
|
||||
const store = useStore();
|
||||
const queryIdentifier = getQueryIdentifier({
|
||||
objectNameSingular,
|
||||
filter,
|
||||
@@ -105,11 +106,11 @@ export const useFetchMoreRecordsWithPagination = <
|
||||
// TODO: put this into a util inspired from https://github.com/apollographql/apollo-client/blob/master/src/utilities/policies/pagination.ts
|
||||
// This function is equivalent to merge function + read function in field policy
|
||||
const fetchMoreRecords = useCallback(async () => {
|
||||
const hasNextPageLocal = jotaiStore.get(
|
||||
const hasNextPageLocal = store.get(
|
||||
hasNextPageFamilyState.atomFamily(queryIdentifier),
|
||||
);
|
||||
|
||||
const lastCursorLocal = jotaiStore.get(
|
||||
const lastCursorLocal = store.get(
|
||||
cursorFamilyState.atomFamily(queryIdentifier),
|
||||
);
|
||||
|
||||
@@ -147,11 +148,11 @@ export const useFetchMoreRecordsWithPagination = <
|
||||
fetchMoreResult?.[objectMetadataItem.namePlural]?.pageInfo;
|
||||
|
||||
if (isDefined(pageInfo)) {
|
||||
jotaiStore.set(
|
||||
store.set(
|
||||
cursorFamilyState.atomFamily(queryIdentifier),
|
||||
pageInfo.endCursor ?? '',
|
||||
);
|
||||
jotaiStore.set(
|
||||
store.set(
|
||||
hasNextPageFamilyState.atomFamily(queryIdentifier),
|
||||
pageInfo.hasNextPage ?? false,
|
||||
);
|
||||
@@ -205,6 +206,7 @@ export const useFetchMoreRecordsWithPagination = <
|
||||
onCompleted,
|
||||
handleFindManyRecordsError,
|
||||
queryIdentifier,
|
||||
store,
|
||||
]);
|
||||
|
||||
const totalCount = data?.[objectMetadataItem.namePlural]?.totalCount;
|
||||
|
||||
+5
-4
@@ -4,9 +4,9 @@ import { type RecordGqlOperationFindManyResult } from '@/object-record/graphql/t
|
||||
import { cursorFamilyState } from '@/object-record/states/cursorFamilyState';
|
||||
import { hasNextPageFamilyState } from '@/object-record/states/hasNextPageFamilyState';
|
||||
import { type OnFindManyRecordsCompleted } from '@/object-record/types/OnFindManyRecordsCompleted';
|
||||
import { jotaiStore } from '@/ui/utilities/state/jotai/jotaiStore';
|
||||
import { useCallback } from 'react';
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
import { useStore } from 'jotai';
|
||||
|
||||
export const useHandleFindManyRecordsCompleted = <T>({
|
||||
queryIdentifier,
|
||||
@@ -17,6 +17,7 @@ export const useHandleFindManyRecordsCompleted = <T>({
|
||||
objectMetadataItem: ObjectMetadataItem;
|
||||
onCompleted?: OnFindManyRecordsCompleted<T>;
|
||||
}) => {
|
||||
const store = useStore();
|
||||
const handleFindManyRecordsCompleted = useCallback(
|
||||
(data: RecordGqlOperationFindManyResult) => {
|
||||
const pageInfo = data?.[objectMetadataItem.namePlural]?.pageInfo;
|
||||
@@ -31,17 +32,17 @@ export const useHandleFindManyRecordsCompleted = <T>({
|
||||
});
|
||||
|
||||
if (isDefined(data?.[objectMetadataItem.namePlural])) {
|
||||
jotaiStore.set(
|
||||
store.set(
|
||||
cursorFamilyState.atomFamily(queryIdentifier),
|
||||
pageInfo.endCursor ?? '',
|
||||
);
|
||||
jotaiStore.set(
|
||||
store.set(
|
||||
hasNextPageFamilyState.atomFamily(queryIdentifier),
|
||||
pageInfo.hasNextPage ?? false,
|
||||
);
|
||||
}
|
||||
},
|
||||
[objectMetadataItem.namePlural, onCompleted, queryIdentifier],
|
||||
[objectMetadataItem.namePlural, onCompleted, queryIdentifier, store],
|
||||
);
|
||||
|
||||
return {
|
||||
|
||||
+7
-5
@@ -29,8 +29,8 @@ import {
|
||||
import { DEFAULT_SEARCH_REQUEST_LIMIT } from '@/object-record/constants/DefaultSearchRequestLimit';
|
||||
import { cursorFamilyState } from '@/object-record/states/cursorFamilyState';
|
||||
import { hasNextPageFamilyState } from '@/object-record/states/hasNextPageFamilyState';
|
||||
import { jotaiStore } from '@/ui/utilities/state/jotai/jotaiStore';
|
||||
import { capitalize, isDefined } from 'twenty-shared/utils';
|
||||
import { useStore } from 'jotai';
|
||||
|
||||
export type UseFindManyRecordsParams<T> = ObjectMetadataItemIdentifier &
|
||||
RecordGqlOperationVariables & {
|
||||
@@ -77,6 +77,7 @@ export const useLazyFetchMoreRecordsWithPagination = <
|
||||
fetchMore,
|
||||
objectMetadataItem,
|
||||
}: UseFindManyRecordsStateParams<T>) => {
|
||||
const store = useStore();
|
||||
const queryIdentifier = getQueryIdentifier({
|
||||
objectNameSingular,
|
||||
filter,
|
||||
@@ -92,11 +93,11 @@ export const useLazyFetchMoreRecordsWithPagination = <
|
||||
// This function is equivalent to merge function + read function in field policy
|
||||
const fetchMoreRecordsLazy = useCallback(
|
||||
async (limit = DEFAULT_SEARCH_REQUEST_LIMIT) => {
|
||||
const hasNextPageLocal = jotaiStore.get(
|
||||
const hasNextPageLocal = store.get(
|
||||
hasNextPageFamilyState.atomFamily(queryIdentifier),
|
||||
);
|
||||
|
||||
const lastCursorLocal = jotaiStore.get(
|
||||
const lastCursorLocal = store.get(
|
||||
cursorFamilyState.atomFamily(queryIdentifier),
|
||||
);
|
||||
|
||||
@@ -135,11 +136,11 @@ export const useLazyFetchMoreRecordsWithPagination = <
|
||||
fetchMoreResult?.[objectMetadataItem.namePlural]?.pageInfo;
|
||||
|
||||
if (isDefined(pageInfo)) {
|
||||
jotaiStore.set(
|
||||
store.set(
|
||||
cursorFamilyState.atomFamily(queryIdentifier),
|
||||
pageInfo.endCursor ?? '',
|
||||
);
|
||||
jotaiStore.set(
|
||||
store.set(
|
||||
hasNextPageFamilyState.atomFamily(queryIdentifier),
|
||||
pageInfo.hasNextPage ?? false,
|
||||
);
|
||||
@@ -188,6 +189,7 @@ export const useLazyFetchMoreRecordsWithPagination = <
|
||||
filter,
|
||||
orderBy,
|
||||
handleFindManyRecordsError,
|
||||
store,
|
||||
],
|
||||
);
|
||||
|
||||
|
||||
@@ -13,10 +13,10 @@ import { useObjectPermissionsForObject } from '@/object-record/hooks/useObjectPe
|
||||
import { cursorFamilyState } from '@/object-record/states/cursorFamilyState';
|
||||
import { hasNextPageFamilyState } from '@/object-record/states/hasNextPageFamilyState';
|
||||
import { type ObjectRecord } from '@/object-record/types/ObjectRecord';
|
||||
import { jotaiStore } from '@/ui/utilities/state/jotai/jotaiStore';
|
||||
import { getQueryIdentifier } from '@/object-record/utils/getQueryIdentifier';
|
||||
import { QUERY_DEFAULT_LIMIT_RECORDS } from 'twenty-shared/constants';
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
import { useStore } from 'jotai';
|
||||
|
||||
type UseLazyFindManyRecordsParams<T> = Omit<
|
||||
UseFindManyRecordsParams<T>,
|
||||
@@ -31,6 +31,7 @@ export const useLazyFindManyRecords = <T extends ObjectRecord = ObjectRecord>({
|
||||
recordGqlFields,
|
||||
fetchPolicy = 'cache-first',
|
||||
}: UseLazyFindManyRecordsParams<T>) => {
|
||||
const store = useStore();
|
||||
const { objectMetadataItem } = useObjectMetadataItem({
|
||||
objectNameSingular,
|
||||
});
|
||||
@@ -83,8 +84,8 @@ export const useLazyFindManyRecords = <T extends ObjectRecord = ObjectRecord>({
|
||||
|
||||
const findManyRecordsLazy = useCallback(async () => {
|
||||
if (!hasReadPermission) {
|
||||
jotaiStore.set(hasNextPageFamilyState.atomFamily(queryIdentifier), false);
|
||||
jotaiStore.set(cursorFamilyState.atomFamily(queryIdentifier), '');
|
||||
store.set(hasNextPageFamilyState.atomFamily(queryIdentifier), false);
|
||||
store.set(cursorFamilyState.atomFamily(queryIdentifier), '');
|
||||
|
||||
return {
|
||||
data: null,
|
||||
@@ -107,11 +108,8 @@ export const useLazyFindManyRecords = <T extends ObjectRecord = ObjectRecord>({
|
||||
const lastCursor =
|
||||
result?.data?.[objectMetadataItem.namePlural]?.pageInfo.endCursor ?? '';
|
||||
|
||||
jotaiStore.set(
|
||||
hasNextPageFamilyState.atomFamily(queryIdentifier),
|
||||
hasNextPage,
|
||||
);
|
||||
jotaiStore.set(cursorFamilyState.atomFamily(queryIdentifier), lastCursor);
|
||||
store.set(hasNextPageFamilyState.atomFamily(queryIdentifier), hasNextPage);
|
||||
store.set(cursorFamilyState.atomFamily(queryIdentifier), lastCursor);
|
||||
|
||||
const records = getRecordsFromRecordConnection({
|
||||
recordConnection: {
|
||||
@@ -141,6 +139,7 @@ export const useLazyFindManyRecords = <T extends ObjectRecord = ObjectRecord>({
|
||||
objectMetadataItem.namePlural,
|
||||
queryIdentifier,
|
||||
handleFindManyRecordsError,
|
||||
store,
|
||||
]);
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user