From 2f723a6724b3f452d8e8505ab4f045b489baf7f3 Mon Sep 17 00:00:00 2001
From: Leonardo <leomilho@gmail.com>
Date: Wed, 11 Dec 2024 17:42:16 +0100
Subject: [PATCH 1/7] feat: client updater bun rework

---
 apps/web/src/app/App.tsx                      |  2 +-
 libs/shared/lib/data-access/broker/broker.tsx |  4 +--
 .../data-access/broker/wsInsightSharing.ts    | 10 +++---
 libs/shared/lib/data-access/broker/wsQuery.ts | 11 ++++---
 .../shared/lib/data-access/broker/wsState.tsx | 20 +++++------
 .../data-access/store/insightSharingSlice.ts  |  6 ++--
 .../data-access/store/querybuilderSlice.ts    |  2 ++
 .../data-access/store/visualizationSlice.ts   | 15 +++++----
 libs/shared/lib/insight-sharing/FormAlert.tsx | 25 +++++++++-----
 .../shared/lib/insight-sharing/FormReport.tsx | 33 +++++++++++--------
 .../insight-sharing/components/AddItem.tsx    | 15 ++++++---
 11 files changed, 83 insertions(+), 60 deletions(-)

diff --git a/apps/web/src/app/App.tsx b/apps/web/src/app/App.tsx
index 7138326df..046a166e1 100644
--- a/apps/web/src/app/App.tsx
+++ b/apps/web/src/app/App.tsx
@@ -44,7 +44,7 @@ export function App(props: App) {
         dispatch(resetGraphQueryResults());
       } else {
         dispatch(queryingBackend());
-        wsQueryRequest(ml);
+        wsQueryRequest(session.currentSaveState, ml);
       }
     }
   };
diff --git a/libs/shared/lib/data-access/broker/broker.tsx b/libs/shared/lib/data-access/broker/broker.tsx
index 41cfe6f8e..284112815 100644
--- a/libs/shared/lib/data-access/broker/broker.tsx
+++ b/libs/shared/lib/data-access/broker/broker.tsx
@@ -200,8 +200,8 @@ export class Broker {
     }
 
     fullMessage.sessionID = this.authHeader?.sessionID ?? '';
