import { useEffect, useState } from 'react'; import { Box, Typography } from '@mui/material'; import { Dialogue } from 'component/common/Dialogue/Dialogue'; import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender'; import { useChangeRequestsEnabled } from 'hooks/useChangeRequestsEnabled'; import { DependenciesUpgradeAlert } from './DependenciesUpgradeAlert'; import { useUiFlag } from 'hooks/useUiFlag'; import type { IDependency } from '../../../interfaces/featureToggle'; import { ParentVariantOptions } from './ParentVariantOptions'; import { type ParentValue, REMOVE_DEPENDENCY_OPTION } from './constants'; import { FeatureStatusOptions } from './FeatureStatusOptions'; import { useManageDependency } from './useManageDependency'; import { LazyParentOptions } from './LazyParentOptions'; interface IAddDependencyDialogueProps { project: string; featureId: string; parentDependency?: IDependency; showDependencyDialogue: boolean; onClose: () => void; } export const AddDependencyDialogue = ({ project, featureId, parentDependency, showDependencyDialogue, onClose, }: IAddDependencyDialogueProps) => { const [parent, setParent] = useState( parentDependency?.feature || REMOVE_DEPENDENCY_OPTION.key, ); const getInitialParentValue = (): ParentValue => { if (!parentDependency) return { status: 'enabled' }; if (parentDependency.variants?.length) return { status: 'enabled_with_variants', variants: parentDependency.variants, }; if (parentDependency.enabled === false) return { status: 'disabled' }; return { status: 'enabled' }; }; const [parentValue, setParentValue] = useState( getInitialParentValue, ); const resetState = () => { setParent(parentDependency?.feature || REMOVE_DEPENDENCY_OPTION.key); setParentValue(getInitialParentValue()); }; useEffect(() => { resetState(); }, [JSON.stringify(parentDependency)]); const manageDependency = useManageDependency( project, featureId, parent, parentValue, onClose, ); const { isChangeRequestConfiguredInAnyEnv } = useChangeRequestsEnabled(project); const variantDependenciesEnabled = useUiFlag('variantDependencies'); const showStatus = parent !== REMOVE_DEPENDENCY_OPTION.key && variantDependenciesEnabled; const showVariants = parent !== REMOVE_DEPENDENCY_OPTION.key && variantDependenciesEnabled && parentValue.status === 'enabled_with_variants'; const selectStatus = (value: string) => { if (value === 'enabled' || value === 'disabled') { setParentValue({ status: value }); } if (value === 'enabled_with_variants') { setParentValue({ status: value, variants: [], }); } }; const selectVariants = (variants: string[]) => { setParentValue({ status: 'enabled_with_variants', variants, }); }; return ( Your feature will be evaluated only when the selected parent feature is{' '} {parentValue.status === 'disabled' ? 'disabled' : 'enabled'} {' '} in the same environment. What feature do you want to depend on? { setParentValue({ status: 'enabled' }); setParent(status); }} /> } /> What feature status do you want to depend on? } /> What variant do you want to depend on? ) } /> ); };