Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 2x 2x 7x 7x 5x 5x 5x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x | import { useCallback } from 'react';
import { match, P } from 'ts-pattern';
import { type SelectProps } from '@allshares/studio-design-system';
import { type AttributeValueForm } from '@amalia/amalia-lang/formula/form/types';
import { type AttributeValue } from '@amalia/amalia-lang/formula/types';
import { TokenType } from '@amalia/amalia-lang/tokens/types';
import { useFormulaBuilderContext } from '../../components/formula-builder/FormulaBuilder.context';
import {
mapFieldToAttributeSelectOption,
mapKeywordToAttributeSelectOption,
mapPropertyToAttributeSelectOption,
mapQuotaToAttributeSelectOption,
mapRelationshipPropertyToAttributeSelectOption,
mapRelationshipToAttributeSelectOption,
mapVariableToAttributeSelectOption,
type AttributeSelectOption,
} from '../use-attributes-options/useAttributesOptions';
import { useGetFormulaBuilderAttribute } from '../use-get-formula-builder-attribute/useGetFormulaBuilderAttribute';
import { useGetFormulaBuilderAttributeLabel } from '../use-get-formula-builder-attribute-label/useGetFormulaBuilderAttributeLabel';
export type UseMapAttributeValueToSelectOptionValue = (
attributeValue: AttributeValueForm,
) => SelectProps<AttributeSelectOption<AttributeValue>, false, true, false>['value'];
/**
* Return a callback taking an AttributeValue as parameter and returning an AttributeSelectOption like useAttributeOptions.
*/
export const useMapAttributeValueToSelectOption = (): UseMapAttributeValueToSelectOptionValue => {
const { customObjectDefinition } = useFormulaBuilderContext();
const getFormulaBuilderAttribute = useGetFormulaBuilderAttribute();
const getFormulaBuilderAttributeLabel = useGetFormulaBuilderAttributeLabel();
return useCallback(
(attributeValue) => {
if (!('fieldType' in attributeValue)) {
return null;
}
return match(attributeValue)
.with({ fieldType: TokenType.VARIABLE }, (attributeValue) => {
const variable = getFormulaBuilderAttribute(attributeValue);
return variable ? mapVariableToAttributeSelectOption(variable, getFormulaBuilderAttributeLabel, false) : null;
})
.with({ fieldType: TokenType.LINK }, (attributeValue) => {
const relationshipAndProperty = getFormulaBuilderAttribute(attributeValue);
if (relationshipAndProperty && 'id' in relationshipAndProperty) {
return mapRelationshipToAttributeSelectOption(
relationshipAndProperty,
getFormulaBuilderAttributeLabel,
false,
);
}
return relationshipAndProperty && customObjectDefinition
? mapRelationshipPropertyToAttributeSelectOption(
customObjectDefinition,
relationshipAndProperty.relationship,
relationshipAndProperty.property,
getFormulaBuilderAttributeLabel,
false,
)
: null;
})
.with({ fieldType: TokenType.FIELD }, (attributeValue) => {
const field = getFormulaBuilderAttribute(attributeValue);
return field ? mapFieldToAttributeSelectOption(field, getFormulaBuilderAttributeLabel, false) : null;
})
.with({ fieldType: P.union(TokenType.PROPERTY, TokenType.VIRTUAL_PROPERTY) }, (attributeValue) => {
const property = getFormulaBuilderAttribute(attributeValue);
return property
? mapPropertyToAttributeSelectOption(
property,
attributeValue.fieldType,
getFormulaBuilderAttributeLabel,
false,
)
: null;
})
.with({ fieldType: TokenType.QUOTA }, (attributeValue) => {
const quota = getFormulaBuilderAttribute(attributeValue);
return quota ? mapQuotaToAttributeSelectOption(quota, getFormulaBuilderAttributeLabel, false) : null;
})
.with({ fieldType: TokenType.KEYWORD }, (attributeValue) =>
mapKeywordToAttributeSelectOption(attributeValue.keyword, getFormulaBuilderAttributeLabel),
)
.otherwise(() => null);
},
[getFormulaBuilderAttribute, getFormulaBuilderAttributeLabel, customObjectDefinition],
);
};
|