mirror of
https://github.com/Unleash/unleash.git
synced 2025-06-27 01:19:00 +02:00
feat: constraints that are in recents will have generated key (#9996)
Previously it was trying to get constraint id for key, but id did not exist. Now for unique keys, I am using the hashed constraint payload.
This commit is contained in:
parent
a2723ec0c0
commit
9aa0c4c738
@ -1,9 +1,9 @@
|
|||||||
import { styled, Typography } from '@mui/material';
|
import { styled, Typography } from '@mui/material';
|
||||||
import { ConstraintAccordionView } from 'component/common/NewConstraintAccordion/ConstraintAccordionView/ConstraintAccordionView';
|
import { ConstraintAccordionView } from 'component/common/NewConstraintAccordion/ConstraintAccordionView/ConstraintAccordionView';
|
||||||
import { constraintId } from 'component/common/LegacyConstraintAccordion/ConstraintAccordionList/createEmptyConstraint';
|
|
||||||
import {
|
import {
|
||||||
useRecentlyUsedConstraints,
|
useRecentlyUsedConstraints,
|
||||||
areConstraintsEqual,
|
areConstraintsEqual,
|
||||||
|
getConstraintKey,
|
||||||
} from './useRecentlyUsedConstraints.ts';
|
} from './useRecentlyUsedConstraints.ts';
|
||||||
import type { IConstraint } from 'interfaces/strategy';
|
import type { IConstraint } from 'interfaces/strategy';
|
||||||
|
|
||||||
@ -55,7 +55,7 @@ export const RecentlyUsedConstraints = ({
|
|||||||
<StyledConstraintsContainer>
|
<StyledConstraintsContainer>
|
||||||
{nonSelectedRecentConstraints.map((constraint) => (
|
{nonSelectedRecentConstraints.map((constraint) => (
|
||||||
<ConstraintAccordionView
|
<ConstraintAccordionView
|
||||||
key={constraint[constraintId]}
|
key={getConstraintKey(constraint)}
|
||||||
constraint={constraint}
|
constraint={constraint}
|
||||||
borderStyle='dashed'
|
borderStyle='dashed'
|
||||||
onUse={() => {
|
onUse={() => {
|
||||||
|
@ -1,32 +1,43 @@
|
|||||||
import { useLocalStorageState } from 'hooks/useLocalStorageState';
|
import { useLocalStorageState } from 'hooks/useLocalStorageState';
|
||||||
import type { IConstraint } from 'interfaces/strategy';
|
import type { IConstraint } from 'interfaces/strategy';
|
||||||
|
|
||||||
|
const hashString = (str: string): number => {
|
||||||
|
let hash = 0;
|
||||||
|
if (str.length === 0) return hash;
|
||||||
|
|
||||||
|
for (let i = 0; i < str.length; i++) {
|
||||||
|
const char = str.charCodeAt(i);
|
||||||
|
hash = (hash << 5) - hash + char;
|
||||||
|
hash = hash & hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Math.abs(hash);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getConstraintKey = (constraint: IConstraint): string => {
|
||||||
|
const sortedValues = (values?: string[]) =>
|
||||||
|
values ? [...values].sort() : undefined;
|
||||||
|
|
||||||
|
const jsonString = JSON.stringify({
|
||||||
|
contextName: constraint.contextName,
|
||||||
|
operator: constraint.operator,
|
||||||
|
values: sortedValues(constraint.values),
|
||||||
|
value: constraint.value,
|
||||||
|
inverted: constraint.inverted,
|
||||||
|
caseInsensitive: constraint.caseInsensitive,
|
||||||
|
});
|
||||||
|
|
||||||
|
return hashString(jsonString).toString();
|
||||||
|
};
|
||||||
|
|
||||||
export const areConstraintsEqual = (
|
export const areConstraintsEqual = (
|
||||||
a: IConstraint,
|
a: IConstraint,
|
||||||
b: IConstraint,
|
b: IConstraint,
|
||||||
): boolean => {
|
): boolean => {
|
||||||
const sortedValues = (values?: string[]) =>
|
const aKey = getConstraintKey(a);
|
||||||
values ? [...values].sort() : undefined;
|
const bKey = getConstraintKey(b);
|
||||||
|
|
||||||
const aJson = JSON.stringify({
|
return aKey === bKey;
|
||||||
contextName: a.contextName,
|
|
||||||
operator: a.operator,
|
|
||||||
values: sortedValues(a.values),
|
|
||||||
value: a.value,
|
|
||||||
inverted: a.inverted,
|
|
||||||
caseInsensitive: a.caseInsensitive,
|
|
||||||
});
|
|
||||||
|
|
||||||
const bJson = JSON.stringify({
|
|
||||||
contextName: b.contextName,
|
|
||||||
operator: b.operator,
|
|
||||||
values: sortedValues(b.values),
|
|
||||||
value: b.value,
|
|
||||||
inverted: b.inverted,
|
|
||||||
caseInsensitive: b.caseInsensitive,
|
|
||||||
});
|
|
||||||
|
|
||||||
return aJson === bJson;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useRecentlyUsedConstraints = (
|
export const useRecentlyUsedConstraints = (
|
||||||
|
Loading…
Reference in New Issue
Block a user