1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-11-10 01:19:53 +01:00
unleash.unleash/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints/RecentlyUsedConstraints/RecentlyUsedConstraints.tsx
Thomas Heartman 6ff20cc7ff
Propagate to use IConstraintWithId everywhere until editor stops complaining.
This now passes checks to be "valid", but it doesn't do anything in and of itself. We still need to actually provide an id for these things to matter.

I'm wondering whether it'd be better to make id mandatory on the regular constraints and add it on every incoming constraint.
2025-07-18 13:18:48 +02:00

70 lines
2.2 KiB
TypeScript

import { styled, Typography } from '@mui/material';
import { ConstraintAccordionView } from 'component/common/NewConstraintAccordion/ConstraintAccordionView/ConstraintAccordionView';
import {
useRecentlyUsedConstraints,
areConstraintsEqual,
getConstraintKey,
} from './useRecentlyUsedConstraints.ts';
import type { IConstraintWithId } from 'interfaces/strategy.ts';
type IRecentlyUsedConstraintsProps = {
setConstraints?: React.Dispatch<React.SetStateAction<IConstraintWithId[]>>;
constraints?: IConstraintWithId[];
};
const StyledContainer = styled('div')(({ theme }) => ({
marginTop: theme.spacing(3),
}));
const StyledHeader = styled(Typography)(({ theme }) => ({
fontSize: theme.fontSizes.smallerBody,
color: theme.palette.text.secondary,
marginBottom: theme.spacing(1),
}));
const StyledConstraintsContainer = styled('div')(({ theme }) => ({
display: 'flex',
flexDirection: 'column',
gap: theme.spacing(1),
}));
export const RecentlyUsedConstraints = ({
setConstraints,
constraints = [],
}: IRecentlyUsedConstraintsProps) => {
const { items: recentlyUsedConstraints } = useRecentlyUsedConstraints();
if (recentlyUsedConstraints.length === 0 || !setConstraints) {
return null;
}
const nonSelectedRecentConstraints = recentlyUsedConstraints.filter(
(recentConstraint) =>
!constraints.some((constraint) =>
areConstraintsEqual(constraint, recentConstraint),
),
);
if (nonSelectedRecentConstraints.length === 0) {
return null;
}
return (
<StyledContainer>
<StyledHeader>Recently used constraints</StyledHeader>
<StyledConstraintsContainer>
{nonSelectedRecentConstraints.map((constraint) => (
<ConstraintAccordionView
key={getConstraintKey(constraint)}
constraint={constraint}
borderStyle='dashed'
onUse={() => {
setConstraints((prev) => [...prev, constraint]);
}}
/>
))}
</StyledConstraintsContainer>
</StyledContainer>
);
};