Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3585 - Fixes Rule Conditions Removal #3607

Merged
merged 9 commits into from
Oct 12, 2024
21 changes: 17 additions & 4 deletions packages/desktop-client/src/components/modals/EditRuleModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,15 @@ function ConditionEditor({
onDelete,
onAdd,
}) {
const { field: originalField, op, value, type, options, error } = condition;
const {
field: originalField,
op,
value,
type,
options,
error,
inputKey,
} = condition;

let field = originalField;
if (field === 'amount' && options) {
Expand All @@ -246,13 +254,15 @@ function ConditionEditor({
if (type === 'number' && op === 'isbetween') {
valueEditor = (
<BetweenAmountInput
key={inputKey}
defaultValue={value}
onChange={v => onChange('value', v)}
/>
);
} else {
valueEditor = (
<GenericInput
key={inputKey}
field={field}
type={type}
value={value}
Expand Down Expand Up @@ -547,7 +557,7 @@ function StageButton({ selected, children, style, onSelect }) {
}

function newInput(item) {
return { ...item, inputKey: '' + Math.random() };
return { ...item, inputKey: uuid() };
}

function ConditionsList({
Expand Down Expand Up @@ -579,6 +589,7 @@ function ConditionsList({
field,
op: 'is',
value: null,
inputKey: uuid(),
});
onChangeConditions(copy);
}
Expand Down Expand Up @@ -742,15 +753,15 @@ const conditionFields = [

export function EditRuleModal({ defaultRule, onSave: originalOnSave }) {
const [conditions, setConditions] = useState(
defaultRule.conditions.map(parse),
defaultRule.conditions.map(parse).map(c => ({ ...c, inputKey: uuid() })),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improvement: Consider memoizing the initial state

The initial state for conditions is being computed on every render. Consider using useMemo to memoize this computation, especially if defaultRule.conditions is large or complex.

-const [conditions, setConditions] = useState(
-  defaultRule.conditions.map(parse).map(c => ({ ...c, inputKey: uuid() })),
-);
+const [conditions, setConditions] = useState(() =>
+  useMemo(() => defaultRule.conditions.map(parse).map(c => ({ ...c, inputKey: uuid() })), [defaultRule.conditions])
+);

Committable suggestion was skipped due to low confidence.

);
const [actionSplits, setActionSplits] = useState(() => {
const parsedActions = defaultRule.actions.map(parse);
return parsedActions.reduce(
(acc, action) => {
const splitIndex = action.options?.splitIndex ?? 0;
acc[splitIndex] = acc[splitIndex] ?? { id: uuid(), actions: [] };
acc[splitIndex].actions.push(action);
acc[splitIndex].actions.push({ ...action, inputKey: uuid() });
return acc;
},
// The pre-split group is always there
Expand Down Expand Up @@ -821,6 +832,7 @@ export function EditRuleModal({ defaultRule, onSave: originalOnSave }) {
op: 'set-split-amount',
options: { method: 'remainder', splitIndex },
value: null,
inputKey: uuid(),
};
} else {
const fieldsArray = splitIndex === 0 ? actionFields : splitActionFields;
Expand All @@ -835,6 +847,7 @@ export function EditRuleModal({ defaultRule, onSave: originalOnSave }) {
op: 'set',
value: null,
options: { splitIndex },
inputKey: uuid(),
};
}

Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/3607.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [attyluccio]
---

Fixes Rule Conditions Removal