import type { MultiValue, SingleValue } from "react-select"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import type { Attribute } from "@calcom/app-store/routing-forms/types/types"; import { Button } from "@calcom/ui/components/button"; import { Input, Select } from "@calcom/ui/components/form"; import { FilterIcon } from "@coss/ui/icons"; import { formatConditionValue, generateConditionId, getConditionTypeOptions, getDefaultAttributeCondition, getDefaultTeamCondition, getOperatorOptionsForAttributeType, getParentOperatorOptions, getTeamOperatorOptions, isArrayOperator, isTeamCondition, type TAttributeSyncRuleConditionWithId, } from "@calcom/features/ee/integration-attribute-sync/lib/ruleHelpers"; import { ConditionIdentifierEnum, type ConditionOperatorEnum, type IAttributeCondition, type IAttributeSyncRule, type ITeamCondition, type RuleOperatorEnum, type TAttributeSyncRuleCondition, } from "@calcom/features/ee/integration-attribute-sync/repositories/IIntegrationAttributeSyncRepository"; const ensureConditionHasId = (condition: TAttributeSyncRuleCondition): TAttributeSyncRuleConditionWithId => { if ("_id" in condition && condition._id) { return condition as TAttributeSyncRuleConditionWithId; } return { ...condition, _id: generateConditionId() } as TAttributeSyncRuleConditionWithId; }; interface RuleBuilderProps { value: IAttributeSyncRule; onChange: (value: IAttributeSyncRule) => void; teamOptions: { value: string; label: string }[]; attributes: Attribute[]; isLoadingTeams?: boolean; isLoadingAttributes?: boolean; } interface ConditionComponentProps { condition: TAttributeSyncRuleCondition; onChange: (condition: TAttributeSyncRuleCondition) => void; onRemove: () => void; teamOptions: { value: string; label: string }[]; attributes: Attribute[]; isLoading?: boolean; } const ConditionComponent = ({ condition, onChange, onRemove, teamOptions, attributes, isLoading, }: ConditionComponentProps) => { const { t } = useLocale(); const conditionTypeOptions = getConditionTypeOptions(t); const conditionTypeOption = conditionTypeOptions.find( (opt: { value: ConditionIdentifierEnum; label: string }) => opt.value === condition.identifier ); const handleTypeChange = (newType: { value: ConditionIdentifierEnum; label: string } | null) => { if (!newType) return; if (newType.value === ConditionIdentifierEnum.TEAM_ID) { onChange(getDefaultTeamCondition()); } else { onChange(getDefaultAttributeCondition()); } }; return (
{/* Condition type selector */}
); } case "TEXT": return (
); case "NUMBER": return (
); } }; return ( <> {t("attribute").toLowerCase()} {renderValueInput()} )} ); }; export const RuleBuilder = ({ value, onChange, teamOptions, attributes, isLoadingTeams, isLoadingAttributes, }: RuleBuilderProps) => { const { t } = useLocale(); const parentOperatorOptions = getParentOperatorOptions(t); const parentOperatorOption = parentOperatorOptions.find( (opt: { value: RuleOperatorEnum; label: string }) => opt.value === value.operator ); const isLoading = isLoadingTeams || isLoadingAttributes; const handleOperatorChange = (newOperator: { value: RuleOperatorEnum; label: string } | null) => { if (!newOperator) return; onChange({ ...value, operator: newOperator.value, }); }; const handleAddCondition = () => { onChange({ ...value, conditions: [...value.conditions, getDefaultTeamCondition()], }); }; const handleConditionChange = (index: number, newCondition: TAttributeSyncRuleCondition) => { const newConditions = [...value.conditions]; newConditions[index] = newCondition; onChange({ ...value, conditions: newConditions, }); }; const handleRemoveCondition = (index: number) => { onChange({ ...value, conditions: value.conditions.filter((_: TAttributeSyncRuleCondition, i: number) => i !== index), }); }; return (
{t("attribute_sync_user_filter_rules")}
{t("attribute_sync_sync_users_where")}