mirror of
https://github.com/Unleash/unleash.git
synced 2025-08-09 13:47:13 +02:00
remove legacy code
This commit is contained in:
parent
cca1c07852
commit
2ddc26be80
@ -1,52 +0,0 @@
|
|||||||
import type { IConstraint } from 'interfaces/strategy';
|
|
||||||
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
|
|
||||||
|
|
||||||
import { ConstraintAccordionEdit } from './ConstraintAccordionEdit/ConstraintAccordionEdit.tsx';
|
|
||||||
import { ConstraintAccordionView } from './ConstraintAccordionView/ConstraintAccordionView.tsx';
|
|
||||||
|
|
||||||
interface IConstraintAccordionProps {
|
|
||||||
compact: boolean;
|
|
||||||
editing: boolean;
|
|
||||||
constraint: IConstraint;
|
|
||||||
onCancel: () => void;
|
|
||||||
onEdit?: () => void;
|
|
||||||
onDelete?: () => void;
|
|
||||||
onSave?: (constraint: IConstraint) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @deprecated use `component/common/NewConstraintAccordion/NewConstraintAccordion`
|
|
||||||
*/
|
|
||||||
export const ConstraintAccordion = ({
|
|
||||||
constraint,
|
|
||||||
compact = false,
|
|
||||||
editing,
|
|
||||||
onEdit,
|
|
||||||
onCancel,
|
|
||||||
onDelete,
|
|
||||||
onSave,
|
|
||||||
}: IConstraintAccordionProps) => {
|
|
||||||
if (!constraint) return null;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<ConditionallyRender
|
|
||||||
condition={Boolean(editing && onSave)}
|
|
||||||
show={
|
|
||||||
<ConstraintAccordionEdit
|
|
||||||
constraint={constraint}
|
|
||||||
onCancel={onCancel}
|
|
||||||
onSave={onSave!}
|
|
||||||
onDelete={onDelete}
|
|
||||||
compact={compact}
|
|
||||||
/>
|
|
||||||
}
|
|
||||||
elseShow={
|
|
||||||
<ConstraintAccordionView
|
|
||||||
constraint={constraint}
|
|
||||||
onEdit={onEdit}
|
|
||||||
onDelete={onDelete}
|
|
||||||
/>
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
};
|
|
@ -1,261 +0,0 @@
|
|||||||
import type React from 'react';
|
|
||||||
import {
|
|
||||||
forwardRef,
|
|
||||||
Fragment,
|
|
||||||
type RefObject,
|
|
||||||
useImperativeHandle,
|
|
||||||
} from 'react';
|
|
||||||
import { Button, styled, Tooltip } from '@mui/material';
|
|
||||||
import HelpOutline from '@mui/icons-material/HelpOutline';
|
|
||||||
import type { IConstraint } from 'interfaces/strategy';
|
|
||||||
import { ConstraintAccordion } from 'component/common/LegacyConstraintAccordion/ConstraintAccordion';
|
|
||||||
import produce from 'immer';
|
|
||||||
import useUnleashContext from 'hooks/api/getters/useUnleashContext/useUnleashContext';
|
|
||||||
import { type IUseWeakMap, useWeakMap } from 'hooks/useWeakMap';
|
|
||||||
import { objectId } from 'utils/objectId';
|
|
||||||
import { createEmptyConstraint } from 'component/common/LegacyConstraintAccordion/ConstraintAccordionList/createEmptyConstraint';
|
|
||||||
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
|
|
||||||
import { StrategySeparator } from 'component/common/StrategySeparator/LegacyStrategySeparator';
|
|
||||||
|
|
||||||
export interface IConstraintAccordionListProps {
|
|
||||||
constraints: IConstraint[];
|
|
||||||
setConstraints?: React.Dispatch<React.SetStateAction<IConstraint[]>>;
|
|
||||||
showCreateButton?: boolean;
|
|
||||||
/* Add "constraints" title on the top - default `true` */
|
|
||||||
showLabel?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ref methods exposed by this component.
|
|
||||||
export interface IConstraintAccordionListRef {
|
|
||||||
addConstraint?: (contextName: string) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extra form state for each constraint.
|
|
||||||
interface IConstraintAccordionListItemState {
|
|
||||||
// Is the constraint new (never been saved)?
|
|
||||||
new?: boolean;
|
|
||||||
// Is the constraint currently being edited?
|
|
||||||
editing?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const constraintAccordionListId = 'constraintAccordionListId';
|
|
||||||
|
|
||||||
const StyledContainer = styled('div')(({ theme }) => ({
|
|
||||||
width: '100%',
|
|
||||||
display: 'flex',
|
|
||||||
flexDirection: 'column',
|
|
||||||
'&.constraint-list-element': {
|
|
||||||
borderRadius: theme.shape.borderRadiusMedium,
|
|
||||||
backgroundColor: theme.palette.background.default,
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
||||||
const StyledHelpWrapper = styled(Tooltip)(({ theme }) => ({
|
|
||||||
marginLeft: theme.spacing(0.75),
|
|
||||||
height: theme.spacing(1.5),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const StyledHelp = styled(HelpOutline)(({ theme }) => ({
|
|
||||||
fill: theme.palette.action.active,
|
|
||||||
[theme.breakpoints.down(860)]: {
|
|
||||||
display: 'none',
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
||||||
const StyledConstraintLabel = styled('p')(({ theme }) => ({
|
|
||||||
marginBottom: theme.spacing(1),
|
|
||||||
color: theme.palette.text.secondary,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const StyledAddCustomLabel = styled('div')(({ theme }) => ({
|
|
||||||
marginTop: theme.spacing(1),
|
|
||||||
marginBottom: theme.spacing(1),
|
|
||||||
color: theme.palette.text.primary,
|
|
||||||
display: 'flex',
|
|
||||||
}));
|
|
||||||
|
|
||||||
export const useConstraintAccordionList = (
|
|
||||||
setConstraints:
|
|
||||||
| React.Dispatch<React.SetStateAction<IConstraint[]>>
|
|
||||||
| undefined,
|
|
||||||
ref: React.RefObject<IConstraintAccordionListRef>,
|
|
||||||
) => {
|
|
||||||
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 };
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @deprecated use NewConstraintAccordion components
|
|
||||||
*/
|
|
||||||
export const ConstraintAccordionList = forwardRef<
|
|
||||||
IConstraintAccordionListRef | undefined,
|
|
||||||
IConstraintAccordionListProps
|
|
||||||
>(
|
|
||||||
(
|
|
||||||
{ constraints, setConstraints, showCreateButton, showLabel = true },
|
|
||||||
ref,
|
|
||||||
) => {
|
|
||||||
const { onAdd, state, context } = useConstraintAccordionList(
|
|
||||||
setConstraints,
|
|
||||||
ref as RefObject<IConstraintAccordionListRef>,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (context.length === 0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<StyledContainer id={constraintAccordionListId}>
|
|
||||||
<ConditionallyRender
|
|
||||||
condition={
|
|
||||||
constraints && constraints.length > 0 && showLabel
|
|
||||||
}
|
|
||||||
show={
|
|
||||||
<StyledConstraintLabel>
|
|
||||||
Constraints
|
|
||||||
</StyledConstraintLabel>
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
<ConstraintList
|
|
||||||
ref={ref}
|
|
||||||
setConstraints={setConstraints}
|
|
||||||
constraints={constraints}
|
|
||||||
state={state}
|
|
||||||
/>
|
|
||||||
<ConditionallyRender
|
|
||||||
condition={Boolean(showCreateButton && onAdd)}
|
|
||||||
show={
|
|
||||||
<div>
|
|
||||||
<StyledAddCustomLabel>
|
|
||||||
<p>Add any number of constraints</p>
|
|
||||||
<StyledHelpWrapper
|
|
||||||
title='View constraints documentation'
|
|
||||||
arrow
|
|
||||||
>
|
|
||||||
<a
|
|
||||||
href={
|
|
||||||
'https://docs.getunleash.io/reference/activation-strategies#constraints'
|
|
||||||
}
|
|
||||||
target='_blank'
|
|
||||||
rel='noopener noreferrer'
|
|
||||||
>
|
|
||||||
<StyledHelp />
|
|
||||||
</a>
|
|
||||||
</StyledHelpWrapper>
|
|
||||||
</StyledAddCustomLabel>
|
|
||||||
<Button
|
|
||||||
type='button'
|
|
||||||
onClick={onAdd}
|
|
||||||
variant='outlined'
|
|
||||||
color='primary'
|
|
||||||
data-testid='ADD_CONSTRAINT_BUTTON'
|
|
||||||
>
|
|
||||||
Add constraint
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
</StyledContainer>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
interface IConstraintList {
|
|
||||||
constraints: IConstraint[];
|
|
||||||
setConstraints?: React.Dispatch<React.SetStateAction<IConstraint[]>>;
|
|
||||||
state: IUseWeakMap<IConstraint, IConstraintAccordionListItemState>;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @deprecated use NewConstraintAccordion components
|
|
||||||
*/
|
|
||||||
export const ConstraintList = forwardRef<
|
|
||||||
IConstraintAccordionListRef | undefined,
|
|
||||||
IConstraintList
|
|
||||||
>(({ constraints, setConstraints, state }, ref) => {
|
|
||||||
const { context } = useUnleashContext();
|
|
||||||
|
|
||||||
const onEdit =
|
|
||||||
setConstraints &&
|
|
||||||
((constraint: IConstraint) => {
|
|
||||||
state.set(constraint, { editing: true });
|
|
||||||
});
|
|
||||||
|
|
||||||
const onRemove =
|
|
||||||
setConstraints &&
|
|
||||||
((index: number) => {
|
|
||||||
const constraint = constraints[index];
|
|
||||||
state.set(constraint, {});
|
|
||||||
setConstraints(
|
|
||||||
produce((draft) => {
|
|
||||||
draft.splice(index, 1);
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
const onSave =
|
|
||||||
setConstraints &&
|
|
||||||
((index: number, constraint: IConstraint) => {
|
|
||||||
state.set(constraint, {});
|
|
||||||
setConstraints(
|
|
||||||
produce((draft) => {
|
|
||||||
draft[index] = constraint;
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
const onCancel = (index: number) => {
|
|
||||||
const constraint = constraints[index];
|
|
||||||
state.get(constraint)?.new && onRemove?.(index);
|
|
||||||
state.set(constraint, {});
|
|
||||||
};
|
|
||||||
|
|
||||||
if (context.length === 0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<StyledContainer
|
|
||||||
id={constraintAccordionListId}
|
|
||||||
className='constraint-list-element'
|
|
||||||
>
|
|
||||||
{constraints.map((constraint, index) => (
|
|
||||||
<Fragment key={objectId(constraint)}>
|
|
||||||
<ConditionallyRender
|
|
||||||
condition={index > 0}
|
|
||||||
show={<StrategySeparator text='AND' />}
|
|
||||||
/>
|
|
||||||
<ConstraintAccordion
|
|
||||||
constraint={constraint}
|
|
||||||
onEdit={onEdit?.bind(null, constraint)}
|
|
||||||
onCancel={onCancel.bind(null, index)}
|
|
||||||
onDelete={onRemove?.bind(null, index)}
|
|
||||||
onSave={onSave?.bind(null, index)}
|
|
||||||
editing={Boolean(state.get(constraint)?.editing)}
|
|
||||||
compact
|
|
||||||
/>
|
|
||||||
</Fragment>
|
|
||||||
))}
|
|
||||||
</StyledContainer>
|
|
||||||
);
|
|
||||||
});
|
|
@ -1,25 +0,0 @@
|
|||||||
import { dateOperators } from 'constants/operators';
|
|
||||||
import type { IConstraint } from 'interfaces/strategy';
|
|
||||||
import { oneOf } from 'utils/oneOf';
|
|
||||||
import { operatorsForContext } from 'utils/operatorsForContext';
|
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
|
||||||
|
|
||||||
export const constraintId = Symbol('id');
|
|
||||||
|
|
||||||
export const createEmptyConstraint = (contextName: string): IConstraint => {
|
|
||||||
const operator = operatorsForContext(contextName)[0];
|
|
||||||
|
|
||||||
const value = oneOf(dateOperators, operator)
|
|
||||||
? new Date().toISOString()
|
|
||||||
: '';
|
|
||||||
|
|
||||||
return {
|
|
||||||
contextName,
|
|
||||||
operator,
|
|
||||||
value,
|
|
||||||
values: [],
|
|
||||||
caseInsensitive: false,
|
|
||||||
inverted: false,
|
|
||||||
[constraintId]: uuidv4(),
|
|
||||||
};
|
|
||||||
};
|
|
Loading…
Reference in New Issue
Block a user