1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-10-27 11:02:16 +01:00
unleash.unleash/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints/useEditableConstraint/constraint-reducer.ts
Thomas Heartman 4ea2499ce7
Chore(1-3688): improve performance for large lists of legal values (#9978)
This PR implements a number of strategies to make the app perform better
when you have large lists. For instance, we have a constraint field that
has ~600 legal values. Currently in main, it is pretty slow and sloggy
to use (about on par with what we see in hosted).

With this PR, it becomes pretty snappy, as shown in this video (should
hopefully be even better in production mode?):


https://www.loom.com/share/2e882bee25a3454a85bec7752e8252dc?sid=7786b22d-6c60-47e8-bd71-cc5f347c4e0f

The steps taken are:
1. Change the `useState` hook to instead use `useReducer`. The reason is
that its dispatch function is guaranteed to have a stable identity. This
lets us use it in memoized functions and components.

2. Because useReducer doesn't update the state variable until the next
render, we need to use `useEffect` to update the constraint when it has
actually updated instead of just calling it after the reducer.

3. Add a `toggle value` action and use that instead of checking whether
the value is equal or not inside an onChange function. If we were to
check the state of the value outside the reducer, the memoized function
would be re-evaluated every time value or values change, which would
result in more renders than necessary. By instead doing this kind of
checking inside the reducer, we can cache more aggressively.

4. Because the onChange function can now be memoized, we can memoize all
the legal value selector labels too. This is the real goal here, because
we don't need to re-render 600 components, because one of them was
checked.

One side effect of using useEffect to call `onUpdate` is that it will
also be called immediately when the hook is invoked the first time, but
it will be called with the same value as the constraint that was passed
in, so I don't think that's an issue.

Second: the `useEffect` call uses `localConstraint` directly as a dep
instead of stringifying it. I'm not sure why, but stringifying it makes
it not update correctly for legal values.

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-05-14 08:04:39 +02:00

175 lines
5.5 KiB
TypeScript

import {
DATE_AFTER,
IN,
type Operator,
isDateOperator,
isMultiValueOperator,
isSingleValueOperator,
} from 'constants/operators';
import { CURRENT_TIME_CONTEXT_FIELD } from 'utils/operatorsForContext';
import {
type EditableDateConstraint,
isDateConstraint,
isSingleValueConstraint,
type EditableConstraint,
type EditableMultiValueConstraint,
type EditableSingleValueConstraint,
isMultiValueConstraint,
} from './editable-constraint-type';
import { difference, union } from './set-functions';
export type ConstraintUpdateAction =
| { type: 'add value(s)'; payload: string | string[] }
| { type: 'clear values' }
| { type: 'remove value'; payload: string }
| { type: 'set context field'; payload: string }
| { type: 'set operator'; payload: Operator }
| { type: 'toggle case sensitivity' }
| { type: 'toggle inverted operator' }
| { type: 'toggle value'; payload: string };
const withValue = <
T extends EditableConstraint & { value?: string; values?: Set<string> },
>(
newValue: string | null,
constraint: T,
): EditableConstraint => {
const { value, values, ...rest } = constraint;
if (isMultiValueOperator(constraint.operator)) {
return {
...rest,
values: new Set([newValue].filter(Boolean)),
} as EditableConstraint;
}
return {
...rest,
value: newValue ?? '',
} as EditableConstraint;
};
export const constraintReducer = (
state: EditableConstraint,
action: ConstraintUpdateAction,
deletedLegalValues?: Set<string>,
): EditableConstraint => {
const removeValue = (value: string) => {
if (isSingleValueConstraint(state)) {
if (state.value === value) {
return {
...state,
value: '',
};
} else {
return state;
}
}
const updatedValues = difference(state.values, new Set([value]));
return {
...state,
values: updatedValues ?? new Set(),
};
};
const addValue = (value: string | string[]) => {
if (isSingleValueConstraint(state)) {
const newValue = Array.isArray(value) ? value[0] : value;
if (deletedLegalValues?.has(newValue)) {
if (deletedLegalValues?.has(state.value)) {
return {
...state,
value: '',
};
}
return state;
}
return {
...state,
value: newValue ?? '',
};
}
const newValues = new Set(Array.isArray(value) ? value : [value]);
const combinedValues = union(state.values, newValues);
const filteredValues = deletedLegalValues
? difference(combinedValues, deletedLegalValues)
: combinedValues;
return {
...state,
values: filteredValues,
};
};
switch (action.type) {
case 'set context field':
if (action.payload === state.contextName) {
return state;
}
if (
action.payload === CURRENT_TIME_CONTEXT_FIELD &&
!isDateOperator(state.operator)
) {
return withValue(new Date().toISOString(), {
...state,
contextName: action.payload,
operator: DATE_AFTER,
} as EditableDateConstraint);
} else if (
action.payload !== CURRENT_TIME_CONTEXT_FIELD &&
isDateOperator(state.operator)
) {
return withValue(null, {
...state,
operator: IN,
contextName: action.payload,
} as EditableMultiValueConstraint);
}
return withValue(null, {
...state,
contextName: action.payload,
});
case 'set operator':
if (action.payload === state.operator) {
return state;
}
if (isDateConstraint(state) && isDateOperator(action.payload)) {
return {
...state,
operator: action.payload,
};
}
if (isSingleValueOperator(action.payload)) {
return withValue(null, {
...state,
operator: action.payload,
} as EditableSingleValueConstraint);
}
return withValue(null, {
...state,
operator: action.payload,
} as EditableMultiValueConstraint);
case 'add value(s)': {
return addValue(action.payload);
}
case 'toggle inverted operator':
return { ...state, inverted: !state.inverted };
case 'toggle case sensitivity':
return { ...state, caseInsensitive: !state.caseInsensitive };
case 'remove value':
return removeValue(action.payload);
case 'clear values':
return withValue(null, state);
case 'toggle value':
if (
(isSingleValueConstraint(state) &&
state.value === action.payload) ||
(isMultiValueConstraint(state) &&
state.values.has(action.payload))
) {
return removeValue(action.payload);
}
return addValue(action.payload);
}
};