tiagobento commented on code in PR #2912:
URL: 
https://github.com/apache/incubator-kie-tools/pull/2912#discussion_r1974377299


##########
packages/boxed-expression-component/src/expressions/DecisionTableExpression/DecisionTableExpression.tsx:
##########
@@ -416,35 +416,60 @@ export function DecisionTableExpression({
     widths,
   ]);
 
-  const beeTableRows = useMemo(
-    () =>
-      (decisionTableExpression.rule ?? []).map((rule) => {
-        const ruleRow = [
-          ...(rule.inputEntry ?? []),
-          ...(rule.outputEntry ?? new 
Array(decisionTableExpression.output.length)),
-          ...(rule.annotationEntry ?? []),
-        ];
-
-        return getColumnsAtLastLevel(beeTableColumns).reduce(
-          (tableRow: ROWTYPE, column, columnIndex) => {
-            tableRow[column.accessor] = {
-              id: (ruleRow[columnIndex] as DMN15__tUnaryTests & 
DMN15__tLiteralExpression)?.["@_id"] ?? "",
-              content: ruleRow[columnIndex]?.text?.__$$text ?? "",
-            };
-            return tableRow;
-          },
-          { id: rule["@_id"] }
-        );
-      }),
-    [beeTableColumns, decisionTableExpression.output.length, 
decisionTableExpression.rule]
-  );
+  const createDefaultRule = () => {
+    const defaultRowToAdd: Normalized<DMN15__tDecisionRule> = {
+      "@_id": generateUuid(),
+      inputEntry: [
+        {
+          "@_id": generateUuid(),
+          text: { __$$text: DECISION_TABLE_INPUT_DEFAULT_VALUE },
+        },
+      ],
+      outputEntry: [
+        {
+          "@_id": generateUuid(),
+          text: { __$$text: DECISION_TABLE_OUTPUT_DEFAULT_VALUE },
+        },
+      ],
+      annotationEntry: [{ text: { __$$text: "// Your annotations here" } }],
+    };
+    return defaultRowToAdd;
+  };
+
+  const beeTableRows = useMemo(() => {
+    const mapRuleToRow = (rule: Normalized<DMN15__tDecisionRule>) => {
+      const ruleRow = [
+        ...(rule.inputEntry ?? []),
+        ...(rule.outputEntry ?? new 
Array(decisionTableExpression.output.length)),
+        ...(rule.annotationEntry ?? []),
+      ];
+
+      return getColumnsAtLastLevel(beeTableColumns).reduce(
+        (tableRow: ROWTYPE, column, columnIndex) => {
+          tableRow[column.accessor] = {
+            id: (ruleRow[columnIndex] as DMN15__tUnaryTests & 
DMN15__tLiteralExpression)?.["@_id"] ?? "",
+            content: ruleRow[columnIndex]?.text?.__$$text ?? "",
+          };
+          return tableRow;
+        },
+        { id: rule["@_id"] }
+      );
+    };
+    const prevRules = decisionTableExpression.rule ?? [];
+    if (prevRules.length === 0) {
+      return [mapRuleToRow(createDefaultRule())];
+    }
+    return prevRules.map(mapRuleToRow);
+  }, [decisionTableExpression.rule, decisionTableExpression.output.length, 
beeTableColumns]);
 
   const onCellUpdates = useCallback(
     (cellUpdates: BeeTableCellUpdate<ROWTYPE>[]) => {
       setExpression({
         setExpressionAction: (prev: Normalized<BoxedDecisionTable>) => {
           let previousExpression: Normalized<BoxedDecisionTable> = { ...prev };
-
+          if (!previousExpression.rule) {
+            previousExpression.rule = [createDefaultRule()];
+          }

Review Comment:
   ```suggestion
             if (!previousExpression.rule || previousExpression.rule.length === 
0) {
               previousExpression.rule = [createDefaultRule()];
             }
   
   ```



##########
packages/boxed-expression-component/src/expressions/DecisionTableExpression/DecisionTableExpression.tsx:
##########
@@ -719,9 +744,11 @@ export function DecisionTableExpression({
     (args: { beforeIndex: number; rowsCount: number }) => {
       setExpression({
         setExpressionAction: (prev: Normalized<BoxedDecisionTable>) => {
+          if (!prev.rule) {
+            prev.rule = [createDefaultRule()];
+          }

Review Comment:
   ```suggestion
             if (!prev.rule || prev.rule.length === 0)) {
               prev.rule = [createDefaultRule()];
             }
   
   ```



##########
packages/boxed-expression-component/src/expressions/DecisionTableExpression/DecisionTableExpression.tsx:
##########
@@ -416,35 +416,60 @@ export function DecisionTableExpression({
     widths,
   ]);
 
-  const beeTableRows = useMemo(
-    () =>
-      (decisionTableExpression.rule ?? []).map((rule) => {
-        const ruleRow = [
-          ...(rule.inputEntry ?? []),
-          ...(rule.outputEntry ?? new 
Array(decisionTableExpression.output.length)),
-          ...(rule.annotationEntry ?? []),
-        ];
-
-        return getColumnsAtLastLevel(beeTableColumns).reduce(
-          (tableRow: ROWTYPE, column, columnIndex) => {
-            tableRow[column.accessor] = {
-              id: (ruleRow[columnIndex] as DMN15__tUnaryTests & 
DMN15__tLiteralExpression)?.["@_id"] ?? "",
-              content: ruleRow[columnIndex]?.text?.__$$text ?? "",
-            };
-            return tableRow;
-          },
-          { id: rule["@_id"] }
-        );
-      }),
-    [beeTableColumns, decisionTableExpression.output.length, 
decisionTableExpression.rule]
-  );
+  const createDefaultRule = () => {
+    const defaultRowToAdd: Normalized<DMN15__tDecisionRule> = {
+      "@_id": generateUuid(),
+      inputEntry: [
+        {
+          "@_id": generateUuid(),
+          text: { __$$text: DECISION_TABLE_INPUT_DEFAULT_VALUE },
+        },
+      ],
+      outputEntry: [
+        {
+          "@_id": generateUuid(),
+          text: { __$$text: DECISION_TABLE_OUTPUT_DEFAULT_VALUE },
+        },
+      ],
+      annotationEntry: [{ text: { __$$text: "// Your annotations here" } }],
+    };
+    return defaultRowToAdd;
+  };
+
+  const beeTableRows = useMemo(() => {
+    const mapRuleToRow = (rule: Normalized<DMN15__tDecisionRule>) => {
+      const ruleRow = [
+        ...(rule.inputEntry ?? []),
+        ...(rule.outputEntry ?? new 
Array(decisionTableExpression.output.length)),
+        ...(rule.annotationEntry ?? []),
+      ];
+
+      return getColumnsAtLastLevel(beeTableColumns).reduce(
+        (tableRow: ROWTYPE, column, columnIndex) => {
+          tableRow[column.accessor] = {
+            id: (ruleRow[columnIndex] as DMN15__tUnaryTests & 
DMN15__tLiteralExpression)?.["@_id"] ?? "",
+            content: ruleRow[columnIndex]?.text?.__$$text ?? "",
+          };
+          return tableRow;
+        },
+        { id: rule["@_id"] }
+      );
+    };
+    const prevRules = decisionTableExpression.rule ?? [];
+    if (prevRules.length === 0) {
+      return [mapRuleToRow(createDefaultRule())];
+    }
+    return prevRules.map(mapRuleToRow);

Review Comment:
   Please be attentive with negative spacing for improved readability. Nice 
function extraction!



##########
packages/boxed-expression-component/src/expressions/DecisionTableExpression/DecisionTableExpression.tsx:
##########
@@ -416,35 +416,60 @@ export function DecisionTableExpression({
     widths,
   ]);
 
-  const beeTableRows = useMemo(
-    () =>
-      (decisionTableExpression.rule ?? []).map((rule) => {
-        const ruleRow = [
-          ...(rule.inputEntry ?? []),
-          ...(rule.outputEntry ?? new 
Array(decisionTableExpression.output.length)),
-          ...(rule.annotationEntry ?? []),
-        ];
-
-        return getColumnsAtLastLevel(beeTableColumns).reduce(
-          (tableRow: ROWTYPE, column, columnIndex) => {
-            tableRow[column.accessor] = {
-              id: (ruleRow[columnIndex] as DMN15__tUnaryTests & 
DMN15__tLiteralExpression)?.["@_id"] ?? "",
-              content: ruleRow[columnIndex]?.text?.__$$text ?? "",
-            };
-            return tableRow;
-          },
-          { id: rule["@_id"] }
-        );
-      }),
-    [beeTableColumns, decisionTableExpression.output.length, 
decisionTableExpression.rule]
-  );
+  const createDefaultRule = () => {
+    const defaultRowToAdd: Normalized<DMN15__tDecisionRule> = {
+      "@_id": generateUuid(),
+      inputEntry: [
+        {
+          "@_id": generateUuid(),
+          text: { __$$text: DECISION_TABLE_INPUT_DEFAULT_VALUE },
+        },
+      ],
+      outputEntry: [
+        {
+          "@_id": generateUuid(),
+          text: { __$$text: DECISION_TABLE_OUTPUT_DEFAULT_VALUE },
+        },
+      ],
+      annotationEntry: [{ text: { __$$text: "// Your annotations here" } }],
+    };
+    return defaultRowToAdd;
+  };
+
+  const beeTableRows = useMemo(() => {
+    const mapRuleToRow = (rule: Normalized<DMN15__tDecisionRule>) => {
+      const ruleRow = [
+        ...(rule.inputEntry ?? []),
+        ...(rule.outputEntry ?? new 
Array(decisionTableExpression.output.length)),
+        ...(rule.annotationEntry ?? []),
+      ];
+
+      return getColumnsAtLastLevel(beeTableColumns).reduce(
+        (tableRow: ROWTYPE, column, columnIndex) => {
+          tableRow[column.accessor] = {
+            id: (ruleRow[columnIndex] as DMN15__tUnaryTests & 
DMN15__tLiteralExpression)?.["@_id"] ?? "",
+            content: ruleRow[columnIndex]?.text?.__$$text ?? "",
+          };
+          return tableRow;
+        },
+        { id: rule["@_id"] }
+      );
+    };
+    const prevRules = decisionTableExpression.rule ?? [];
+    if (prevRules.length === 0) {
+      return [mapRuleToRow(createDefaultRule())];
+    }
+    return prevRules.map(mapRuleToRow);

Review Comment:
   I also removed the temporary variable in my suggestion.



##########
packages/boxed-expression-component/src/expressions/DecisionTableExpression/DecisionTableExpression.tsx:
##########
@@ -416,35 +416,60 @@ export function DecisionTableExpression({
     widths,
   ]);
 
-  const beeTableRows = useMemo(
-    () =>
-      (decisionTableExpression.rule ?? []).map((rule) => {
-        const ruleRow = [
-          ...(rule.inputEntry ?? []),
-          ...(rule.outputEntry ?? new 
Array(decisionTableExpression.output.length)),
-          ...(rule.annotationEntry ?? []),
-        ];
-
-        return getColumnsAtLastLevel(beeTableColumns).reduce(
-          (tableRow: ROWTYPE, column, columnIndex) => {
-            tableRow[column.accessor] = {
-              id: (ruleRow[columnIndex] as DMN15__tUnaryTests & 
DMN15__tLiteralExpression)?.["@_id"] ?? "",
-              content: ruleRow[columnIndex]?.text?.__$$text ?? "",
-            };
-            return tableRow;
-          },
-          { id: rule["@_id"] }
-        );
-      }),
-    [beeTableColumns, decisionTableExpression.output.length, 
decisionTableExpression.rule]
-  );
+  const createDefaultRule = () => {
+    const defaultRowToAdd: Normalized<DMN15__tDecisionRule> = {
+      "@_id": generateUuid(),
+      inputEntry: [
+        {
+          "@_id": generateUuid(),
+          text: { __$$text: DECISION_TABLE_INPUT_DEFAULT_VALUE },
+        },
+      ],
+      outputEntry: [
+        {
+          "@_id": generateUuid(),
+          text: { __$$text: DECISION_TABLE_OUTPUT_DEFAULT_VALUE },
+        },
+      ],
+      annotationEntry: [{ text: { __$$text: "// Your annotations here" } }],
+    };
+    return defaultRowToAdd;
+  };
+
+  const beeTableRows = useMemo(() => {
+    const mapRuleToRow = (rule: Normalized<DMN15__tDecisionRule>) => {
+      const ruleRow = [
+        ...(rule.inputEntry ?? []),
+        ...(rule.outputEntry ?? new 
Array(decisionTableExpression.output.length)),
+        ...(rule.annotationEntry ?? []),
+      ];
+
+      return getColumnsAtLastLevel(beeTableColumns).reduce(
+        (tableRow: ROWTYPE, column, columnIndex) => {
+          tableRow[column.accessor] = {
+            id: (ruleRow[columnIndex] as DMN15__tUnaryTests & 
DMN15__tLiteralExpression)?.["@_id"] ?? "",
+            content: ruleRow[columnIndex]?.text?.__$$text ?? "",
+          };
+          return tableRow;
+        },
+        { id: rule["@_id"] }
+      );
+    };
+    const prevRules = decisionTableExpression.rule ?? [];
+    if (prevRules.length === 0) {
+      return [mapRuleToRow(createDefaultRule())];
+    }
+    return prevRules.map(mapRuleToRow);

Review Comment:
   ```suggestion
       };
   
       if (!decisionTableExpression.rule || decisionTableExpression.rule.length 
=== 0) {
         return [mapRuleToRow(createDefaultRule())];
       }
   
       return prevRules.map(mapRuleToRow);
   ```



##########
packages/boxed-expression-component/src/expressions/DecisionTableExpression/DecisionTableExpression.tsx:
##########
@@ -416,35 +416,60 @@ export function DecisionTableExpression({
     widths,
   ]);
 
-  const beeTableRows = useMemo(
-    () =>
-      (decisionTableExpression.rule ?? []).map((rule) => {
-        const ruleRow = [
-          ...(rule.inputEntry ?? []),
-          ...(rule.outputEntry ?? new 
Array(decisionTableExpression.output.length)),
-          ...(rule.annotationEntry ?? []),
-        ];
-
-        return getColumnsAtLastLevel(beeTableColumns).reduce(
-          (tableRow: ROWTYPE, column, columnIndex) => {
-            tableRow[column.accessor] = {
-              id: (ruleRow[columnIndex] as DMN15__tUnaryTests & 
DMN15__tLiteralExpression)?.["@_id"] ?? "",
-              content: ruleRow[columnIndex]?.text?.__$$text ?? "",
-            };
-            return tableRow;
-          },
-          { id: rule["@_id"] }
-        );
-      }),
-    [beeTableColumns, decisionTableExpression.output.length, 
decisionTableExpression.rule]
-  );
+  const createDefaultRule = () => {
+    const defaultRowToAdd: Normalized<DMN15__tDecisionRule> = {
+      "@_id": generateUuid(),
+      inputEntry: [
+        {
+          "@_id": generateUuid(),
+          text: { __$$text: DECISION_TABLE_INPUT_DEFAULT_VALUE },
+        },
+      ],
+      outputEntry: [
+        {
+          "@_id": generateUuid(),
+          text: { __$$text: DECISION_TABLE_OUTPUT_DEFAULT_VALUE },
+        },
+      ],
+      annotationEntry: [{ text: { __$$text: "// Your annotations here" } }],
+    };
+    return defaultRowToAdd;
+  };

Review Comment:
   Not memoized. Unnecessary local variable. Could be outside of the React 
component in the global scope as a named function.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to