From b1242bb85059abd0cd1e4470c341bda099bbb163 Mon Sep 17 00:00:00 2001 From: Lucas Bordeau Date: Tue, 9 Apr 2024 09:19:52 +0200 Subject: [PATCH] 4087 refactor object metadata item hooks and utils (#4861) - Extracted each exported element from useObjectMetadataItem into its own hook. --- .../components/CalendarEventDetails.tsx | 4 +- .../components/ActivityBodyEditor.tsx | 4 +- .../components/ActivityEditorFields.tsx | 7 +- .../activities/components/ActivityTitle.tsx | 4 +- .../hooks/useActivityTargetObjectRecords.ts | 8 +- .../hooks/useCreateActivityInCache.ts | 6 +- .../usePrepareFindManyActivitiesQuery.ts | 6 +- .../ActivityTargetInlineCellEditMode.tsx | 4 +- .../useGetObjectOrderByField.test.ts | 21 ---- .../useGetObjectOrderByField.test.tsx | 33 +++++ .../useMapToObjectRecordIdentifier.test.tsx | 16 ++- .../__tests__/useObjectMetadataItem.test.tsx | 34 +---- .../hooks/useCreateOneObjectMetadataItem.ts | 5 +- .../hooks/useGetObjectOrderByField.ts | 18 ++- .../useLabelIdentifierFieldMetadataItem.ts | 17 +++ .../hooks/useMapToObjectRecordIdentifier.ts | 21 +++- .../hooks/useObjectMetadataItem.ts | 118 +----------------- .../hooks/useObjectMetadataItemOnly.ts | 47 ------- .../__tests__/getObjectOrderByField.test.ts | 4 +- .../utils/getBasePathToShowPage.ts | 8 +- .../utils/getObjectOrderByField.ts | 2 +- .../utils/getObjectRecordIdentifier.ts | 2 +- .../cache/hooks/useCreateOneRecordInCache.ts | 2 +- .../cache/hooks/useDeleteRecordFromCache.ts | 4 +- .../cache/hooks/useGetRecordFromCache.ts | 10 +- .../useReadFindManyRecordsQueryInCache.ts | 6 +- .../useUpsertFindManyRecordsQueryInCache.ts | 9 +- .../object-record/components/RecordChip.tsx | 7 +- .../object-record/constants/EmptyMutation.ts | 7 ++ .../object-record/constants/EmptyQuery.ts | 7 ++ .../hooks/useCreateManyRecords.ts | 10 +- ...ion.ts => useCreateManyRecordsMutation.ts} | 31 +++-- .../object-record/hooks/useCreateOneRecord.ts | 16 ++- ...ation.ts => useCreateOneRecordMutation.ts} | 31 +++-- .../hooks/useDeleteManyRecords.ts | 17 ++- .../hooks/useDeleteManyRecordsMutation.ts | 41 ++++++ .../object-record/hooks/useDeleteOneRecord.ts | 17 ++- .../hooks/useDeleteOneRecordMutation.ts | 39 ++++++ .../hooks/useExecuteQuickActionOnOneRecord.ts | 17 ++- ...eExecuteQuickActionOnOneRecordMutation.ts} | 20 +-- .../object-record/hooks/useFieldContext.tsx | 7 +- .../hooks/useFindDuplicateRecords.ts | 13 +- ...ry.ts => useFindDuplicatesRecordsQuery.ts} | 31 +++-- .../object-record/hooks/useFindManyRecords.ts | 15 +-- .../hooks/useFindManyRecordsQuery.ts | 36 ++++++ .../object-record/hooks/useFindOneRecord.ts | 15 ++- ...ecordQuery.ts => useFindOneRecordQuery.ts} | 28 +++-- .../useGenerateDeleteManyRecordMutation.ts | 36 ------ .../hooks/useGenerateFindManyRecordsQuery.ts | 55 -------- .../hooks/useLazyFindOneRecord.ts | 12 +- .../object-record/hooks/useUpdateOneRecord.ts | 17 ++- ...ation.ts => useUpdateOneRecordMutation.ts} | 33 ++--- ...FindManyRecordsForMultipleMetadataItems.ts | 2 +- .../hooks/useOptionsForSelect.ts | 4 +- .../query-keys/types/QueryFields.ts | 1 + .../query-keys/types/QueryKey.ts | 5 +- .../components/RecordIndexBoardContainer.tsx | 4 +- .../RecordIndexBoardContainerEffect.tsx | 4 +- .../components/RecordIndexContainer.tsx | 4 +- .../RecordIndexTableContainerEffect.tsx | 4 +- .../hooks/useLoadRecordIndexBoard.ts | 4 +- .../hooks/useLoadRecordIndexTable.ts | 4 +- .../hooks/useRecordIndexOptionsForBoard.ts | 4 +- .../components/RecordShowContainer.tsx | 23 ++-- .../RecordDetailRelationRecordsListItem.tsx | 4 +- .../RecordDetailRelationSection.tsx | 4 +- .../record-table/components/RecordTable.tsx | 4 +- .../components/RecordTableRow.tsx | 4 +- ...atchesSearchFilterAndSelectedItemsQuery.ts | 2 +- ...archMatchesSearchFilterAndToSelectQuery.ts | 2 +- .../hooks/useOrderByFieldPerMetadataItem.ts | 5 +- .../select/hooks/useRecordsForSelect.ts | 14 ++- .../useSpreadsheetRecordImport.ts | 4 +- .../utils/generateDeleteOneRecordMutation.ts | 34 ----- .../utils/generateFindManyRecordsQuery.ts | 49 ++++++++ ...tCreateManyRecordsMutationResponseField.ts | 5 + ...getCreateOneRecordMutationResponseField.ts | 5 + ...tDeleteManyRecordsMutationResponseField.ts | 5 + ...getDeleteOneRecordMutationResponseField.ts | 5 + ...tFindDuplicateRecordsQueryResponseField.ts | 3 + ...getUpdateOneRecordMutationResponseField.ts | 5 + .../hooks/useFilteredSearchEntityQuery.ts | 5 +- .../internal/usePersistViewFieldRecords.ts | 22 +++- .../internal/usePersistViewFilterRecords.ts | 28 +++-- .../internal/usePersistViewSortRecords.ts | 28 +++-- .../hooks/internal/useViewFromQueryParams.ts | 15 ++- .../views/hooks/useGetViewFromCache.ts | 4 +- .../pages/object-record/RecordShowPage.tsx | 15 ++- .../SettingsObjectNewFieldStep2.tsx | 9 +- 89 files changed, 699 insertions(+), 622 deletions(-) delete mode 100644 packages/twenty-front/src/modules/object-metadata/hooks/__tests__/useGetObjectOrderByField.test.ts create mode 100644 packages/twenty-front/src/modules/object-metadata/hooks/__tests__/useGetObjectOrderByField.test.tsx create mode 100644 packages/twenty-front/src/modules/object-metadata/hooks/useLabelIdentifierFieldMetadataItem.ts delete mode 100644 packages/twenty-front/src/modules/object-metadata/hooks/useObjectMetadataItemOnly.ts create mode 100644 packages/twenty-front/src/modules/object-record/constants/EmptyMutation.ts create mode 100644 packages/twenty-front/src/modules/object-record/constants/EmptyQuery.ts rename packages/twenty-front/src/modules/object-record/hooks/{useGenerateCreateManyRecordMutation.ts => useCreateManyRecordsMutation.ts} (60%) rename packages/twenty-front/src/modules/object-record/hooks/{useGenerateCreateOneRecordMutation.ts => useCreateOneRecordMutation.ts} (62%) create mode 100644 packages/twenty-front/src/modules/object-record/hooks/useDeleteManyRecordsMutation.ts create mode 100644 packages/twenty-front/src/modules/object-record/hooks/useDeleteOneRecordMutation.ts rename packages/twenty-front/src/modules/object-record/hooks/{useGenerateExecuteQuickActionOnOneRecordMutation.ts => useExecuteQuickActionOnOneRecordMutation.ts} (71%) rename packages/twenty-front/src/modules/object-record/hooks/{useGenerateFindDuplicateRecordsQuery.ts => useFindDuplicatesRecordsQuery.ts} (61%) create mode 100644 packages/twenty-front/src/modules/object-record/hooks/useFindManyRecordsQuery.ts rename packages/twenty-front/src/modules/object-record/hooks/{useGenerateFindOneRecordQuery.ts => useFindOneRecordQuery.ts} (64%) delete mode 100644 packages/twenty-front/src/modules/object-record/hooks/useGenerateDeleteManyRecordMutation.ts delete mode 100644 packages/twenty-front/src/modules/object-record/hooks/useGenerateFindManyRecordsQuery.ts rename packages/twenty-front/src/modules/object-record/hooks/{useGenerateUpdateOneRecordMutation.ts => useUpdateOneRecordMutation.ts} (63%) create mode 100644 packages/twenty-front/src/modules/object-record/query-keys/types/QueryFields.ts delete mode 100644 packages/twenty-front/src/modules/object-record/utils/generateDeleteOneRecordMutation.ts create mode 100644 packages/twenty-front/src/modules/object-record/utils/generateFindManyRecordsQuery.ts create mode 100644 packages/twenty-front/src/modules/object-record/utils/getCreateManyRecordsMutationResponseField.ts create mode 100644 packages/twenty-front/src/modules/object-record/utils/getCreateOneRecordMutationResponseField.ts create mode 100644 packages/twenty-front/src/modules/object-record/utils/getDeleteManyRecordsMutationResponseField.ts create mode 100644 packages/twenty-front/src/modules/object-record/utils/getDeleteOneRecordMutationResponseField.ts create mode 100644 packages/twenty-front/src/modules/object-record/utils/getFindDuplicateRecordsQueryResponseField.ts create mode 100644 packages/twenty-front/src/modules/object-record/utils/getUpdateOneRecordMutationResponseField.ts diff --git a/packages/twenty-front/src/modules/activities/calendar/components/CalendarEventDetails.tsx b/packages/twenty-front/src/modules/activities/calendar/components/CalendarEventDetails.tsx index 393b17971efa..5457fd1fed7c 100644 --- a/packages/twenty-front/src/modules/activities/calendar/components/CalendarEventDetails.tsx +++ b/packages/twenty-front/src/modules/activities/calendar/components/CalendarEventDetails.tsx @@ -3,7 +3,7 @@ import styled from '@emotion/styled'; import { IconCalendarEvent } from 'twenty-ui'; import { CalendarEvent } from '@/activities/calendar/types/CalendarEvent'; -import { useObjectMetadataItemOnly } from '@/object-metadata/hooks/useObjectMetadataItemOnly'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; import { formatFieldMetadataItemAsFieldDefinition } from '@/object-metadata/utils/formatFieldMetadataItemAsFieldDefinition'; import { FieldContext } from '@/object-record/record-field/contexts/FieldContext'; @@ -71,7 +71,7 @@ export const CalendarEventDetails = ({ calendarEvent, }: CalendarEventDetailsProps) => { const theme = useTheme(); - const { objectMetadataItem } = useObjectMetadataItemOnly({ + const { objectMetadataItem } = useObjectMetadataItem({ objectNameSingular: CoreObjectNameSingular.CalendarEvent, }); diff --git a/packages/twenty-front/src/modules/activities/components/ActivityBodyEditor.tsx b/packages/twenty-front/src/modules/activities/components/ActivityBodyEditor.tsx index 2dcde447ee42..0b3994278a6c 100644 --- a/packages/twenty-front/src/modules/activities/components/ActivityBodyEditor.tsx +++ b/packages/twenty-front/src/modules/activities/components/ActivityBodyEditor.tsx @@ -14,7 +14,7 @@ import { activityTitleHasBeenSetFamilyState } from '@/activities/states/activity import { canCreateActivityState } from '@/activities/states/canCreateActivityState'; import { Activity } from '@/activities/types/Activity'; import { ActivityEditorHotkeyScope } from '@/activities/types/ActivityEditorHotkeyScope'; -import { useObjectMetadataItemOnly } from '@/object-metadata/hooks/useObjectMetadataItemOnly'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; import { modifyRecordFromCache } from '@/object-record/cache/utils/modifyRecordFromCache'; import { recordStoreFamilyState } from '@/object-record/record-store/states/recordStoreFamilyState'; @@ -58,7 +58,7 @@ export const ActivityBodyEditor = ({ ); const { objectMetadataItem: objectMetadataItemActivity } = - useObjectMetadataItemOnly({ + useObjectMetadataItem({ objectNameSingular: CoreObjectNameSingular.Activity, }); diff --git a/packages/twenty-front/src/modules/activities/components/ActivityEditorFields.tsx b/packages/twenty-front/src/modules/activities/components/ActivityEditorFields.tsx index 4b370a3b24bd..31ae39ceef38 100644 --- a/packages/twenty-front/src/modules/activities/components/ActivityEditorFields.tsx +++ b/packages/twenty-front/src/modules/activities/components/ActivityEditorFields.tsx @@ -4,7 +4,6 @@ import { useRecoilValue } from 'recoil'; import { useUpsertActivity } from '@/activities/hooks/useUpsertActivity'; import { ActivityTargetsInlineCell } from '@/activities/inline-cell/components/ActivityTargetsInlineCell'; import { Activity } from '@/activities/types/Activity'; -import { useObjectMetadataItemOnly } from '@/object-metadata/hooks/useObjectMetadataItemOnly'; import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; import { useGetRecordFromCache } from '@/object-record/cache/hooks/useGetRecordFromCache'; import { useFieldContext } from '@/object-record/hooks/useFieldContext'; @@ -28,12 +27,8 @@ export const ActivityEditorFields = ({ }) => { const { upsertActivity } = useUpsertActivity(); - const { objectMetadataItem } = useObjectMetadataItemOnly({ - objectNameSingular: CoreObjectNameSingular.Activity, - }); - const getRecordFromCache = useGetRecordFromCache({ - objectMetadataItem, + objectNameSingular: CoreObjectNameSingular.Activity, }); const activityFromCache = getRecordFromCache(activityId); diff --git a/packages/twenty-front/src/modules/activities/components/ActivityTitle.tsx b/packages/twenty-front/src/modules/activities/components/ActivityTitle.tsx index dd7ec9f97d49..288447dbec41 100644 --- a/packages/twenty-front/src/modules/activities/components/ActivityTitle.tsx +++ b/packages/twenty-front/src/modules/activities/components/ActivityTitle.tsx @@ -12,7 +12,7 @@ import { activityTitleHasBeenSetFamilyState } from '@/activities/states/activity import { canCreateActivityState } from '@/activities/states/canCreateActivityState'; import { Activity } from '@/activities/types/Activity'; import { ActivityEditorHotkeyScope } from '@/activities/types/ActivityEditorHotkeyScope'; -import { useObjectMetadataItemOnly } from '@/object-metadata/hooks/useObjectMetadataItemOnly'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; import { modifyRecordFromCache } from '@/object-record/cache/utils/modifyRecordFromCache'; import { recordStoreFamilyState } from '@/object-record/record-store/states/recordStoreFamilyState'; @@ -111,7 +111,7 @@ export const ActivityTitle = ({ activityId }: ActivityTitleProps) => { ); const { objectMetadataItem: objectMetadataItemActivity } = - useObjectMetadataItemOnly({ + useObjectMetadataItem({ objectNameSingular: CoreObjectNameSingular.Activity, }); diff --git a/packages/twenty-front/src/modules/activities/hooks/useActivityTargetObjectRecords.ts b/packages/twenty-front/src/modules/activities/hooks/useActivityTargetObjectRecords.ts index 455cb965593a..7f403d89dd4d 100644 --- a/packages/twenty-front/src/modules/activities/hooks/useActivityTargetObjectRecords.ts +++ b/packages/twenty-front/src/modules/activities/hooks/useActivityTargetObjectRecords.ts @@ -4,7 +4,6 @@ import { useRecoilValue } from 'recoil'; import { Activity } from '@/activities/types/Activity'; import { ActivityTarget } from '@/activities/types/ActivityTarget'; import { ActivityTargetWithTargetRecord } from '@/activities/types/ActivityTargetObject'; -import { useObjectMetadataItemOnly } from '@/object-metadata/hooks/useObjectMetadataItemOnly'; import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState'; import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; import { useGetRecordFromCache } from '@/object-record/cache/hooks/useGetRecordFromCache'; @@ -16,13 +15,8 @@ export const useActivityTargetObjectRecords = (activity: Activity) => { const activityTargets = activity.activityTargets ?? []; - const { objectMetadataItem: objectMetadataItemActivityTarget } = - useObjectMetadataItemOnly({ - objectNameSingular: CoreObjectNameSingular.ActivityTarget, - }); - const getRecordFromCache = useGetRecordFromCache({ - objectMetadataItem: objectMetadataItemActivityTarget, + objectNameSingular: CoreObjectNameSingular.ActivityTarget, }); const apolloClient = useApolloClient(); diff --git a/packages/twenty-front/src/modules/activities/hooks/useCreateActivityInCache.ts b/packages/twenty-front/src/modules/activities/hooks/useCreateActivityInCache.ts index c58b5a9ef6b2..8f54fdf1f7d5 100644 --- a/packages/twenty-front/src/modules/activities/hooks/useCreateActivityInCache.ts +++ b/packages/twenty-front/src/modules/activities/hooks/useCreateActivityInCache.ts @@ -7,7 +7,7 @@ import { ActivityTarget } from '@/activities/types/ActivityTarget'; import { ActivityTargetableObject } from '@/activities/types/ActivityTargetableEntity'; import { makeActivityTargetsToCreateFromTargetableObjects } from '@/activities/utils/getActivityTargetsToCreateFromTargetableObjects'; import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState'; -import { useObjectMetadataItemOnly } from '@/object-metadata/hooks/useObjectMetadataItemOnly'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState'; import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; import { useCreateManyRecordsInCache } from '@/object-record/cache/hooks/useCreateManyRecordsInCache'; @@ -38,12 +38,12 @@ export const useCreateActivityInCache = () => { }); const { objectMetadataItem: objectMetadataItemActivity } = - useObjectMetadataItemOnly({ + useObjectMetadataItem({ objectNameSingular: CoreObjectNameSingular.Activity, }); const { objectMetadataItem: objectMetadataItemActivityTarget } = - useObjectMetadataItemOnly({ + useObjectMetadataItem({ objectNameSingular: CoreObjectNameSingular.ActivityTarget, }); diff --git a/packages/twenty-front/src/modules/activities/hooks/usePrepareFindManyActivitiesQuery.ts b/packages/twenty-front/src/modules/activities/hooks/usePrepareFindManyActivitiesQuery.ts index c6771ec7eb37..9d2e50b837da 100644 --- a/packages/twenty-front/src/modules/activities/hooks/usePrepareFindManyActivitiesQuery.ts +++ b/packages/twenty-front/src/modules/activities/hooks/usePrepareFindManyActivitiesQuery.ts @@ -4,7 +4,7 @@ import { FIND_MANY_ACTIVITIES_QUERY_KEY } from '@/activities/query-keys/FindMany import { Activity } from '@/activities/types/Activity'; import { ActivityTarget } from '@/activities/types/ActivityTarget'; import { ActivityTargetableObject } from '@/activities/types/ActivityTargetableEntity'; -import { useObjectMetadataItemOnly } from '@/object-metadata/hooks/useObjectMetadataItemOnly'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems'; import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; import { useGetRecordFromCache } from '@/object-record/cache/hooks/useGetRecordFromCache'; @@ -16,12 +16,12 @@ import { isDefined } from '~/utils/isDefined'; export const usePrepareFindManyActivitiesQuery = () => { const { objectMetadataItem: objectMetadataItemActivity } = - useObjectMetadataItemOnly({ + useObjectMetadataItem({ objectNameSingular: CoreObjectNameSingular.Activity, }); const getActivityFromCache = useGetRecordFromCache({ - objectMetadataItem: objectMetadataItemActivity, + objectNameSingular: CoreObjectNameSingular.Activity, }); const cache = useApolloClient().cache; diff --git a/packages/twenty-front/src/modules/activities/inline-cell/components/ActivityTargetInlineCellEditMode.tsx b/packages/twenty-front/src/modules/activities/inline-cell/components/ActivityTargetInlineCellEditMode.tsx index bb971eb92b24..16a51164a88f 100644 --- a/packages/twenty-front/src/modules/activities/inline-cell/components/ActivityTargetInlineCellEditMode.tsx +++ b/packages/twenty-front/src/modules/activities/inline-cell/components/ActivityTargetInlineCellEditMode.tsx @@ -10,7 +10,7 @@ import { ActivityTarget } from '@/activities/types/ActivityTarget'; import { ActivityTargetWithTargetRecord } from '@/activities/types/ActivityTargetObject'; import { getActivityTargetObjectFieldIdName } from '@/activities/utils/getActivityTargetObjectFieldIdName'; import { getActivityTargetObjectFieldName } from '@/activities/utils/getActivityTargetObjectFieldName'; -import { useObjectMetadataItemOnly } from '@/object-metadata/hooks/useObjectMetadataItemOnly'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; import { useCreateManyRecordsInCache } from '@/object-record/cache/hooks/useCreateManyRecordsInCache'; import { useCreateManyRecords } from '@/object-record/hooks/useCreateManyRecords'; @@ -61,7 +61,7 @@ export const ActivityTargetInlineCellEditMode = ({ const { upsertActivity } = useUpsertActivity(); const { objectMetadataItem: objectMetadataItemActivityTarget } = - useObjectMetadataItemOnly({ + useObjectMetadataItem({ objectNameSingular: CoreObjectNameSingular.ActivityTarget, }); diff --git a/packages/twenty-front/src/modules/object-metadata/hooks/__tests__/useGetObjectOrderByField.test.ts b/packages/twenty-front/src/modules/object-metadata/hooks/__tests__/useGetObjectOrderByField.test.ts deleted file mode 100644 index c71a90358fb1..000000000000 --- a/packages/twenty-front/src/modules/object-metadata/hooks/__tests__/useGetObjectOrderByField.test.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { renderHook } from '@testing-library/react'; - -import { useGetObjectOrderByField } from '@/object-metadata/hooks/useGetObjectOrderByField'; -import { getObjectMetadataItemsMock } from '@/object-metadata/utils/getObjectMetadataItemsMock'; - -const mockObjectMetadataItems = getObjectMetadataItemsMock(); - -describe('useGetObjectOrderByField', () => { - it('should work as expected', () => { - const objectMetadataItem = mockObjectMetadataItems.find( - (item) => item.nameSingular === 'person', - )!; - - const { result } = renderHook(() => - useGetObjectOrderByField({ objectMetadataItem })('AscNullsLast'), - ); - expect(result.current).toEqual({ - name: { firstName: 'AscNullsLast', lastName: 'AscNullsLast' }, - }); - }); -}); diff --git a/packages/twenty-front/src/modules/object-metadata/hooks/__tests__/useGetObjectOrderByField.test.tsx b/packages/twenty-front/src/modules/object-metadata/hooks/__tests__/useGetObjectOrderByField.test.tsx new file mode 100644 index 000000000000..646806c9762a --- /dev/null +++ b/packages/twenty-front/src/modules/object-metadata/hooks/__tests__/useGetObjectOrderByField.test.tsx @@ -0,0 +1,33 @@ +import { ReactNode } from 'react'; +import { MockedProvider } from '@apollo/client/testing'; +import { renderHook } from '@testing-library/react'; +import { RecoilRoot } from 'recoil'; + +import { useGetObjectOrderByField } from '@/object-metadata/hooks/useGetObjectOrderByField'; + +const Wrapper = ({ children }: { children: ReactNode }) => ( + + {children} + +); + +describe('useGetObjectOrderByField', () => { + it('should work as expected', () => { + const { result } = renderHook( + () => { + const { getObjectOrderByField } = useGetObjectOrderByField({ + objectNameSingular: 'person', + }); + + return getObjectOrderByField('AscNullsLast'); + }, + { + wrapper: Wrapper, + }, + ); + + expect(result.current).toEqual({ + name: { firstName: 'AscNullsLast', lastName: 'AscNullsLast' }, + }); + }); +}); diff --git a/packages/twenty-front/src/modules/object-metadata/hooks/__tests__/useMapToObjectRecordIdentifier.test.tsx b/packages/twenty-front/src/modules/object-metadata/hooks/__tests__/useMapToObjectRecordIdentifier.test.tsx index 11fb35d6e7cf..17337fd905f6 100644 --- a/packages/twenty-front/src/modules/object-metadata/hooks/__tests__/useMapToObjectRecordIdentifier.test.tsx +++ b/packages/twenty-front/src/modules/object-metadata/hooks/__tests__/useMapToObjectRecordIdentifier.test.tsx @@ -2,21 +2,19 @@ import { renderHook } from '@testing-library/react'; import { RecoilRoot } from 'recoil'; import { useMapToObjectRecordIdentifier } from '@/object-metadata/hooks/useMapToObjectRecordIdentifier'; -import { getObjectMetadataItemsMock } from '@/object-metadata/utils/getObjectMetadataItemsMock'; - -const mockObjectMetadataItems = getObjectMetadataItemsMock(); describe('useMapToObjectRecordIdentifier', () => { it('should work as expected', async () => { const { result } = renderHook( () => { - const objectMetadataItem = mockObjectMetadataItems.find( - (item) => item.nameSingular === 'person', - )!; + const { mapToObjectRecordIdentifier } = useMapToObjectRecordIdentifier({ + objectNameSingular: 'person', + }); - return useMapToObjectRecordIdentifier({ - objectMetadataItem, - })({ id: 'id', name: { firstName: 'Sheldon', lastName: 'Cooper' } }); + return mapToObjectRecordIdentifier({ + id: 'id', + name: { firstName: 'Sheldon', lastName: 'Cooper' }, + }); }, { wrapper: RecoilRoot, diff --git a/packages/twenty-front/src/modules/object-metadata/hooks/__tests__/useObjectMetadataItem.test.tsx b/packages/twenty-front/src/modules/object-metadata/hooks/__tests__/useObjectMetadataItem.test.tsx index 6ea19e3bda54..7c232e600e3d 100644 --- a/packages/twenty-front/src/modules/object-metadata/hooks/__tests__/useObjectMetadataItem.test.tsx +++ b/packages/twenty-front/src/modules/object-metadata/hooks/__tests__/useObjectMetadataItem.test.tsx @@ -11,6 +11,7 @@ const Wrapper = ({ children }: { children: ReactNode }) => ( ); +// Split into tests for each new hook describe('useObjectMetadataItem', () => { it('should return correct properties', async () => { const { result } = renderHook( @@ -20,39 +21,8 @@ describe('useObjectMetadataItem', () => { }, ); - const { - basePathToShowPage, - objectMetadataItem, - labelIdentifierFieldMetadata, - getRecordFromCache, - findManyRecordsQuery, - findOneRecordQuery, - createOneRecordMutation, - updateOneRecordMutation, - deleteOneRecordMutation, - executeQuickActionOnOneRecordMutation, - createManyRecordsMutation, - deleteManyRecordsMutation, - mapToObjectRecordIdentifier, - getObjectOrderByField, - } = result.current; + const { objectMetadataItem } = result.current; - expect(labelIdentifierFieldMetadata).toBeUndefined(); - expect(basePathToShowPage).toBe('/object/opportunity/'); expect(objectMetadataItem.id).toBe('20202020-cae9-4ff4-9579-f7d9fe44c937'); - expect(typeof getRecordFromCache).toBe('function'); - expect(typeof mapToObjectRecordIdentifier).toBe('function'); - expect(typeof getObjectOrderByField).toBe('function'); - expect(findManyRecordsQuery).toHaveProperty('kind', 'Document'); - expect(findOneRecordQuery).toHaveProperty('kind', 'Document'); - expect(createOneRecordMutation).toHaveProperty('kind', 'Document'); - expect(updateOneRecordMutation).toHaveProperty('kind', 'Document'); - expect(deleteOneRecordMutation).toHaveProperty('kind', 'Document'); - expect(executeQuickActionOnOneRecordMutation).toHaveProperty( - 'kind', - 'Document', - ); - expect(createManyRecordsMutation).toHaveProperty('kind', 'Document'); - expect(deleteManyRecordsMutation).toHaveProperty('kind', 'Document'); }); }); diff --git a/packages/twenty-front/src/modules/object-metadata/hooks/useCreateOneObjectMetadataItem.ts b/packages/twenty-front/src/modules/object-metadata/hooks/useCreateOneObjectMetadataItem.ts index 7e4f54f65203..19df20de7c30 100644 --- a/packages/twenty-front/src/modules/object-metadata/hooks/useCreateOneObjectMetadataItem.ts +++ b/packages/twenty-front/src/modules/object-metadata/hooks/useCreateOneObjectMetadataItem.ts @@ -1,8 +1,8 @@ import { ApolloClient, useApolloClient, useMutation } from '@apollo/client'; import { getOperationName } from '@apollo/client/utilities'; -import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; +import { useFindManyRecordsQuery } from '@/object-record/hooks/useFindManyRecordsQuery'; import { CreateObjectInput, CreateOneObjectMetadataItemMutation, @@ -17,7 +17,8 @@ import { useApolloMetadataClient } from './useApolloMetadataClient'; export const useCreateOneObjectMetadataItem = () => { const apolloMetadataClient = useApolloMetadataClient(); const apolloClient = useApolloClient(); - const { findManyRecordsQuery } = useObjectMetadataItem({ + + const { findManyRecordsQuery } = useFindManyRecordsQuery({ objectNameSingular: CoreObjectNameSingular.View, }); diff --git a/packages/twenty-front/src/modules/object-metadata/hooks/useGetObjectOrderByField.ts b/packages/twenty-front/src/modules/object-metadata/hooks/useGetObjectOrderByField.ts index bd7bd3702058..111058a5efec 100644 --- a/packages/twenty-front/src/modules/object-metadata/hooks/useGetObjectOrderByField.ts +++ b/packages/twenty-front/src/modules/object-metadata/hooks/useGetObjectOrderByField.ts @@ -1,14 +1,20 @@ -import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { OrderBy } from '@/object-metadata/types/OrderBy'; import { OrderByField } from '@/object-metadata/types/OrderByField'; -import { getObjectOrderByField } from '@/object-metadata/utils/getObjectOrderByField'; +import { getOrderByFieldForObjectMetadataItem } from '@/object-metadata/utils/getObjectOrderByField'; export const useGetObjectOrderByField = ({ - objectMetadataItem, + objectNameSingular, }: { - objectMetadataItem: ObjectMetadataItem; + objectNameSingular: string; }) => { - return (orderBy: OrderBy): OrderByField => { - return getObjectOrderByField(objectMetadataItem, orderBy); + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + + const getObjectOrderByField = (orderBy: OrderBy): OrderByField => { + return getOrderByFieldForObjectMetadataItem(objectMetadataItem, orderBy); }; + + return { getObjectOrderByField }; }; diff --git a/packages/twenty-front/src/modules/object-metadata/hooks/useLabelIdentifierFieldMetadataItem.ts b/packages/twenty-front/src/modules/object-metadata/hooks/useLabelIdentifierFieldMetadataItem.ts new file mode 100644 index 000000000000..9970267052f8 --- /dev/null +++ b/packages/twenty-front/src/modules/object-metadata/hooks/useLabelIdentifierFieldMetadataItem.ts @@ -0,0 +1,17 @@ +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; +import { getLabelIdentifierFieldMetadataItem } from '@/object-metadata/utils/getLabelIdentifierFieldMetadataItem'; + +export const useLabelIdentifierFieldMetadataItem = ({ + objectNameSingular, +}: { + objectNameSingular: string; +}) => { + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + + const labelIdentifierFieldMetadataItem = + getLabelIdentifierFieldMetadataItem(objectMetadataItem); + + return { labelIdentifierFieldMetadataItem }; +}; diff --git a/packages/twenty-front/src/modules/object-metadata/hooks/useMapToObjectRecordIdentifier.ts b/packages/twenty-front/src/modules/object-metadata/hooks/useMapToObjectRecordIdentifier.ts index f25b74293374..4de7ad2c9ea8 100644 --- a/packages/twenty-front/src/modules/object-metadata/hooks/useMapToObjectRecordIdentifier.ts +++ b/packages/twenty-front/src/modules/object-metadata/hooks/useMapToObjectRecordIdentifier.ts @@ -1,8 +1,19 @@ -import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { getObjectRecordIdentifier } from '@/object-metadata/utils/getObjectRecordIdentifier'; import { ObjectRecord } from '@/object-record/types/ObjectRecord'; -export const useMapToObjectRecordIdentifier = - ({ objectMetadataItem }: { objectMetadataItem: ObjectMetadataItem }) => - (record: ObjectRecord) => - getObjectRecordIdentifier({ objectMetadataItem, record }); +export const useMapToObjectRecordIdentifier = ({ + objectNameSingular, +}: { + objectNameSingular: string; +}) => { + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + + const mapToObjectRecordIdentifier = (record: ObjectRecord) => { + return getObjectRecordIdentifier({ objectMetadataItem, record }); + }; + + return { mapToObjectRecordIdentifier }; +}; diff --git a/packages/twenty-front/src/modules/object-metadata/hooks/useObjectMetadataItem.ts b/packages/twenty-front/src/modules/object-metadata/hooks/useObjectMetadataItem.ts index 4f0ce1e765c2..5c8472a54e47 100644 --- a/packages/twenty-front/src/modules/object-metadata/hooks/useObjectMetadataItem.ts +++ b/packages/twenty-front/src/modules/object-metadata/hooks/useObjectMetadataItem.ts @@ -1,47 +1,17 @@ -import { gql } from '@apollo/client'; import { useRecoilValue } from 'recoil'; import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState.ts'; import { ObjectMetadataItemNotFoundError } from '@/object-metadata/errors/ObjectMetadataNotFoundError'; -import { useGetObjectOrderByField } from '@/object-metadata/hooks/useGetObjectOrderByField'; -import { useMapToObjectRecordIdentifier } from '@/object-metadata/hooks/useMapToObjectRecordIdentifier'; import { objectMetadataItemFamilySelector } from '@/object-metadata/states/objectMetadataItemFamilySelector'; import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState'; -import { getBasePathToShowPage } from '@/object-metadata/utils/getBasePathToShowPage'; -import { getLabelIdentifierFieldMetadataItem } from '@/object-metadata/utils/getLabelIdentifierFieldMetadataItem'; import { getObjectMetadataItemsMock } from '@/object-metadata/utils/getObjectMetadataItemsMock'; -import { useGetRecordFromCache } from '@/object-record/cache/hooks/useGetRecordFromCache'; -import { useGenerateCreateManyRecordMutation } from '@/object-record/hooks/useGenerateCreateManyRecordMutation'; -import { useGenerateCreateOneRecordMutation } from '@/object-record/hooks/useGenerateCreateOneRecordMutation'; -import { useGenerateDeleteManyRecordMutation } from '@/object-record/hooks/useGenerateDeleteManyRecordMutation'; -import { useGenerateExecuteQuickActionOnOneRecordMutation } from '@/object-record/hooks/useGenerateExecuteQuickActionOnOneRecordMutation'; -import { useGenerateFindDuplicateRecordsQuery } from '@/object-record/hooks/useGenerateFindDuplicateRecordsQuery'; -import { useGenerateFindManyRecordsQuery } from '@/object-record/hooks/useGenerateFindManyRecordsQuery'; -import { useGenerateFindOneRecordQuery } from '@/object-record/hooks/useGenerateFindOneRecordQuery'; -import { useGenerateUpdateOneRecordMutation } from '@/object-record/hooks/useGenerateUpdateOneRecordMutation'; -import { generateDeleteOneRecordMutation } from '@/object-record/utils/generateDeleteOneRecordMutation'; import { isDefined } from '~/utils/isDefined'; import { ObjectMetadataItemIdentifier } from '../types/ObjectMetadataItemIdentifier'; -export const EMPTY_QUERY = gql` - query EmptyQuery { - empty - } -`; - -export const EMPTY_MUTATION = gql` - mutation EmptyMutation { - empty - } -`; - -export const useObjectMetadataItem = ( - { objectNameSingular }: ObjectMetadataItemIdentifier, - depth?: number, - queryFields?: Record, - computeReferences = false, -) => { +export const useObjectMetadataItem = ({ + objectNameSingular, +}: ObjectMetadataItemIdentifier) => { const currentWorkspace = useRecoilValue(currentWorkspaceState); const mockObjectMetadataItems = getObjectMetadataItemsMock(); @@ -71,89 +41,7 @@ export const useObjectMetadataItem = ( ); } - const mapToObjectRecordIdentifier = useMapToObjectRecordIdentifier({ - objectMetadataItem, - }); - - const getObjectOrderByField = useGetObjectOrderByField({ - objectMetadataItem, - }); - - const getRecordFromCache = useGetRecordFromCache({ - objectMetadataItem, - }); - - const generateFindManyRecordsQuery = useGenerateFindManyRecordsQuery(); - const findManyRecordsQuery = generateFindManyRecordsQuery({ - objectMetadataItem, - depth, - queryFields, - }); - - const generateFindDuplicateRecordsQuery = - useGenerateFindDuplicateRecordsQuery(); - const findDuplicateRecordsQuery = generateFindDuplicateRecordsQuery({ - objectMetadataItem, - depth, - }); - - const generateFindOneRecordQuery = useGenerateFindOneRecordQuery(); - const findOneRecordQuery = generateFindOneRecordQuery({ - objectMetadataItem, - depth, - }); - - const createOneRecordMutation = useGenerateCreateOneRecordMutation({ - objectMetadataItem, - depth, - }); - - const createManyRecordsMutation = useGenerateCreateManyRecordMutation({ - objectMetadataItem, - depth, - }); - - const updateOneRecordMutation = useGenerateUpdateOneRecordMutation({ - objectMetadataItem, - depth, - computeReferences, - }); - - const deleteOneRecordMutation = generateDeleteOneRecordMutation({ - objectMetadataItem, - }); - - const deleteManyRecordsMutation = useGenerateDeleteManyRecordMutation({ - objectMetadataItem, - }); - - const executeQuickActionOnOneRecordMutation = - useGenerateExecuteQuickActionOnOneRecordMutation({ - objectMetadataItem, - }); - - const labelIdentifierFieldMetadata = - getLabelIdentifierFieldMetadataItem(objectMetadataItem); - - const basePathToShowPage = getBasePathToShowPage({ - objectMetadataItem, - }); - return { - labelIdentifierFieldMetadata, - basePathToShowPage, objectMetadataItem, - getRecordFromCache, - findManyRecordsQuery, - findDuplicateRecordsQuery, - findOneRecordQuery, - createOneRecordMutation, - updateOneRecordMutation, - deleteOneRecordMutation, - executeQuickActionOnOneRecordMutation, - createManyRecordsMutation, - deleteManyRecordsMutation, - mapToObjectRecordIdentifier, - getObjectOrderByField, }; }; diff --git a/packages/twenty-front/src/modules/object-metadata/hooks/useObjectMetadataItemOnly.ts b/packages/twenty-front/src/modules/object-metadata/hooks/useObjectMetadataItemOnly.ts deleted file mode 100644 index d5e5b3f552dc..000000000000 --- a/packages/twenty-front/src/modules/object-metadata/hooks/useObjectMetadataItemOnly.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { useRecoilValue } from 'recoil'; - -import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState.ts'; -import { ObjectMetadataItemNotFoundError } from '@/object-metadata/errors/ObjectMetadataNotFoundError'; -import { objectMetadataItemFamilySelector } from '@/object-metadata/states/objectMetadataItemFamilySelector'; -import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState'; -import { getObjectMetadataItemsMock } from '@/object-metadata/utils/getObjectMetadataItemsMock'; -import { isDefined } from '~/utils/isDefined'; - -import { ObjectMetadataItemIdentifier } from '../types/ObjectMetadataItemIdentifier'; - -export const useObjectMetadataItemOnly = ({ - objectNameSingular, -}: ObjectMetadataItemIdentifier) => { - const currentWorkspace = useRecoilValue(currentWorkspaceState); - - const mockObjectMetadataItems = getObjectMetadataItemsMock(); - - let objectMetadataItem = useRecoilValue( - objectMetadataItemFamilySelector({ - objectName: objectNameSingular, - objectNameType: 'singular', - }), - ); - - let objectMetadataItems = useRecoilValue(objectMetadataItemsState); - - if (currentWorkspace?.activationStatus !== 'active') { - objectMetadataItem = - mockObjectMetadataItems.find( - (objectMetadataItem) => - objectMetadataItem.nameSingular === objectNameSingular, - ) ?? null; - objectMetadataItems = mockObjectMetadataItems; - } - - if (!isDefined(objectMetadataItem)) { - throw new ObjectMetadataItemNotFoundError( - objectNameSingular, - objectMetadataItems, - ); - } - - return { - objectMetadataItem, - }; -}; diff --git a/packages/twenty-front/src/modules/object-metadata/utils/__tests__/getObjectOrderByField.test.ts b/packages/twenty-front/src/modules/object-metadata/utils/__tests__/getObjectOrderByField.test.ts index 591711e36683..7cecf5668b25 100644 --- a/packages/twenty-front/src/modules/object-metadata/utils/__tests__/getObjectOrderByField.test.ts +++ b/packages/twenty-front/src/modules/object-metadata/utils/__tests__/getObjectOrderByField.test.ts @@ -1,5 +1,5 @@ import { getObjectMetadataItemsMock } from '@/object-metadata/utils/getObjectMetadataItemsMock'; -import { getObjectOrderByField } from '@/object-metadata/utils/getObjectOrderByField'; +import { getOrderByFieldForObjectMetadataItem } from '@/object-metadata/utils/getObjectOrderByField'; const mockObjectMetadataItems = getObjectMetadataItemsMock(); @@ -8,7 +8,7 @@ describe('getObjectOrderByField', () => { const objectMetadataItem = mockObjectMetadataItems.find( (item) => item.nameSingular === 'person', )!; - const res = getObjectOrderByField(objectMetadataItem); + const res = getOrderByFieldForObjectMetadataItem(objectMetadataItem); expect(res).toEqual({ name: { firstName: 'AscNullsLast', lastName: 'AscNullsLast' }, }); diff --git a/packages/twenty-front/src/modules/object-metadata/utils/getBasePathToShowPage.ts b/packages/twenty-front/src/modules/object-metadata/utils/getBasePathToShowPage.ts index 7a42595acb0c..1d5999c97ad1 100644 --- a/packages/twenty-front/src/modules/object-metadata/utils/getBasePathToShowPage.ts +++ b/packages/twenty-front/src/modules/object-metadata/utils/getBasePathToShowPage.ts @@ -1,11 +1,9 @@ -import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; - export const getBasePathToShowPage = ({ - objectMetadataItem, + objectNameSingular, }: { - objectMetadataItem: ObjectMetadataItem; + objectNameSingular: string; }) => { - const basePathToShowPage = `/object/${objectMetadataItem.nameSingular}/`; + const basePathToShowPage = `/object/${objectNameSingular}/`; return basePathToShowPage; }; diff --git a/packages/twenty-front/src/modules/object-metadata/utils/getObjectOrderByField.ts b/packages/twenty-front/src/modules/object-metadata/utils/getObjectOrderByField.ts index 89fe7fa2b9f9..2219b1a0246b 100644 --- a/packages/twenty-front/src/modules/object-metadata/utils/getObjectOrderByField.ts +++ b/packages/twenty-front/src/modules/object-metadata/utils/getObjectOrderByField.ts @@ -5,7 +5,7 @@ import { getLabelIdentifierFieldMetadataItem } from '@/object-metadata/utils/get import { FieldMetadataType } from '~/generated-metadata/graphql'; import { isDefined } from '~/utils/isDefined'; -export const getObjectOrderByField = ( +export const getOrderByFieldForObjectMetadataItem = ( objectMetadataItem: ObjectMetadataItem, orderBy?: OrderBy | null, ): OrderByField => { diff --git a/packages/twenty-front/src/modules/object-metadata/utils/getObjectRecordIdentifier.ts b/packages/twenty-front/src/modules/object-metadata/utils/getObjectRecordIdentifier.ts index 41d4cea56a46..6b2fb1dfaee5 100644 --- a/packages/twenty-front/src/modules/object-metadata/utils/getObjectRecordIdentifier.ts +++ b/packages/twenty-front/src/modules/object-metadata/utils/getObjectRecordIdentifier.ts @@ -68,7 +68,7 @@ export const getObjectRecordIdentifier = ({ : imageIdentifierFieldValue) ?? ''; const basePathToShowPage = getBasePathToShowPage({ - objectMetadataItem, + objectNameSingular: objectMetadataItem.nameSingular, }); const isWorkspaceMemberObjectMetadata = diff --git a/packages/twenty-front/src/modules/object-record/cache/hooks/useCreateOneRecordInCache.ts b/packages/twenty-front/src/modules/object-record/cache/hooks/useCreateOneRecordInCache.ts index de0e241355d0..4ee8ad510f88 100644 --- a/packages/twenty-front/src/modules/object-record/cache/hooks/useCreateOneRecordInCache.ts +++ b/packages/twenty-front/src/modules/object-record/cache/hooks/useCreateOneRecordInCache.ts @@ -17,7 +17,7 @@ export const useCreateOneRecordInCache = ({ objectMetadataItem: ObjectMetadataItem; }) => { const getRecordFromCache = useGetRecordFromCache({ - objectMetadataItem, + objectNameSingular: objectMetadataItem.nameSingular, }); const objectMetadataItems = useRecoilValue(objectMetadataItemsState); const apolloClient = useApolloClient(); diff --git a/packages/twenty-front/src/modules/object-record/cache/hooks/useDeleteRecordFromCache.ts b/packages/twenty-front/src/modules/object-record/cache/hooks/useDeleteRecordFromCache.ts index 427a5e86aa8a..693236975e51 100644 --- a/packages/twenty-front/src/modules/object-record/cache/hooks/useDeleteRecordFromCache.ts +++ b/packages/twenty-front/src/modules/object-record/cache/hooks/useDeleteRecordFromCache.ts @@ -1,6 +1,6 @@ import { useApolloClient } from '@apollo/client'; -import { useObjectMetadataItemOnly } from '@/object-metadata/hooks/useObjectMetadataItemOnly'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems'; import { deleteRecordFromCache } from '@/object-record/cache/utils/deleteRecordFromCache'; import { ObjectRecord } from '@/object-record/types/ObjectRecord'; @@ -12,7 +12,7 @@ export const useDeleteRecordFromCache = ({ }) => { const apolloClient = useApolloClient(); - const { objectMetadataItem } = useObjectMetadataItemOnly({ + const { objectMetadataItem } = useObjectMetadataItem({ objectNameSingular, }); diff --git a/packages/twenty-front/src/modules/object-record/cache/hooks/useGetRecordFromCache.ts b/packages/twenty-front/src/modules/object-record/cache/hooks/useGetRecordFromCache.ts index 5fe2cf5b2984..edf234883af8 100644 --- a/packages/twenty-front/src/modules/object-record/cache/hooks/useGetRecordFromCache.ts +++ b/packages/twenty-front/src/modules/object-record/cache/hooks/useGetRecordFromCache.ts @@ -2,16 +2,20 @@ import { useCallback } from 'react'; import { useApolloClient } from '@apollo/client'; import { useRecoilValue } from 'recoil'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState'; -import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; import { getRecordFromCache } from '@/object-record/cache/utils/getRecordFromCache'; import { ObjectRecord } from '@/object-record/types/ObjectRecord'; export const useGetRecordFromCache = ({ - objectMetadataItem, + objectNameSingular, }: { - objectMetadataItem: ObjectMetadataItem; + objectNameSingular: string; }) => { + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + const objectMetadataItems = useRecoilValue(objectMetadataItemsState); const apolloClient = useApolloClient(); diff --git a/packages/twenty-front/src/modules/object-record/cache/hooks/useReadFindManyRecordsQueryInCache.ts b/packages/twenty-front/src/modules/object-record/cache/hooks/useReadFindManyRecordsQueryInCache.ts index d680582e96c0..405815034d4d 100644 --- a/packages/twenty-front/src/modules/object-record/cache/hooks/useReadFindManyRecordsQueryInCache.ts +++ b/packages/twenty-front/src/modules/object-record/cache/hooks/useReadFindManyRecordsQueryInCache.ts @@ -1,11 +1,12 @@ import { useApolloClient } from '@apollo/client'; +import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems'; import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; import { getRecordsFromRecordConnection } from '@/object-record/cache/utils/getRecordsFromRecordConnection'; -import { useGenerateFindManyRecordsQuery } from '@/object-record/hooks/useGenerateFindManyRecordsQuery'; import { ObjectRecord } from '@/object-record/types/ObjectRecord'; import { ObjectRecordQueryResult } from '@/object-record/types/ObjectRecordQueryResult'; import { ObjectRecordQueryVariables } from '@/object-record/types/ObjectRecordQueryVariables'; +import { generateFindManyRecordsQuery } from '@/object-record/utils/generateFindManyRecordsQuery'; import { isDefined } from '~/utils/isDefined'; export const useReadFindManyRecordsQueryInCache = ({ @@ -15,7 +16,7 @@ export const useReadFindManyRecordsQueryInCache = ({ }) => { const apolloClient = useApolloClient(); - const generateFindManyRecordsQuery = useGenerateFindManyRecordsQuery(); + const { objectMetadataItems } = useObjectMetadataItems(); const readFindManyRecordsQueryInCache = < T extends ObjectRecord = ObjectRecord, @@ -30,6 +31,7 @@ export const useReadFindManyRecordsQueryInCache = ({ }) => { const findManyRecordsQueryForCacheRead = generateFindManyRecordsQuery({ objectMetadataItem, + objectMetadataItems, queryFields, depth, }); diff --git a/packages/twenty-front/src/modules/object-record/cache/hooks/useUpsertFindManyRecordsQueryInCache.ts b/packages/twenty-front/src/modules/object-record/cache/hooks/useUpsertFindManyRecordsQueryInCache.ts index 83bd27859275..01be96226139 100644 --- a/packages/twenty-front/src/modules/object-record/cache/hooks/useUpsertFindManyRecordsQueryInCache.ts +++ b/packages/twenty-front/src/modules/object-record/cache/hooks/useUpsertFindManyRecordsQueryInCache.ts @@ -5,21 +5,17 @@ import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadat import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; import { MAX_QUERY_DEPTH_FOR_CACHE_INJECTION } from '@/object-record/cache/constants/MaxQueryDepthForCacheInjection'; import { getRecordConnectionFromRecords } from '@/object-record/cache/utils/getRecordConnectionFromRecords'; -import { useGenerateFindManyRecordsQuery } from '@/object-record/hooks/useGenerateFindManyRecordsQuery'; import { ObjectRecord } from '@/object-record/types/ObjectRecord'; import { ObjectRecordQueryVariables } from '@/object-record/types/ObjectRecordQueryVariables'; +import { generateFindManyRecordsQuery } from '@/object-record/utils/generateFindManyRecordsQuery'; export const useUpsertFindManyRecordsQueryInCache = ({ objectMetadataItem, }: { - objectMetadataItem: Pick< - ObjectMetadataItem, - 'fields' | 'namePlural' | 'nameSingular' - >; + objectMetadataItem: ObjectMetadataItem; }) => { const apolloClient = useApolloClient(); - const generateFindManyRecordsQuery = useGenerateFindManyRecordsQuery(); const objectMetadataItems = useRecoilValue(objectMetadataItemsState); const upsertFindManyRecordsQueryInCache = < @@ -39,6 +35,7 @@ export const useUpsertFindManyRecordsQueryInCache = ({ }) => { const findManyRecordsQueryForCacheOverwrite = generateFindManyRecordsQuery({ objectMetadataItem, + objectMetadataItems, depth, queryFields, computeReferences, diff --git a/packages/twenty-front/src/modules/object-record/components/RecordChip.tsx b/packages/twenty-front/src/modules/object-record/components/RecordChip.tsx index 0da8ecf3b793..20ef7dd1eaf9 100644 --- a/packages/twenty-front/src/modules/object-record/components/RecordChip.tsx +++ b/packages/twenty-front/src/modules/object-record/components/RecordChip.tsx @@ -1,7 +1,6 @@ import * as React from 'react'; import { useMapToObjectRecordIdentifier } from '@/object-metadata/hooks/useMapToObjectRecordIdentifier'; -import { useObjectMetadataItemOnly } from '@/object-metadata/hooks/useObjectMetadataItemOnly'; import { ObjectRecord } from '@/object-record/types/ObjectRecord'; import { EntityChip } from '@/ui/display/chip/components/EntityChip'; @@ -18,14 +17,10 @@ export const RecordChip = ({ maxWidth, className, }: RecordChipProps) => { - const { objectMetadataItem } = useObjectMetadataItemOnly({ + const { mapToObjectRecordIdentifier } = useMapToObjectRecordIdentifier({ objectNameSingular, }); - const mapToObjectRecordIdentifier = useMapToObjectRecordIdentifier({ - objectMetadataItem, - }); - const objectRecordIdentifier = mapToObjectRecordIdentifier(record); return ( diff --git a/packages/twenty-front/src/modules/object-record/constants/EmptyMutation.ts b/packages/twenty-front/src/modules/object-record/constants/EmptyMutation.ts new file mode 100644 index 000000000000..21a255ac56f3 --- /dev/null +++ b/packages/twenty-front/src/modules/object-record/constants/EmptyMutation.ts @@ -0,0 +1,7 @@ +import gql from 'graphql-tag'; + +export const EMPTY_MUTATION = gql` + mutation EmptyMutation { + empty + } +`; diff --git a/packages/twenty-front/src/modules/object-record/constants/EmptyQuery.ts b/packages/twenty-front/src/modules/object-record/constants/EmptyQuery.ts new file mode 100644 index 000000000000..e046ec1726ad --- /dev/null +++ b/packages/twenty-front/src/modules/object-record/constants/EmptyQuery.ts @@ -0,0 +1,7 @@ +import gql from 'graphql-tag'; + +export const EMPTY_QUERY = gql` + query EmptyQuery { + empty + } +`; diff --git a/packages/twenty-front/src/modules/object-record/hooks/useCreateManyRecords.ts b/packages/twenty-front/src/modules/object-record/hooks/useCreateManyRecords.ts index e2926f2c89e5..1a62d1e46e49 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useCreateManyRecords.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useCreateManyRecords.ts @@ -6,11 +6,9 @@ import { CachedObjectRecord } from '@/apollo/types/CachedObjectRecord'; import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems'; import { useCreateOneRecordInCache } from '@/object-record/cache/hooks/useCreateOneRecordInCache'; -import { - getCreateManyRecordsMutationResponseField, - useGenerateCreateManyRecordMutation, -} from '@/object-record/hooks/useGenerateCreateManyRecordMutation'; +import { useCreateManyRecordsMutation } from '@/object-record/hooks/useCreateManyRecordsMutation'; import { ObjectRecord } from '@/object-record/types/ObjectRecord'; +import { getCreateManyRecordsMutationResponseField } from '@/object-record/utils/getCreateManyRecordsMutationResponseField'; import { sanitizeRecordInput } from '@/object-record/utils/sanitizeRecordInput'; import { isDefined } from '~/utils/isDefined'; @@ -35,8 +33,8 @@ export const useCreateManyRecords = < objectNameSingular, }); - const createManyRecordsMutation = useGenerateCreateManyRecordMutation({ - objectMetadataItem, + const { createManyRecordsMutation } = useCreateManyRecordsMutation({ + objectNameSingular, queryFields, depth, }); diff --git a/packages/twenty-front/src/modules/object-record/hooks/useGenerateCreateManyRecordMutation.ts b/packages/twenty-front/src/modules/object-record/hooks/useCreateManyRecordsMutation.ts similarity index 60% rename from packages/twenty-front/src/modules/object-record/hooks/useGenerateCreateManyRecordMutation.ts rename to packages/twenty-front/src/modules/object-record/hooks/useCreateManyRecordsMutation.ts index 91d33f9ad966..5c43384ad851 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useGenerateCreateManyRecordMutation.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useCreateManyRecordsMutation.ts @@ -1,37 +1,38 @@ -import { gql } from '@apollo/client'; +import gql from 'graphql-tag'; import { useRecoilValue } from 'recoil'; -import { EMPTY_MUTATION } from '@/object-metadata/hooks/useObjectMetadataItem'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState'; -import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; import { mapObjectMetadataToGraphQLQuery } from '@/object-metadata/utils/mapObjectMetadataToGraphQLQuery'; +import { EMPTY_MUTATION } from '@/object-record/constants/EmptyMutation'; +import { getCreateManyRecordsMutationResponseField } from '@/object-record/utils/getCreateManyRecordsMutationResponseField'; import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull'; import { capitalize } from '~/utils/string/capitalize'; -export const getCreateManyRecordsMutationResponseField = ( - objectNamePlural: string, -) => `create${capitalize(objectNamePlural)}`; - -export const useGenerateCreateManyRecordMutation = ({ - objectMetadataItem, +export const useCreateManyRecordsMutation = ({ + objectNameSingular, queryFields, - depth = 1, + depth, }: { - objectMetadataItem: ObjectMetadataItem; + objectNameSingular: string; queryFields?: Record; depth?: number; }) => { + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + const objectMetadataItems = useRecoilValue(objectMetadataItemsState); if (isUndefinedOrNull(objectMetadataItem)) { - return EMPTY_MUTATION; + return { createManyRecordsMutation: EMPTY_MUTATION }; } const mutationResponseField = getCreateManyRecordsMutationResponseField( objectMetadataItem.namePlural, ); - return gql` + const createManyRecordsMutation = gql` mutation Create${capitalize( objectMetadataItem.namePlural, )}($data: [${capitalize(objectMetadataItem.nameSingular)}CreateInput!]!) { @@ -42,4 +43,8 @@ export const useGenerateCreateManyRecordMutation = ({ depth, })} }`; + + return { + createManyRecordsMutation, + }; }; diff --git a/packages/twenty-front/src/modules/object-record/hooks/useCreateOneRecord.ts b/packages/twenty-front/src/modules/object-record/hooks/useCreateOneRecord.ts index 59b560fcd587..ba227173d7cf 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useCreateOneRecord.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useCreateOneRecord.ts @@ -6,11 +6,9 @@ import { CachedObjectRecord } from '@/apollo/types/CachedObjectRecord'; import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems'; import { useCreateOneRecordInCache } from '@/object-record/cache/hooks/useCreateOneRecordInCache'; -import { - getCreateOneRecordMutationResponseField, - useGenerateCreateOneRecordMutation, -} from '@/object-record/hooks/useGenerateCreateOneRecordMutation'; +import { useCreateOneRecordMutation } from '@/object-record/hooks/useCreateOneRecordMutation'; import { ObjectRecord } from '@/object-record/types/ObjectRecord'; +import { getCreateOneRecordMutationResponseField } from '@/object-record/utils/getCreateOneRecordMutationResponseField'; import { sanitizeRecordInput } from '@/object-record/utils/sanitizeRecordInput'; import { isDefined } from '~/utils/isDefined'; @@ -26,17 +24,17 @@ export const useCreateOneRecord = < >({ objectNameSingular, queryFields, - depth = 1, skipPostOptmisticEffect = false, }: useCreateOneRecordProps) => { const apolloClient = useApolloClient(); - const { objectMetadataItem } = useObjectMetadataItem({ objectNameSingular }); + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); - const createOneRecordMutation = useGenerateCreateOneRecordMutation({ - objectMetadataItem, + const { createOneRecordMutation } = useCreateOneRecordMutation({ + objectNameSingular, queryFields, - depth, }); const createOneRecordInCache = useCreateOneRecordInCache({ diff --git a/packages/twenty-front/src/modules/object-record/hooks/useGenerateCreateOneRecordMutation.ts b/packages/twenty-front/src/modules/object-record/hooks/useCreateOneRecordMutation.ts similarity index 62% rename from packages/twenty-front/src/modules/object-record/hooks/useGenerateCreateOneRecordMutation.ts rename to packages/twenty-front/src/modules/object-record/hooks/useCreateOneRecordMutation.ts index 291a15d853d5..679133685553 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useGenerateCreateOneRecordMutation.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useCreateOneRecordMutation.ts @@ -1,30 +1,31 @@ -import { gql } from '@apollo/client'; +import gql from 'graphql-tag'; import { useRecoilValue } from 'recoil'; -import { EMPTY_MUTATION } from '@/object-metadata/hooks/useObjectMetadataItem'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState'; -import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; import { mapObjectMetadataToGraphQLQuery } from '@/object-metadata/utils/mapObjectMetadataToGraphQLQuery'; +import { EMPTY_MUTATION } from '@/object-record/constants/EmptyMutation'; +import { getCreateOneRecordMutationResponseField } from '@/object-record/utils/getCreateOneRecordMutationResponseField'; import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull'; import { capitalize } from '~/utils/string/capitalize'; -export const getCreateOneRecordMutationResponseField = ( - objectNameSingular: string, -) => `create${capitalize(objectNameSingular)}`; - -export const useGenerateCreateOneRecordMutation = ({ - objectMetadataItem, +export const useCreateOneRecordMutation = ({ + objectNameSingular, queryFields, - depth = 1, + depth, }: { - objectMetadataItem: ObjectMetadataItem; + objectNameSingular: string; queryFields?: Record; depth?: number; }) => { + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + const objectMetadataItems = useRecoilValue(objectMetadataItemsState); if (isUndefinedOrNull(objectMetadataItem)) { - return EMPTY_MUTATION; + return { createOneRecordMutation: EMPTY_MUTATION }; } const capitalizedObjectName = capitalize(objectMetadataItem.nameSingular); @@ -33,7 +34,7 @@ export const useGenerateCreateOneRecordMutation = ({ objectMetadataItem.nameSingular, ); - return gql` + const createOneRecordMutation = gql` mutation CreateOne${capitalizedObjectName}($input: ${capitalizedObjectName}CreateInput!) { ${mutationResponseField}(data: $input) ${mapObjectMetadataToGraphQLQuery({ objectMetadataItems, @@ -43,4 +44,8 @@ export const useGenerateCreateOneRecordMutation = ({ })} } `; + + return { + createOneRecordMutation, + }; }; diff --git a/packages/twenty-front/src/modules/object-record/hooks/useDeleteManyRecords.ts b/packages/twenty-front/src/modules/object-record/hooks/useDeleteManyRecords.ts index b3236cc5d79f..f189d516da1b 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useDeleteManyRecords.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useDeleteManyRecords.ts @@ -3,7 +3,9 @@ import { useApolloClient } from '@apollo/client'; import { triggerDeleteRecordsOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerDeleteRecordsOptimisticEffect'; import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems'; -import { getDeleteManyRecordsMutationResponseField } from '@/object-record/hooks/useGenerateDeleteManyRecordMutation'; +import { useGetRecordFromCache } from '@/object-record/cache/hooks/useGetRecordFromCache'; +import { useDeleteManyRecordsMutation } from '@/object-record/hooks/useDeleteManyRecordsMutation'; +import { getDeleteManyRecordsMutationResponseField } from '@/object-record/utils/getDeleteManyRecordsMutationResponseField'; import { isDefined } from '~/utils/isDefined'; import { capitalize } from '~/utils/string/capitalize'; @@ -21,8 +23,17 @@ export const useDeleteManyRecords = ({ }: useDeleteOneRecordProps) => { const apolloClient = useApolloClient(); - const { objectMetadataItem, deleteManyRecordsMutation, getRecordFromCache } = - useObjectMetadataItem({ objectNameSingular }); + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + + const getRecordFromCache = useGetRecordFromCache({ + objectNameSingular, + }); + + const { deleteManyRecordsMutation } = useDeleteManyRecordsMutation({ + objectNameSingular, + }); const { objectMetadataItems } = useObjectMetadataItems(); diff --git a/packages/twenty-front/src/modules/object-record/hooks/useDeleteManyRecordsMutation.ts b/packages/twenty-front/src/modules/object-record/hooks/useDeleteManyRecordsMutation.ts new file mode 100644 index 000000000000..2e13249be8aa --- /dev/null +++ b/packages/twenty-front/src/modules/object-record/hooks/useDeleteManyRecordsMutation.ts @@ -0,0 +1,41 @@ +import gql from 'graphql-tag'; + +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; +import { EMPTY_MUTATION } from '@/object-record/constants/EmptyMutation'; +import { getDeleteManyRecordsMutationResponseField } from '@/object-record/utils/getDeleteManyRecordsMutationResponseField'; +import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull'; +import { capitalize } from '~/utils/string/capitalize'; + +export const useDeleteManyRecordsMutation = ({ + objectNameSingular, +}: { + objectNameSingular: string; +}) => { + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + + if (isUndefinedOrNull(objectMetadataItem)) { + return { deleteManyRecordsMutation: EMPTY_MUTATION }; + } + + const capitalizedObjectName = capitalize(objectMetadataItem.namePlural); + + const mutationResponseField = getDeleteManyRecordsMutationResponseField( + objectMetadataItem.namePlural, + ); + + const deleteManyRecordsMutation = gql` + mutation DeleteMany${capitalizedObjectName}($filter: ${capitalize( + objectMetadataItem.nameSingular, + )}FilterInput!) { + ${mutationResponseField}(filter: $filter) { + id + } + } + `; + + return { + deleteManyRecordsMutation, + }; +}; diff --git a/packages/twenty-front/src/modules/object-record/hooks/useDeleteOneRecord.ts b/packages/twenty-front/src/modules/object-record/hooks/useDeleteOneRecord.ts index e43a30e85ade..bf7dcd778fd0 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useDeleteOneRecord.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useDeleteOneRecord.ts @@ -4,7 +4,9 @@ import { useApolloClient } from '@apollo/client'; import { triggerDeleteRecordsOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerDeleteRecordsOptimisticEffect'; import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems'; -import { getDeleteOneRecordMutationResponseField } from '@/object-record/utils/generateDeleteOneRecordMutation'; +import { useGetRecordFromCache } from '@/object-record/cache/hooks/useGetRecordFromCache'; +import { useDeleteOneRecordMutation } from '@/object-record/hooks/useDeleteOneRecordMutation'; +import { getDeleteOneRecordMutationResponseField } from '@/object-record/utils/getDeleteOneRecordMutationResponseField'; import { capitalize } from '~/utils/string/capitalize'; type useDeleteOneRecordProps = { @@ -17,8 +19,17 @@ export const useDeleteOneRecord = ({ }: useDeleteOneRecordProps) => { const apolloClient = useApolloClient(); - const { objectMetadataItem, deleteOneRecordMutation, getRecordFromCache } = - useObjectMetadataItem({ objectNameSingular }); + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + + const getRecordFromCache = useGetRecordFromCache({ + objectNameSingular, + }); + + const { deleteOneRecordMutation } = useDeleteOneRecordMutation({ + objectNameSingular, + }); const { objectMetadataItems } = useObjectMetadataItems(); diff --git a/packages/twenty-front/src/modules/object-record/hooks/useDeleteOneRecordMutation.ts b/packages/twenty-front/src/modules/object-record/hooks/useDeleteOneRecordMutation.ts new file mode 100644 index 000000000000..ea8a3d458c77 --- /dev/null +++ b/packages/twenty-front/src/modules/object-record/hooks/useDeleteOneRecordMutation.ts @@ -0,0 +1,39 @@ +import gql from 'graphql-tag'; + +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; +import { EMPTY_MUTATION } from '@/object-record/constants/EmptyMutation'; +import { getDeleteOneRecordMutationResponseField } from '@/object-record/utils/getDeleteOneRecordMutationResponseField'; +import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull'; +import { capitalize } from '~/utils/string/capitalize'; + +export const useDeleteOneRecordMutation = ({ + objectNameSingular, +}: { + objectNameSingular: string; +}) => { + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + + if (isUndefinedOrNull(objectMetadataItem)) { + return { deleteOneRecordMutation: EMPTY_MUTATION }; + } + + const capitalizedObjectName = capitalize(objectMetadataItem.nameSingular); + + const mutationResponseField = getDeleteOneRecordMutationResponseField( + objectMetadataItem.nameSingular, + ); + + const deleteOneRecordMutation = gql` + mutation DeleteOne${capitalizedObjectName}($idToDelete: ID!) { + ${mutationResponseField}(id: $idToDelete) { + id + } + } + `; + + return { + deleteOneRecordMutation, + }; +}; diff --git a/packages/twenty-front/src/modules/object-record/hooks/useExecuteQuickActionOnOneRecord.ts b/packages/twenty-front/src/modules/object-record/hooks/useExecuteQuickActionOnOneRecord.ts index fc9884473e70..10b67d64182a 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useExecuteQuickActionOnOneRecord.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useExecuteQuickActionOnOneRecord.ts @@ -3,6 +3,8 @@ import { useApolloClient } from '@apollo/client'; import { getOperationName } from '@apollo/client/utilities'; import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; +import { useExecuteQuickActionOnOneRecordMutation } from '@/object-record/hooks/useExecuteQuickActionOnOneRecordMutation'; +import { useFindManyRecordsQuery } from '@/object-record/hooks/useFindManyRecordsQuery'; import { capitalize } from '~/utils/string/capitalize'; type useExecuteQuickActionOnOneRecordProps = { @@ -12,11 +14,16 @@ type useExecuteQuickActionOnOneRecordProps = { export const useExecuteQuickActionOnOneRecord = ({ objectNameSingular, }: useExecuteQuickActionOnOneRecordProps) => { - const { - objectMetadataItem, - executeQuickActionOnOneRecordMutation, - findManyRecordsQuery, - } = useObjectMetadataItem({ + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + + const { executeQuickActionOnOneRecordMutation } = + useExecuteQuickActionOnOneRecordMutation({ + objectNameSingular, + }); + + const { findManyRecordsQuery } = useFindManyRecordsQuery({ objectNameSingular, }); diff --git a/packages/twenty-front/src/modules/object-record/hooks/useGenerateExecuteQuickActionOnOneRecordMutation.ts b/packages/twenty-front/src/modules/object-record/hooks/useExecuteQuickActionOnOneRecordMutation.ts similarity index 71% rename from packages/twenty-front/src/modules/object-record/hooks/useGenerateExecuteQuickActionOnOneRecordMutation.ts rename to packages/twenty-front/src/modules/object-record/hooks/useExecuteQuickActionOnOneRecordMutation.ts index 0eb9d6b6c7d2..4785029e819b 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useGenerateExecuteQuickActionOnOneRecordMutation.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useExecuteQuickActionOnOneRecordMutation.ts @@ -1,10 +1,10 @@ import { gql } from '@apollo/client'; import { useRecoilValue } from 'recoil'; -import { EMPTY_MUTATION } from '@/object-metadata/hooks/useObjectMetadataItem'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState'; -import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; import { mapObjectMetadataToGraphQLQuery } from '@/object-metadata/utils/mapObjectMetadataToGraphQLQuery'; +import { EMPTY_MUTATION } from '@/object-record/constants/EmptyMutation'; import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull'; import { capitalize } from '~/utils/string/capitalize'; @@ -16,15 +16,19 @@ export const getExecuteQuickActionOnOneRecordMutationGraphQLField = ({ return `executeQuickActionOn${capitalize(objectNameSingular)}`; }; -export const useGenerateExecuteQuickActionOnOneRecordMutation = ({ - objectMetadataItem, +export const useExecuteQuickActionOnOneRecordMutation = ({ + objectNameSingular, }: { - objectMetadataItem: ObjectMetadataItem; + objectNameSingular: string; }) => { + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + const objectMetadataItems = useRecoilValue(objectMetadataItemsState); if (isUndefinedOrNull(objectMetadataItem)) { - return EMPTY_MUTATION; + return { executeQuickActionOnOneRecordMutation: EMPTY_MUTATION }; } const capitalizedObjectName = capitalize(objectMetadataItem.nameSingular); @@ -34,7 +38,7 @@ export const useGenerateExecuteQuickActionOnOneRecordMutation = ({ objectNameSingular: objectMetadataItem.nameSingular, }); - return gql` + const executeQuickActionOnOneRecordMutation = gql` mutation ExecuteQuickActionOnOne${capitalizedObjectName}($idToExecuteQuickActionOn: ID!) { ${graphQLFieldForExecuteQuickActionOnOneRecordMutation}(id: $idToExecuteQuickActionOn) ${mapObjectMetadataToGraphQLQuery( { @@ -44,4 +48,6 @@ export const useGenerateExecuteQuickActionOnOneRecordMutation = ({ )} } `; + + return { executeQuickActionOnOneRecordMutation }; }; diff --git a/packages/twenty-front/src/modules/object-record/hooks/useFieldContext.tsx b/packages/twenty-front/src/modules/object-record/hooks/useFieldContext.tsx index 3429c83406e5..a2ea0646b173 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useFieldContext.tsx +++ b/packages/twenty-front/src/modules/object-record/hooks/useFieldContext.tsx @@ -2,6 +2,7 @@ import { ReactNode } from 'react'; import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { formatFieldMetadataItemAsColumnDefinition } from '@/object-metadata/utils/formatFieldMetadataItemAsColumnDefinition'; +import { getBasePathToShowPage } from '@/object-metadata/utils/getBasePathToShowPage'; import { useUpdateOneRecord } from '@/object-record/hooks/useUpdateOneRecord'; import { FieldContext, @@ -27,7 +28,11 @@ export const useFieldContext = ({ objectRecordId: string; customUseUpdateOneObjectHook?: RecordUpdateHook; }) => { - const { basePathToShowPage, objectMetadataItem } = useObjectMetadataItem({ + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + + const basePathToShowPage = getBasePathToShowPage({ objectNameSingular, }); diff --git a/packages/twenty-front/src/modules/object-record/hooks/useFindDuplicateRecords.ts b/packages/twenty-front/src/modules/object-record/hooks/useFindDuplicateRecords.ts index 19f0c38cc1f0..f1c2efbec9d7 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useFindDuplicateRecords.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useFindDuplicateRecords.ts @@ -4,9 +4,10 @@ import { useQuery } from '@apollo/client'; import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { ObjectMetadataItemIdentifier } from '@/object-metadata/types/ObjectMetadataItemIdentifier'; import { getRecordsFromRecordConnection } from '@/object-record/cache/utils/getRecordsFromRecordConnection'; -import { getFindDuplicateRecordsQueryResponseField } from '@/object-record/hooks/useGenerateFindDuplicateRecordsQuery'; +import { useFindDuplicateRecordsQuery } from '@/object-record/hooks/useFindDuplicatesRecordsQuery'; import { ObjectRecord } from '@/object-record/types/ObjectRecord'; import { ObjectRecordConnection } from '@/object-record/types/ObjectRecordConnection'; +import { getFindDuplicateRecordsQueryResponseField } from '@/object-record/utils/getFindDuplicateRecordsQueryResponseField'; import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar'; import { logError } from '~/utils/logError'; @@ -25,8 +26,14 @@ export const useFindDuplicateRecords = ({ }) => { const findDuplicateQueryStateIdentifier = objectNameSingular; - const { objectMetadataItem, findDuplicateRecordsQuery } = - useObjectMetadataItem({ objectNameSingular }, depth); + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + + const { findDuplicateRecordsQuery } = useFindDuplicateRecordsQuery({ + objectNameSingular, + depth, + }); const { enqueueSnackBar } = useSnackBar(); diff --git a/packages/twenty-front/src/modules/object-record/hooks/useGenerateFindDuplicateRecordsQuery.ts b/packages/twenty-front/src/modules/object-record/hooks/useFindDuplicatesRecordsQuery.ts similarity index 61% rename from packages/twenty-front/src/modules/object-record/hooks/useGenerateFindDuplicateRecordsQuery.ts rename to packages/twenty-front/src/modules/object-record/hooks/useFindDuplicatesRecordsQuery.ts index 747018d0da8d..12874fd59a55 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useGenerateFindDuplicateRecordsQuery.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useFindDuplicatesRecordsQuery.ts @@ -1,25 +1,26 @@ -import { gql } from '@apollo/client'; +import gql from 'graphql-tag'; import { useRecoilValue } from 'recoil'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState'; -import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; import { mapObjectMetadataToGraphQLQuery } from '@/object-metadata/utils/mapObjectMetadataToGraphQLQuery'; +import { getFindDuplicateRecordsQueryResponseField } from '@/object-record/utils/getFindDuplicateRecordsQueryResponseField'; import { capitalize } from '~/utils/string/capitalize'; -export const getFindDuplicateRecordsQueryResponseField = ( - objectNameSingular: string, -) => `${objectNameSingular}Duplicates`; +export const useFindDuplicateRecordsQuery = ({ + objectNameSingular, + depth, +}: { + objectNameSingular: string; + depth?: number; +}) => { + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); -export const useGenerateFindDuplicateRecordsQuery = () => { const objectMetadataItems = useRecoilValue(objectMetadataItemsState); - return ({ - objectMetadataItem, - depth, - }: { - objectMetadataItem: Pick; - depth?: number; - }) => gql` + const findDuplicateRecordsQuery = gql` query FindDuplicate${capitalize(objectMetadataItem.nameSingular)}($id: ID) { ${getFindDuplicateRecordsQueryResponseField( objectMetadataItem.nameSingular, @@ -41,4 +42,8 @@ export const useGenerateFindDuplicateRecordsQuery = () => { } } `; + + return { + findDuplicateRecordsQuery, + }; }; diff --git a/packages/twenty-front/src/modules/object-record/hooks/useFindManyRecords.ts b/packages/twenty-front/src/modules/object-record/hooks/useFindManyRecords.ts index ec39dd385b13..c32e0c17f9cd 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useFindManyRecords.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useFindManyRecords.ts @@ -8,6 +8,7 @@ import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMembe import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { ObjectMetadataItemIdentifier } from '@/object-metadata/types/ObjectMetadataItemIdentifier'; import { getRecordsFromRecordConnection } from '@/object-record/cache/utils/getRecordsFromRecordConnection'; +import { useFindManyRecordsQuery } from '@/object-record/hooks/useFindManyRecordsQuery'; import { ObjectRecord } from '@/object-record/types/ObjectRecord'; import { ObjectRecordConnection } from '@/object-record/types/ObjectRecordConnection'; import { ObjectRecordEdge } from '@/object-record/types/ObjectRecordEdge'; @@ -30,7 +31,6 @@ export const useFindManyRecords = ({ limit, onCompleted, skip, - depth = 1, queryFields, }: ObjectMetadataItemIdentifier & ObjectRecordQueryVariables & { @@ -63,13 +63,14 @@ export const useFindManyRecords = ({ isFetchingMoreRecordsFamilyState(findManyQueryStateIdentifier), ); - const { objectMetadataItem, findManyRecordsQuery } = useObjectMetadataItem( - { - objectNameSingular, - }, - depth, + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + + const { findManyRecordsQuery } = useFindManyRecordsQuery({ + objectNameSingular, queryFields, - ); + }); const { enqueueSnackBar } = useSnackBar(); const currentWorkspaceMember = useRecoilValue(currentWorkspaceMemberState); diff --git a/packages/twenty-front/src/modules/object-record/hooks/useFindManyRecordsQuery.ts b/packages/twenty-front/src/modules/object-record/hooks/useFindManyRecordsQuery.ts new file mode 100644 index 000000000000..e41481c41c3d --- /dev/null +++ b/packages/twenty-front/src/modules/object-record/hooks/useFindManyRecordsQuery.ts @@ -0,0 +1,36 @@ +import { useRecoilValue } from 'recoil'; + +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; +import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState'; +import { QueryFields } from '@/object-record/query-keys/types/QueryFields'; +import { generateFindManyRecordsQuery } from '@/object-record/utils/generateFindManyRecordsQuery'; + +export const useFindManyRecordsQuery = ({ + objectNameSingular, + queryFields, + depth, + computeReferences, +}: { + objectNameSingular: string; + queryFields?: QueryFields; + depth?: number; + computeReferences?: boolean; +}) => { + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + + const objectMetadataItems = useRecoilValue(objectMetadataItemsState); + + const findManyRecordsQuery = generateFindManyRecordsQuery({ + objectMetadataItem, + objectMetadataItems, + queryFields, + depth, + computeReferences, + }); + + return { + findManyRecordsQuery, + }; +}; diff --git a/packages/twenty-front/src/modules/object-record/hooks/useFindOneRecord.ts b/packages/twenty-front/src/modules/object-record/hooks/useFindOneRecord.ts index 269c1715c0b9..acb2f92c45ed 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useFindOneRecord.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useFindOneRecord.ts @@ -4,26 +4,30 @@ import { useQuery } from '@apollo/client'; import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { ObjectMetadataItemIdentifier } from '@/object-metadata/types/ObjectMetadataItemIdentifier'; import { getRecordFromRecordNode } from '@/object-record/cache/utils/getRecordFromRecordNode'; +import { useFindOneRecordQuery } from '@/object-record/hooks/useFindOneRecordQuery'; import { ObjectRecord } from '@/object-record/types/ObjectRecord'; import { isDefined } from '~/utils/isDefined'; -// TODO: fix connection in relation => automatically change to an array export const useFindOneRecord = ({ objectNameSingular, objectRecordId = '', onCompleted, - depth, skip, + depth, }: ObjectMetadataItemIdentifier & { objectRecordId: string | undefined; onCompleted?: (data: T) => void; skip?: boolean; depth?: number; }) => { - const { objectMetadataItem, findOneRecordQuery } = useObjectMetadataItem( - { objectNameSingular }, + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + + const { findOneRecordQuery } = useFindOneRecordQuery({ + objectNameSingular, depth, - ); + }); const { data, loading, error } = useQuery< { [nameSingular: string]: T }, @@ -42,6 +46,7 @@ export const useFindOneRecord = ({ }, }); + // TODO: Remove connection from record const recordWithoutConnection = useMemo( () => data?.[objectNameSingular] diff --git a/packages/twenty-front/src/modules/object-record/hooks/useGenerateFindOneRecordQuery.ts b/packages/twenty-front/src/modules/object-record/hooks/useFindOneRecordQuery.ts similarity index 64% rename from packages/twenty-front/src/modules/object-record/hooks/useGenerateFindOneRecordQuery.ts rename to packages/twenty-front/src/modules/object-record/hooks/useFindOneRecordQuery.ts index e9260cd710f7..87f141f5cbc1 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useGenerateFindOneRecordQuery.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useFindOneRecordQuery.ts @@ -1,22 +1,25 @@ -import { gql } from '@apollo/client'; +import gql from 'graphql-tag'; import { useRecoilValue } from 'recoil'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState'; -import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; import { mapObjectMetadataToGraphQLQuery } from '@/object-metadata/utils/mapObjectMetadataToGraphQLQuery'; import { capitalize } from '~/utils/string/capitalize'; -export const useGenerateFindOneRecordQuery = () => { +export const useFindOneRecordQuery = ({ + objectNameSingular, + depth, +}: { + objectNameSingular: string; + depth?: number; +}) => { + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + const objectMetadataItems = useRecoilValue(objectMetadataItemsState); - return ({ - objectMetadataItem, - depth, - }: { - objectMetadataItem: Pick; - depth?: number; - }) => { - return gql` + const findOneRecordQuery = gql` query FindOne${capitalize( objectMetadataItem.nameSingular, )}($objectRecordId: UUID!) { @@ -31,5 +34,8 @@ export const useGenerateFindOneRecordQuery = () => { })} } `; + + return { + findOneRecordQuery, }; }; diff --git a/packages/twenty-front/src/modules/object-record/hooks/useGenerateDeleteManyRecordMutation.ts b/packages/twenty-front/src/modules/object-record/hooks/useGenerateDeleteManyRecordMutation.ts deleted file mode 100644 index 4d7279a1be6b..000000000000 --- a/packages/twenty-front/src/modules/object-record/hooks/useGenerateDeleteManyRecordMutation.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { gql } from '@apollo/client'; - -import { EMPTY_MUTATION } from '@/object-metadata/hooks/useObjectMetadataItem'; -import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; -import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull'; -import { capitalize } from '~/utils/string/capitalize'; - -export const getDeleteManyRecordsMutationResponseField = ( - objectNamePlural: string, -) => `delete${capitalize(objectNamePlural)}`; - -export const useGenerateDeleteManyRecordMutation = ({ - objectMetadataItem, -}: { - objectMetadataItem: ObjectMetadataItem; -}) => { - if (isUndefinedOrNull(objectMetadataItem)) { - return EMPTY_MUTATION; - } - - const capitalizedObjectName = capitalize(objectMetadataItem.namePlural); - - const mutationResponseField = getDeleteManyRecordsMutationResponseField( - objectMetadataItem.namePlural, - ); - - return gql` - mutation DeleteMany${capitalizedObjectName}($filter: ${capitalize( - objectMetadataItem.nameSingular, - )}FilterInput!) { - ${mutationResponseField}(filter: $filter) { - id - } - } - `; -}; diff --git a/packages/twenty-front/src/modules/object-record/hooks/useGenerateFindManyRecordsQuery.ts b/packages/twenty-front/src/modules/object-record/hooks/useGenerateFindManyRecordsQuery.ts deleted file mode 100644 index e190ca5f2394..000000000000 --- a/packages/twenty-front/src/modules/object-record/hooks/useGenerateFindManyRecordsQuery.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { gql } from '@apollo/client'; -import { useRecoilValue } from 'recoil'; - -import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState'; -import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; -import { mapObjectMetadataToGraphQLQuery } from '@/object-metadata/utils/mapObjectMetadataToGraphQLQuery'; -import { capitalize } from '~/utils/string/capitalize'; - -export const useGenerateFindManyRecordsQuery = () => { - const objectMetadataItems = useRecoilValue(objectMetadataItemsState); - - return ({ - objectMetadataItem, - depth, - queryFields, - computeReferences = false, - }: { - objectMetadataItem: Pick< - ObjectMetadataItem, - 'fields' | 'nameSingular' | 'namePlural' - >; - depth?: number; - queryFields?: Record; - computeReferences?: boolean; - }) => gql` - query FindMany${capitalize( - objectMetadataItem.namePlural, - )}($filter: ${capitalize( - objectMetadataItem.nameSingular, - )}FilterInput, $orderBy: ${capitalize( - objectMetadataItem.nameSingular, - )}OrderByInput, $lastCursor: String, $limit: Float) { - ${ - objectMetadataItem.namePlural - }(filter: $filter, orderBy: $orderBy, first: $limit, after: $lastCursor){ - edges { - node ${mapObjectMetadataToGraphQLQuery({ - objectMetadataItems, - objectMetadataItem, - depth, - queryFields, - computeReferences, - })} - cursor - } - pageInfo { - hasNextPage - startCursor - endCursor - } - totalCount - } - } - `; -}; diff --git a/packages/twenty-front/src/modules/object-record/hooks/useLazyFindOneRecord.ts b/packages/twenty-front/src/modules/object-record/hooks/useLazyFindOneRecord.ts index c8a37252634f..a97a4cca87d9 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useLazyFindOneRecord.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useLazyFindOneRecord.ts @@ -1,9 +1,8 @@ import { useLazyQuery } from '@apollo/client'; -import { useObjectMetadataItemOnly } from '@/object-metadata/hooks/useObjectMetadataItemOnly'; import { ObjectMetadataItemIdentifier } from '@/object-metadata/types/ObjectMetadataItemIdentifier'; import { getRecordFromRecordNode } from '@/object-record/cache/utils/getRecordFromRecordNode'; -import { useGenerateFindOneRecordQuery } from '@/object-record/hooks/useGenerateFindOneRecordQuery'; +import { useFindOneRecordQuery } from '@/object-record/hooks/useFindOneRecordQuery'; import { ObjectRecord } from '@/object-record/types/ObjectRecord'; type UseLazyFindOneRecordParams = ObjectMetadataItemIdentifier & { @@ -19,14 +18,13 @@ export const useLazyFindOneRecord = ({ objectNameSingular, depth, }: UseLazyFindOneRecordParams) => { - const { objectMetadataItem } = useObjectMetadataItemOnly({ + const { findOneRecordQuery } = useFindOneRecordQuery({ objectNameSingular, + depth, }); - const findOneRecordQuery = useGenerateFindOneRecordQuery(); - const [findOneRecord, { loading, error, data, called }] = useLazyQuery( - findOneRecordQuery({ objectMetadataItem, depth }), - ); + const [findOneRecord, { loading, error, data, called }] = + useLazyQuery(findOneRecordQuery); return { findOneRecord: ({ objectRecordId, onCompleted }: FindOneRecordParams) => diff --git a/packages/twenty-front/src/modules/object-record/hooks/useUpdateOneRecord.ts b/packages/twenty-front/src/modules/object-record/hooks/useUpdateOneRecord.ts index 073cb86fd184..a9c50331ba31 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useUpdateOneRecord.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useUpdateOneRecord.ts @@ -3,10 +3,12 @@ import { useApolloClient } from '@apollo/client'; import { triggerUpdateRecordOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerUpdateRecordOptimisticEffect'; import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems'; +import { useGetRecordFromCache } from '@/object-record/cache/hooks/useGetRecordFromCache'; import { getRecordNodeFromRecord } from '@/object-record/cache/utils/getRecordNodeFromRecord'; import { updateRecordFromCache } from '@/object-record/cache/utils/updateRecordFromCache'; -import { getUpdateOneRecordMutationResponseField } from '@/object-record/hooks/useGenerateUpdateOneRecordMutation'; +import { useUpdateOneRecordMutation } from '@/object-record/hooks/useUpdateOneRecordMutation'; import { ObjectRecord } from '@/object-record/types/ObjectRecord'; +import { getUpdateOneRecordMutationResponseField } from '@/object-record/utils/getUpdateOneRecordMutationResponseField'; import { sanitizeRecordInput } from '@/object-record/utils/sanitizeRecordInput'; type useUpdateOneRecordProps = { @@ -24,8 +26,17 @@ export const useUpdateOneRecord = < }: useUpdateOneRecordProps) => { const apolloClient = useApolloClient(); - const { objectMetadataItem, updateOneRecordMutation, getRecordFromCache } = - useObjectMetadataItem({ objectNameSingular }, depth, queryFields); + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + + const getRecordFromCache = useGetRecordFromCache({ + objectNameSingular, + }); + + const { updateOneRecordMutation } = useUpdateOneRecordMutation({ + objectNameSingular, + }); const { objectMetadataItems } = useObjectMetadataItems(); diff --git a/packages/twenty-front/src/modules/object-record/hooks/useGenerateUpdateOneRecordMutation.ts b/packages/twenty-front/src/modules/object-record/hooks/useUpdateOneRecordMutation.ts similarity index 63% rename from packages/twenty-front/src/modules/object-record/hooks/useGenerateUpdateOneRecordMutation.ts rename to packages/twenty-front/src/modules/object-record/hooks/useUpdateOneRecordMutation.ts index 5bda2bea6b4e..9067845a76e3 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useGenerateUpdateOneRecordMutation.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useUpdateOneRecordMutation.ts @@ -1,30 +1,31 @@ -import { gql } from '@apollo/client'; +import gql from 'graphql-tag'; import { useRecoilValue } from 'recoil'; -import { EMPTY_MUTATION } from '@/object-metadata/hooks/useObjectMetadataItem'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState'; -import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; import { mapObjectMetadataToGraphQLQuery } from '@/object-metadata/utils/mapObjectMetadataToGraphQLQuery'; +import { EMPTY_MUTATION } from '@/object-record/constants/EmptyMutation'; +import { getUpdateOneRecordMutationResponseField } from '@/object-record/utils/getUpdateOneRecordMutationResponseField'; import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull'; import { capitalize } from '~/utils/string/capitalize'; -export const getUpdateOneRecordMutationResponseField = ( - objectNameSingular: string, -) => `update${capitalize(objectNameSingular)}`; - -export const useGenerateUpdateOneRecordMutation = ({ - objectMetadataItem, - depth = 1, +export const useUpdateOneRecordMutation = ({ + objectNameSingular, computeReferences = false, + depth, }: { - objectMetadataItem: ObjectMetadataItem; - depth?: number; + objectNameSingular: string; computeReferences?: boolean; + depth?: number; }) => { + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + const objectMetadataItems = useRecoilValue(objectMetadataItemsState); if (isUndefinedOrNull(objectMetadataItem)) { - return EMPTY_MUTATION; + return { updateOneRecordMutation: EMPTY_MUTATION }; } const capitalizedObjectName = capitalize(objectMetadataItem.nameSingular); @@ -33,7 +34,7 @@ export const useGenerateUpdateOneRecordMutation = ({ objectMetadataItem.nameSingular, ); - return gql` + const updateOneRecordMutation = gql` mutation UpdateOne${capitalizedObjectName}($idToUpdate: ID!, $input: ${capitalizedObjectName}UpdateInput!) { ${mutationResponseField}(id: $idToUpdate, data: $input) ${mapObjectMetadataToGraphQLQuery( { @@ -45,4 +46,8 @@ export const useGenerateUpdateOneRecordMutation = ({ )} } `; + + return { + updateOneRecordMutation, + }; }; diff --git a/packages/twenty-front/src/modules/object-record/multiple-objects/hooks/useFindManyRecordsForMultipleMetadataItems.ts b/packages/twenty-front/src/modules/object-record/multiple-objects/hooks/useFindManyRecordsForMultipleMetadataItems.ts index 13b7d5a7a977..3640dee6a618 100644 --- a/packages/twenty-front/src/modules/object-record/multiple-objects/hooks/useFindManyRecordsForMultipleMetadataItems.ts +++ b/packages/twenty-front/src/modules/object-record/multiple-objects/hooks/useFindManyRecordsForMultipleMetadataItems.ts @@ -1,8 +1,8 @@ import { useQuery } from '@apollo/client'; -import { EMPTY_QUERY } from '@/object-metadata/hooks/useObjectMetadataItem'; import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; import { getRecordsFromRecordConnection } from '@/object-record/cache/utils/getRecordsFromRecordConnection'; +import { EMPTY_QUERY } from '@/object-record/constants/EmptyQuery'; import { useGenerateFindManyRecordsForMultipleMetadataItemsQuery } from '@/object-record/multiple-objects/hooks/useGenerateFindManyRecordsForMultipleMetadataItemsQuery'; import { MultiObjectRecordQueryResult } from '@/object-record/relation-picker/hooks/useMultiObjectRecordsQueryResultFormattedAsObjectRecordForSelectArray'; diff --git a/packages/twenty-front/src/modules/object-record/object-filter-dropdown/hooks/useOptionsForSelect.ts b/packages/twenty-front/src/modules/object-record/object-filter-dropdown/hooks/useOptionsForSelect.ts index 8db977b5707f..cf8440a10f9f 100644 --- a/packages/twenty-front/src/modules/object-record/object-filter-dropdown/hooks/useOptionsForSelect.ts +++ b/packages/twenty-front/src/modules/object-record/object-filter-dropdown/hooks/useOptionsForSelect.ts @@ -12,7 +12,9 @@ export const useOptionsForSelect = (fieldMetadataId: string) => { objectNamePlural, }); - const { objectMetadataItem } = useObjectMetadataItem({ objectNameSingular }); + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); const fieldMetadataItem = objectMetadataItem.fields.find( (field) => field.id === fieldMetadataId, diff --git a/packages/twenty-front/src/modules/object-record/query-keys/types/QueryFields.ts b/packages/twenty-front/src/modules/object-record/query-keys/types/QueryFields.ts new file mode 100644 index 000000000000..78b2e21c3bc3 --- /dev/null +++ b/packages/twenty-front/src/modules/object-record/query-keys/types/QueryFields.ts @@ -0,0 +1 @@ +export type QueryFields = Record; diff --git a/packages/twenty-front/src/modules/object-record/query-keys/types/QueryKey.ts b/packages/twenty-front/src/modules/object-record/query-keys/types/QueryKey.ts index 31d1191b0f65..c1a7a598495b 100644 --- a/packages/twenty-front/src/modules/object-record/query-keys/types/QueryKey.ts +++ b/packages/twenty-front/src/modules/object-record/query-keys/types/QueryKey.ts @@ -1,9 +1,10 @@ +import { QueryFields } from '@/object-record/query-keys/types/QueryFields'; import { ObjectRecordQueryVariables } from '@/object-record/types/ObjectRecordQueryVariables'; export type QueryKey = { objectNameSingular: string; variables: ObjectRecordQueryVariables; depth?: number; - fields?: Record; // Todo: Fields should be required - fieldsFactory?: (fieldsFactoryParam: any) => Record; + fields?: QueryFields; // Todo: Fields should be required + fieldsFactory?: (fieldsFactoryParam: any) => QueryFields; }; diff --git a/packages/twenty-front/src/modules/object-record/record-index/components/RecordIndexBoardContainer.tsx b/packages/twenty-front/src/modules/object-record/record-index/components/RecordIndexBoardContainer.tsx index 57b8ffe4cb51..bd412f91020e 100644 --- a/packages/twenty-front/src/modules/object-record/record-index/components/RecordIndexBoardContainer.tsx +++ b/packages/twenty-front/src/modules/object-record/record-index/components/RecordIndexBoardContainer.tsx @@ -1,6 +1,6 @@ import { useRecoilValue } from 'recoil'; -import { useObjectMetadataItemOnly } from '@/object-metadata/hooks/useObjectMetadataItemOnly'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useCreateOneRecord } from '@/object-record/hooks/useCreateOneRecord'; import { useDeleteOneRecord } from '@/object-record/hooks/useDeleteOneRecord'; import { useUpdateOneRecord } from '@/object-record/hooks/useUpdateOneRecord'; @@ -21,7 +21,7 @@ export const RecordIndexBoardContainer = ({ recordBoardId, objectNameSingular, }: RecordIndexBoardContainerProps) => { - const { objectMetadataItem } = useObjectMetadataItemOnly({ + const { objectMetadataItem } = useObjectMetadataItem({ objectNameSingular, }); diff --git a/packages/twenty-front/src/modules/object-record/record-index/components/RecordIndexBoardContainerEffect.tsx b/packages/twenty-front/src/modules/object-record/record-index/components/RecordIndexBoardContainerEffect.tsx index df517ccbe8f4..4a755169d0e6 100644 --- a/packages/twenty-front/src/modules/object-record/record-index/components/RecordIndexBoardContainerEffect.tsx +++ b/packages/twenty-front/src/modules/object-record/record-index/components/RecordIndexBoardContainerEffect.tsx @@ -2,7 +2,7 @@ import { useCallback, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import { useRecoilValue, useSetRecoilState } from 'recoil'; -import { useObjectMetadataItemOnly } from '@/object-metadata/hooks/useObjectMetadataItemOnly'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useRecordActionBar } from '@/object-record/record-action-bar/hooks/useRecordActionBar'; import { useRecordBoard } from '@/object-record/record-board/hooks/useRecordBoard'; import { useRecordBoardSelection } from '@/object-record/record-board/hooks/useRecordBoardSelection'; @@ -24,7 +24,7 @@ export const RecordIndexBoardContainerEffect = ({ recordBoardId, viewBarId, }: RecordIndexBoardContainerEffectProps) => { - const { objectMetadataItem } = useObjectMetadataItemOnly({ + const { objectMetadataItem } = useObjectMetadataItem({ objectNameSingular, }); diff --git a/packages/twenty-front/src/modules/object-record/record-index/components/RecordIndexContainer.tsx b/packages/twenty-front/src/modules/object-record/record-index/components/RecordIndexContainer.tsx index 9d15808ccf3a..3fceb3f2cce3 100644 --- a/packages/twenty-front/src/modules/object-record/record-index/components/RecordIndexContainer.tsx +++ b/packages/twenty-front/src/modules/object-record/record-index/components/RecordIndexContainer.tsx @@ -2,7 +2,7 @@ import styled from '@emotion/styled'; import { useRecoilCallback, useRecoilState, useSetRecoilState } from 'recoil'; import { useColumnDefinitionsFromFieldMetadata } from '@/object-metadata/hooks/useColumnDefinitionsFromFieldMetadata'; -import { useObjectMetadataItemOnly } from '@/object-metadata/hooks/useObjectMetadataItemOnly'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useObjectNameSingularFromPlural } from '@/object-metadata/hooks/useObjectNameSingularFromPlural'; import { RecordIndexBoardContainer } from '@/object-record/record-index/components/RecordIndexBoardContainer'; import { RecordIndexBoardContainerEffect } from '@/object-record/record-index/components/RecordIndexBoardContainerEffect'; @@ -53,7 +53,7 @@ export const RecordIndexContainer = ({ objectNamePlural, }); - const { objectMetadataItem } = useObjectMetadataItemOnly({ + const { objectMetadataItem } = useObjectMetadataItem({ objectNameSingular, }); diff --git a/packages/twenty-front/src/modules/object-record/record-index/components/RecordIndexTableContainerEffect.tsx b/packages/twenty-front/src/modules/object-record/record-index/components/RecordIndexTableContainerEffect.tsx index 10fc39632a4e..6677d042665d 100644 --- a/packages/twenty-front/src/modules/object-record/record-index/components/RecordIndexTableContainerEffect.tsx +++ b/packages/twenty-front/src/modules/object-record/record-index/components/RecordIndexTableContainerEffect.tsx @@ -2,7 +2,7 @@ import { useEffect } from 'react'; import { useRecoilValue } from 'recoil'; import { useColumnDefinitionsFromFieldMetadata } from '@/object-metadata/hooks/useColumnDefinitionsFromFieldMetadata'; -import { useObjectMetadataItemOnly } from '@/object-metadata/hooks/useObjectMetadataItemOnly'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useRecordActionBar } from '@/object-record/record-action-bar/hooks/useRecordActionBar'; import { useHandleToggleColumnFilter } from '@/object-record/record-index/hooks/useHandleToggleColumnFilter'; import { useHandleToggleColumnSort } from '@/object-record/record-index/hooks/useHandleToggleColumnSort'; @@ -31,7 +31,7 @@ export const RecordIndexTableContainerEffect = ({ recordTableId, }); - const { objectMetadataItem } = useObjectMetadataItemOnly({ + const { objectMetadataItem } = useObjectMetadataItem({ objectNameSingular, }); diff --git a/packages/twenty-front/src/modules/object-record/record-index/hooks/useLoadRecordIndexBoard.ts b/packages/twenty-front/src/modules/object-record/record-index/hooks/useLoadRecordIndexBoard.ts index 14bcdad20eaa..3e10a603374c 100644 --- a/packages/twenty-front/src/modules/object-record/record-index/hooks/useLoadRecordIndexBoard.ts +++ b/packages/twenty-front/src/modules/object-record/record-index/hooks/useLoadRecordIndexBoard.ts @@ -1,7 +1,7 @@ import { useEffect } from 'react'; import { useRecoilValue, useSetRecoilState } from 'recoil'; -import { useObjectMetadataItemOnly } from '@/object-metadata/hooks/useObjectMetadataItemOnly'; +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 { useRecordBoard } from '@/object-record/record-board/hooks/useRecordBoard'; @@ -25,7 +25,7 @@ export const useLoadRecordIndexBoard = ({ viewBarId, recordBoardId, }: UseLoadRecordIndexBoardProps) => { - const { objectMetadataItem } = useObjectMetadataItemOnly({ + const { objectMetadataItem } = useObjectMetadataItem({ objectNameSingular, }); const { diff --git a/packages/twenty-front/src/modules/object-record/record-index/hooks/useLoadRecordIndexTable.ts b/packages/twenty-front/src/modules/object-record/record-index/hooks/useLoadRecordIndexTable.ts index ca616895b0f3..5fd83e30672b 100644 --- a/packages/twenty-front/src/modules/object-record/record-index/hooks/useLoadRecordIndexTable.ts +++ b/packages/twenty-front/src/modules/object-record/record-index/hooks/useLoadRecordIndexTable.ts @@ -1,7 +1,7 @@ import { useRecoilValue, useSetRecoilState } from 'recoil'; import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState.ts'; -import { useObjectMetadataItemOnly } from '@/object-metadata/hooks/useObjectMetadataItemOnly'; +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 { turnObjectDropdownFilterIntoQueryFilter } from '@/object-record/record-filter/utils/turnObjectDropdownFilterIntoQueryFilter'; @@ -14,7 +14,7 @@ export const useFindManyParams = ( objectNameSingular: string, recordTableId?: string, ) => { - const { objectMetadataItem } = useObjectMetadataItemOnly({ + const { objectMetadataItem } = useObjectMetadataItem({ objectNameSingular, }); diff --git a/packages/twenty-front/src/modules/object-record/record-index/options/hooks/useRecordIndexOptionsForBoard.ts b/packages/twenty-front/src/modules/object-record/record-index/options/hooks/useRecordIndexOptionsForBoard.ts index da059ea5045d..9e1c2b28b6ed 100644 --- a/packages/twenty-front/src/modules/object-record/record-index/options/hooks/useRecordIndexOptionsForBoard.ts +++ b/packages/twenty-front/src/modules/object-record/record-index/options/hooks/useRecordIndexOptionsForBoard.ts @@ -3,7 +3,7 @@ import { OnDragEndResponder } from '@hello-pangea/dnd'; import { useRecoilState } from 'recoil'; import { useColumnDefinitionsFromFieldMetadata } from '@/object-metadata/hooks/useColumnDefinitionsFromFieldMetadata'; -import { useObjectMetadataItemOnly } from '@/object-metadata/hooks/useObjectMetadataItemOnly'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useRecordBoard } from '@/object-record/record-board/hooks/useRecordBoard'; import { FieldMetadata } from '@/object-record/record-field/types/FieldMetadata'; import { recordIndexFieldDefinitionsState } from '@/object-record/record-index/states/recordIndexFieldDefinitionsState'; @@ -38,7 +38,7 @@ export const useRecordIndexOptionsForBoard = ({ isCompactModeActiveState, ); - const { objectMetadataItem } = useObjectMetadataItemOnly({ + const { objectMetadataItem } = useObjectMetadataItem({ objectNameSingular, }); diff --git a/packages/twenty-front/src/modules/object-record/record-show/components/RecordShowContainer.tsx b/packages/twenty-front/src/modules/object-record/record-show/components/RecordShowContainer.tsx index 0ab972672c1f..26a39d36696d 100644 --- a/packages/twenty-front/src/modules/object-record/record-show/components/RecordShowContainer.tsx +++ b/packages/twenty-front/src/modules/object-record/record-show/components/RecordShowContainer.tsx @@ -1,6 +1,7 @@ import groupBy from 'lodash.groupby'; import { useRecoilState, useRecoilValue } from 'recoil'; +import { useLabelIdentifierFieldMetadataItem } from '@/object-metadata/hooks/useLabelIdentifierFieldMetadataItem'; import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { formatFieldMetadataItemAsColumnDefinition } from '@/object-metadata/utils/formatFieldMetadataItemAsColumnDefinition'; import { useUpdateOneRecord } from '@/object-record/hooks/useUpdateOneRecord'; @@ -41,8 +42,12 @@ export const RecordShowContainer = ({ objectNameSingular, objectRecordId, }: RecordShowContainerProps) => { - const { objectMetadataItem, labelIdentifierFieldMetadata } = - useObjectMetadataItem({ + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + + const { labelIdentifierFieldMetadataItem } = + useLabelIdentifierFieldMetadataItem({ objectNameSingular, }); @@ -105,7 +110,7 @@ export const RecordShowContainer = ({ .filter( (fieldMetadataItem) => isFieldCellSupported(fieldMetadataItem) && - fieldMetadataItem.id !== labelIdentifierFieldMetadata?.id, + fieldMetadataItem.id !== labelIdentifierFieldMetadataItem?.id, ) .sort((fieldMetadataItemA, fieldMetadataItemB) => fieldMetadataItemA.name.localeCompare(fieldMetadataItemB.name), @@ -135,17 +140,19 @@ export const RecordShowContainer = ({ value={{ entityId: objectRecordId, recoilScopeId: - objectRecordId + labelIdentifierFieldMetadata?.id, + objectRecordId + labelIdentifierFieldMetadataItem?.id, isLabelIdentifier: false, fieldDefinition: { type: - labelIdentifierFieldMetadata?.type || + labelIdentifierFieldMetadataItem?.type || FieldMetadataType.Text, iconName: '', - fieldMetadataId: labelIdentifierFieldMetadata?.id ?? '', - label: labelIdentifierFieldMetadata?.label || '', + fieldMetadataId: + labelIdentifierFieldMetadataItem?.id ?? '', + label: labelIdentifierFieldMetadataItem?.label || '', metadata: { - fieldName: labelIdentifierFieldMetadata?.name || '', + fieldName: + labelIdentifierFieldMetadataItem?.name || '', }, }, useUpdateRecord: useUpdateOneObjectRecordMutation, diff --git a/packages/twenty-front/src/modules/object-record/record-show/record-detail-section/components/RecordDetailRelationRecordsListItem.tsx b/packages/twenty-front/src/modules/object-record/record-show/record-detail-section/components/RecordDetailRelationRecordsListItem.tsx index f6b0ccf412be..fd46315aeaa5 100644 --- a/packages/twenty-front/src/modules/object-record/record-show/record-detail-section/components/RecordDetailRelationRecordsListItem.tsx +++ b/packages/twenty-front/src/modules/object-record/record-show/record-detail-section/components/RecordDetailRelationRecordsListItem.tsx @@ -9,7 +9,7 @@ import { IconUnlink, } from 'twenty-ui'; -import { useObjectMetadataItemOnly } from '@/object-metadata/hooks/useObjectMetadataItemOnly'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; import { formatFieldMetadataItemAsColumnDefinition } from '@/object-metadata/utils/formatFieldMetadataItemAsColumnDefinition'; import { RecordChip } from '@/object-record/components/RecordChip'; @@ -92,7 +92,7 @@ export const RecordDetailRelationRecordsListItem = ({ const isToOneObject = relationType === 'TO_ONE_OBJECT'; const { objectMetadataItem: relationObjectMetadataItem } = - useObjectMetadataItemOnly({ + useObjectMetadataItem({ objectNameSingular: relationObjectMetadataNameSingular, }); diff --git a/packages/twenty-front/src/modules/object-record/record-show/record-detail-section/components/RecordDetailRelationSection.tsx b/packages/twenty-front/src/modules/object-record/record-show/record-detail-section/components/RecordDetailRelationSection.tsx index 2decc344135f..7a3252e7d82d 100644 --- a/packages/twenty-front/src/modules/object-record/record-show/record-detail-section/components/RecordDetailRelationSection.tsx +++ b/packages/twenty-front/src/modules/object-record/record-show/record-detail-section/components/RecordDetailRelationSection.tsx @@ -4,7 +4,7 @@ import qs from 'qs'; import { useRecoilValue } from 'recoil'; import { IconForbid, IconPencil, IconPlus } from 'twenty-ui'; -import { useObjectMetadataItemOnly } from '@/object-metadata/hooks/useObjectMetadataItemOnly'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useUpdateOneRecord } from '@/object-record/hooks/useUpdateOneRecord'; import { FieldContext } from '@/object-record/record-field/contexts/FieldContext'; import { usePersistField } from '@/object-record/record-field/hooks/usePersistField'; @@ -42,7 +42,7 @@ export const RecordDetailRelationSection = () => { const record = useRecoilValue(recordStoreFamilyState(entityId)); const { objectMetadataItem: relationObjectMetadataItem } = - useObjectMetadataItemOnly({ + useObjectMetadataItem({ objectNameSingular: relationObjectMetadataNameSingular, }); diff --git a/packages/twenty-front/src/modules/object-record/record-table/components/RecordTable.tsx b/packages/twenty-front/src/modules/object-record/record-table/components/RecordTable.tsx index 68ca007a00dd..aeeae2897ffe 100644 --- a/packages/twenty-front/src/modules/object-record/record-table/components/RecordTable.tsx +++ b/packages/twenty-front/src/modules/object-record/record-table/components/RecordTable.tsx @@ -2,7 +2,7 @@ import { css } from '@emotion/react'; import styled from '@emotion/styled'; import { useRecoilValue } from 'recoil'; -import { useObjectMetadataItemOnly } from '@/object-metadata/hooks/useObjectMetadataItemOnly'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { RecordTableBody } from '@/object-record/record-table/components/RecordTableBody'; import { RecordTableBodyEffect } from '@/object-record/record-table/components/RecordTableBodyEffect'; import { RecordTableHeader } from '@/object-record/record-table/components/RecordTableHeader'; @@ -141,7 +141,7 @@ export const RecordTable = ({ const scrollLeft = useRecoilValue(scrollLeftState); const scrollTop = useRecoilValue(scrollTopState); - const { objectMetadataItem } = useObjectMetadataItemOnly({ + const { objectMetadataItem } = useObjectMetadataItem({ objectNameSingular, }); diff --git a/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableRow.tsx b/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableRow.tsx index 9f018f8f5cc8..1fa3794c1b24 100644 --- a/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableRow.tsx +++ b/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableRow.tsx @@ -43,7 +43,9 @@ export const RecordTableRow = ({ recordId, rowIndex }: RecordTableRowProps) => { recordId, rowIndex, pathToShowPage: - getBasePathToShowPage({ objectMetadataItem }) + recordId, + getBasePathToShowPage({ + objectNameSingular: objectMetadataItem.nameSingular, + }) + recordId, isSelected: currentRowSelected, }} > diff --git a/packages/twenty-front/src/modules/object-record/relation-picker/hooks/useMultiObjectSearchMatchesSearchFilterAndSelectedItemsQuery.ts b/packages/twenty-front/src/modules/object-record/relation-picker/hooks/useMultiObjectSearchMatchesSearchFilterAndSelectedItemsQuery.ts index 688cd9f12095..a8cd2b5094b9 100644 --- a/packages/twenty-front/src/modules/object-record/relation-picker/hooks/useMultiObjectSearchMatchesSearchFilterAndSelectedItemsQuery.ts +++ b/packages/twenty-front/src/modules/object-record/relation-picker/hooks/useMultiObjectSearchMatchesSearchFilterAndSelectedItemsQuery.ts @@ -2,8 +2,8 @@ import { useQuery } from '@apollo/client'; import { isNonEmptyArray } from '@sniptt/guards'; import { useRecoilValue } from 'recoil'; -import { EMPTY_QUERY } from '@/object-metadata/hooks/useObjectMetadataItem'; import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState'; +import { EMPTY_QUERY } from '@/object-record/constants/EmptyQuery'; import { useGenerateFindManyRecordsForMultipleMetadataItemsQuery } from '@/object-record/multiple-objects/hooks/useGenerateFindManyRecordsForMultipleMetadataItemsQuery'; import { useLimitPerMetadataItem } from '@/object-record/relation-picker/hooks/useLimitPerMetadataItem'; import { diff --git a/packages/twenty-front/src/modules/object-record/relation-picker/hooks/useMultiObjectSearchMatchesSearchFilterAndToSelectQuery.ts b/packages/twenty-front/src/modules/object-record/relation-picker/hooks/useMultiObjectSearchMatchesSearchFilterAndToSelectQuery.ts index 75395c27aa94..8719c81fdf3d 100644 --- a/packages/twenty-front/src/modules/object-record/relation-picker/hooks/useMultiObjectSearchMatchesSearchFilterAndToSelectQuery.ts +++ b/packages/twenty-front/src/modules/object-record/relation-picker/hooks/useMultiObjectSearchMatchesSearchFilterAndToSelectQuery.ts @@ -1,9 +1,9 @@ import { useQuery } from '@apollo/client'; import { useRecoilValue } from 'recoil'; -import { EMPTY_QUERY } from '@/object-metadata/hooks/useObjectMetadataItem'; import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState'; import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; +import { EMPTY_QUERY } from '@/object-record/constants/EmptyQuery'; import { useGenerateFindManyRecordsForMultipleMetadataItemsQuery } from '@/object-record/multiple-objects/hooks/useGenerateFindManyRecordsForMultipleMetadataItemsQuery'; import { useLimitPerMetadataItem } from '@/object-record/relation-picker/hooks/useLimitPerMetadataItem'; import { diff --git a/packages/twenty-front/src/modules/object-record/relation-picker/hooks/useOrderByFieldPerMetadataItem.ts b/packages/twenty-front/src/modules/object-record/relation-picker/hooks/useOrderByFieldPerMetadataItem.ts index 42dd28f99f22..07f60b012fce 100644 --- a/packages/twenty-front/src/modules/object-record/relation-picker/hooks/useOrderByFieldPerMetadataItem.ts +++ b/packages/twenty-front/src/modules/object-record/relation-picker/hooks/useOrderByFieldPerMetadataItem.ts @@ -1,5 +1,5 @@ import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; -import { getObjectOrderByField } from '@/object-metadata/utils/getObjectOrderByField'; +import { getOrderByFieldForObjectMetadataItem } from '@/object-metadata/utils/getObjectOrderByField'; import { isDefined } from '~/utils/isDefined'; import { capitalize } from '~/utils/string/capitalize'; @@ -11,7 +11,8 @@ export const useOrderByFieldPerMetadataItem = ({ const orderByFieldPerMetadataItem = Object.fromEntries( objectMetadataItems .map((objectMetadataItem) => { - const orderByField = getObjectOrderByField(objectMetadataItem); + const orderByField = + getOrderByFieldForObjectMetadataItem(objectMetadataItem); return [ `orderBy${capitalize(objectMetadataItem.nameSingular)}`, diff --git a/packages/twenty-front/src/modules/object-record/select/hooks/useRecordsForSelect.ts b/packages/twenty-front/src/modules/object-record/select/hooks/useRecordsForSelect.ts index fb93d44cc79b..e12d3cec060b 100644 --- a/packages/twenty-front/src/modules/object-record/select/hooks/useRecordsForSelect.ts +++ b/packages/twenty-front/src/modules/object-record/select/hooks/useRecordsForSelect.ts @@ -1,6 +1,7 @@ import { isNonEmptyString } from '@sniptt/guards'; -import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; +import { useGetObjectOrderByField } from '@/object-metadata/hooks/useGetObjectOrderByField'; +import { useMapToObjectRecordIdentifier } from '@/object-metadata/hooks/useMapToObjectRecordIdentifier'; import { OrderBy } from '@/object-metadata/types/OrderBy'; import { DEFAULT_SEARCH_REQUEST_LIMIT } from '@/object-record/constants/DefaultSearchRequestLimit'; import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords'; @@ -24,10 +25,9 @@ export const useRecordsForSelect = ({ excludeEntityIds?: string[]; objectNameSingular: string; }) => { - const { mapToObjectRecordIdentifier, getObjectOrderByField } = - useObjectMetadataItem({ - objectNameSingular, - }); + const { mapToObjectRecordIdentifier } = useMapToObjectRecordIdentifier({ + objectNameSingular, + }); const filters = [ { @@ -36,6 +36,10 @@ export const useRecordsForSelect = ({ }, ]; + const { getObjectOrderByField } = useGetObjectOrderByField({ + objectNameSingular, + }); + const orderByField = getObjectOrderByField(sortOrder); const selectedIdsFilter = { id: { in: selectedIds } }; diff --git a/packages/twenty-front/src/modules/object-record/spreadsheet-import/useSpreadsheetRecordImport.ts b/packages/twenty-front/src/modules/object-record/spreadsheet-import/useSpreadsheetRecordImport.ts index ad8da55e80ef..56453adf17ad 100644 --- a/packages/twenty-front/src/modules/object-record/spreadsheet-import/useSpreadsheetRecordImport.ts +++ b/packages/twenty-front/src/modules/object-record/spreadsheet-import/useSpreadsheetRecordImport.ts @@ -19,7 +19,9 @@ export const useSpreadsheetRecordImport = (objectNameSingular: string) => { const { enqueueSnackBar } = useSnackBar(); const { getIcon } = useIcons(); - const { objectMetadataItem } = useObjectMetadataItem({ objectNameSingular }); + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); const fields = objectMetadataItem.fields .filter( (x) => diff --git a/packages/twenty-front/src/modules/object-record/utils/generateDeleteOneRecordMutation.ts b/packages/twenty-front/src/modules/object-record/utils/generateDeleteOneRecordMutation.ts deleted file mode 100644 index e941e79005b9..000000000000 --- a/packages/twenty-front/src/modules/object-record/utils/generateDeleteOneRecordMutation.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { gql } from '@apollo/client'; - -import { EMPTY_MUTATION } from '@/object-metadata/hooks/useObjectMetadataItem'; -import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; -import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull'; -import { capitalize } from '~/utils/string/capitalize'; - -export const getDeleteOneRecordMutationResponseField = ( - objectNameSingular: string, -) => `delete${capitalize(objectNameSingular)}`; - -export const generateDeleteOneRecordMutation = ({ - objectMetadataItem, -}: { - objectMetadataItem: ObjectMetadataItem; -}) => { - if (isUndefinedOrNull(objectMetadataItem)) { - return EMPTY_MUTATION; - } - - const capitalizedObjectName = capitalize(objectMetadataItem.nameSingular); - - const mutationResponseField = getDeleteOneRecordMutationResponseField( - objectMetadataItem.nameSingular, - ); - - return gql` - mutation DeleteOne${capitalizedObjectName}($idToDelete: ID!) { - ${mutationResponseField}(id: $idToDelete) { - id - } - } - `; -}; diff --git a/packages/twenty-front/src/modules/object-record/utils/generateFindManyRecordsQuery.ts b/packages/twenty-front/src/modules/object-record/utils/generateFindManyRecordsQuery.ts new file mode 100644 index 000000000000..7a4f4507078b --- /dev/null +++ b/packages/twenty-front/src/modules/object-record/utils/generateFindManyRecordsQuery.ts @@ -0,0 +1,49 @@ +import gql from 'graphql-tag'; + +import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; +import { mapObjectMetadataToGraphQLQuery } from '@/object-metadata/utils/mapObjectMetadataToGraphQLQuery'; +import { QueryFields } from '@/object-record/query-keys/types/QueryFields'; +import { capitalize } from '~/utils/string/capitalize'; + +export const generateFindManyRecordsQuery = ({ + objectMetadataItem, + objectMetadataItems, + depth, + queryFields, + computeReferences, +}: { + objectMetadataItem: ObjectMetadataItem; + objectMetadataItems: ObjectMetadataItem[]; + queryFields?: QueryFields; + depth?: number; + computeReferences?: boolean; +}) => gql` +query FindMany${capitalize( + objectMetadataItem.namePlural, +)}($filter: ${capitalize( + objectMetadataItem.nameSingular, +)}FilterInput, $orderBy: ${capitalize( + objectMetadataItem.nameSingular, +)}OrderByInput, $lastCursor: String, $limit: Float) { + ${ + objectMetadataItem.namePlural + }(filter: $filter, orderBy: $orderBy, first: $limit, after: $lastCursor){ + edges { + node ${mapObjectMetadataToGraphQLQuery({ + objectMetadataItems, + objectMetadataItem, + depth, + queryFields, + computeReferences, + })} + cursor + } + pageInfo { + hasNextPage + startCursor + endCursor + } + totalCount + } +} +`; diff --git a/packages/twenty-front/src/modules/object-record/utils/getCreateManyRecordsMutationResponseField.ts b/packages/twenty-front/src/modules/object-record/utils/getCreateManyRecordsMutationResponseField.ts new file mode 100644 index 000000000000..7bd7f58a690d --- /dev/null +++ b/packages/twenty-front/src/modules/object-record/utils/getCreateManyRecordsMutationResponseField.ts @@ -0,0 +1,5 @@ +import { capitalize } from '~/utils/string/capitalize'; + +export const getCreateManyRecordsMutationResponseField = ( + objectNamePlural: string, +) => `create${capitalize(objectNamePlural)}`; diff --git a/packages/twenty-front/src/modules/object-record/utils/getCreateOneRecordMutationResponseField.ts b/packages/twenty-front/src/modules/object-record/utils/getCreateOneRecordMutationResponseField.ts new file mode 100644 index 000000000000..9c46ccdfbb3c --- /dev/null +++ b/packages/twenty-front/src/modules/object-record/utils/getCreateOneRecordMutationResponseField.ts @@ -0,0 +1,5 @@ +import { capitalize } from '~/utils/string/capitalize'; + +export const getCreateOneRecordMutationResponseField = ( + objectNameSingular: string, +) => `create${capitalize(objectNameSingular)}`; diff --git a/packages/twenty-front/src/modules/object-record/utils/getDeleteManyRecordsMutationResponseField.ts b/packages/twenty-front/src/modules/object-record/utils/getDeleteManyRecordsMutationResponseField.ts new file mode 100644 index 000000000000..e95201af7bd7 --- /dev/null +++ b/packages/twenty-front/src/modules/object-record/utils/getDeleteManyRecordsMutationResponseField.ts @@ -0,0 +1,5 @@ +import { capitalize } from '~/utils/string/capitalize'; + +export const getDeleteManyRecordsMutationResponseField = ( + objectNamePlural: string, +) => `delete${capitalize(objectNamePlural)}`; diff --git a/packages/twenty-front/src/modules/object-record/utils/getDeleteOneRecordMutationResponseField.ts b/packages/twenty-front/src/modules/object-record/utils/getDeleteOneRecordMutationResponseField.ts new file mode 100644 index 000000000000..fef546c878a3 --- /dev/null +++ b/packages/twenty-front/src/modules/object-record/utils/getDeleteOneRecordMutationResponseField.ts @@ -0,0 +1,5 @@ +import { capitalize } from '~/utils/string/capitalize'; + +export const getDeleteOneRecordMutationResponseField = ( + objectNameSingular: string, +) => `delete${capitalize(objectNameSingular)}`; diff --git a/packages/twenty-front/src/modules/object-record/utils/getFindDuplicateRecordsQueryResponseField.ts b/packages/twenty-front/src/modules/object-record/utils/getFindDuplicateRecordsQueryResponseField.ts new file mode 100644 index 000000000000..a3df4ecd3b05 --- /dev/null +++ b/packages/twenty-front/src/modules/object-record/utils/getFindDuplicateRecordsQueryResponseField.ts @@ -0,0 +1,3 @@ +export const getFindDuplicateRecordsQueryResponseField = ( + objectNameSingular: string, +) => `${objectNameSingular}Duplicates`; diff --git a/packages/twenty-front/src/modules/object-record/utils/getUpdateOneRecordMutationResponseField.ts b/packages/twenty-front/src/modules/object-record/utils/getUpdateOneRecordMutationResponseField.ts new file mode 100644 index 000000000000..29b16ad24436 --- /dev/null +++ b/packages/twenty-front/src/modules/object-record/utils/getUpdateOneRecordMutationResponseField.ts @@ -0,0 +1,5 @@ +import { capitalize } from '~/utils/string/capitalize'; + +export const getUpdateOneRecordMutationResponseField = ( + objectNameSingular: string, +) => `update${capitalize(objectNameSingular)}`; diff --git a/packages/twenty-front/src/modules/search/hooks/useFilteredSearchEntityQuery.ts b/packages/twenty-front/src/modules/search/hooks/useFilteredSearchEntityQuery.ts index c6767d1a157f..d7beaefce4fb 100644 --- a/packages/twenty-front/src/modules/search/hooks/useFilteredSearchEntityQuery.ts +++ b/packages/twenty-front/src/modules/search/hooks/useFilteredSearchEntityQuery.ts @@ -1,6 +1,6 @@ import { isNonEmptyString } from '@sniptt/guards'; -import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; +import { useMapToObjectRecordIdentifier } from '@/object-metadata/hooks/useMapToObjectRecordIdentifier'; import { OrderBy } from '@/object-metadata/types/OrderBy'; import { DEFAULT_SEARCH_REQUEST_LIMIT } from '@/object-record/constants/DefaultSearchRequestLimit'; import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords'; @@ -33,9 +33,10 @@ export const useFilteredSearchEntityQuery = ({ excludeEntityIds?: string[]; objectNameSingular: string; }): EntitiesForMultipleEntitySelect => { - const { mapToObjectRecordIdentifier } = useObjectMetadataItem({ + const { mapToObjectRecordIdentifier } = useMapToObjectRecordIdentifier({ objectNameSingular, }); + const mappingFunction = (record: ObjectRecord) => ({ ...mapToObjectRecordIdentifier(record), record, diff --git a/packages/twenty-front/src/modules/views/hooks/internal/usePersistViewFieldRecords.ts b/packages/twenty-front/src/modules/views/hooks/internal/usePersistViewFieldRecords.ts index 0b7db03d039d..c341b0a060a7 100644 --- a/packages/twenty-front/src/modules/views/hooks/internal/usePersistViewFieldRecords.ts +++ b/packages/twenty-front/src/modules/views/hooks/internal/usePersistViewFieldRecords.ts @@ -7,17 +7,27 @@ import { triggerUpdateRecordOptimisticEffect } from '@/apollo/optimistic-effect/ import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems'; import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; +import { useGetRecordFromCache } from '@/object-record/cache/hooks/useGetRecordFromCache'; +import { useCreateOneRecordMutation } from '@/object-record/hooks/useCreateOneRecordMutation'; +import { useUpdateOneRecordMutation } from '@/object-record/hooks/useUpdateOneRecordMutation'; import { ObjectRecord } from '@/object-record/types/ObjectRecord'; import { GraphQLView } from '@/views/types/GraphQLView'; import { ViewField } from '@/views/types/ViewField'; export const usePersistViewFieldRecords = () => { - const { - updateOneRecordMutation, - createOneRecordMutation, - getRecordFromCache, - objectMetadataItem, - } = useObjectMetadataItem({ + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular: CoreObjectNameSingular.ViewField, + }); + + const getRecordFromCache = useGetRecordFromCache({ + objectNameSingular: CoreObjectNameSingular.ViewField, + }); + + const { createOneRecordMutation } = useCreateOneRecordMutation({ + objectNameSingular: CoreObjectNameSingular.ViewField, + }); + + const { updateOneRecordMutation } = useUpdateOneRecordMutation({ objectNameSingular: CoreObjectNameSingular.ViewField, }); diff --git a/packages/twenty-front/src/modules/views/hooks/internal/usePersistViewFilterRecords.ts b/packages/twenty-front/src/modules/views/hooks/internal/usePersistViewFilterRecords.ts index 2a6527c0cf72..4161777a574d 100644 --- a/packages/twenty-front/src/modules/views/hooks/internal/usePersistViewFilterRecords.ts +++ b/packages/twenty-front/src/modules/views/hooks/internal/usePersistViewFilterRecords.ts @@ -7,18 +7,32 @@ import { triggerUpdateRecordOptimisticEffect } from '@/apollo/optimistic-effect/ import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems'; import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; +import { useGetRecordFromCache } from '@/object-record/cache/hooks/useGetRecordFromCache'; +import { useCreateOneRecordMutation } from '@/object-record/hooks/useCreateOneRecordMutation'; +import { useDeleteOneRecordMutation } from '@/object-record/hooks/useDeleteOneRecordMutation'; +import { useUpdateOneRecordMutation } from '@/object-record/hooks/useUpdateOneRecordMutation'; import { ObjectRecord } from '@/object-record/types/ObjectRecord'; import { GraphQLView } from '@/views/types/GraphQLView'; import { ViewFilter } from '@/views/types/ViewFilter'; export const usePersistViewFilterRecords = () => { - const { - updateOneRecordMutation, - createOneRecordMutation, - deleteOneRecordMutation, - objectMetadataItem, - getRecordFromCache, - } = useObjectMetadataItem({ + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular: CoreObjectNameSingular.ViewFilter, + }); + + const getRecordFromCache = useGetRecordFromCache({ + objectNameSingular: CoreObjectNameSingular.ViewFilter, + }); + + const { deleteOneRecordMutation } = useDeleteOneRecordMutation({ + objectNameSingular: CoreObjectNameSingular.ViewFilter, + }); + + const { createOneRecordMutation } = useCreateOneRecordMutation({ + objectNameSingular: CoreObjectNameSingular.ViewFilter, + }); + + const { updateOneRecordMutation } = useUpdateOneRecordMutation({ objectNameSingular: CoreObjectNameSingular.ViewFilter, }); diff --git a/packages/twenty-front/src/modules/views/hooks/internal/usePersistViewSortRecords.ts b/packages/twenty-front/src/modules/views/hooks/internal/usePersistViewSortRecords.ts index 4c927df23cb4..139fff85ffb5 100644 --- a/packages/twenty-front/src/modules/views/hooks/internal/usePersistViewSortRecords.ts +++ b/packages/twenty-front/src/modules/views/hooks/internal/usePersistViewSortRecords.ts @@ -7,18 +7,32 @@ import { triggerUpdateRecordOptimisticEffect } from '@/apollo/optimistic-effect/ import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems'; import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; +import { useGetRecordFromCache } from '@/object-record/cache/hooks/useGetRecordFromCache'; +import { useCreateOneRecordMutation } from '@/object-record/hooks/useCreateOneRecordMutation'; +import { useDeleteOneRecordMutation } from '@/object-record/hooks/useDeleteOneRecordMutation'; +import { useUpdateOneRecordMutation } from '@/object-record/hooks/useUpdateOneRecordMutation'; import { ObjectRecord } from '@/object-record/types/ObjectRecord'; import { GraphQLView } from '@/views/types/GraphQLView'; import { ViewSort } from '@/views/types/ViewSort'; export const usePersistViewSortRecords = () => { - const { - updateOneRecordMutation, - createOneRecordMutation, - deleteOneRecordMutation, - objectMetadataItem, - getRecordFromCache, - } = useObjectMetadataItem({ + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular: CoreObjectNameSingular.ViewSort, + }); + + const getRecordFromCache = useGetRecordFromCache({ + objectNameSingular: CoreObjectNameSingular.ViewSort, + }); + + const { deleteOneRecordMutation } = useDeleteOneRecordMutation({ + objectNameSingular: CoreObjectNameSingular.ViewSort, + }); + + const { createOneRecordMutation } = useCreateOneRecordMutation({ + objectNameSingular: CoreObjectNameSingular.ViewSort, + }); + + const { updateOneRecordMutation } = useUpdateOneRecordMutation({ objectNameSingular: CoreObjectNameSingular.ViewSort, }); diff --git a/packages/twenty-front/src/modules/views/hooks/internal/useViewFromQueryParams.ts b/packages/twenty-front/src/modules/views/hooks/internal/useViewFromQueryParams.ts index f0555f6c5a61..9198268946d4 100644 --- a/packages/twenty-front/src/modules/views/hooks/internal/useViewFromQueryParams.ts +++ b/packages/twenty-front/src/modules/views/hooks/internal/useViewFromQueryParams.ts @@ -3,16 +3,17 @@ import { useParams, useSearchParams } from 'react-router-dom'; import { useApolloClient } from '@apollo/client'; import { isNonEmptyString } from '@sniptt/guards'; import qs from 'qs'; -import { useRecoilCallback } from 'recoil'; +import { useRecoilCallback, useRecoilValue } from 'recoil'; import z from 'zod'; import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useObjectNameSingularFromPlural } from '@/object-metadata/hooks/useObjectNameSingularFromPlural'; import { objectMetadataItemFamilySelector } from '@/object-metadata/states/objectMetadataItemFamilySelector'; +import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState'; import { formatFieldMetadataItemAsFilterDefinition } from '@/object-metadata/utils/formatFieldMetadataItemsAsFilterDefinitions'; import { getObjectRecordIdentifier } from '@/object-metadata/utils/getObjectRecordIdentifier'; -import { useGenerateFindManyRecordsQuery } from '@/object-record/hooks/useGenerateFindManyRecordsQuery'; import { ObjectRecord } from '@/object-record/types/ObjectRecord'; +import { generateFindManyRecordsQuery } from '@/object-record/utils/generateFindManyRecordsQuery'; import { ViewFilter } from '@/views/types/ViewFilter'; import { ViewFilterOperand } from '@/views/types/ViewFilterOperand'; import { isDefined } from '~/utils/isDefined'; @@ -39,8 +40,11 @@ export const useViewFromQueryParams = () => { const { objectNameSingular } = useObjectNameSingularFromPlural({ objectNamePlural, }); - const { objectMetadataItem } = useObjectMetadataItem({ objectNameSingular }); - const generateFindManyRecordsQuery = useGenerateFindManyRecordsQuery(); + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + + const objectMetadataItems = useRecoilValue(objectMetadataItemsState); const queryParamsValidation = filterQueryParamsSchema.safeParse( qs.parse(searchParams.toString()), @@ -119,6 +123,7 @@ export const useViewFromQueryParams = () => { >({ query: generateFindManyRecordsQuery({ objectMetadataItem: relationObjectMetadataItem, + objectMetadataItems, }), variables: { filter: { id: { in: filterValueFromURL } }, @@ -165,9 +170,9 @@ export const useViewFromQueryParams = () => { [ apolloClient, filterQueryParams, - generateFindManyRecordsQuery, hasFiltersQueryParams, objectMetadataItem.fields, + objectMetadataItems, ], ); diff --git a/packages/twenty-front/src/modules/views/hooks/useGetViewFromCache.ts b/packages/twenty-front/src/modules/views/hooks/useGetViewFromCache.ts index 273b5fde6140..cdc95e1e8fbd 100644 --- a/packages/twenty-front/src/modules/views/hooks/useGetViewFromCache.ts +++ b/packages/twenty-front/src/modules/views/hooks/useGetViewFromCache.ts @@ -1,15 +1,15 @@ import { useCallback } from 'react'; import { useApolloClient } from '@apollo/client'; -import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; +import { useGetRecordFromCache } from '@/object-record/cache/hooks/useGetRecordFromCache'; import { View } from '@/views/types/View'; export const useGetViewFromCache = () => { const client = useApolloClient(); const cache = client.cache; - const { getRecordFromCache } = useObjectMetadataItem({ + const getRecordFromCache = useGetRecordFromCache({ objectNameSingular: CoreObjectNameSingular.View, }); diff --git a/packages/twenty-front/src/pages/object-record/RecordShowPage.tsx b/packages/twenty-front/src/pages/object-record/RecordShowPage.tsx index bb649ea19252..20b63e0c5475 100644 --- a/packages/twenty-front/src/pages/object-record/RecordShowPage.tsx +++ b/packages/twenty-front/src/pages/object-record/RecordShowPage.tsx @@ -3,6 +3,7 @@ import { useParams } from 'react-router-dom'; import { useSetRecoilState } from 'recoil'; import { useFavorites } from '@/favorites/hooks/useFavorites'; +import { useLabelIdentifierFieldMetadataItem } from '@/object-metadata/hooks/useLabelIdentifierFieldMetadataItem'; import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useFindOneRecord } from '@/object-record/hooks/useFindOneRecord'; import { RecordShowContainer } from '@/object-record/record-show/components/RecordShowContainer'; @@ -32,8 +33,14 @@ export const RecordShowPage = () => { throw new Error(`Record id is not defined`); } - const { labelIdentifierFieldMetadata, objectMetadataItem } = - useObjectMetadataItem({ objectNameSingular }); + const { objectMetadataItem } = useObjectMetadataItem({ + objectNameSingular, + }); + + const { labelIdentifierFieldMetadataItem } = + useLabelIdentifierFieldMetadataItem({ + objectNameSingular, + }); const { favorites, createFavorite, deleteFavorite } = useFavorites(); @@ -72,9 +79,9 @@ export const RecordShowPage = () => { }; const labelIdentifierFieldValue = - record?.[labelIdentifierFieldMetadata?.name ?? '']; + record?.[labelIdentifierFieldMetadataItem?.name ?? '']; const pageName = - labelIdentifierFieldMetadata?.type === FieldMetadataType.FullName + labelIdentifierFieldMetadataItem?.type === FieldMetadataType.FullName ? [ labelIdentifierFieldValue?.firstName, labelIdentifierFieldValue?.lastName, diff --git a/packages/twenty-front/src/pages/settings/data-model/SettingsObjectNewField/SettingsObjectNewFieldStep2.tsx b/packages/twenty-front/src/pages/settings/data-model/SettingsObjectNewField/SettingsObjectNewFieldStep2.tsx index d707e90ddba1..5feeaee1ddb3 100644 --- a/packages/twenty-front/src/pages/settings/data-model/SettingsObjectNewField/SettingsObjectNewFieldStep2.tsx +++ b/packages/twenty-front/src/pages/settings/data-model/SettingsObjectNewField/SettingsObjectNewFieldStep2.tsx @@ -8,7 +8,7 @@ import { CachedObjectRecordEdge } from '@/apollo/types/CachedObjectRecordEdge'; import { useCreateOneRelationMetadataItem } from '@/object-metadata/hooks/useCreateOneRelationMetadataItem'; import { useFieldMetadataItem } from '@/object-metadata/hooks/useFieldMetadataItem'; import { useFilteredObjectMetadataItems } from '@/object-metadata/hooks/useFilteredObjectMetadataItems'; -import { useObjectMetadataItemOnly } from '@/object-metadata/hooks/useObjectMetadataItemOnly'; +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; import { modifyRecordFromCache } from '@/object-record/cache/utils/modifyRecordFromCache'; import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords'; @@ -85,10 +85,9 @@ export const SettingsObjectNewFieldStep2 = () => { const [objectViews, setObjectViews] = useState([]); const [relationObjectViews, setRelationObjectViews] = useState([]); - const { objectMetadataItem: viewObjectMetadataItem } = - useObjectMetadataItemOnly({ - objectNameSingular: CoreObjectNameSingular.View, - }); + const { objectMetadataItem: viewObjectMetadataItem } = useObjectMetadataItem({ + objectNameSingular: CoreObjectNameSingular.View, + }); useFindManyRecords({ objectNameSingular: CoreObjectNameSingular.View,