Skip to content

Commit

Permalink
4087 refactor object metadata item hooks and utils (#4861)
Browse files Browse the repository at this point in the history
- Extracted each exported element from useObjectMetadataItem into its
own hook.
  • Loading branch information
lucasbordeau authored Apr 9, 2024
1 parent 651af1c commit b1242bb
Show file tree
Hide file tree
Showing 89 changed files with 699 additions and 622 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -71,7 +71,7 @@ export const CalendarEventDetails = ({
calendarEvent,
}: CalendarEventDetailsProps) => {
const theme = useTheme();
const { objectMetadataItem } = useObjectMetadataItemOnly({
const { objectMetadataItem } = useObjectMetadataItem({
objectNameSingular: CoreObjectNameSingular.CalendarEvent,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -58,7 +58,7 @@ export const ActivityBodyEditor = ({
);

const { objectMetadataItem: objectMetadataItemActivity } =
useObjectMetadataItemOnly({
useObjectMetadataItem({
objectNameSingular: CoreObjectNameSingular.Activity,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<Activity>(activityId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -111,7 +111,7 @@ export const ActivityTitle = ({ activityId }: ActivityTitleProps) => {
);

const { objectMetadataItem: objectMetadataItemActivity } =
useObjectMetadataItemOnly({
useObjectMetadataItem({
objectNameSingular: CoreObjectNameSingular.Activity,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -38,12 +38,12 @@ export const useCreateActivityInCache = () => {
});

const { objectMetadataItem: objectMetadataItemActivity } =
useObjectMetadataItemOnly({
useObjectMetadataItem({
objectNameSingular: CoreObjectNameSingular.Activity,
});

const { objectMetadataItem: objectMetadataItemActivityTarget } =
useObjectMetadataItemOnly({
useObjectMetadataItem({
objectNameSingular: CoreObjectNameSingular.ActivityTarget,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -61,7 +61,7 @@ export const ActivityTargetInlineCellEditMode = ({
const { upsertActivity } = useUpsertActivity();

const { objectMetadataItem: objectMetadataItemActivityTarget } =
useObjectMetadataItemOnly({
useObjectMetadataItem({
objectNameSingular: CoreObjectNameSingular.ActivityTarget,
});

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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 }) => (
<RecoilRoot>
<MockedProvider addTypename={false}>{children}</MockedProvider>
</RecoilRoot>
);

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' },
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const Wrapper = ({ children }: { children: ReactNode }) => (
</RecoilRoot>
);

// Split into tests for each new hook
describe('useObjectMetadataItem', () => {
it('should return correct properties', async () => {
const { result } = renderHook(
Expand All @@ -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');
});
});
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -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 };
};
Original file line number Diff line number Diff line change
@@ -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 };
};
Original file line number Diff line number Diff line change
@@ -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 };
};
Loading

0 comments on commit b1242bb

Please sign in to comment.