From 1639b2ad0e85f97c7c7e137c8b7bf83a16e8111c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tha=C3=AFs?= Date: Mon, 25 Mar 2024 12:27:00 +0100 Subject: [PATCH] refactor: improve Settings supported field types validation (#4496) * refactor: improve Settings supported field types validation Related to: #4084, #4295 * fix: fix wrong import --- ...taTypes.ts => SettingsFieldTypeConfigs.ts} | 15 +++++--- .../SettingsDataModelFieldTypeSelect.tsx | 35 ++++++++++++------- .../forms/hooks/useFieldMetadataForm.ts | 3 +- .../SettingsObjectFieldDataType.tsx | 16 ++++++--- .../SettingsObjectFieldItemTableRow.tsx | 13 ++++--- .../types/SettingsSupportedFieldType.ts | 6 ++++ .../utils/getFieldDefaultPreviewValue.ts | 20 +++++++---- .../utils/getSettingsFieldTypeConfig.ts | 8 +++++ .../utils/isFieldTypeSupportedInSettings.ts | 8 +++++ .../data-model/SettingsObjectFieldEdit.tsx | 8 ++++- .../SettingsObjectNewFieldStep2.tsx | 3 +- 11 files changed, 95 insertions(+), 40 deletions(-) rename packages/twenty-front/src/modules/settings/data-model/constants/{SettingsFieldMetadataTypes.ts => SettingsFieldTypeConfigs.ts} (89%) create mode 100644 packages/twenty-front/src/modules/settings/data-model/types/SettingsSupportedFieldType.ts create mode 100644 packages/twenty-front/src/modules/settings/data-model/utils/getSettingsFieldTypeConfig.ts create mode 100644 packages/twenty-front/src/modules/settings/data-model/utils/isFieldTypeSupportedInSettings.ts diff --git a/packages/twenty-front/src/modules/settings/data-model/constants/SettingsFieldMetadataTypes.ts b/packages/twenty-front/src/modules/settings/data-model/constants/SettingsFieldTypeConfigs.ts similarity index 89% rename from packages/twenty-front/src/modules/settings/data-model/constants/SettingsFieldMetadataTypes.ts rename to packages/twenty-front/src/modules/settings/data-model/constants/SettingsFieldTypeConfigs.ts index 6b8f276d0aa6..b0a02592c732 100644 --- a/packages/twenty-front/src/modules/settings/data-model/constants/SettingsFieldMetadataTypes.ts +++ b/packages/twenty-front/src/modules/settings/data-model/constants/SettingsFieldTypeConfigs.ts @@ -1,5 +1,6 @@ import { CurrencyCode } from '@/object-record/record-field/types/CurrencyCode'; import { DEFAULT_DATE_VALUE } from '@/settings/data-model/constants/DefaultDateValue'; +import { SettingsSupportedFieldType } from '@/settings/data-model/types/SettingsSupportedFieldType'; import { IconCalendarEvent, IconCheck, @@ -20,11 +21,15 @@ import { FieldMetadataType } from '~/generated-metadata/graphql'; DEFAULT_DATE_VALUE.setFullYear(DEFAULT_DATE_VALUE.getFullYear() + 2); -export const SETTINGS_FIELD_METADATA_TYPES: Partial< - Record< - FieldMetadataType, - { label: string; Icon: IconComponent; defaultValue?: unknown } - > +export type SettingsFieldTypeConfig = { + label: string; + Icon: IconComponent; + defaultValue?: unknown; +}; + +export const SETTINGS_FIELD_TYPE_CONFIGS: Record< + SettingsSupportedFieldType, + SettingsFieldTypeConfig > = { [FieldMetadataType.Uuid]: { label: 'Unique ID', diff --git a/packages/twenty-front/src/modules/settings/data-model/fields/forms/components/SettingsDataModelFieldTypeSelect.tsx b/packages/twenty-front/src/modules/settings/data-model/fields/forms/components/SettingsDataModelFieldTypeSelect.tsx index 6bb0a6488544..843b0bc096b0 100644 --- a/packages/twenty-front/src/modules/settings/data-model/fields/forms/components/SettingsDataModelFieldTypeSelect.tsx +++ b/packages/twenty-front/src/modules/settings/data-model/fields/forms/components/SettingsDataModelFieldTypeSelect.tsx @@ -1,29 +1,38 @@ -import { SETTINGS_FIELD_METADATA_TYPES } from '@/settings/data-model/constants/SettingsFieldMetadataTypes'; +import omit from 'lodash.omit'; + +import { + SETTINGS_FIELD_TYPE_CONFIGS, + SettingsFieldTypeConfig, +} from '@/settings/data-model/constants/SettingsFieldTypeConfigs'; +import { SettingsSupportedFieldType } from '@/settings/data-model/types/SettingsSupportedFieldType'; import { Select, SelectOption } from '@/ui/input/components/Select'; -import { FieldMetadataType } from '~/generated-metadata/graphql'; type SettingsDataModelFieldTypeSelectProps = { className?: string; disabled?: boolean; - excludedFieldTypes?: FieldMetadataType[]; - onChange?: ({ type }: { type: FieldMetadataType }) => void; - value?: FieldMetadataType; + excludedFieldTypes?: SettingsSupportedFieldType[]; + onChange?: ({ type }: { type: SettingsSupportedFieldType }) => void; + value?: SettingsSupportedFieldType; }; export const SettingsDataModelFieldTypeSelect = ({ className, disabled, - excludedFieldTypes, + excludedFieldTypes = [], onChange, value, }: SettingsDataModelFieldTypeSelectProps) => { - const fieldTypeOptions = Object.entries(SETTINGS_FIELD_METADATA_TYPES) - .filter(([key]) => !excludedFieldTypes?.includes(key as FieldMetadataType)) - .map>(([key, dataTypeConfig]) => ({ - Icon: dataTypeConfig.Icon, - label: dataTypeConfig.label, - value: key as FieldMetadataType, - })); + const fieldTypeConfigs = omit( + SETTINGS_FIELD_TYPE_CONFIGS, + excludedFieldTypes, + ); + const fieldTypeOptions = Object.entries( + fieldTypeConfigs, + ).map>(([key, dataTypeConfig]) => ({ + Icon: dataTypeConfig.Icon, + label: dataTypeConfig.label, + value: key as SettingsSupportedFieldType, + })); return (