1
0
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:
Jaanus Sellin 2025-05-15 10:50:59 +03:00 committed by GitHub
parent a2723ec0c0
commit 9aa0c4c738
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 23 deletions

View File

@ -1,9 +1,9 @@
import { styled, Typography } from '@mui/material';
import { ConstraintAccordionView } from 'component/common/NewConstraintAccordion/ConstraintAccordionView/ConstraintAccordionView';
import { constraintId } from 'component/common/LegacyConstraintAccordion/ConstraintAccordionList/createEmptyConstraint';
import {
useRecentlyUsedConstraints,
areConstraintsEqual,
getConstraintKey,
} from './useRecentlyUsedConstraints.ts';
import type { IConstraint } from 'interfaces/strategy';
@ -55,7 +55,7 @@ export const RecentlyUsedConstraints = ({
<StyledConstraintsContainer>
{nonSelectedRecentConstraints.map((constraint) => (
<ConstraintAccordionView
key={constraint[constraintId]}
key={getConstraintKey(constraint)}
constraint={constraint}
borderStyle='dashed'
onUse={() => {

View File

@ -1,32 +1,43 @@
import { useLocalStorageState } from 'hooks/useLocalStorageState';
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 = (
a: IConstraint,
b: IConstraint,
): boolean => {
const sortedValues = (values?: string[]) =>
values ? [...values].sort() : undefined;
const aKey = getConstraintKey(a);
const bKey = getConstraintKey(b);
const aJson = JSON.stringify({
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;
return aKey === bKey;
};
export const useRecentlyUsedConstraints = (