handreyrc commented on code in PR #3252:
URL: 
https://github.com/apache/incubator-kie-tools/pull/3252#discussion_r2342819426


##########
packages/swf-editor/src/store/Store.ts:
##########
@@ -0,0 +1,304 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { enableMapSet, immerable } from "immer";
+import * as RF from "reactflow";
+import { create } from "zustand";
+import { immer } from "zustand/middleware/immer";
+import { SwfDiagramNodeData } from "../diagram/nodes/SwfNodes";
+import { ComputedStateCache } from "./ComputedStateCache";
+import { computeDiagramData } from "./computed/computeDiagramData";
+import { computeIndexedSwf } from "./computed/computeIndexes";
+import { computeIsDropTargetNodeValidForSelection } from 
"./computed/computeIsDropTargetNodeValidForSelection";
+import { DEFAULT_VIEWPORT } from "../diagram/Diagram";
+
+import { Specification } from "@severlessworkflow/sdk-typescript";
+
+enableMapSet(); // Necessary because `Computed` has a lot of Maps and Sets.
+
+export interface SwfEditorDiagramNodeStatus {
+  selected: boolean;
+  dragging: boolean;
+}
+
+export interface SwfEditorDiagramEdgeStatus {
+  selected: boolean;
+  draggingWaypoint: boolean;
+}
+
+export interface SnapGrid {
+  isEnabled: boolean;
+  x: number;
+  y: number;
+}
+
+export type DropTargetNode = undefined | RF.Node<SwfDiagramNodeData>;
+
+export interface State {
+  dispatch: (s: State) => Dispatch;
+  computed: (s: State) => Computed;
+  layout: (s: State) => Layout;
+  swf: { model: Specification.Workflow };
+  focus: {
+    consumableId: string | undefined;
+  };
+  diagram: {
+    autoLayout: {
+      canAutoGenerate: boolean;
+    };
+    __unsafeSwfIndex: number;
+    edgeIdBeingUpdated: string | undefined;
+    dropTargetNode: DropTargetNode;
+    ongoingConnection: RF.OnConnectStartParams | undefined;
+    overlaysPanel: {
+      isOpen: boolean;
+    };
+    overlays: {
+      enableNodeHierarchyHighlight: boolean;
+      enableCustomNodeStyles: boolean;
+      enableEvaluationHighlights: boolean;
+    };
+    snapGrid: SnapGrid;
+    nodesPosition: NodesPosition;
+    edgesWaypoints: EdgesWaypoints;
+    _selectedNodes: Array<string>;
+    _selectedEdges: Array<string>;
+    draggingNodes: Array<string>;
+    draggingWaypoints: Array<string>;
+    isEditingStyle: boolean;
+    viewport: {
+      x: number;
+      y: number;
+      zoom: number;
+    };
+  };
+}
+
+// Read this to understand why we need computed as part of the store.
+// https://github.com/pmndrs/zustand/issues/132#issuecomment-1120467721
+export type Computed = {
+  isDiagramEditingInProgress(): boolean;
+
+  indexedSwf(): ReturnType<typeof computeIndexedSwf>;
+
+  getDiagramData(requiresLayout?: boolean): ReturnType<typeof 
computeDiagramData>;
+
+  isDropTargetNodeValidForSelection(): boolean;
+};
+
+//Handle layout data out of the model
+export type Layout = {
+  setNodePosition: (nodeId: string, newPosition: RF.XYPosition | undefined) => 
void;
+  setEdgeWaypoints: (edgeId: string, newWaypoints: RF.XYPosition[] | 
undefined) => void;
+  updateWaypointPosition: (edgeId: string, index: number, newPosition: 
RF.XYPosition | undefined) => void;
+};
+
+export class NodesPosition {
+  // Handle node position out of the model
+  [immerable] = false;

Review Comment:
   @tiagobento This is a workaround for and issue I was facing with states in 
Zustand in this specific scenario.
   SWF do not store nodes and edges positional dada. In this case I have to 
calculate the autolayout and apply those coordinates / waypoints to nodes and 
lines in the "computeDiagramData" before the diagram is rendered.
   Otherwise the diagram would be rendered twice and that's pretty unpleasant 
visually. Without setting  "[immerable] = false;" for the nodes position / 
waypoints  zustand will wouldn't allow to change / set  new calculated 
coordinates in the  "computeDiagramData" because in that context they would be 
immutable.  
   That's the way I found to make it work. I'm open to suggestions if there is  
better way to do that.



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