mirror of
https://github.com/Unleash/unleash.git
synced 2025-11-10 01:19:53 +01:00
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.
72 lines
2.0 KiB
TypeScript
72 lines
2.0 KiB
TypeScript
import { useLocalStorageState } from 'hooks/useLocalStorageState';
|
|
import type { IConstraint, IConstraintWithId } 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 aKey = getConstraintKey(a);
|
|
const bKey = getConstraintKey(b);
|
|
|
|
return aKey === bKey;
|
|
};
|
|
|
|
export const useRecentlyUsedConstraints = (
|
|
initialItems: IConstraintWithId[] = [],
|
|
) => {
|
|
const [items, setItems] = useLocalStorageState<IConstraintWithId[]>(
|
|
'recently-used-constraints',
|
|
initialItems,
|
|
);
|
|
|
|
const addItem = (newItem: IConstraintWithId | IConstraintWithId[]) => {
|
|
setItems((prevItems) => {
|
|
const itemsToAdd = Array.isArray(newItem) ? newItem : [newItem];
|
|
|
|
let updatedItems = [...prevItems];
|
|
|
|
itemsToAdd.forEach((item) => {
|
|
updatedItems = updatedItems.filter(
|
|
(existingItem) => !areConstraintsEqual(existingItem, item),
|
|
);
|
|
updatedItems = [item, ...updatedItems];
|
|
});
|
|
return updatedItems.slice(0, 3);
|
|
});
|
|
};
|
|
|
|
return {
|
|
items,
|
|
addItem,
|
|
};
|
|
};
|