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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | 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 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x | import { useCallback, useMemo } from 'react';
import { type DesignerLibrary } from '@amalia/amalia-lang/formula/components';
import { AmaliaFunction } from '@amalia/amalia-lang/formula/evaluate/shared';
import {
QUOTA_TYPES,
TokenType,
VariableType,
type FieldVariable,
type Quota,
type StatementVariable,
} from '@amalia/amalia-lang/tokens/types';
import { type Property } from '@amalia/data-capture/fields/types';
import {
useRealCustomObjectDefinitions,
useVirtualCustomObjectDefinitions,
} from '@amalia/data-capture/record-models/state';
import { type CustomObjectDefinition } from '@amalia/data-capture/record-models/types';
import { useShallowObjectMemo } from '@amalia/ext/react/hooks';
import { type Filter, type Relationship, type Rule } from '@amalia/payout-definition/plans/types';
import { useFilters, usePlan, useRelationships, useRules, useVariables } from '@amalia/payout-definition/state';
import { usePlanHubRuleDesignerContext } from '../PlanHubRuleDesigner.context';
export const useDesignerLibrary = () => {
const { planId } = usePlanHubRuleDesignerContext();
const { data: plan } = usePlan(planId);
const { isLoading: isFiltersLoading, data: filters = [], isPending: isFiltersPending } = useFilters(planId);
const {
isLoading: isObjectDefinitionsLoading,
data: objectDefinitions = [],
isPending: isObjectDefinitionsPending,
} = useRealCustomObjectDefinitions();
const {
isLoading: isRelationshipsLoading,
data: relationships = [],
isPending: isRelationshipsPending,
} = useRelationships();
const {
isLoading: isRulesLoading,
data: rules = [],
isPending: isRulesPending,
} = useRules({
select: (rules) => rules.filter((rule) => rule.planId === planId || !rule.planId),
});
const {
isLoading: isVariablesLoading,
data: variables = [],
isPending: isVariablesPending,
} = useVariables({
select: (variables) => variables.filter((variable) => variable.planId === planId || !variable.planId),
});
const {
isLoading: isVirtualObjectDefinitionsLoading,
data: virtualObjectDefinitions = [],
isPending: isVirtualObjectDefinitionsPending,
} = useVirtualCustomObjectDefinitions({ planId });
const { fields, quotas, statementVariables } = useMemo(
() => ({
statementVariables: variables.filter(
(variable): variable is StatementVariable => variable.type === VariableType.statement,
),
fields: variables.filter((variable): variable is FieldVariable => variable.type === VariableType.object),
quotas: variables.filter((variable): variable is Quota => QUOTA_TYPES.includes(variable.type)),
}),
[variables],
);
const amaliaFunctions = useMemo(() => AmaliaFunction.getAllFunctionsArray().map((f) => ({ ...f, id: f.name })), []);
const tokens: DesignerLibrary = useShallowObjectMemo<DesignerLibrary>({
OBJECT_DEFINITIONS: objectDefinitions,
VIRTUAL_OBJECT_DEFINITIONS: virtualObjectDefinitions,
[TokenType.FIELD]: fields,
[TokenType.FILTER]: filters,
[TokenType.QUOTA]: quotas,
[TokenType.LINK]: relationships,
[TokenType.RULE]: rules,
[TokenType.VARIABLE]: statementVariables,
[TokenType.FUNCTION]: amaliaFunctions,
});
const allTokenIds = useMemo(
() => [...variables, ...filters, ...relationships].map((token) => token.id).filter(Boolean),
[variables, filters, relationships],
);
const accessors = useShallowObjectMemo({
getFilterById: useCallback(
(id?: Filter['id'] | null): Filter | undefined => filters.find((filter) => filter.id === id),
[filters],
),
getQuotaById: useCallback(
(id?: Quota['id'] | null): Quota | undefined => quotas.find((quota) => quota.id === id),
[quotas],
),
getRuleById: useCallback(
(id?: Rule['id'] | null): Rule | undefined => rules.find((rule) => rule.id === id),
[rules],
),
getFieldById: useCallback(
(id?: FieldVariable['id'] | null): FieldVariable | undefined => fields.find((field) => field.id === id),
[fields],
),
getStatementVariableById: useCallback(
(id?: StatementVariable['id'] | null): StatementVariable | undefined =>
statementVariables.find((statementVariable) => statementVariable.id === id),
[statementVariables],
),
getRelationshipById: useCallback(
(id?: Relationship['id'] | null): Relationship | undefined =>
relationships.find((relationship) => relationship.id === id),
[relationships],
),
getPropertyByIdentifier: useCallback(
(identifier?: string): { definition: CustomObjectDefinition; property: Property } | undefined => {
if (!identifier) {
return undefined;
}
const [recordModelMachineName, propertyName] = identifier.split('.');
const foundDefinition = [...tokens.OBJECT_DEFINITIONS, ...tokens.VIRTUAL_OBJECT_DEFINITIONS].find(
(d) => d.machineName === recordModelMachineName,
);
if (!foundDefinition) {
return undefined;
}
const foundProperty = foundDefinition.properties[propertyName];
if (!foundProperty) {
return undefined;
}
return { property: foundProperty, definition: foundDefinition };
},
[tokens.OBJECT_DEFINITIONS, tokens.VIRTUAL_OBJECT_DEFINITIONS],
),
getObjectDefinitionById: useCallback(
(objectId: string) => objectDefinitions.find((definition) => definition.id === objectId),
[objectDefinitions],
),
getObjectDefinitionByMachineName: useCallback(
(machineName: string) =>
[...objectDefinitions, ...virtualObjectDefinitions].find(
(definition) => definition.machineName === machineName,
),
[objectDefinitions, virtualObjectDefinitions],
),
});
return {
tokens,
allTokenIds,
accessors,
plan,
isLoading: [
isFiltersLoading,
isObjectDefinitionsLoading,
isRelationshipsLoading,
isRulesLoading,
isVariablesLoading,
isVirtualObjectDefinitionsLoading,
].some((v) => !!v),
isPending: [
isFiltersPending,
isObjectDefinitionsPending,
isRelationshipsPending,
isRulesPending,
isVariablesPending,
isVirtualObjectDefinitionsPending,
].some((v) => !!v),
};
};
|