import * as React from 'react'; import { Button, Flex, FlexItem, Label, LabelGroup, Truncate, } from '@patternfly/react-core'; import { MinusCircleIcon } from '@patternfly/react-icons/dist/esm/icons/minus-circle-icon'; import { useField } from 'formik'; import { SegmentedToggle } from '~/shared/components/segmented-toggle'; import { MultiSelectComponentsDropdown, SingleSelectComponentDropdown, } from './ComponentRelationshipDropdowns'; import { ComponentRelationNudgeType } from './type'; type ComponentRelationProps = { componentNames: string[]; sortedGroupedComponents: { [application: string]: string[] }; index?: number; removeProps: { disableRemove: boolean; onRemove: () => void; }; }; const TARGET_LABELS_VISIBLE = 3; const getNudgeToggleOptions = (index?: number) => [ { value: ComponentRelationNudgeType.NUDGES, label: 'Nudges', tooltip: "The component's builds propagate changes to the nudged component.", id: `nudges-${index}`, }, { value: ComponentRelationNudgeType.NUDGED_BY, label: 'Nudged by', tooltip: "The component will be changed by nudging component's build.", id: `nudged-by-${index}`, }, ]; export const ComponentRelation: React.FC = ({ index, componentNames, sortedGroupedComponents, removeProps: { disableRemove, onRemove }, }) => { const sourceName = `relations.${index.toString()}.source`; const nudgeName = `relations.${index.toString()}.nudgeType`; const targetName = `relations.${index.toString()}.target`; const [{ value: sourceValue }] = useField(sourceName); const [{ value: nudgeValue }, , { setValue: setNudgeValue }] = useField(nudgeName); const [{ value: targetValue }, , { setValue: setTargetValue }] = useField(targetName); const [isTargetMenuOpen, setIsTargetMenuOpen] = React.useState(false); const visibleTargetValues = React.useMemo( () => targetValue.slice(0, TARGET_LABELS_VISIBLE), [targetValue], ); const hiddenTargetCount = targetValue.length - visibleTargetValues.length; const handleRemoveTarget = (component: string) => { void setTargetValue(targetValue.filter((item) => item !== component)); }; const handleShowMoreTargets = React.useCallback((event: React.MouseEvent) => { event.preventDefault(); event.stopPropagation(); setIsTargetMenuOpen(true); }, []); return ( targetValue.includes(item)} /> void setNudgeValue(value)} options={getNudgeToggleOptions(index)} /> {targetValue?.length > 0 ? ( {visibleTargetValues.map((component) => ( ))} {hiddenTargetCount > 0 ? ( ) : null} ) : null} ); };