From 9557fcd01a8ca3184b8db792993e044a78eb7045 Mon Sep 17 00:00:00 2001
From: Leonardo <leomilho@gmail.com>
Date: Tue, 23 Jul 2024 17:25:25 +0200
Subject: [PATCH] feat: query all schema at once button

---
 .../lib/querybuilder/panel/QueryBuilder.tsx   | 79 +++++++++++++++++++
 libs/shared/lib/schema/panel/Schema.tsx       | 14 ++--
 libs/shared/lib/schema/panel/utils.ts         |  0
 3 files changed, 84 insertions(+), 9 deletions(-)
 create mode 100644 libs/shared/lib/schema/panel/utils.ts

diff --git a/libs/shared/lib/querybuilder/panel/QueryBuilder.tsx b/libs/shared/lib/querybuilder/panel/QueryBuilder.tsx
index ec05da457..b1d7f3fc9 100644
--- a/libs/shared/lib/querybuilder/panel/QueryBuilder.tsx
+++ b/libs/shared/lib/querybuilder/panel/QueryBuilder.tsx
@@ -165,6 +165,71 @@ export const QueryBuilderInner = (props: QueryBuilderProps) => {
     event.dataTransfer.dropEffect = 'move';
   };
 
+  const onAddSchemaToQueryBuilder = () => {
+    // TODO: incomplete (not being shown) for now due to lack of "OPTIONAL MATCH" support in the backend
+
+    const addedNodes: Record<string, string> = {};
+    // loop through all nodes in the schema and add them to the graph
+    schemaGraph.nodes.forEach((node) => {
+      if (!node.key) return;
+
+      const { id } = graphologyGraph.addPill2Graphology(
+        {
+          type: QueryElementTypes.Entity,
+          // x: position.x - mouse_x,
+          // y: position.y - mouse_y,
+          x: 0,
+          y: 0,
+          name: node.key,
+          schemaKey: node.key,
+          attributes: [],
+        },
+        schema.getNodeAttribute(node.key, 'attributes'),
+        schemaInference,
+      );
+      if (!id) return;
+      addedNodes[node.key] = id;
+    });
+
+    // loop all edges in the schema and add them to the graph
+    schemaGraph.edges.forEach((edge) => {
+      if (!edge.key || !edge.source || !edge.target) return;
+      const fromNode = graphologyGraph.getNodeAttributes(addedNodes[edge.source]);
+      const toNode = graphologyGraph.getNodeAttributes(addedNodes[edge.target]);
+      if (!fromNode || !toNode) return;
+
+      const relation = graphologyGraph.addPill2Graphology(
+        {
+          type: QueryElementTypes.Relation,
+          // x: position.x,
+          // y: position.y,
+          x: 0,
+          y: 0,
+          depth: { min: queryBuilderSettings.depth.min, max: queryBuilderSettings.depth.max },
+          name: edge.attributes?.collection,
+          schemaKey: edge.attributes?.name,
+          collection: edge.attributes?.collection,
+          attributes: [],
+        },
+        schema.getEdgeAttribute(edge.key, 'attributes'),
+      );
+
+      graphologyGraph.addEdge2Graphology(fromNode, relation, {
+        type: 'connection',
+        sourceHandleData: toHandleData(edge.source),
+        targetHandleData: toHandleData(edge.key),
+      });
+
+      graphologyGraph.addEdge2Graphology(relation, toNode, {
+        type: 'connection',
+        sourceHandleData: toHandleData(edge.key),
+        targetHandleData: toHandleData(edge.target),
+      });
+    });
+
+    dispatch(setQuerybuilderGraphology(graphologyGraph));
+  };
+
   /**
    * The onDrop is called when the user drops an element from the schema onto the QueryBuilder.
    * In the onDrop query elements will be created based on the data stored in the drag event (datastrasfer).
@@ -611,6 +676,20 @@ export const QueryBuilderInner = (props: QueryBuilderProps) => {
                     <QueryMLDialog />
                   </PopoverContent>
                 </Popover>
+                {/* <Tooltip>
+                  <TooltipTrigger>
+                    <Button
+                      variantType="secondary"
+                      variant="ghost"
+                      size="xs"
+                      iconComponent="icon-[ic--baseline-drive-file-move]"
+                      onClick={() => onAddSchemaToQueryBuilder()}
+                    />
+                  </TooltipTrigger>
+                  <TooltipContent>
+                    <p>Query All Data</p>
+                  </TooltipContent>
+                </Tooltip> */}
               </TooltipProvider>
             </ControlContainer>
           </div>
diff --git a/libs/shared/lib/schema/panel/Schema.tsx b/libs/shared/lib/schema/panel/Schema.tsx
index cd8bdbc39..086af4dbb 100644
--- a/libs/shared/lib/schema/panel/Schema.tsx
+++ b/libs/shared/lib/schema/panel/Schema.tsx
@@ -46,12 +46,8 @@ export const Schema = (props: Props) => {
   const settings = useSchemaSettings();
   const searchResults = useSearchResultSchema();
   const dispatch = useDispatch();
-  const [toggleSchemaSettings, setToggleSchemaSettings] = useState(false);
   const [nodes, setNodes, onNodesChange] = useNodesState([] as Node[]);
   const [edges, setEdges, onEdgesChange] = useEdgesState([] as Edge[]);
-  const [firstUserConnection, setFirstUserConnection] = useState<boolean>(true);
-  const [auth, setAuth] = useState(props.auth);
-  const [expanded, setExpanded] = useState<boolean>(false);
 
   const reactFlowInstanceRef = useRef<ReactFlowInstance | null>(null);
   const reactFlowRef = useRef<HTMLDivElement>(null);
@@ -74,13 +70,13 @@ export const Schema = (props: Props) => {
 
   useEffect(() => {
     updateLayout();
-    sessionStorage.setItem('firstUserConnection', firstUserConnection.toString());
+    if (sessionStorage.getItem('firstUserConnection') === 'true') {
+      sessionStorage.setItem('firstUserConnection', 'false');
+    } else {
+      sessionStorage.setItem('firstUserConnection', 'true');
+    }
   }, []);
 
-  useEffect(() => {
-    setAuth(props.auth);
-  }, [props.auth]);
-
   async function layoutGraph() {
     if (schemaGraphology === undefined || schemaGraphology.order == 0) {
       setNodes([]);
diff --git a/libs/shared/lib/schema/panel/utils.ts b/libs/shared/lib/schema/panel/utils.ts
new file mode 100644
index 000000000..e69de29bb
-- 
GitLab