mirror of
https://github.com/Unleash/unleash.git
synced 2025-07-26 13:48:33 +02:00
chore: fix setConstraints being undefined/null (#9972)
This commit is contained in:
parent
333f1203dc
commit
3c42edfbe8
@ -1,14 +1,11 @@
|
|||||||
import type React from 'react';
|
import type React from 'react';
|
||||||
import { forwardRef, useImperativeHandle } from 'react';
|
import { forwardRef } from 'react';
|
||||||
import { styled } from '@mui/material';
|
import { styled } from '@mui/material';
|
||||||
import type { IConstraint } from 'interfaces/strategy';
|
import type { IConstraint } from 'interfaces/strategy';
|
||||||
import produce from 'immer';
|
import produce from 'immer';
|
||||||
import useUnleashContext from 'hooks/api/getters/useUnleashContext/useUnleashContext';
|
import useUnleashContext from 'hooks/api/getters/useUnleashContext/useUnleashContext';
|
||||||
import { type IUseWeakMap, useWeakMap } from 'hooks/useWeakMap';
|
import type { IUseWeakMap } from 'hooks/useWeakMap';
|
||||||
import {
|
import { constraintId } from 'component/common/LegacyConstraintAccordion/ConstraintAccordionList/createEmptyConstraint';
|
||||||
constraintId,
|
|
||||||
createEmptyConstraint,
|
|
||||||
} from 'component/common/LegacyConstraintAccordion/ConstraintAccordionList/createEmptyConstraint';
|
|
||||||
import { NewConstraintAccordion } from 'component/common/NewConstraintAccordion/NewConstraintAccordion';
|
import { NewConstraintAccordion } from 'component/common/NewConstraintAccordion/NewConstraintAccordion';
|
||||||
import { ConstraintsList } from 'component/common/ConstraintsList/ConstraintsList';
|
import { ConstraintsList } from 'component/common/ConstraintsList/ConstraintsList';
|
||||||
import { useUiFlag } from 'hooks/useUiFlag';
|
import { useUiFlag } from 'hooks/useUiFlag';
|
||||||
@ -44,36 +41,6 @@ const StyledContainer = styled('div')({
|
|||||||
flexDirection: 'column',
|
flexDirection: 'column',
|
||||||
});
|
});
|
||||||
|
|
||||||
export const useConstraintAccordionList = (
|
|
||||||
setConstraints:
|
|
||||||
| React.Dispatch<React.SetStateAction<IConstraint[]>>
|
|
||||||
| undefined,
|
|
||||||
ref: React.RefObject<IConstraintAccordionListRef>,
|
|
||||||
) => {
|
|
||||||
// Constraint metadata: This is a weak map to give a constraint an ID by using the placement in memory.
|
|
||||||
const state = useWeakMap<IConstraint, IConstraintAccordionListItemState>();
|
|
||||||
const { context } = useUnleashContext();
|
|
||||||
|
|
||||||
const addConstraint =
|
|
||||||
setConstraints &&
|
|
||||||
((contextName: string) => {
|
|
||||||
const constraint = createEmptyConstraint(contextName);
|
|
||||||
state.set(constraint, { editing: true, new: true });
|
|
||||||
setConstraints((prev) => [...prev, constraint]);
|
|
||||||
});
|
|
||||||
|
|
||||||
useImperativeHandle(ref, () => ({
|
|
||||||
addConstraint,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const onAdd =
|
|
||||||
addConstraint &&
|
|
||||||
(() => {
|
|
||||||
addConstraint(context[0].name);
|
|
||||||
});
|
|
||||||
|
|
||||||
return { onAdd, state, context };
|
|
||||||
};
|
|
||||||
interface IConstraintList {
|
interface IConstraintList {
|
||||||
constraints: IConstraint[];
|
constraints: IConstraint[];
|
||||||
setConstraints?: React.Dispatch<React.SetStateAction<IConstraint[]>>;
|
setConstraints?: React.Dispatch<React.SetStateAction<IConstraint[]>>;
|
||||||
|
@ -93,12 +93,14 @@ export const FeatureStrategyConstraintAccordionList = forwardRef<
|
|||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
</StyledHelpIconBox>
|
</StyledHelpIconBox>
|
||||||
{addEditStrategy && setConstraints ? (
|
{addEditStrategy ? (
|
||||||
<EditableConstraintsList
|
setConstraints ? (
|
||||||
ref={ref}
|
<EditableConstraintsList
|
||||||
setConstraints={setConstraints}
|
ref={ref}
|
||||||
constraints={constraints}
|
setConstraints={setConstraints}
|
||||||
/>
|
constraints={constraints}
|
||||||
|
/>
|
||||||
|
) : null
|
||||||
) : (
|
) : (
|
||||||
<NewConstraintAccordionList
|
<NewConstraintAccordionList
|
||||||
ref={ref}
|
ref={ref}
|
||||||
|
Loading…
Reference in New Issue
Block a user