Files
twenty/packages/twenty-front/src/modules/object-metadata/utils/isManyToOneRelationField.ts
T
Félix Malfait 41239c9f0e refactor(twenty-front): move sub-menu type / constant out of state, extract relation sub-menu
Two review nits:

1. `RELATION_SUB_MENU_FIELD_TYPE` and `ObjectFilterDropdownSubMenuFieldType`
   lived inside the state file. They aren't state — moved to
   `object-filter-dropdown/constants/` and `record-filter/types/`
   respectively, alongside the rest of the constants and types.

2. The relation sub-menu branch in `AdvancedFilterSubFieldSelectMenu`
   computed `targetObjectMetadataId` with an empty-string sentinel so
   the always-running `useFilterableFieldMetadataItems` hook could
   accept it. Fragile (the `''` sentinel is unnamed, untyped, and
   relies on the selector silently returning `[]`). Extracted the
   relation branch into its own `AdvancedFilterRelationSubMenu`
   component that only mounts when actually in relation mode — the
   hook now always receives a real object id, no sentinel.

Also: turned `isManyToOneRelationField` into a generic type guard so
callers can read `field.relation.targetObjectMetadata.id` after the
check without a non-null assertion.
2026-05-15 10:32:20 +02:00

13 lines
426 B
TypeScript

import { FieldMetadataType, RelationType } from '~/generated-metadata/graphql';
type FieldWithRelation = {
type: FieldMetadataType;
relation?: { type: RelationType } | null;
};
export const isManyToOneRelationField = <T extends FieldWithRelation>(
field: T,
): field is T & { relation: NonNullable<T['relation']> } =>
field.type === FieldMetadataType.RELATION &&
field.relation?.type === RelationType.MANY_TO_ONE;