-    if (message.body && typeof message.body !== 'string') {
-      fullMessage.body = JSON.stringify(message.body);
+    if (message.body) {
+      fullMessage.body = message.body;
     }
 
     if (this.webSocket && this.connected && this.webSocket.readyState === 1) this.webSocket.send(JSON.stringify(fullMessage));
diff --git a/libs/shared/lib/data-access/broker/wsInsightSharing.ts b/libs/shared/lib/data-access/broker/wsInsightSharing.ts
index 04c366a3e..8159d8472 100644
--- a/libs/shared/lib/data-access/broker/wsInsightSharing.ts
+++ b/libs/shared/lib/data-access/broker/wsInsightSharing.ts
@@ -11,7 +11,7 @@ export function wsGetInsights(saveStateId: string, callback?: GetInsightsRespons
     {
       key: 'insight',
       subKey: 'getAll',
-      body: JSON.stringify({ saveStateId: saveStateId })
+      body: { saveStateId: saveStateId }
     },
     internalCallback
   );
@@ -22,7 +22,7 @@ export function wsCreateInsight(insight: InsightRequest, callback?: Function) {
     {
       key: 'insight',
       subKey: 'create',
-      body: JSON.stringify(insight),
+      body: insight,
     },
     (data: any, status: string) => {
       if (status === 'Bad Request') {
@@ -32,7 +32,7 @@ export function wsCreateInsight(insight: InsightRequest, callback?: Function) {
       }
 
       if (!data || typeof data !== 'object') {
-        console.error('Invalid repsonse data', data)
+        console.error('Invalid response data', data)
         if (callback) callback(null, 'error');
         return;
       }
@@ -57,7 +57,7 @@ export function wsUpdateInsight(
     {
       key: 'insight',
       subKey: 'update',
-      body: JSON.stringify({ id: id, insight: insight }),
+      body: { id: id, insight: insight },
     },
     (data: any, status: string) => {
       if (status === 'Bad Request') {
@@ -74,7 +74,7 @@ export function wsDeleteInsight(id: string, callback?: Function) {
     {
       key: 'insight',
       subKey: 'delete',
-      body: JSON.stringify({ id }),
+      body: { id },
     },
     (data: any, status: string) => {
       if (status === 'Bad Request') {
diff --git a/libs/shared/lib/data-access/broker/wsQuery.ts b/libs/shared/lib/data-access/broker/wsQuery.ts
index c5e6ba7e3..55b456422 100644
--- a/libs/shared/lib/data-access/broker/wsQuery.ts
+++ b/libs/shared/lib/data-access/broker/wsQuery.ts
@@ -6,10 +6,10 @@ import { QueryBuilderText } from '../store/querybuilderSlice';
 import { GraphQueryResultFromBackendPayload } from '../store/graphQueryResultSlice';
 import { ML, MLTypes } from '../store/mlSlice';
 
-export function wsQueryRequest(ml: ML) {
+export function wsQueryRequest(saveStateID: string, ml: ML) {
   const mlEnabled = Object.entries(ml)
-    .filter(([_,value]) => value.enabled)
-    .map(([key,_]) => {
+    .filter(([_, value]) => value.enabled)
+    .map(([key, _]) => {
       return {
         type: key as MLTypes,
         //parameters: []
@@ -18,11 +18,12 @@ export function wsQueryRequest(ml: ML) {
   Broker.instance().sendMessage({
     key: 'query',
     subKey: 'get',
-    body: { 
+    body: {
+      saveStateID: saveStateID,
       queryID: "0", // TODO: not used yet, but used when multiple queries per save state are supported
       ml: mlEnabled,
       cached: false
-     }, 
+    },
   });
 }
 
diff --git a/libs/shared/lib/data-access/broker/wsState.tsx b/libs/shared/lib/data-access/broker/wsState.tsx
index 9dd85998d..8876ab666 100644
--- a/libs/shared/lib/data-access/broker/wsState.tsx
+++ b/libs/shared/lib/data-access/broker/wsState.tsx
@@ -54,12 +54,12 @@ export type SaveStateI = {
 };
 
 type GetStateResponse = (data: SaveStateI, status: string) => void;
-export function wsGetState(saveStateId: string, callback?: GetStateResponse) {
+export function wsGetState(saveStateID: string, callback?: GetStateResponse) {
   Broker.instance().sendMessage(
     {
       key: 'state',
       subKey: 'get',
-      body: { saveStateId: saveStateId }, //messageTypeGetSaveState
+      body: { saveStateID: saveStateID }, //messageTypeGetSaveState
     },
     callback,
   );
@@ -93,19 +93,19 @@ export function wsGetStatesSubscription(callback: GetStatesResponse) {
 }
 
 type SelectStateResponse = (data: { saveStateID: string; success: boolean }) => void;
-export function wsSelectState(saveStateId: string | undefined, callback?: SelectStateResponse) {
-  if (saveStateId === undefined) saveStateId = '';
+export function wsSelectState(saveStateID: string | undefined, callback?: SelectStateResponse) {
+  if (saveStateID === undefined) saveStateID = '';
   Broker.instance().sendMessage(
     {
       key: 'state',
       subKey: 'select',
-      body: { saveStateId: saveStateId }, //messageTypeGetSaveState
+      body: { saveStateID: saveStateID }, //messageTypeGetSaveState
     },
     callback,
   );
 
-  Broker.instance().useSaveStateID(saveStateId);
-  setParam(URLParams.saveState, saveStateId);
+  Broker.instance().useSaveStateID(saveStateID);
+  setParam(URLParams.saveState, saveStateID);
 }
 export function wsSelectStateSubscription(callback: SelectStateResponse) {
   const id = Broker.instance().subscribe(callback, 'save_state_selected');
@@ -132,7 +132,7 @@ export function wsDeleteState(id: string, callback?: Function) {
     {
       key: 'state',
       subKey: 'delete',
-      body: { saveStateId: id }, //messageTypeGetSaveState
+      body: { saveStateID: id }, //messageTypeGetSaveState
     },
     callback,
   );
@@ -150,7 +150,7 @@ export function wsTestSaveStateConnection(id: string, callback?: Function) {
     {
       key: 'state',
       subKey: 'testConnection',
-      body: { saveStateId: id }, //messageTypeGetSaveState
+      body: { saveStateID: id }, //messageTypeGetSaveState
     },
     callback,
   );
@@ -196,7 +196,7 @@ export function wsStateGetPolicy(saveStateID: string, callback?: StateGetPolicyR
     {
       key: 'state',
       subKey: 'getPolicy',
-      body: { SaveStateID: saveStateID },
+      body: { saveStateID: saveStateID },
     },
     callback,
   );
diff --git a/libs/shared/lib/data-access/store/insightSharingSlice.ts b/libs/shared/lib/data-access/store/insightSharingSlice.ts
index ed0bd66ec..4dd06e448 100644
--- a/libs/shared/lib/data-access/store/insightSharingSlice.ts
+++ b/libs/shared/lib/data-access/store/insightSharingSlice.ts
@@ -67,8 +67,8 @@ const insightSharingSlice = createSlice({
 
 export const { setInsights, addInsight, updateInsight, deleteInsight } = insightSharingSlice.actions;
 
-export const selectReports = (state: RootState): InsightResponse[] => state.insightSharing?.insights?.filter((i) => i.type === 'report') ?? [];
-export const selectAlerts = (state: RootState): InsightResponse[] => state.insightSharing?.insights?.filter((i) => i.type === 'alert') ?? [];
-export const selectInsights = (state: RootState): InsightResponse[] => state.insightSharing?.insights ?? [];
+export const selectReports = (state: RootState): InsightResponse[] => state.insightSharing.insights?.filter((i) => i.type === 'report') ?? [];
+export const selectAlerts = (state: RootState): InsightResponse[] => state.insightSharing.insights?.filter((i) => i.type === 'alert') ?? [];
+export const selectInsights = (state: RootState): InsightResponse[] => state.insightSharing.insights ?? [];
 
 export default insightSharingSlice.reducer;
diff --git a/libs/shared/lib/data-access/store/querybuilderSlice.ts b/libs/shared/lib/data-access/store/querybuilderSlice.ts
index 79dba6327..24aefae67 100644
--- a/libs/shared/lib/data-access/store/querybuilderSlice.ts
+++ b/libs/shared/lib/data-access/store/querybuilderSlice.ts
@@ -29,6 +29,7 @@ export enum QueryUnionType {
 export type QueryBuilderAttributeBeingShown = {};
 
 export type QueryBuilderState = {
+  id?: number;
   graph: QueryMultiGraph;
   ignoreReactivity: boolean;
   settings: QueryBuilderSettings;
@@ -37,6 +38,7 @@ export type QueryBuilderState = {
 };
 
 export type SchemaState = {
+  id?: number;
   settings: Record<string, any>;
 }
 
diff --git a/libs/shared/lib/data-access/store/visualizationSlice.ts b/libs/shared/lib/data-access/store/visualizationSlice.ts
index 97cb40bdd..961874b9c 100644
--- a/libs/shared/lib/data-access/store/visualizationSlice.ts
+++ b/libs/shared/lib/data-access/store/visualizationSlice.ts
@@ -5,6 +5,7 @@ import { isEqual } from 'lodash-es';
 
 export type VisStateSettings = VisualizationSettingsType[];
 export type VisState = {
+  id?: number;
   activeVisualizationIndex: number;
   openVisualizationArray: VisStateSettings;
 };
@@ -20,10 +21,10 @@ export const visualizationSlice = createSlice({
   reducers: {
     removeVisualization: (state, action: PayloadAction<number | undefined>) => {
       const index = action.payload ?? state.activeVisualizationIndex;
-      
+
       if (index >= 0 && index < state.openVisualizationArray.length) {
         state.openVisualizationArray.splice(index, 1);
-      
+
         if (state.openVisualizationArray.length === 0) {
           state.activeVisualizationIndex = -1;
         } else if (state.activeVisualizationIndex >= state.openVisualizationArray.length) {
@@ -81,9 +82,9 @@ export const visualizationSlice = createSlice({
     ) => {
       const { id, newPosition } = action.payload;
       if (
-        id >= 0 && 
-        id < state.openVisualizationArray.length && 
-        newPosition >= 0 && 
+        id >= 0 &&
+        id < state.openVisualizationArray.length &&
+        newPosition >= 0 &&
         newPosition < state.openVisualizationArray.length
       ) {
         const settingsCopy = [...state.openVisualizationArray];
@@ -94,12 +95,12 @@ export const visualizationSlice = createSlice({
         if (state.activeVisualizationIndex === id) {
           state.activeVisualizationIndex = newPosition;
         } else if (
-          state.activeVisualizationIndex > id && 
+          state.activeVisualizationIndex > id &&
           state.activeVisualizationIndex <= newPosition
         ) {
           state.activeVisualizationIndex--;
         } else if (
-          state.activeVisualizationIndex < id && 
+          state.activeVisualizationIndex < id &&
           state.activeVisualizationIndex >= newPosition
         ) {
           state.activeVisualizationIndex++;
diff --git a/libs/shared/lib/insight-sharing/FormAlert.tsx b/libs/shared/lib/insight-sharing/FormAlert.tsx
index 4f4d2c6af..1909f4e31 100644
--- a/libs/shared/lib/insight-sharing/FormAlert.tsx
+++ b/libs/shared/lib/insight-sharing/FormAlert.tsx
@@ -63,12 +63,17 @@ export function FormAlert(props: Props) {
       return;
     }
 
+    if (!session.currentSaveState) {
+      dispatch(addError('No save state selected'));
+      return;
+    }
+
     const alertData: InsightRequest = {
       name,
       description,
       recipients,
       template: JSON.stringify(element),
-      saveStateId: session.currentSaveState || '',
+      saveStateId: session.currentSaveState,
       type: 'alert' as const,
       frequency: '',
     };
@@ -162,14 +167,16 @@ export function FormAlert(props: Props) {
             <span className="font-semibold">Alerting text</span>
           </AccordionHead>
           <AccordionBody>
-          <TextEditor
-                key={`editor-${props.insight.id}`}
-                editorState={editorState}
-                setEditorState={setEditorState}
-                showToolbar={true}
-                placeholder="Start typing your alert template..."
-                handleSave={handleSave}
-              ><Button label="Delete" variantType="secondary" variant="outline" onClick={handleDelete} /></TextEditor>
+            <TextEditor
+              key={`editor-${props.insight.id}`}
+              editorState={editorState}
+              setEditorState={setEditorState}
+              showToolbar={true}
+              placeholder="Start typing your alert template..."
+              handleSave={handleSave}
+            >
+              <Button label="Delete" variantType="secondary" variant="outline" onClick={handleDelete} />
+            </TextEditor>
           </AccordionBody>
         </AccordionItem>
       </Accordion>
diff --git a/libs/shared/lib/insight-sharing/FormReport.tsx b/libs/shared/lib/insight-sharing/FormReport.tsx
index 69e7a95bd..85ff270a9 100644
--- a/libs/shared/lib/insight-sharing/FormReport.tsx
+++ b/libs/shared/lib/insight-sharing/FormReport.tsx
@@ -28,11 +28,11 @@ export function FormReport(props: Props) {
   const [recipients, setRecipients] = useState(props.insight.recipients || []);
   const [recipientInput, setRecipientInput] = useState<string>('');
   const [frequency, setFrequency] = useState(props.insight.frequency || 'Daily');
-  const [editorState, setEditorState] = useState<SerializedEditorState| null>(null);
+  const [editorState, setEditorState] = useState<SerializedEditorState | null>(null);
 
   useEffect(() => {
     let isMounted = true;
-    
+
     if (isMounted) {
       setName(props.insight.name);
       setDescription(props.insight.description);
@@ -50,7 +50,7 @@ export function FormReport(props: Props) {
         setEditorState(null);
       }
     }
-  
+
     return () => {
       isMounted = false;
       setEditorState(null);
@@ -75,13 +75,18 @@ export function FormReport(props: Props) {
       return;
     }
 
+    if (!session.currentSaveState) {
+      dispatch(addError('No save state ID found.'));
+      return;
+    }
+
     const reportData: InsightRequest = {
       name,
       description,
       recipients,
       frequency,
       template: JSON.stringify(elements),
-      saveStateId: session.currentSaveState || '',
+      saveStateId: session.currentSaveState,
       type: 'report' as const,
     };
 
@@ -99,8 +104,8 @@ export function FormReport(props: Props) {
         }
       });
     } else {
+      debugger;
       wsCreateInsight(reportData, (data: any, status: string) => {
-        debugger;
         setLoading(false);
         if (status === 'success') {
           dispatch(updateInsight(data));
@@ -187,14 +192,16 @@ export function FormReport(props: Props) {
             <span className="font-semibold">Email Template</span>
           </AccordionHead>
           <AccordionBody>
-              <TextEditor
-                key={`editor-${props.insight.id}`}
-                editorState={editorState}
-                setEditorState={setEditorState}
-                showToolbar={true}
-                placeholder="Start typing your report template..."
-                handleSave={handleSave}
-              ><Button label="Delete" variantType="secondary" variant="outline" onClick={handleDelete} /></TextEditor>
+            <TextEditor
+              key={`editor-${props.insight.id}`}
+              editorState={editorState}
+              setEditorState={setEditorState}
+              showToolbar={true}
+              placeholder="Start typing your report template..."
+              handleSave={handleSave}
+            >
+              <Button label="Delete" variantType="secondary" variant="outline" onClick={handleDelete} />
+            </TextEditor>
           </AccordionBody>
         </AccordionItem>
       </Accordion>
diff --git a/libs/shared/lib/insight-sharing/components/AddItem.tsx b/libs/shared/lib/insight-sharing/components/AddItem.tsx
index b207b9ec6..75b2e07f0 100644
--- a/libs/shared/lib/insight-sharing/components/AddItem.tsx
+++ b/libs/shared/lib/insight-sharing/components/AddItem.tsx
@@ -2,9 +2,9 @@ import React, { useState } from 'react';
 import { Input, Button } from '../../components';
 import { MonitorType as InsightType, MonitorType } from './Sidebar';
 import { useAppDispatch, useSessionCache } from '../../data-access';
-import { addInsight } from '../../data-access/store/insightSharingSlice';
+import { addInsight, InsightRequest } from '../../data-access/store/insightSharingSlice';
 import { wsCreateInsight } from '../../data-access/broker/wsInsightSharing';
-import { addError, addSuccess,  } from '@graphpolaris/shared/lib/data-access/store/configSlice';
+import { addError, addSuccess } from '@graphpolaris/shared/lib/data-access/store/configSlice';
 
 type Props = {
   setAdding: (val: false | MonitorType) => void;
@@ -24,13 +24,18 @@ export function AddItem(props: Props) {
       return;
     }
 
-    const newInsight = {
+    if (!session.currentSaveState) {
+      dispatch(addError('No save state selected'));
+      return;
+    }
+
+    const newInsight: InsightRequest = {
       name,
       description,
       recipients: [],
       template: '',
       frequency: props.type === 'report' ? 'Daily' : '',
-      saveStateId: session.currentSaveState || '',
+      saveStateId: session.currentSaveState,
       type: props.type,
     };
 
@@ -42,7 +47,7 @@ export function AddItem(props: Props) {
         dispatch(addSuccess('Succesfully created ' + props.type));
       } else {
         console.error('Failed to create insight:', data);
-        dispatch(addError('Failed to create new ' + props.type))
+        dispatch(addError('Failed to create new ' + props.type));
       }
     });
   };
-- 
GitLab


From 3177cfb57bf7fc6e510af94fff3106ff4113a848 Mon Sep 17 00:00:00 2001
From: Leonardo <leomilho@gmail.com>
Date: Thu, 12 Dec 2024 14:33:20 +0100
Subject: [PATCH 2/7] chore: remove select and allow for optional saveStateID
 return of connection tester

---
 libs/shared/lib/data-access/api/eventBus.tsx  |  4 ----
 .../shared/lib/data-access/broker/wsState.tsx | 24 +------------------
 .../lib/data-access/store/sessionSlice.ts     |  3 +--
 3 files changed, 2 insertions(+), 29 deletions(-)

diff --git a/libs/shared/lib/data-access/api/eventBus.tsx b/libs/shared/lib/data-access/api/eventBus.tsx
index d58c915cf..63aa85acc 100644
--- a/libs/shared/lib/data-access/api/eventBus.tsx
+++ b/libs/shared/lib/data-access/api/eventBus.tsx
@@ -25,12 +25,10 @@ import {
   wsGetState,
   wsGetStates,
   wsUpdateState,
-  wsSelectState,
   nilUUID,
   wsGetStatesSubscription,
   wsDeleteStateSubscription,
   wsGetStateSubscription,
-  wsSelectStateSubscription,
   wsTestSaveStateConnectionSubscription,
   wsStateGetPolicy,
 } from '../broker/wsState';
@@ -170,7 +168,6 @@ export const EventBus = (props: { onRunQuery: Function; onAuthorized: Function }
     );
 
     unsubs.push(wsDeleteStateSubscription((data) => {}));
-    unsubs.push(wsSelectStateSubscription((data) => {}));
 
     // Broker.instance().subscribe((response: TestDatabaseConnectionResponse) => {
     //   if (response && response.status === 'success') dispatch(testedSaveState(response.saveStateID));
@@ -234,7 +231,6 @@ export const EventBus = (props: { onRunQuery: Function; onAuthorized: Function }
       dispatch(setSchemaLoading(true));
       wsSchemaRequest(session.currentSaveState);
       wsSchemaStatsRequest(session.currentSaveState);
-      wsSelectState(session.currentSaveState);
       loadSaveState(session.currentSaveState, session.saveStates);
     }
   }, [session.currentSaveState]);
diff --git a/libs/shared/lib/data-access/broker/wsState.tsx b/libs/shared/lib/data-access/broker/wsState.tsx
index 8876ab666..7c2e21fed 100644
--- a/libs/shared/lib/data-access/broker/wsState.tsx
+++ b/libs/shared/lib/data-access/broker/wsState.tsx
@@ -92,28 +92,6 @@ export function wsGetStatesSubscription(callback: GetStatesResponse) {
   };
 }
 
-type SelectStateResponse = (data: { saveStateID: string; success: boolean }) => void;
-export function wsSelectState(saveStateID: string | undefined, callback?: SelectStateResponse) {
-  if (saveStateID === undefined) saveStateID = '';
-  Broker.instance().sendMessage(
-    {
-      key: 'state',
-      subKey: 'select',
-      body: { saveStateID: saveStateID }, //messageTypeGetSaveState
-    },
-    callback,
-  );
-
-  Broker.instance().useSaveStateID(saveStateID);
-  setParam(URLParams.saveState, saveStateID);
-}
-export function wsSelectStateSubscription(callback: SelectStateResponse) {
-  const id = Broker.instance().subscribe(callback, 'save_state_selected');
-  return () => {
-    Broker.instance().unSubscribe('save_state_selected', id);
-  };
-}
-
 export function wsCreateState(request: SaveStateI, callback?: GetStateResponse) {
   Broker.instance().sendMessage(
     {
@@ -144,7 +122,7 @@ export function wsDeleteStateSubscription(callback: DeleteStateResponse) {
   };
 }
 
-type TestSaveStateConnectionResponse = (data: { status: 'success' | 'fail'; saveStateID: string }) => void;
+type TestSaveStateConnectionResponse = (data: { status: 'success' | 'fail'; saveStateID?: string }) => void;
 export function wsTestSaveStateConnection(id: string, callback?: Function) {
   Broker.instance().sendMessage(
     {
diff --git a/libs/shared/lib/data-access/store/sessionSlice.ts b/libs/shared/lib/data-access/store/sessionSlice.ts
index 08d5dc8ab..19c892374 100644
--- a/libs/shared/lib/data-access/store/sessionSlice.ts
+++ b/libs/shared/lib/data-access/store/sessionSlice.ts
@@ -1,8 +1,7 @@
 import { createSlice, PayloadAction } from '@reduxjs/toolkit';
 import type { RootState } from './store';
-import { DatabaseStatus, SaveStateAuthorizationHeaders, SaveStateAuthorizationObjectsArray, SaveStateI } from '../broker/wsState';
+import { DatabaseStatus, SaveStateAuthorizationHeaders, SaveStateI } from '../broker/wsState';
 import { getParam, URLParams } from '../api/url';
-import { AuthorizationOperations } from './authSlice';
 import { cloneDeep } from 'lodash-es';
 
 /** Message format of the error message from the backend */
-- 
GitLab


From 544cc2a66584d45fe7b87c03f7b60e982f44e312 Mon Sep 17 00:00:00 2001
From: Leonardo <leomilho@gmail.com>
Date: Fri, 13 Dec 2024 11:42:19 +0100
Subject: [PATCH 3/7] feat: remove sessionid from auth and instead get from ws
 callbacl

---
 libs/shared/lib/data-access/api/eventBus.tsx        | 10 ++++++++--
 libs/shared/lib/data-access/broker/broker.tsx       |  2 --
 libs/shared/lib/data-access/broker/wsUser.tsx       | 13 ++++++++++++-
 .../lib/data-access/security/useAuthentication.tsx  |  5 ++---
 libs/shared/lib/data-access/store/authSlice.ts      | 12 +++++++++---
 5 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/libs/shared/lib/data-access/api/eventBus.tsx b/libs/shared/lib/data-access/api/eventBus.tsx
index 63aa85acc..74c8d75ac 100644
--- a/libs/shared/lib/data-access/api/eventBus.tsx
+++ b/libs/shared/lib/data-access/api/eventBus.tsx
@@ -48,8 +48,8 @@ import { setSchemaAttributeDimensions, setSchemaAttributeInformation, setSchemaL
 import { addError } from '@graphpolaris/shared/lib/data-access/store/configSlice';
 import { unSelect } from '../store/interactionSlice';
 import { SchemaGraphStats } from '../../schema';
-import { wsUserGetPolicy } from '../broker/wsUser';
-import { authorized } from '../store/authSlice';
+import { wsReconnectSubscription, wsUserGetPolicy } from '../broker/wsUser';
+import { authorized, setSessionID } from '../store/authSlice';
 
 export const EventBus = (props: { onRunQuery: Function; onAuthorized: Function }) => {
   const { login } = useAuthentication();
@@ -113,6 +113,12 @@ export const EventBus = (props: { onRunQuery: Function; onAuthorized: Function }
       }),
     );
 
+    unsubs.push(
+      wsReconnectSubscription((data) => {
+        if (data) dispatch(setSessionID(data));
+      }),
+    );
+
     Broker.instance().subscribe((data: SchemaGraphStats) => {
       dispatch(setSchemaAttributeInformation(data));
       dispatch(addInfo('Received attribute information'));
diff --git a/libs/shared/lib/data-access/broker/broker.tsx b/libs/shared/lib/data-access/broker/broker.tsx
index 284112815..a5ec8c9d4 100644
--- a/libs/shared/lib/data-access/broker/broker.tsx
+++ b/libs/shared/lib/data-access/broker/broker.tsx
@@ -139,7 +139,6 @@ export class Broker {
 
     if (this.authHeader?.roomID) params.set('roomID', this.authHeader?.roomID ?? '');
     if (this.saveStateID) params.set('saveStateID', this.saveStateID ?? '');
-    if (this.authHeader?.sessionID) params.set('sessionID', this.authHeader?.sessionID ?? '');
     // if (this.authHeader?.jwt) params.set('jwt', this.authHeader?.jwt ?? '');
     this.webSocket = new WebSocket(this.url + '?' + params.toString());
     this.webSocket.onopen = () => {
@@ -199,7 +198,6 @@ export class Broker {
       this.callbackListeners[uuid] = callback;
     }
 
-    fullMessage.sessionID = this.authHeader?.sessionID ?? '';
     if (message.body) {
       fullMessage.body = message.body;
     }
diff --git a/libs/shared/lib/data-access/broker/wsUser.tsx b/libs/shared/lib/data-access/broker/wsUser.tsx
index 9b6e50a6a..e1ad44e6c 100644
--- a/libs/shared/lib/data-access/broker/wsUser.tsx
+++ b/libs/shared/lib/data-access/broker/wsUser.tsx
@@ -1,4 +1,4 @@
-import { UserAuthorizationHeaders } from '../store/authSlice';
+import { ReconnectPayload, UserAuthorizationHeaders } from '../store/authSlice';
 import { Broker } from './broker';
 
 type UserPolicyCheckResponse = (data: boolean) => void;
@@ -24,3 +24,14 @@ export function wsUserGetPolicy(callback?: UserGetPolicyResponse) {
     callback,
   );
 }
+
+export function wsReconnectSubscription(callback: (data: ReconnectPayload) => void) {
+  const id = Broker.instance().subscribe((data: any) => {
+    if (data) {
+      callback(data);
+    }
+  }, 'reconnect');
+  return () => {
+    Broker.instance().unSubscribe('reconnect', id);
+  };
+}
diff --git a/libs/shared/lib/data-access/security/useAuthentication.tsx b/libs/shared/lib/data-access/security/useAuthentication.tsx
index 33893871a..eef1af9e6 100644
--- a/libs/shared/lib/data-access/security/useAuthentication.tsx
+++ b/libs/shared/lib/data-access/security/useAuthentication.tsx
@@ -1,6 +1,6 @@
 import { getEnvVariable } from 'config';
-import { useAppDispatch, useAuthCache, useSessionCache } from '../store';
-import { authenticated, UserAuthenticationHeader } from '../store/authSlice';
+import { useAppDispatch, useSessionCache } from '../store';
+import { authenticated, setSessionID, UserAuthenticationHeader } from '../store/authSlice';
 import { addInfo, addError } from '../store/configSlice';
 import { wsShareSaveState } from '../api';
 import { getParam, URLParams } from '../api/url';
@@ -34,7 +34,6 @@ export const useAuthentication = () => {
               authenticated({
                 username: res.username,
                 userID: res.userID,
-                sessionID: res.sessionID,
                 jwt: res.jwt,
                 authenticated: true,
                 roomID: res.roomID,
diff --git a/libs/shared/lib/data-access/store/authSlice.ts b/libs/shared/lib/data-access/store/authSlice.ts
index 12c3b14fd..b9f5a6dd5 100644
--- a/libs/shared/lib/data-access/store/authSlice.ts
+++ b/libs/shared/lib/data-access/store/authSlice.ts
@@ -28,7 +28,6 @@ export const UserAuthorizationHeadersDefaults: UserAuthorizationHeaders = {
 export type UserAuthenticationHeader = {
   username: string;
   userID: string;
-  sessionID: string;
   roomID: string;
   jwt: string;
 };
@@ -40,7 +39,6 @@ export type UseIsAuthorizedState = SingleIsAuthorizedState & {
 export type SingleIsAuthorizedState = {
   authenticated: boolean;
   jwt: string;
-  sessionID: string;
   userID: string;
   username: string;
   roomID: string | undefined;
@@ -48,12 +46,16 @@ export type SingleIsAuthorizedState = {
 
 export type AuthSliceState = {
   authentication: SingleIsAuthorizedState | undefined;
+  sessionID?: string;
   authorization: UserAuthorizationHeaders;
 };
 
+export type ReconnectPayload = { sessionID: string };
+
 export const initialState: AuthSliceState = {
   authentication: undefined,
   authorization: cloneDeep(UserAuthorizationHeadersDefaults),
+  sessionID: undefined,
 };
 
 export const authSlice = createSlice({
@@ -94,6 +96,10 @@ export const authSlice = createSlice({
       query.delete('roomID');
       history.pushState(null, '', '?' + query.toString());
     },
+    setSessionID(state, action: PayloadAction<ReconnectPayload>) {
+      console.info('Reconnecting with', action.payload);
+      state.sessionID = action.payload.sessionID;
+    },
     // unauthorized(state) {
     //   console.warn('Unauthorized');
     //   state.authentication.authenticated = false;
@@ -104,7 +110,7 @@ export const authSlice = createSlice({
   },
 });
 
-export const { authorized, authenticated, logout, changeRoom } = authSlice.actions;
+export const { authorized, authenticated, logout, changeRoom, setSessionID } = authSlice.actions;
 
 // Other code such as selectors can use the imported `RootState` type
 export const authState = (state: RootState) => state.auth;
-- 
GitLab


From e530f1e936752ce48d80ac83cee3488a9db7dc63 Mon Sep 17 00:00:00 2001
From: Leonardo <leomilho@gmail.com>
Date: Sat, 14 Dec 2024 17:32:37 +0100
Subject: [PATCH 4/7] fix: build

---
 libs/shared/lib/management/database/useHandleDatabase.ts | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/libs/shared/lib/management/database/useHandleDatabase.ts b/libs/shared/lib/management/database/useHandleDatabase.ts
index 8ee9639a5..450571fb6 100644
--- a/libs/shared/lib/management/database/useHandleDatabase.ts
+++ b/libs/shared/lib/management/database/useHandleDatabase.ts
@@ -68,7 +68,9 @@ export const useHandleDatabase = () => {
             concludedCallback();
           });
         } else {
-          dispatch(testedSaveState(data.saveStateID));
+          if (data.saveStateID) {
+            dispatch(testedSaveState(data.saveStateID));
+          }
           wsUpdateState(saveStateData, (updatedSaveState) => {
             dispatch(addSaveState(updatedSaveState));
             setConnectionStatus({
-- 
GitLab


From d47315835c992be11245b31576e9810c1207a91a Mon Sep 17 00:00:00 2001
From: Leonardo <leomilho@gmail.com>
Date: Sat, 14 Dec 2024 17:33:28 +0100
Subject: [PATCH 5/7] chore: remove debugger

---
 libs/shared/lib/insight-sharing/FormReport.tsx | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libs/shared/lib/insight-sharing/FormReport.tsx b/libs/shared/lib/insight-sharing/FormReport.tsx
index 85ff270a9..f721eda44 100644
--- a/libs/shared/lib/insight-sharing/FormReport.tsx
+++ b/libs/shared/lib/insight-sharing/FormReport.tsx
@@ -104,7 +104,6 @@ export function FormReport(props: Props) {
         }
       });
     } else {
-      debugger;
       wsCreateInsight(reportData, (data: any, status: string) => {
         setLoading(false);
         if (status === 'success') {
-- 
GitLab


From 19b4a84f6d341687bdf7e424a0e8362df75a732b Mon Sep 17 00:00:00 2001
From: Leonardo <leomilho@gmail.com>
Date: Sat, 14 Dec 2024 17:33:57 +0100
Subject: [PATCH 6/7] fix: build

---
 apps/web/src/components/navbar/navbar.tsx | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/apps/web/src/components/navbar/navbar.tsx b/apps/web/src/components/navbar/navbar.tsx
index aead53e59..0e3340946 100644
--- a/apps/web/src/components/navbar/navbar.tsx
+++ b/apps/web/src/components/navbar/navbar.tsx
@@ -74,7 +74,7 @@ export const Navbar = () => {
             <PopoverContent className="w-56 z-30 bg-light rounded-sm border-[1px] outline-none">
               <div className="p-2 text-sm border-b">
                 <h2 className="font-bold">user: {authCache.authentication?.username}</h2>
-                <h3 className="text-xs break-words">session: {authCache.authentication?.sessionID}</h3>
+                <h3 className="text-xs break-words">session: {authCache?.sessionID}</h3>
                 <h3 className="text-xs break-words">license: Creator</h3>
               </div>
               {authCache.authentication?.authenticated ? (
-- 
GitLab


From 51cc6f2b6e69a7c2b9045009b656decc6945152e Mon Sep 17 00:00:00 2001
From: Leonardo <leomilho@gmail.com>
Date: Sat, 14 Dec 2024 17:40:55 +0100
Subject: [PATCH 7/7] feat: handle main query call though wsUpdate and use
 wsQuery only for reruns

fixes temporal issue of the save state not being updated before query service being called
---
 apps/web/src/app/App.tsx                      |  6 +++---
 libs/shared/lib/data-access/api/eventBus.tsx  | 13 ++-----------
 libs/shared/lib/data-access/broker/wsQuery.ts |  1 -
 3 files changed, 5 insertions(+), 15 deletions(-)

diff --git a/apps/web/src/app/App.tsx b/apps/web/src/app/App.tsx
index 046a166e1..929f0f35b 100644
--- a/apps/web/src/app/App.tsx
+++ b/apps/web/src/app/App.tsx
@@ -38,7 +38,7 @@ export function App(props: App) {
   const dispatch = useAppDispatch();
   const [monitoringOpen, setMonitoringOpen] = useState<boolean>(false);
 
-  const runQuery = () => {
+  const rerunQuery = () => {
     if (session?.currentSaveState && query) {
       if (query.nodes.length === 0) {
         dispatch(resetGraphQueryResults());
@@ -66,7 +66,7 @@ export function App(props: App) {
   return (
     <div className="h-screen w-screen overflow-clip">
       <EventBus
-        onRunQuery={runQuery}
+        onRunQuery={rerunQuery}
         onAuthorized={() => {
           setAuthCheck(true);
         }}
@@ -129,7 +129,7 @@ export function App(props: App) {
                         fallback={<div>Something went wrong</div>}
                         onError={() => dispatch(addError('Something went wrong while trying to load the query builder'))}
                       >
-                        <QueryBuilder onRunQuery={runQuery} />
+                        <QueryBuilder onRunQuery={rerunQuery} />
                       </ErrorBoundary>
                     </Resizable>
                   </Resizable>
diff --git a/libs/shared/lib/data-access/api/eventBus.tsx b/libs/shared/lib/data-access/api/eventBus.tsx
index 74c8d75ac..4bbc062ba 100644
--- a/libs/shared/lib/data-access/api/eventBus.tsx
+++ b/libs/shared/lib/data-access/api/eventBus.tsx
@@ -50,6 +50,7 @@ import { unSelect } from '../store/interactionSlice';
 import { SchemaGraphStats } from '../../schema';
 import { wsReconnectSubscription, wsUserGetPolicy } from '../broker/wsUser';
 import { authorized, setSessionID } from '../store/authSlice';
+import { queryingBackend } from '../store/graphQueryResultSlice';
 
 export const EventBus = (props: { onRunQuery: Function; onAuthorized: Function }) => {
   const { login } = useAuthentication();
@@ -213,6 +214,7 @@ export const EventBus = (props: { onRunQuery: Function; onAuthorized: Function }
       if (!isEqual(state.queries?.[0], queryBuilder)) {
         console.debug('Updating queryBuilder state', state.queries, queryBuilder);
         state.queries = [{ ...queryBuilder }];
+        dispatch(queryingBackend());
         dispatch(updateSelectedSaveState(state));
         wsUpdateState(state);
       }
@@ -280,16 +282,5 @@ export const EventBus = (props: { onRunQuery: Function; onAuthorized: Function }
     }
   }, [auth.authentication]);
 
-  useEffect(() => {
-    if (!queryBuilder.ignoreReactivity) {
-      props.onRunQuery();
-      // Broker.instance().sendMessage({ //TODO!!
-      //   sessionID: auth?.sessionID || '',
-      //   key: 'broadcastState',
-      //   body: { type: 'query_builder_state', status: '', value: queryBuilder },
-      // });
-    }
-  }, [queryHash, mlHash, queryBuilderSettings, queryBuilderSettings.unionTypes]);
-
   return <div className="hide"></div>;
 };
diff --git a/libs/shared/lib/data-access/broker/wsQuery.ts b/libs/shared/lib/data-access/broker/wsQuery.ts
index 55b456422..9a3caff0e 100644
--- a/libs/shared/lib/data-access/broker/wsQuery.ts
+++ b/libs/shared/lib/data-access/broker/wsQuery.ts
@@ -1,6 +1,5 @@
 // All database related API calls
 
-import { BackendQueryFormat } from '../../querybuilder';
 import { Broker } from './broker';
 import { QueryBuilderText } from '../store/querybuilderSlice';
 import { GraphQueryResultFromBackendPayload } from '../store/graphQueryResultSlice';
-- 
GitLab