1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-07-31 13:47:02 +02:00

add utils, fix some imports

This commit is contained in:
Thomas Heartman 2025-06-10 13:41:07 +02:00
parent bf8a1b763e
commit 5403ac0751
5 changed files with 75 additions and 3 deletions

View File

@ -1,7 +1,6 @@
import type { ComponentProps, FC, ReactNode } from 'react';
import { StrategyEvaluationItem } from '../StrategyEvaluationItem/StrategyEvaluationItem.tsx';
import type { ConstraintSchema } from 'openapi';
import { formatOperatorDescription } from 'component/common/LegacyConstraintAccordion/ConstraintOperator/formatOperatorDescription';
import { StrategyEvaluationChip } from '../StrategyEvaluationChip/StrategyEvaluationChip.tsx';
import { styled, Tooltip } from '@mui/material';
import { Truncator } from 'component/common/Truncator/Truncator';
@ -11,6 +10,7 @@ import { formatConstraintValue } from 'utils/formatConstraintValue';
import { useConstraintTooltips } from './hooks/useConstraintTooltips.ts';
import { ReactComponent as CaseSensitiveIcon } from 'assets/icons/case-sensitive.svg';
import { isCaseSensitive } from './isCaseSensitive.ts';
import { formatOperatorDescription } from 'utils/formatOperatorDescription.ts';
const Operator: FC<{
label: ConstraintSchema['operator'];

View File

@ -15,7 +15,7 @@ import {
inOperators,
} from 'constants/operators';
import { useState } from 'react';
import { formatOperatorDescription } from 'component/common/LegacyConstraintAccordion/ConstraintOperator/formatOperatorDescription';
import { formatOperatorDescription } from 'utils/formatOperatorDescription';
interface IConstraintOperatorSelectProps {
options: Operator[];

View File

@ -14,10 +14,10 @@ import {
numOperators,
inOperators,
} from 'constants/operators';
import { formatOperatorDescription } from 'component/common/LegacyConstraintAccordion/ConstraintOperator/formatOperatorDescription';
import { useId } from 'react';
import { ScreenReaderOnly } from 'component/common/ScreenReaderOnly/ScreenReaderOnly';
import KeyboardArrowDownOutlined from '@mui/icons-material/KeyboardArrowDownOutlined';
import { formatOperatorDescription } from 'utils/formatOperatorDescription';
interface IConstraintOperatorSelectProps {
options: Operator[];

View File

@ -0,0 +1,25 @@
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(),
};
};

View File

@ -0,0 +1,47 @@
import type { Operator } from 'constants/operators';
export const formatOperatorDescription = (
operator: Operator,
inverted?: boolean,
): string => {
if (inverted) {
return invertedConstraintOperatorDescriptions[operator];
}
return constraintOperatorDescriptions[operator];
};
const constraintOperatorDescriptions = {
IN: 'is one of',
NOT_IN: 'is not one of',
STR_CONTAINS: 'is a string that contains',
STR_STARTS_WITH: 'is a string that starts with',
STR_ENDS_WITH: 'is a string that ends with',
NUM_EQ: 'is a number equal to',
NUM_GT: 'is a number greater than',
NUM_GTE: 'is a number greater than or equal to',
NUM_LT: 'is a number less than',
NUM_LTE: 'is a number less than or equal to',
DATE_BEFORE: 'is a date before',
DATE_AFTER: 'is a date after',
SEMVER_EQ: 'is a SemVer equal to',
SEMVER_GT: 'is a SemVer greater than',
SEMVER_LT: 'is a SemVer less than',
};
const invertedConstraintOperatorDescriptions = {
IN: 'is not one of',
NOT_IN: 'is one of',
STR_CONTAINS: 'is a string that does not contain',
STR_STARTS_WITH: 'is a string that does not start with',
STR_ENDS_WITH: 'is a string that does not end with',
NUM_EQ: 'is a number not equal to',
NUM_GT: 'is a number not greater than',
NUM_GTE: 'is a number less than',
NUM_LT: 'is a number not less than',
NUM_LTE: 'is a number greater than',
DATE_BEFORE: 'is a date not before',
DATE_AFTER: 'is a date not after',
SEMVER_EQ: 'is a SemVer not equal to',
SEMVER_GT: 'is a SemVer not greater than',
SEMVER_LT: 'is a SemVer not less than',
};