The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
feat(front): allow relations to system objects under an Advanced section (#24173)
Félix Malfait3597176adecf
diff --git a/packages/twenty-front/src/modules/object-metadata/utils/__tests__/isAdvancedRelationTargetObjectMetadata.test.ts b/packages/twenty-front/src/modules/object-metadata/utils/__tests__/isAdvancedRelationTargetObjectMetadata.test.tsnew file mode 100644index 00000000000..889c471b8a2--- /dev/null+++ b/packages/twenty-front/src/modules/object-metadata/utils/__tests__/isAdvancedRelationTargetObjectMetadata.test.ts@@ -0,0 +1,33 @@+import { isAdvancedRelationTargetObjectMetadata } from '@/object-metadata/utils/isAdvancedRelationTargetObjectMetadata';+import { getTestEnrichedObjectMetadataItemsMock } from '~/testing/utils/getTestEnrichedObjectMetadataItemsMock';++const getObjectMetadataItemMock = (nameSingular: string) =>+ getTestEnrichedObjectMetadataItemsMock().find(+ (item) => item.nameSingular === nameSingular,+ )!;++describe('isAdvancedRelationTargetObjectMetadata', () => {+ it('should return true when the object is a system object', () => {+ expect(+ isAdvancedRelationTargetObjectMetadata(+ getObjectMetadataItemMock('messageThread'),+ ),+ ).toBe(true);+ });++ it('should return false when the object is a business object', () => {+ expect(+ isAdvancedRelationTargetObjectMetadata(+ getObjectMetadataItemMock('company'),+ ),+ ).toBe(false);+ });++ it('should return false when the object is workspaceMember', () => {+ expect(+ isAdvancedRelationTargetObjectMetadata(+ getObjectMetadataItemMock('workspaceMember'),+ ),+ ).toBe(false);+ });+});diff --git a/packages/twenty-front/src/modules/object-metadata/utils/__tests__/isObjectMetadataEligibleAsRelationTarget.test.ts b/packages/twenty-front/src/modules/object-metadata/utils/__tests__/isObjectMetadataEligibleAsRelationTarget.test.tsnew file mode 100644index 00000000000..23e11d2340a--- /dev/null+++ b/packages/twenty-front/src/modules/object-metadata/utils/__tests__/isObjectMetadataEligibleAsRelationTarget.test.ts@@ -0,0 +1,31 @@+import { isObjectMetadataEligibleAsRelationTarget } from '@/object-metadata/utils/isObjectMetadataEligibleAsRelationTarget';+import { getTestEnrichedObjectMetadataItemsMock } from '~/testing/utils/getTestEnrichedObjectMetadataItemsMock';++const getObjectMetadataItemMock = (nameSingular: string) =>+ getTestEnrichedObjectMetadataItemsMock().find(+ (item) => item.nameSingular === nameSingular,+ )!;++describe('isObjectMetadataEligibleAsRelationTarget', () => {+ it('should return true when the object is a business object', () => {+ expect(+ isObjectMetadataEligibleAsRelationTarget(+ getObjectMetadataItemMock('person'),+ ),+ ).toBe(true);+ });++ it('should return true when the object is a system object', () => {+ expect(+ isObjectMetadataEligibleAsRelationTarget(+ getObjectMetadataItemMock('messageThread'),+ ),+ ).toBe(true);+ });++ it('should return false when the object is remote', () => {+ expect(isObjectMetadataEligibleAsRelationTarget({ isRemote: true })).toBe(+ false,+ );+ });+});diff --git a/packages/twenty-front/src/modules/object-metadata/utils/isAdvancedRelationTargetObjectMetadata.ts b/packages/twenty-front/src/modules/object-metadata/utils/isAdvancedRelationTargetObjectMetadata.tsnew file mode 100644index 00000000000..3bd10e60c21--- /dev/null+++ b/packages/twenty-front/src/modules/object-metadata/utils/isAdvancedRelationTargetObjectMetadata.ts@@ -0,0 +1,14 @@+import { CoreObjectNameSingular } from 'twenty-shared/types';+import { type EnrichedObjectMetadataItem } from '@/object-metadata/types/EnrichedObjectMetadataItem';++// System objects are valid relation targets, but they are technical tables that+// would confuse less technical users, so pickers list them under an "Advanced"+// section instead of next to business objects.+export const isAdvancedRelationTargetObjectMetadata = (+ objectMetadataItem: Pick<+ EnrichedObjectMetadataItem,+ 'isSystem' | 'nameSingular'+ >,+) =>+ objectMetadataItem.isSystem &&+ objectMetadataItem.nameSingular !== CoreObjectNameSingular.WorkspaceMember;diff --git a/packages/twenty-front/src/modules/object-metadata/utils/isObjectMetadataEligibleAsRelationTarget.ts b/packages/twenty-front/src/modules/object-metadata/utils/isObjectMetadataEligibleAsRelationTarget.tsnew file mode 100644index 00000000000..2bf2e4308e5--- /dev/null+++ b/packages/twenty-front/src/modules/object-metadata/utils/isObjectMetadataEligibleAsRelationTarget.ts@@ -0,0 +1,9 @@+import { type EnrichedObjectMetadataItem } from '@/object-metadata/types/EnrichedObjectMetadataItem';++// Whether an object can be picked as the destination when creating a relation,+// mirroring the server rule (field creation only rejects remote parent objects).+// Distinct from isObjectMetadataAvailableForRelation, which answers whether a+// relation cell targeting the object renders in record UIs by default.+export const isObjectMetadataEligibleAsRelationTarget = (+ objectMetadataItem: Pick<EnrichedObjectMetadataItem, 'isRemote'>,+) => !objectMetadataItem.isRemote;diff --git a/packages/twenty-front/src/modules/settings/components/SettingsMorphRelationMultiSelect.tsx b/packages/twenty-front/src/modules/settings/components/SettingsMorphRelationMultiSelect.tsxindex 8434fa9c282..de950575fea 100644--- a/packages/twenty-front/src/modules/settings/components/SettingsMorphRelationMultiSelect.tsx+++ b/packages/twenty-front/src/modules/settings/components/SettingsMorphRelationMultiSelect.tsx@@ -1,15 +1,17 @@import { styled } from '@linaria/react';-import { plural } from '@lingui/core/macro';-import { useMemo, useRef, useState, type MouseEvent } from 'react';+import { plural, t } from '@lingui/core/macro';+import { Fragment, useMemo, useRef, useState, type MouseEvent } from 'react';import { Dropdown } from '@/ui/layout/dropdown/components/Dropdown';import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer';import { DropdownMenuSearchInput } from '@/ui/layout/dropdown/components/DropdownMenuSearchInput';+import { DropdownMenuSectionLabel } from '@/ui/layout/dropdown/components/DropdownMenuSectionLabel';import { DropdownMenuSeparator } from '@/ui/layout/dropdown/components/DropdownMenuSeparator';import { useFilteredObjectMetadataItems } from '@/object-metadata/hooks/useFilteredObjectMetadataItems';import { useObjectMetadataSelectHelpers } from '@/object-metadata/hooks/useObjectMetadataSelectHelpers';-import { isObjectMetadataAvailableForRelation } from '@/object-metadata/utils/isObjectMetadataAvailableForRelation';+import { isAdvancedRelationTargetObjectMetadata } from '@/object-metadata/utils/isAdvancedRelationTargetObjectMetadata';+import { isObjectMetadataEligibleAsRelationTarget } from '@/object-metadata/utils/isObjectMetadataEligibleAsRelationTarget';import { MultiSelectControl } from '@/ui/input/components/MultiSelectControl';import { DropdownContent } from '@/ui/layout/dropdown/components/DropdownContent';import { GenericDropdownContentWidth } from '@/ui/layout/dropdown/constants/GenericDropdownContentWidth';@@ -111,13 +113,14 @@ export const SettingsMorphRelationMultiSelect = ({useState<string[]>(selectedObjectMetadataIds);const options = activeObjectMetadataItems- .filter(isObjectMetadataAvailableForRelation)+ .filter(isObjectMetadataEligibleAsRelationTarget).sort((item1, item2) =>item1.labelSingular.localeCompare(item2.labelSingular),).map((objectMetadataItem) => ({label: objectMetadataItem.labelSingular,objectMetadataId: objectMetadataItem.id,+ isAdvanced: isAdvancedRelationTargetObjectMetadata(objectMetadataItem),...getSelectIconPropsFromObjectMetadataItem(objectMetadataItem),}));@@ -125,14 +128,21 @@ export const SettingsMorphRelationMultiSelect = ({localSelectedObjectMetadataIds.includes(option.objectMetadataId),);- const filteredOptions = useMemo(- () =>- searchInputValue- ? options.filter(({ label }) =>- label.toLowerCase().includes(searchInputValue.toLowerCase()),- )- : options,- [options, searchInputValue],+ const filteredOptions = useMemo(() => {+ const matchingOptions = searchInputValue+ ? options.filter(({ label }) =>+ label.toLowerCase().includes(searchInputValue.toLowerCase()),+ )+ : options;++ return [+ ...matchingOptions.filter(({ isAdvanced }) => !isAdvanced),+ ...matchingOptions.filter(({ isAdvanced }) => isAdvanced),+ ];+ }, [options, searchInputValue]);++ const advancedSectionStartIndex = filteredOptions.findIndex(+ ({ isAdvanced }) => isAdvanced,);const isDisabled =@@ -146,7 +156,9 @@ export const SettingsMorphRelationMultiSelect = ({? selectContainerRef.current?.clientWidth: dropdownWidth;- const selectableItemIdArray = filteredOptions.map((option) => option.label);+ const selectableItemIdArray = filteredOptions.map(+ (option) => option.objectMetadataId,+ );const selectedItemId = useAtomComponentStateValue(selectedItemIdComponentState,@@ -157,7 +169,7 @@ export const SettingsMorphRelationMultiSelect = ({const handleDropdownOpen = () => {if (selectedOptions.length > 0 && !searchInputValue) {- setSelectedItemId(selectedOptions[0].label);+ setSelectedItemId(selectedOptions[0].objectMetadataId);}};@@ -238,37 +250,18 @@ export const SettingsMorphRelationMultiSelect = ({focusId={dropdownId}selectableItemIdArray={selectableItemIdArray}>- {filteredOptions.map((option) => (- <SelectableListItem- key={`${option.objectMetadataId}-${option.label}`}- itemId={option.label}- onEnter={() => {- const newSelectedObjectMetadataIds =- addOrRemoveFromArray(- localSelectedObjectMetadataIds,- option.objectMetadataId,- );- setLocalSelectedObjectMetadataIds(- newSelectedObjectMetadataIds,- );- onChange?.(newSelectedObjectMetadataIds);- onBlur?.();- closeDropdown(dropdownId);- }}- >- <MenuItemMultiSelect- className=""- LeftIcon={option.Icon ?? undefined}- iconThemeColor={option.iconThemeColor}- text={option.label}- selected={selectedObjectMetadataIds.some(- (selectedObjectMetadataId) =>- selectedObjectMetadataId ===- option.objectMetadataId,- )}- isKeySelected={selectedItemId === option.label}- onSelectChange={() => {- let newSelectedObjectMetadataIds =+ {filteredOptions.map((option, optionIndex) => (+ <Fragment key={option.objectMetadataId}>+ {optionIndex === advancedSectionStartIndex && (+ <>+ {optionIndex > 0 && <DropdownMenuSeparator />}+ <DropdownMenuSectionLabel label={t`Advanced`} />+ </>+ )}+ <SelectableListItem+ itemId={option.objectMetadataId}+ onEnter={() => {+ const newSelectedObjectMetadataIds =addOrRemoveFromArray(localSelectedObjectMetadataIds,option.objectMetadataId,@@ -278,9 +271,37 @@ export const SettingsMorphRelationMultiSelect = ({);onChange?.(newSelectedObjectMetadataIds);onBlur?.();+ closeDropdown(dropdownId);}}- />- </SelectableListItem>+ >+ <MenuItemMultiSelect+ className=""+ LeftIcon={option.Icon ?? undefined}+ iconThemeColor={option.iconThemeColor}+ text={option.label}+ selected={selectedObjectMetadataIds.some(+ (selectedObjectMetadataId) =>+ selectedObjectMetadataId ===+ option.objectMetadataId,+ )}+ isKeySelected={+ selectedItemId === option.objectMetadataId+ }+ onSelectChange={() => {+ const newSelectedObjectMetadataIds =+ addOrRemoveFromArray(+ localSelectedObjectMetadataIds,+ option.objectMetadataId,+ );+ setLocalSelectedObjectMetadataIds(+ newSelectedObjectMetadataIds,+ );+ onChange?.(newSelectedObjectMetadataIds);+ onBlur?.();+ }}+ />+ </SelectableListItem>+ </Fragment>))}</SelectableList></DropdownMenuItemsContainer>diff --git a/packages/twenty-front/src/modules/settings/data-model/fields/forms/morph-relation/hooks/useRelationSettingsFormInitialTargetObjectMetadatas.ts b/packages/twenty-front/src/modules/settings/data-model/fields/forms/morph-relation/hooks/useRelationSettingsFormInitialTargetObjectMetadatas.tsindex 24974db6221..9600bcb5602 100644--- a/packages/twenty-front/src/modules/settings/data-model/fields/forms/morph-relation/hooks/useRelationSettingsFormInitialTargetObjectMetadatas.ts+++ b/packages/twenty-front/src/modules/settings/data-model/fields/forms/morph-relation/hooks/useRelationSettingsFormInitialTargetObjectMetadatas.ts@@ -1,7 +1,8 @@import { useFilteredObjectMetadataItems } from '@/object-metadata/hooks/useFilteredObjectMetadataItems';import { useGetIsMetadataItemCustom } from '@/object-metadata/hooks/useGetIsMetadataItemCustom';import { type FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';-import { isObjectMetadataAvailableForRelation } from '@/object-metadata/utils/isObjectMetadataAvailableForRelation';+import { isAdvancedRelationTargetObjectMetadata } from '@/object-metadata/utils/isAdvancedRelationTargetObjectMetadata';+import { isObjectMetadataEligibleAsRelationTarget } from '@/object-metadata/utils/isObjectMetadataEligibleAsRelationTarget';import { fieldMetadataItemHasMorphRelations } from '@/settings/data-model/fields/forms/morph-relation/utils/fieldMetadataItemHasMorphRelations';import { isDefined } from 'twenty-shared/utils';@@ -40,9 +41,14 @@ export const useRelationSettingsFormInitialTargetObjectMetadatas = ({}const availableItems = activeObjectMetadataItems- .filter(isObjectMetadataAvailableForRelation)+ .filter(isObjectMetadataEligibleAsRelationTarget).filter((item) => item.id !== sourceObjectMetadataId).sort((a, b) => {+ const aIsAdvanced = isAdvancedRelationTargetObjectMetadata(a);+ const bIsAdvanced = isAdvancedRelationTargetObjectMetadata(b);+ if (aIsAdvanced !== bIsAdvanced) {+ return aIsAdvanced ? 1 : -1;+ }const aIsCustom = getIsMetadataItemCustom(a);const bIsCustom = getIsMetadataItemCustom(b);if (aIsCustom === bIsCustom) {