Skip to content

Commit

Permalink
fix: local tabs state
Browse files Browse the repository at this point in the history
  • Loading branch information
jacovinus committed Dec 22, 2023
1 parent 5d00cda commit 89153f6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/main/sections/Queries/QueryItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
filterLocal,
getStoredQueries,
setStoredQuery,
setLocalTabsState,
getLocalTabsState
} from "./helpers";
import { useIdRefs } from "./hooks";

Expand Down Expand Up @@ -52,7 +54,7 @@ const QueryItem = (props: any) => {
const isQueryOpen = useState(true);
const idRefs = useIdRefs(name);
const theme = useTheme();
const [tabsValue, setTabsValue] = useState(0);
const [tabsValue, setTabsValue] = useState(getLocalTabsState(name, id));

const onAddQuery = () => {
const panelData = setNewPanelData(panelSelected, data, idRefs);
Expand Down Expand Up @@ -96,6 +98,7 @@ const QueryItem = (props: any) => {
};

const onTabChange = (e: React.SyntheticEvent, tabValue: number) => {
setLocalTabsState(name, id, tabValue);
setTabsValue(() => tabValue);
};
const { activeTabs, isActiveTabs } = useActiveTabs(`Query Item`);
Expand Down
24 changes: 24 additions & 0 deletions packages/main/sections/Queries/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,27 @@ export const dataViewAction = (panel: any, data: any) => {
return setRightDataView(data);
}
};

export const setLocalTabsState = (panel: string, queryId: string, value: number) => {
try {
const localTabs = JSON.parse(localStorage.getItem("localTabsState") || "{}");
const panelState = localTabs[panel] || {};

panelState[queryId] = value;
localTabs[panel] = panelState;

localStorage.setItem("localTabsState", JSON.stringify(localTabs));
} catch (e) {
console.log(e);
}
};

export const getLocalTabsState = (panel: string, queryId: string) => {
try {
const tabsState = JSON.parse(localStorage.getItem("localTabsState") || "{}");
return tabsState[panel]?.[queryId] || 0;
} catch (e) {
console.log(e);
return 0;
}
};

0 comments on commit 89153f6

Please sign in to comment.