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


##########
packages/dmn-editor/src/DmnEditor.tsx:
##########
@@ -207,6 +210,12 @@ export const DmnEditorInternal = ({
   const { dmnModelBeforeEditingRef, dmnEditorRootElementRef } = useDmnEditor();
   const { externalModelsByNamespace } = useExternalModels();
 
+  dmnEditorStoreApi.subscribe((newState, prevState) => {
+    if (newState.boxedExpressionEditor.activeDrgElementId !== 
prevState.boxedExpressionEditor.activeDrgElementId) {
+      
onOpenedBoxedExpressionChange?.(newState.boxedExpressionEditor.activeDrgElementId
 ?? "");
+    }
+  });

Review Comment:
   Ok, so this, although works, is a memory leak. Whenever there is a subscribe 
without an unsubscribe, be suspicious! Another problem here is that this code 
will be executed everytime `DmnEditorInternal` renders, so there will be one 
new subscription for each render.
   
   There are two ways of doing this, IMHO:
   
   1. `useEffect`
   ```ts
     const activeDrgElementId = useDmnEditorStore(s => 
s.boxedExpressionEditor.activeDrgElementId);
     useEffect(() => {
       onOpenedBoxedExpressionChange?.(activeDrgElementId);
     }, [activeDrgElementId]);
   ```
   
   Note that there's no `if` here, which can be a problem is you really want to 
avoid calling the callback.
   
   ---
   
   2. Unsubscribing
   ```ts
     useEffect(() => {
       const unsubscribe = dmnEditorStoreApi.subscribe((newState, prevState) => 
{
         if (newState.boxedExpressionEditor.activeDrgElementId !== 
prevState.boxedExpressionEditor.activeDrgElementId) {
             
onOpenedBoxedExpressionChange?.(newState.boxedExpressionEditor.activeDrgElementId
 ?? "");
          }
        });
   
        return () => {
           unsubscribe();
        }
     }, []);
   ```



##########
packages/form-dmn/src/FormDmnOutputs.tsx:
##########
@@ -66,11 +66,19 @@ export interface FormDmnOutputsProps {
   notificationsPanel: boolean;
   openEvaluationTab?: () => void;
   openBoxedExpressionEditor?: (nodeId: string) => void;
+  openedBoxedExpressionId?: string;
 }
 
-export function FormDmnOutputs({ openEvaluationTab, openBoxedExpressionEditor, 
...props }: FormDmnOutputsProps) {
+export function FormDmnOutputs({
+  openEvaluationTab,
+  openBoxedExpressionEditor,
+  openedBoxedExpressionId,
+  ...props
+}: FormDmnOutputsProps) {
   const [formResultStatus, setFormResultStatus] = 
useState<FormDmnOutputsStatus>(FormDmnOutputsStatus.EMPTY);
   const [formResultError, setFormResultError] = useState<boolean>(false);
+  const [openedExpressionEditorForNodeId, setOpenedExpressionEditorForNodeId] 
= useState<string>("");

Review Comment:
   Why do you need this state? I mean, the source of truth is the Shared value. 
If you need to know what's the currently open Boxed Expression ID, you have the 
new prop `openedBoxedExpressionId`. If you need to change it, you have 
`openBoxedExpressionEditor`. There's no need for this state, I believe.



##########
packages/dmn-editor-envelope/src/NewDmnEditorEnvelopeApiFactory.ts:
##########
@@ -45,6 +46,12 @@ export class NewDmnEditorEnvelopeApiImpl
     this.getEditorOrThrowError().openBoxedExpressionEditor(nodeId);
   }
 
+  public newDmnEditor_openedBoxedExpressionId(): SharedValueProvider<string> {
+    return {
+      defaultValue: "",
+    };
+  }

Review Comment:
   Why not
   
   ```suggestion
     public newDmnEditor_openedBoxedExpressionId(): 
SharedValueProvider<undefined> {
       return {
         defaultValue: undefined,
       };
     }
   ```
   
   ?



##########
packages/form-dmn/src/FormDmnOutputs.tsx:
##########
@@ -66,11 +66,19 @@ export interface FormDmnOutputsProps {
   notificationsPanel: boolean;
   openEvaluationTab?: () => void;
   openBoxedExpressionEditor?: (nodeId: string) => void;
+  openedBoxedExpressionId?: string;

Review Comment:
   ```suggestion
     openedBoxedExpressionId: string | undefined;
   ```
   
   This way, the prop is mandatory, but can be undefined.



##########
packages/form-dmn/src/FormDmnOutputs.tsx:
##########
@@ -268,13 +279,13 @@ export function FormDmnOutputs({ openEvaluationTab, 
openBoxedExpressionEditor, .
           <Card
             id={`${index}-dmn-result`}
             isFlat={true}
-            className={"kogito--editor__dmn-form-result__results-card"}
+            
className={`kogito--editor__dmn-form-result__results-card${openedExpressionEditorForNodeId
 === dmnFormResult.decisionId ? "-highlight" : ""}`}

Review Comment:
   ```suggestion
               className={
               openedExpressionEditorForNodeId === dmnFormResult.decisionId 
                   ? "kogito--editor__dmn-form-result__results-card-highlight" 
                   : "kogito--editor__dmn-form-result__results-card"
               }
   ```



-- 
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