The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(front): keep cached relation picker results clickable while Search refreshes (#28458)
lineicod2aab27b3411
diff --git a/packages/twenty-front/src/modules/object-record/hooks/useObjectRecordSearchRecords.ts b/packages/twenty-front/src/modules/object-record/hooks/useObjectRecordSearchRecords.tsindex bfa5c0b9c2e..8d474b9f21a 100644--- a/packages/twenty-front/src/modules/object-record/hooks/useObjectRecordSearchRecords.ts+++ b/packages/twenty-front/src/modules/object-record/hooks/useObjectRecordSearchRecords.ts@@ -3,6 +3,7 @@ import { MAX_SEARCH_RESULTS } from '@/command-menu/constants/MaxSearchResults';import { useApolloCoreClient } from '@/object-metadata/hooks/useApolloCoreClient';import { useDoObjectMetadataItemsExist } from '@/object-metadata/hooks/useDoObjectMetadataItemsExist';import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';+import { isSearchQueryWaitingForData } from '@/object-record/utils/isSearchQueryWaitingForData';import { type WatchQueryFetchPolicy } from '@apollo/client';import { useQuery } from '@apollo/client/react';import { useMemo } from 'react';@@ -36,20 +37,28 @@ export const useObjectRecordSearchRecords = ({const apolloCoreClient = useApolloCoreClient();- const { data, loading, error, previousData } = useQuery(SearchDocument, {- skip:- skip || !areDefined || !currentWorkspaceMember || !isDefined(searchInput),- variables: {- searchInput: searchInput ?? '',- limit: limit ?? MAX_SEARCH_RESULTS,- filter: filter ?? {},- includedObjectNameSingulars: objectNameSingulars,+ const { data, dataState, loading, error, previousData } = useQuery(+ SearchDocument,+ {+ skip:+ skip ||+ !areDefined ||+ !currentWorkspaceMember ||+ !isDefined(searchInput),+ variables: {+ searchInput: searchInput ?? '',+ limit: limit ?? MAX_SEARCH_RESULTS,+ filter: filter ?? {},+ includedObjectNameSingulars: objectNameSingulars,+ },+ fetchPolicy: fetchPolicy,+ client: apolloCoreClient,},- fetchPolicy: fetchPolicy,- client: apolloCoreClient,- });+ );++ const isWaitingForData = isSearchQueryWaitingForData({ loading, dataState });- const effectiveData = loading ? previousData : data;+ const effectiveData = isWaitingForData ? previousData : data;const searchRecords = useMemo(() => effectiveData?.search?.edges?.map((edge) => edge.node) || [],@@ -58,7 +67,7 @@ export const useObjectRecordSearchRecords = ({return {searchRecords,- loading,+ loading: isWaitingForData,error,};};diff --git a/packages/twenty-front/src/modules/object-record/record-picker/single-record-picker/components/SingleRecordPickerLoadingEffect.tsx b/packages/twenty-front/src/modules/object-record/record-picker/single-record-picker/components/SingleRecordPickerLoadingEffect.tsxindex 4ee0f128ac8..ecbbb66cf0b 100644--- a/packages/twenty-front/src/modules/object-record/record-picker/single-record-picker/components/SingleRecordPickerLoadingEffect.tsx+++ b/packages/twenty-front/src/modules/object-record/record-picker/single-record-picker/components/SingleRecordPickerLoadingEffect.tsx@@ -8,7 +8,10 @@ export const SingleRecordPickerLoadingEffect = ({}: {loading: boolean;}) => {- const [previousLoading, setPreviousLoading] = useState(false);+ // Undefined on mount, so a picker opened on cached results clears the skeleton+ const [previousLoading, setPreviousLoading] = useState<boolean | undefined>(+ undefined,+ );const setSingleRecordPickerShouldShowSkeleton = useSetAtomComponentState(singleRecordPickerShouldShowSkeletonComponentState,diff --git a/packages/twenty-front/src/modules/object-record/record-picker/single-record-picker/components/SingleRecordPickerMenuItems.tsx b/packages/twenty-front/src/modules/object-record/record-picker/single-record-picker/components/SingleRecordPickerMenuItems.tsxindex fb0184e5e8d..3c95e2bfded 100644--- a/packages/twenty-front/src/modules/object-record/record-picker/single-record-picker/components/SingleRecordPickerMenuItems.tsx+++ b/packages/twenty-front/src/modules/object-record/record-picker/single-record-picker/components/SingleRecordPickerMenuItems.tsx@@ -92,6 +92,9 @@ export const SingleRecordPickerMenuItems = ({const searchHasNoResults = itemsMatchingSearchFilter.length === 0;+ const shouldShowInitialLoading =+ singleRecordPickerShouldShowInitialLoading && searchHasNoResults;+return (<SelectableListselectableListInstanceId={selectableListComponentInstanceId}@@ -125,7 +128,7 @@ export const SingleRecordPickerMenuItems = ({</ListItemButton></SelectableListItem>)}- {singleRecordPickerShouldShowInitialLoading ? (+ {shouldShowInitialLoading ? (<RecordPickerInitialLoadingEmptyContainer />) : singleRecordPickerShouldShowSkeleton ? (<RecordPickerLoadingSkeletonList />diff --git a/packages/twenty-front/src/modules/object-record/utils/__tests__/isSearchQueryWaitingForData.test.ts b/packages/twenty-front/src/modules/object-record/utils/__tests__/isSearchQueryWaitingForData.test.tsnew file mode 100644index 00000000000..1e8c9d8cd48--- /dev/null+++ b/packages/twenty-front/src/modules/object-record/utils/__tests__/isSearchQueryWaitingForData.test.ts@@ -0,0 +1,21 @@+import { isSearchQueryWaitingForData } from '@/object-record/utils/isSearchQueryWaitingForData';++describe('isSearchQueryWaitingForData', () => {+ it('should not wait on a cached result that is refreshing', () => {+ expect(+ isSearchQueryWaitingForData({ loading: true, dataState: 'complete' }),+ ).toBe(false);+ });++ it('should wait while nothing is available for the current input', () => {+ expect(+ isSearchQueryWaitingForData({ loading: true, dataState: 'empty' }),+ ).toBe(true);+ });++ it('should not wait once the query has loaded', () => {+ expect(+ isSearchQueryWaitingForData({ loading: false, dataState: 'complete' }),+ ).toBe(false);+ });+});diff --git a/packages/twenty-front/src/modules/object-record/utils/isSearchQueryWaitingForData.ts b/packages/twenty-front/src/modules/object-record/utils/isSearchQueryWaitingForData.tsnew file mode 100644index 00000000000..34f4874ac5e--- /dev/null+++ b/packages/twenty-front/src/modules/object-record/utils/isSearchQueryWaitingForData.ts@@ -0,0 +1,10 @@+import { type DataState } from '@apollo/client';++// With cache-and-network, a cache hit for the current input is complete while it still refreshes+export const isSearchQueryWaitingForData = ({+ loading,+ dataState,+}: {+ loading: boolean;+ dataState: DataState<unknown>['dataState'];+}): boolean => loading && dataState !== 'complete';