diff --git a/packages/twenty-front/src/modules/object-record/record-board/record-board-column/hooks/useAddNewOpportunity.ts b/packages/twenty-front/src/modules/object-record/record-board/record-board-column/hooks/useAddNewOpportunity.ts index 230eedb281..9f3baf4c60 100644 --- a/packages/twenty-front/src/modules/object-record/record-board/record-board-column/hooks/useAddNewOpportunity.ts +++ b/packages/twenty-front/src/modules/object-record/record-board/record-board-column/hooks/useAddNewOpportunity.ts @@ -1,10 +1,12 @@ import { RecordBoardContext } from '@/object-record/record-board/contexts/RecordBoardContext'; import { RecordBoardColumnContext } from '@/object-record/record-board/record-board-column/contexts/RecordBoardColumnContext'; +import { useIsOpportunitiesCompanyFieldDisabled } from '@/object-record/record-board/record-board-column/hooks/useIsOpportunitiesCompanyFieldDisabled'; import { useEntitySelectSearch } from '@/object-record/relation-picker/hooks/useEntitySelectSearch'; import { EntityForSelect } from '@/object-record/relation-picker/types/EntityForSelect'; import { RelationPickerHotkeyScope } from '@/object-record/relation-picker/types/RelationPickerHotkeyScope'; import { usePreviousHotkeyScope } from '@/ui/utilities/hotkey/hooks/usePreviousHotkeyScope'; import { useCallback, useContext, useState } from 'react'; +import { isDefined } from 'twenty-ui'; export const useAddNewOpportunity = (position: string) => { const [isCreatingCard, setIsCreatingCard] = useState(false); @@ -20,21 +22,19 @@ export const useAddNewOpportunity = (position: string) => { const { resetSearchFilter } = useEntitySelectSearch({ relationPickerScopeId: 'relation-picker', }); - + const { isOpportunitiesCompanyFieldDisabled } = + useIsOpportunitiesCompanyFieldDisabled(); const handleEntitySelect = useCallback( (company?: EntityForSelect) => { setIsCreatingCard(false); goBackToPreviousHotkeyScope(); resetSearchFilter(); - - if (company !== undefined) { - createOneRecord({ - name: company.name, - companyId: company.id, - position: position, - [selectFieldMetadataItem.name]: columnDefinition.value, - }); - } + createOneRecord({ + name: company?.name, + companyId: company?.id, + position: position, + [selectFieldMetadataItem.name]: columnDefinition.value, + }); }, [ columnDefinition, @@ -47,11 +47,19 @@ export const useAddNewOpportunity = (position: string) => { ); const handleAddNewOpportunityClick = useCallback(() => { - setIsCreatingCard(true); + if (isOpportunitiesCompanyFieldDisabled) { + handleEntitySelect(); + } else { + setIsCreatingCard(true); + } setHotkeyScopeAndMemorizePreviousScope( RelationPickerHotkeyScope.RelationPicker, ); - }, [setHotkeyScopeAndMemorizePreviousScope]); + }, [ + setHotkeyScopeAndMemorizePreviousScope, + isOpportunitiesCompanyFieldDisabled, + handleEntitySelect, + ]); const handleCancel = useCallback(() => { resetSearchFilter(); diff --git a/packages/twenty-front/src/modules/object-record/record-board/record-board-column/hooks/useIsOpportunitiesCompanyFieldDisabled.ts b/packages/twenty-front/src/modules/object-record/record-board/record-board-column/hooks/useIsOpportunitiesCompanyFieldDisabled.ts new file mode 100644 index 0000000000..f2895e8199 --- /dev/null +++ b/packages/twenty-front/src/modules/object-record/record-board/record-board-column/hooks/useIsOpportunitiesCompanyFieldDisabled.ts @@ -0,0 +1,17 @@ +import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; +import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; + +export const useIsOpportunitiesCompanyFieldDisabled = () => { + const { objectMetadataItem: opportunityMetadataItem } = useObjectMetadataItem( + { + objectNameSingular: CoreObjectNameSingular.Opportunity, + }, + ); + const isOpportunitiesCompanyFieldDisabled = + !opportunityMetadataItem.fields.find( + (field) => field.name === CoreObjectNameSingular.Company, + )?.isActive || false; + return { + isOpportunitiesCompanyFieldDisabled, + }; +}; diff --git a/packages/twenty-front/vite.config.ts b/packages/twenty-front/vite.config.ts index 7139e23285..4fe754f5b7 100644 --- a/packages/twenty-front/vite.config.ts +++ b/packages/twenty-front/vite.config.ts @@ -25,6 +25,7 @@ export default defineConfig(({ command, mode }) => { ? path.resolve(__dirname, './tsconfig.build.json') : path.resolve(__dirname, './tsconfig.dev.json'); + const checkers: Checkers = { overlay: false, };