Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,16 @@ interface ArrayPropertyProps {
property: PropertyAllType;
}

type ValuePropertyControlType = keyof typeof VALUE_PROPERTY_CONTROL_TYPES;

const initialAvailablePropertyTypes = Object.keys(VALUE_PROPERTY_CONTROL_TYPES).map((type) => ({
label: type as ValuePropertyControlType,
value: type as ValuePropertyControlType,
label: type as string,
value: type as string,
}));

const ArrayProperty = ({onDeleteClick, path, property}: ArrayPropertyProps) => {
const [arrayItems, setArrayItems] = useState<Array<ArrayPropertyType | Array<ArrayPropertyType>>>([]);
const [availablePropertyTypes, setAvailablePropertyTypes] =
useState<Array<{label: ValuePropertyControlType; value: ValuePropertyControlType}>>(
initialAvailablePropertyTypes
);
const [newPropertyType, setNewPropertyType] = useState<ValuePropertyControlType>();
useState<Array<{label: string; value: string}>>(initialAvailablePropertyTypes);
const [newPropertyType, setNewPropertyType] = useState<string>();

const {currentComponent} = useWorkflowNodeDetailsPanelStore();

Expand All @@ -44,18 +40,24 @@ const ArrayProperty = ({onDeleteClick, path, property}: ArrayPropertyProps) => {
return;
}

const controlType: ControlType = matchingItem
? (matchingItem.controlType as ControlType)
: newPropertyType && newPropertyType in VALUE_PROPERTY_CONTROL_TYPES
? (VALUE_PROPERTY_CONTROL_TYPES[
newPropertyType as keyof typeof VALUE_PROPERTY_CONTROL_TYPES
] as ControlType)
: ('STRING' as ControlType);

const newItem = {
...matchingItem,
controlType: matchingItem
? matchingItem?.controlType
: (VALUE_PROPERTY_CONTROL_TYPES[newPropertyType!] as ControlType),
controlType,
custom: true,
expressionEnabled: true,
key: getRandomId(),
label: `Item ${arrayItems.length.toString()}`,
name: `${name}__${arrayItems.length.toString()}`,
path: `${path}[${arrayItems.length.toString()}]`,
type: matchingItem?.type || newPropertyType || 'STRING',
type: (matchingItem?.type as PropertyType) || (newPropertyType as PropertyType) || 'STRING',
};

setArrayItems([...arrayItems, newItem]);
Expand All @@ -75,16 +77,28 @@ const ArrayProperty = ({onDeleteClick, path, property}: ArrayPropertyProps) => {

// get available property types from items and additional properties
useEffect(() => {
let propertyTypes: Array<{label: ValuePropertyControlType; value: ValuePropertyControlType}> = [];
let propertyTypes: Array<{label: string; value: string}> = [];

const hasDuplicateTypes = items?.some(
(item, index) => items.findIndex((otherItem) => otherItem.type === item.type) !== index
);

const processItems = (items: Array<PropertyAllType>) =>
items.reduce((types: Array<{label: ValuePropertyControlType; value: ValuePropertyControlType}>, item) => {
items.reduce((types: Array<{label: string; value: string}>, item) => {
if (item.type) {
types.push({
label: item.type as ValuePropertyControlType,
value: item.type as ValuePropertyControlType,
});
if (currentComponent?.componentName === 'condition' && hasDuplicateTypes) {
types.push({
label: item.label ?? item.type,
value: `${item.type}_${item.label}`,
});
} else {
types.push({
label: item.type,
value: item.type,
});
}
}

return types;
}, []);

Expand Down Expand Up @@ -116,6 +130,12 @@ const ArrayProperty = ({onDeleteClick, path, property}: ArrayPropertyProps) => {
}
}, [availablePropertyTypes]);

useEffect(() => {
if (currentComponent?.componentName === 'condition' && availablePropertyTypes.length) {
setNewPropertyType(availablePropertyTypes[0].value);
}
}, [currentComponent?.componentName, availablePropertyTypes]);

// render individual array items with data gathered from parameters
useEffect(() => {
if (
Expand Down Expand Up @@ -169,11 +189,16 @@ const ArrayProperty = ({onDeleteClick, path, property}: ArrayPropertyProps) => {
parameterItemType = 'ARRAY';
}

const controlType: ControlType =
parameterItemType && parameterItemType in VALUE_PROPERTY_CONTROL_TYPES
? (VALUE_PROPERTY_CONTROL_TYPES[
parameterItemType as keyof typeof VALUE_PROPERTY_CONTROL_TYPES
] as ControlType)
: ('STRING' as ControlType);

const newSubProperty = {
arrayName: name,
controlType: VALUE_PROPERTY_CONTROL_TYPES[
parameterItemType as ValuePropertyControlType
] as ControlType,
controlType,
custom: true,
defaultValue: parameterItemValue,
expressionEnabled: true,
Expand Down Expand Up @@ -236,6 +261,12 @@ const ArrayProperty = ({onDeleteClick, path, property}: ArrayPropertyProps) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
if (property.items?.length) {
setArrayItems(property.items);
}
}, [property.items]);

return (
<Fragment key={`${path}_${name}_arrayProperty`}>
<ul className="ml-2 flex flex-col space-y-4 border-l">
Expand Down Expand Up @@ -272,6 +303,7 @@ const ArrayProperty = ({onDeleteClick, path, property}: ArrayPropertyProps) => {
<SubPropertyPopover
array
availablePropertyTypes={availablePropertyTypes}
condition={currentComponent?.componentName === 'condition'}
handleClick={handleAddItemClick}
key={`${path}_${name}_subPropertyPopoverButton`}
newPropertyType={newPropertyType}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const PropertySelect = ({
'relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 pl-2 pr-8 text-sm outline-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50',
option.value === value && 'px-2'
)}
key={option.value}
key={`${option.value}_${option.label}`}
value={option.value}
>
<span className="absolute right-2 flex size-3.5 items-center justify-center">
Expand All @@ -136,7 +136,7 @@ const PropertySelect = ({
</div>
</Item>
) : (
<SelectItem key={option.value} value={option.value}>
<SelectItem key={`${option.value}_${option.label}`} value={option.value}>
{option.label}
</SelectItem>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ import PropertySelect from './PropertySelect';
interface SubPropertyPopoverProps {
array?: boolean;
availablePropertyTypes: Array<{label: string; value: string}>;
condition?: boolean;
handleClick: () => void;
newPropertyName?: string;
newPropertyType: keyof typeof VALUE_PROPERTY_CONTROL_TYPES;
newPropertyType: keyof typeof VALUE_PROPERTY_CONTROL_TYPES | string;
setNewPropertyName?: (value: string) => void;
setNewPropertyType: (value: keyof typeof VALUE_PROPERTY_CONTROL_TYPES) => void;
}

const SubPropertyPopover = ({
array,
availablePropertyTypes,
condition,
handleClick,
newPropertyName,
newPropertyType,
Expand Down Expand Up @@ -77,29 +79,44 @@ const SubPropertyPopover = ({
/>
)}

{availablePropertyTypes?.length > 1 ? (
{condition && availablePropertyTypes?.length > 1 && (
<PropertySelect
label="Type"
onValueChange={(value) =>
setNewPropertyType(value as keyof typeof VALUE_PROPERTY_CONTROL_TYPES)
}
options={availablePropertyTypes.map((property) => ({
label: property.value!,
value: property.value!,
label: property.label!,
value: property.label!,
}))}
value={newPropertyType}
/>
) : (
<div className="flex w-full items-center gap-2 text-sm">
<span className="font-medium">Type</span>

{availablePropertyTypes[0] && (
<span className="inline-flex w-full rounded-md bg-white">
{availablePropertyTypes[0].value}
</span>
)}
</div>
)}

{!condition &&
(availablePropertyTypes?.length > 1 ? (
<PropertySelect
label="Type"
onValueChange={(value) =>
setNewPropertyType(value as keyof typeof VALUE_PROPERTY_CONTROL_TYPES)
}
options={availablePropertyTypes.map((property) => ({
label: property.label!,
value: property.value!,
}))}
value={newPropertyType}
/>
) : (
<div className="flex w-full items-center gap-2 text-sm">
<span className="font-medium">Type</span>

{availablePropertyTypes[0] && (
<span className="inline-flex w-full rounded-md bg-white">
{availablePropertyTypes[0].value}
</span>
)}
</div>
))}
</main>

<footer className="flex items-center justify-end space-x-2">
Expand Down