Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor(sql-editor): replace tab store using pinia (#986)

Yunwei Xiao8264c58970aa
diff --git a/frontend/src/components/MonacoEditor/MonacoEditor.vue b/frontend/src/components/MonacoEditor/MonacoEditor.vue
index c8c6cd80d8..2b934fa826 100644
--- a/frontend/src/components/MonacoEditor/MonacoEditor.vue
+++ b/frontend/src/components/MonacoEditor/MonacoEditor.vue
@@ -11,6 +11,7 @@ import {
nextTick,
onUnmounted,
watch,
+ computed,
} from "vue";
import { useStore } from "vuex";
import { useI18n } from "vue-i18n";
@@ -23,8 +24,8 @@ import {
useNamespacedState,
} from "vuex-composition-helpers";
+import { useTabStore } from "@/store";
import {
- TabGetters,
SqlEditorActions,
SqlEditorState,
SheetGetters,
@@ -61,6 +62,7 @@ const editorRef = ref();
const sqlCode = toRef(props, "value");
const language = toRef(props, "language");
+const tabStore = useTabStore();
const store = useStore();
const { t } = useI18n();
const { shouldSetContent, shouldFormatContent } =
@@ -68,7 +70,6 @@ const { shouldSetContent, shouldFormatContent } =
"shouldSetContent",
"shouldFormatContent",
]);
-const { currentTab } = useNamespacedGetters<TabGetters>("tab", ["currentTab"]);
const { isReadOnly } = useNamespacedGetters<SheetGetters>("sheet", [
"isReadOnly",
]);
@@ -78,6 +79,8 @@ const { setShouldSetContent, setShouldFormatContent } =
"setShouldFormatContent",
]);
+const readonly = computed(() => isReadOnly.value(tabStore.currentTab));
+
let editorInstance: Editor.IStandaloneCodeEditor;
const {
@@ -157,7 +160,7 @@ const init = async () => {
contextMenuGroupId: "operation",
contextMenuOrder: 1,
run: () => {
- if (isReadOnly.value) {
+ if (readonly.value) {
store.dispatch("notification/pushNotification", {
module: "bytebase",
style: "INFO",
@@ -203,14 +206,14 @@ const init = async () => {
});
// set the editor focus when the tab is selected
- if (!isReadOnly.value) {
+ if (!readonly.value) {
editorInstance.focus();
nextTick(() => setPositionAtEndOfLine(editorInstance));
}
watch(
- () => isReadOnly.value,
+ () => readonly.value,
(readOnly) => {
if (editorInstance) {
editorInstance.updateOptions({ readOnly });
@@ -235,7 +238,7 @@ watch(
() => {
if (shouldSetContent.value) {
setShouldSetContent(false);
- setContent(editorInstance, currentTab.value.statement);
+ setContent(editorInstance, tabStore.currentTab.statement);
}
}
);
diff --git a/frontend/src/composables/useExecuteSQL.ts b/frontend/src/composables/useExecuteSQL.ts
index 6ac98c5c26..1db612521b 100644
--- a/frontend/src/composables/useExecuteSQL.ts
+++ b/frontend/src/composables/useExecuteSQL.ts
@@ -11,6 +11,7 @@ import {
isDDLStatement,
isDMLStatement,
} from "../components/MonacoEditor/sqlParser";
+import { useTabStore } from "@/store";
type ExecuteConfig = {
databaseType: string;
@@ -23,6 +24,7 @@ type ExecuteOption = {
const useExecuteSQL = () => {
const store = useStore();
const { t } = useI18n();
+ const tabStore = useTabStore();
const state = reactive({
isLoadingData: false,
});
@@ -44,7 +46,7 @@ const useExecuteSQL = () => {
notify("INFO", t("common.tips"), t("sql-editor.can-not-execute-query"));
}
- const currentTab = store.getters["tab/currentTab"];
+ const currentTab = tabStore.currentTab;
const isDisconnected = store.getters["sqlEditor/isDisconnected"];
const statement = currentTab.statement;
const selectedStatement = currentTab.selectedStatement;
@@ -104,10 +106,10 @@ const useExecuteSQL = () => {
const queryResult = await store.dispatch("sqlEditor/executeQuery", {
statement: isExplain ? explainStatement : selectStatement,
});
- store.dispatch("tab/updateCurrentTab", { queryResult });
+ tabStore.updateCurrentTab({ queryResult });
store.dispatch("sqlEditor/fetchQueryHistoryList");
} catch (error) {
- store.dispatch("tab/updateCurrentTab", { queryResult: undefined });
+ tabStore.updateCurrentTab({ queryResult: undefined });
notify("CRITICAL", error as string);
} finally {
state.isLoadingData = false;
diff --git a/frontend/src/composables/useSQLEditorConnection.ts b/frontend/src/composables/useSQLEditorConnection.ts
index 911dcbdbba..16c89e8780 100644
--- a/frontend/src/composables/useSQLEditorConnection.ts
+++ b/frontend/src/composables/useSQLEditorConnection.ts
@@ -4,10 +4,12 @@ import { useStore } from "vuex";
import { DEFAULT_PROJECT_ID, SheetState, UNKNOWN_ID } from "../types";
import { connectionSlug } from "../utils";
import { getDefaultConnectionContext } from "../store/modules/sqlEditor";
+import { useTabStore } from "@/store";
const useSQLEditorConnection = () => {
const router = useRouter();
const store = useStore();
+ const tabStore = useTabStore();
/**
* Set the connection by tab info
@@ -15,7 +17,7 @@ const useSQLEditorConnection = () => {
* @param payload
*/
const setConnectionContextFromCurrentTab = () => {
- const currentTab = store.getters["tab/currentTab"];
+ const currentTab = tabStore.currentTab;
const sheetById = store.state.sheet.sheetById as SheetState["sheetById"];
if (currentTab.sheetId && sheetById.has(currentTab.sheetId)) {
diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts
index 49ea58996a..4579eadd21 100644
--- a/frontend/src/router/index.ts
+++ b/frontend/src/router/index.ts
@@ -20,6 +20,7 @@ import Signin from "../views/auth/Signin.vue";
import Signup from "../views/auth/Signup.vue";
import DashboardSidebar from "../views/DashboardSidebar.vue";
import Home from "../views/Home.vue";
+import { useTabStore } from "@/store";
const HOME_MODULE = "workspace.home";
const AUTH_MODULE = "auth";
@@ -800,6 +801,7 @@ export const router = createRouter({
router.beforeEach((to, from, next) => {
console.debug("Router %s -> %s", from.name, to.name);
+ const tabStore = useTabStore();
const isLoggedIn = store.getters["auth/isLoggedIn"]();
@@ -1141,12 +1143,12 @@ router.beforeEach((to, from, next) => {
store
.dispatch("sheet/fetchSheetById", sheetId)
.then((sheet: Sheet) => {
- store.dispatch("tab/addTab", {
+ tabStore.addTab({
name: sheet.name,
statement: sheet.statement,
isSaved: true,
});
- store.dispatch("tab/updateCurrentTab", {
+ tabStore.updateCurrentTab({
sheetId: sheet.id,
});
store.dispatch("sqlEditor/setSqlEditorState", {
diff --git a/frontend/src/store/modules/sheet.ts b/frontend/src/store/modules/sheet.ts
index 8a90f531a7..318ff5de1b 100644
--- a/frontend/src/store/modules/sheet.ts
+++ b/frontend/src/store/modules/sheet.ts
@@ -8,7 +8,6 @@ import type {
SheetId,
SheetState,
SheetPatch,
- CreateSheetState,
Principal,
ResourceObject,
ConnectionContext,
@@ -64,33 +63,25 @@ const state: () => SheetState = () => {
};
const getters = {
- currentSheet: (
- state: SheetState,
- getters: any,
- rootState: any,
- rootGetters: any
- ): Sheet => {
- const currentTab = rootGetters["tab/currentTab"];
-
- if (!currentTab || isEmpty(currentTab)) return unknown("SHEET") as Sheet;
+ currentSheet:
+ (state: SheetState) =>
+ (currentTab: TabInfo): Sheet => {
+ if (!currentTab || isEmpty(currentTab)) return unknown("SHEET") as Sheet;
- const sheetId = currentTab.sheetId;
+ const sheetId = currentTab.sheetId || UNKNOWN_ID;
- return state.sheetById.get(sheetId) || (unknown("SHEET") as Sheet);
- },
- isCreator: (
- state: SheetState,
- getters: any,
- rootState: any,
- rootGetters: any
- ) => {
- const currentUser = rootGetters["auth/currentUser"]();
- const currentSheet = getters.currentSheet;
+ return state.sheetById.get(sheetId) || (unknown("SHEET") as Sheet);
+ },
+ isCreator:
+ (state: SheetState, getters: any, rootState: any, rootGetters: any) =>
+ (currentTab: TabInfo): boolean => {
+ const currentUser = rootGetters["auth/currentUser"]();
+ const currentSheet = getters.currentSheet(currentTab);
- if (!currentSheet) return false;
+ if (!currentSheet) return false;
- return currentUser.id === currentSheet!.creator.id;
- },
+ return currentUser.id === currentSheet!.creator.id;
+ },
/**
* Check the sheet whether is read-only.
* 1、If the sheet is not created yet, it can not be edited.
@@ -99,47 +90,46 @@ const getters = {
* a) If the sheet's visibility is private or public, it can be edited only if the current user is the creator of the sheet.
* b) If the sheet's visibility is project, will be checked whether the current user is the `OWNER` of the project, only the current user is the `OWNER` of the project, it can be edited.
*/
- isReadOnly: (
- state: SheetState,
- getters: any,
- rootState: any,
- rootGetters: any
- ) => {
- const currentUser = rootGetters["auth/currentUser"]();
- const sharedSheet = rootState.sqlEditor.sharedSheet;
- const currentSheet = getters.currentSheet;
- const isSharedByOthers = sharedSheet.id !== UNKNOWN_ID;
-
- if (!currentSheet) return true;
- // normal sheet can be edit by anyone
- if (!isSharedByOthers) return false;
-
- // if the sheet is shared by others, will be checked the visibility of the sheet.
- // creator always can edit
- if (getters.isCreator) return false;
- const isPrivate = currentSheet?.visibility === "PRIVATE" ?? false;
- const isProject = currentSheet?.visibility === "PROJECT" ?? false;
- const isPublic = currentSheet?.visibility === "PUBLIC" ?? false;
-
- const isCurrentUserProjectOwner = () => {
- const projectMemberList = currentSheet?.project.memberList;
-
- if (projectMemberList && projectMemberList.length > 0) {
- const currentMemberByProjectMember = projectMemberList?.find(
- (member: ProjectMember) => {
- return member.principal.id === currentUser.id;
- }
- );
-
- return currentMemberByProjectMember.role !== "OWNER";
- }
-
- return false;
- };
-
- // if current user is not creator, check the link access level by project relationship
- return isPrivate || isPublic || (isProject && isCurrentUserProjectOwner());
- },
+ isReadOnly:
+ (state: SheetState, getters: any, rootState: any, rootGetters: any) =>
+ (currentTab: TabInfo): boolean => {
+ const currentUser = rootGetters["auth/currentUser"]();
+ const sharedSheet = rootState.sqlEditor.sharedSheet;
+ const currentSheet = getters.currentSheet(currentTab);
+ const isSharedByOthers = sharedSheet.id !== UNKNOWN_ID;
+
+ if (!currentSheet) return true;
+ // normal sheet can be edit by anyone
+ if (!isSharedByOthers) return false;
+
+ // if the sheet is shared by others, will be checked the visibility of the sheet.
+ // creator always can edit
+ if (getters.isCreator(currentTab)) return false;
+ const isPrivate = currentSheet?.visibility === "PRIVATE" ?? false;
+ const isProject = currentSheet?.visibility === "PROJECT" ?? false;
+ const isPublic = currentSheet?.visibility === "PUBLIC" ?? false;
+
+ const isCurrentUserProjectOwner = () => {
+ const projectMemberList = currentSheet?.project.memberList;
+
+ if (projectMemberList && projectMemberList.length > 0) {
+ const currentMemberByProjectMember = projectMemberList?.find(
+ (member: ProjectMember) => {
+ return member.principal.id === currentUser.id;
+ }
+ );
+
+ return currentMemberByProjectMember.role !== "OWNER";
+ }
+
+ return false;
+ };
+
+ // if current user is not creator, check the link access level by project relationship
+ return (
+ isPrivate || isPublic || (isProject && isCurrentUserProjectOwner())
+ );
+ },
};
const mutations = {
@@ -182,10 +172,9 @@ const actions = {
// create
async createSheet(
{ commit, state, rootState, rootGetters }: any,
- sheetRecord?: CreateSheetState
+ currentTab: TabInfo
): Promise<Sheet> {
const ctx = rootState.sqlEditor.connectionContext as ConnectionContext;
- const tab = rootGetters["tab/currentTab"] as TabInfo;
const result = (
await axios.post(`/api/sheet`, {
@@ -194,8 +183,8 @@ const actions = {
attributes: {
projectId: ctx.projectId,
databaseId: ctx.databaseId,
- name: tab.name,
- statement: tab.statement,
+ name: currentTab.name,
+ statement: currentTab.statement,
visibility: "PRIVATE",
},
},
@@ -300,14 +289,15 @@ const actions = {
// upsert
async upsertSheet(
{ commit, dispatch, state }: any,
- payload: Partial<Sheet>
+ payload: { sheet: Partial<Sheet>; currentTab: TabInfo }
): Promise<Sheet> {
- const hasSheet = state.sheetById.has(payload.id);
+ const { sheet, currentTab } = payload;
+ const hasSheet = state.sheetById.has(sheet.id);
if (hasSheet) {
- return dispatch("patchSheetById", payload);
+ return dispatch("patchSheetById", sheet);
} else {
- return dispatch("createSheet");
+ return dispatch("createSheet", currentTab);
}
},
};
diff --git a/frontend/src/store/modules/sqlEditor.ts b/frontend/src/store/modules/sqlEditor.ts
index c542295df1..01d03693a1 100644
--- a/frontend/src/store/modules/sqlEditor.ts
+++ b/frontend/src/store/modules/sqlEditor.ts
@@ -195,13 +195,11 @@ const actions = {
{ dispatch, state, rootGetters }: any,
payload: Partial<QueryInfo> = {}
) {
- const currentTab = rootGetters["tab/currentTab"];
const queryResult = await dispatch(
"sql/query",
{
instanceId: state.connectionContext.instanceId,
databaseName: state.connectionContext.databaseName,
- statement: currentTab.selectedStatement || currentTab.statement,
...payload,
},
{ root: true }
diff --git a/frontend/src/store/modules/tab.ts b/frontend/src/store/modules/tab.ts
deleted file mode 100644
index 7c3f7a66d7..0000000000
--- a/frontend/src/store/modules/tab.ts
+++ /dev/null
@@ -1,93 +0,0 @@
-import { TabInfo, AnyTabInfo, TabState } from "@/types";
-import * as types from "../mutation-types";
-import { makeActions } from "../actions";
-import { getDefaultTab } from "../../utils/tab";
-
-// tab store is the sheet store for frontend
-const state: () => TabState = () => {
- return {
- tabList: [],
- currentTabId: "",
- };
-};
-
-const getters = {
- currentTab(state: TabState) {
- const idx = state.tabList.findIndex(
- (tab: TabInfo) => tab.id === state.currentTabId
- );
- return (idx === -1 ? {} : state.tabList[idx]) as TabInfo;
- },
- hasTabs(state: TabState) {
- return state.tabList.length > 0;
- },
-};
-
-const mutations = {
- [types.SET_TAB_STATE](state: TabState, payload: Partial<TabState>) {
- Object.assign(state, payload);
- },
- [types.ADD_TAB](state: TabState, payload: TabInfo) {
- state.tabList.push(payload);
- },
- [types.REMOVE_TAB](state: TabState, payload: TabInfo) {
- state.tabList.splice(state.tabList.indexOf(payload), 1);
- },
- [types.UPDATE_TAB](state: TabState, payload: TabInfo) {
- const idx = state.tabList.findIndex(
- (tab: TabInfo) => tab.id === payload.id
- );
- Object.assign(state.tabList[idx], payload);
- },
- [types.UPDATE_CURRENT_TAB](state: TabState, payload: AnyTabInfo) {
- const idx = state.tabList.findIndex(
- (tab: TabInfo) => tab.id === state.currentTabId
- );
- Object.assign(state.tabList[idx], {
- ...state.tabList[idx],
- ...payload,
- });
- },
- [types.SET_CURRENT_TAB_ID](state: TabState, payload: string) {
- state.currentTabId = payload;
- },
-};
-
-type ActionsMap = {
- setTabState: typeof mutations.SET_TAB_STATE;
- removeTab: typeof mutations.REMOVE_TAB;
-};
-
-const actions = {
- ...makeActions<ActionsMap>({
- setTabState: types.SET_TAB_STATE,
- removeTab: types.REMOVE_TAB,
- }),
- addTab({ commit }: any, payload: AnyTabInfo) {
- const defaultTab = getDefaultTab();
-
- const newTab = {
- ...defaultTab,
- ...payload,
- };
-
- commit(types.SET_TAB_STATE, {
- currentTabId: newTab.id,
- });
- commit(types.ADD_TAB, newTab);
- },
- updateCurrentTab({ commit }: any, payload: AnyTabInfo) {
- commit(types.UPDATE_CURRENT_TAB, payload);
- },
- setCurrentTabId({ commit, state }: any, payload: string) {
- commit(types.SET_CURRENT_TAB_ID, payload);
- },
-};
-
-export default {
- namespaced: true,
- state,
- getters,
- actions,
- mutations,
-};
diff --git a/frontend/src/store/mutation-types.ts b/frontend/src/store/mutation-types.ts
index 8eff9d8968..eb109853d6 100644
--- a/frontend/src/store/mutation-types.ts
+++ b/frontend/src/store/mutation-types.ts
@@ -8,14 +8,6 @@ export const SET_IS_EXECUTING = "SET_IS_EXECUTING";
export const SET_QUERY_HISTORY_LIST = "SET_QUERY_HISTORY_LIST";
export const SET_IS_FETCHING_QUERY_HISTORY = "SET_IS_FETCHING_QUERY_HISTORY";
-// Editor Selector mutation types
-export const SET_TAB_STATE = "SET_TAB_STATE";
-export const ADD_TAB = "ADD_TAB";
-export const REMOVE_TAB = "REMOVE_TAB";
-export const UPDATE_TAB = "UPDATE_TAB";
-export const UPDATE_CURRENT_TAB = "UPDATE_CURRENT_TAB";
-export const SET_CURRENT_TAB_ID = "SET_CURRENT_TAB_ID";
-
// Sheet mutation types
export const SET_SHEET_STATE = "SET_SHEET_STATE";
export const SET_SHEET_LIST = "SET_SHEET_LIST";
diff --git a/frontend/src/store/pinia-modules/index.ts b/frontend/src/store/pinia-modules/index.ts
index e3803b788b..7605b3bb13 100644
--- a/frontend/src/store/pinia-modules/index.ts
+++ b/frontend/src/store/pinia-modules/index.ts
@@ -1,2 +1,3 @@
export * from "./actuator";
export * from "./debug";
+export * from "./tab";
diff --git a/frontend/src/store/pinia-modules/tab.ts b/frontend/src/store/pinia-modules/tab.ts
new file mode 100644
index 0000000000..166bd55b01
--- /dev/null
+++ b/frontend/src/store/pinia-modules/tab.ts
@@ -0,0 +1,62 @@
+import { defineStore } from "pinia";
+
+import { TabInfo, AnyTabInfo, TabState } from "@/types";
+import { getDefaultTab } from "@/utils/tab";
+
+export const useTabStore = defineStore("tab", {
+ persist: {
+ enabled: true,
+ strategy: "localStorage",
+ },
+
+ state: (): TabState => ({
+ tabList: [],
+ currentTabId: "",
+ }),
+
+ getters: {
+ currentTab(state) {
+ const idx = state.tabList.findIndex(
+ (tab: TabInfo) => tab.id === state.currentTabId
+ );
+ return (idx === -1 ? {} : state.tabList[idx]) as TabInfo;
+ },
+ hasTabs(state: TabState) {
+ return state.tabList.length > 0;
+ },
+ },
+
+ actions: {
+ setTabState(payload: Partial<TabState>) {
+ Object.assign(this, payload);
+ },
+ addTab(payload: AnyTabInfo) {
+ const defaultTab = getDefaultTab();
+
+ const newTab = {
+ ...defaultTab,
+ ...payload,
+ };
+
+ this.setTabState({
+ currentTabId: newTab.id,
+ });
+ this.tabList.push(newTab);
+ },
+ removeTab(payload: TabInfo) {
+ this.tabList.splice(this.tabList.indexOf(payload), 1);
+ },
+ updateCurrentTab(payload: AnyTabInfo) {
+ const idx = this.tabList.findIndex(
+ (tab: TabInfo) => tab.id === this.currentTabId
+ );
+ Object.assign(this.tabList[idx], {
+ ...this.tabList[idx],
+ ...payload,
+ });
+ },
+ setCurrentTabId(payload: string) {
+ this.currentTabId = payload;
+ },
+ },
+});
diff --git a/frontend/src/views/SqlEditor/AsidePanel/QueryHistoryContainer.vue b/frontend/src/views/SqlEditor/AsidePanel/QueryHistoryContainer.vue
index 32cf207250..7d6e27b307 100644
--- a/frontend/src/views/SqlEditor/AsidePanel/QueryHistoryContainer.vue
+++ b/frontend/src/views/SqlEditor/AsidePanel/QueryHistoryContainer.vue
@@ -71,12 +71,8 @@ import {
} from "vuex-composition-helpers";
import { useDialog } from "naive-ui";
-import {
- TabActions,
- QueryHistory,
- SqlEditorActions,
- SqlEditorState,
-} from "@/types";
+import { useTabStore } from "@/store";
+import { QueryHistory, SqlEditorActions, SqlEditorState } from "@/types";
import { getHighlightHTMLByKeyWords } from "@/utils";
interface State {
@@ -87,6 +83,7 @@ interface State {
const { t } = useI18n();
const store = useStore();
const dialog = useDialog();
+const tabStore = useTabStore();
const { queryHistoryList, isFetchingQueryHistory: isLoading } =
useNamespacedState<SqlEditorState>("sqlEditor", [
@@ -97,7 +94,6 @@ const { deleteQueryHistory } = useNamespacedActions<SqlEditorActions>(
"sqlEditor",
["deleteQueryHistory"]
);
-const { addTab } = useNamespacedActions<TabActions>("tab", ["addTab"]);
const state = reactive<State>({
search: "",
@@ -203,7 +199,7 @@ const handleActionBtnOutsideClick = () => {
};
const handleQueryHistoryClick = async (queryHistory: QueryHistory) => {
- addTab({
+ tabStore.addTab({
statement: queryHistory.statement,
selectedStatement: "",
});
diff --git a/frontend/src/views/SqlEditor/AsidePanel/SheetContainer.vue b/frontend/src/views/SqlEditor/AsidePanel/SheetContainer.vue
index 6ee6dee86d..abe48615aa 100644
--- a/frontend/src/views/SqlEditor/AsidePanel/SheetContainer.vue
+++ b/frontend/src/views/SqlEditor/AsidePanel/SheetContainer.vue
@@ -18,7 +18,7 @@
:key="query.id"
class="w-full px-1 pr-2 py-2 border-b flex flex-col justify-start items-start cursor-pointer"
:class="`${
- currentTab.sheetId === query.id
+ tabStore.currentTab.sheetId === query.id
? 'bg-gray-100 rounded border-b-0'
: ''
}`"
@@ -86,15 +86,12 @@ import { computed, reactive, ref, nextTick } from "vue";
import { useI18n } from "vue-i18n";
import {
useNamespacedActions,
- useNamespacedGetters,
useNamespacedState,
} from "vuex-composition-helpers";
import { useDialog } from "naive-ui";
+import { useTabStore } from "@/store";
import {
- TabActions,
- TabGetters,
- TabState,
SheetActions,
SheetState,
Sheet,
@@ -115,6 +112,7 @@ interface State {
const { t } = useI18n();
const { setConnectionContextFromCurrentTab } = useSQLEditorConnection();
const dialog = useDialog();
+const tabStore = useTabStore();
const { sharedSheet, isFetchingSheet: isLoading } =
useNamespacedState<SqlEditorState>("sqlEditor", [
@@ -122,20 +120,13 @@ const { sharedSheet, isFetchingSheet: isLoading } =
"isFetchingSheet",
]);
const { sheetList } = useNamespacedState<SheetState>("sheet", ["sheetList"]);
-const { tabList } = useNamespacedState<TabState>("tab", ["tabList"]);
-const { currentTab } = useNamespacedGetters<TabGetters>("tab", ["currentTab"]);
// actions
const { setShouldSetContent } = useNamespacedActions<SqlEditorActions>(
"sqlEditor",
["setShouldSetContent"]
);
-const { updateCurrentTab, setCurrentTabId, addTab } =
- useNamespacedActions<TabActions>("tab", [
- "updateCurrentTab",
- "setCurrentTabId",
- "addTab",
- ]);
+
const { deleteSheet, patchSheetById } = useNamespacedActions<SheetActions>(
"sheet",
["deleteSheet", "patchSheetById"]
@@ -225,8 +216,8 @@ const handleSheetNameChanged = () => {
name: state.currentSheetName,
});
- if (currentTab.value.sheetId === state.editingSheetId) {
- updateCurrentTab({
+ if (tabStore.currentTab.sheetId === state.editingSheetId) {
+ tabStore.updateCurrentTab({
name: state.currentSheetName,
});
}
@@ -238,8 +229,8 @@ const handleDeleteSheet = () => {
if (state.currentActionSheet) {
deleteSheet(state.currentActionSheet.id);
- if (currentTab.value.sheetId === state.currentActionSheet.id) {
- updateCurrentTab({
+ if (tabStore.currentTab.sheetId === state.currentActionSheet.id) {
+ tabStore.updateCurrentTab({
sheetId: undefined,
});
}
@@ -273,11 +264,11 @@ const handleActionBtnOutsideClick = () => {
};
const handleSheetClick = async (sheet: Sheet) => {
- if (currentTab.value.sheetId !== sheet.id) {
+ if (tabStore.currentTab.sheetId !== sheet.id) {
// open opened tab first
- for (const tab of tabList.value) {
+ for (const tab of tabStore.tabList) {
if (tab.sheetId === sheet.id) {
- setCurrentTabId(tab.id);
+ tabStore.setCurrentTabId(tab.id);
setConnectionContextFromCurrentTab();
setShouldSetContent(true);
return;
@@ -285,7 +276,7 @@ const handleSheetClick = async (sheet: Sheet) => {
}
// otherwise, add new tab
- addTab({
+ tabStore.addTab({
name: sheet.name,
statement: sheet.statement,
selectedStatement: "",
diff --git a/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vue b/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vue
index e4231cd967..62a72afdab 100644
--- a/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vue
+++ b/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vue
@@ -91,7 +91,7 @@
secondary
strong
type="primary"
- :disabled="isEmptyStatement || currentTab.isSaved"
+ :disabled="isEmptyStatement || tabStore.currentTab.isSaved"
@click="() => emit('save-sheet')"
>
<carbon:save class="h-5 w-5" /> &nbsp; {{ $t("common.save") }} (⌘+S)
@@ -106,7 +106,7 @@
<template #trigger>
<NButton
:disabled="
- isEmptyStatement || isDisconnected || !currentTab.isSaved
+ isEmptyStatement || isDisconnected || !tabStore.currentTab.isSaved
"
@click.stop.prevent="toggleSharePopover"
>
@@ -128,11 +128,11 @@ import {
} from "vuex-composition-helpers";
import { useStore } from "vuex";
+import { useTabStore } from "@/store";
import {
SqlEditorState,
SqlEditorGetters,
SqlEditorActions,
- TabGetters,
UNKNOWN_ID,
Instance,
} from "@/types";
@@ -147,6 +147,7 @@ const emit = defineEmits<{
const store = useStore();
const router = useRouter();
+const tabStore = useTabStore();
const { connectionContext } = useNamespacedState<SqlEditorState>("sqlEditor", [
"connectionContext",
@@ -155,8 +156,6 @@ const { isDisconnected } = useNamespacedGetters<SqlEditorGetters>("sqlEditor", [
"isDisconnected",
]);
-const { currentTab } = useNamespacedGetters<TabGetters>("tab", ["currentTab"]);
-
const { setShouldFormatContent } = useNamespacedActions<SqlEditorActions>(
"sqlEditor",
["setShouldFormatContent"]
@@ -165,7 +164,7 @@ const { setShouldFormatContent } = useNamespacedActions<SqlEditorActions>(
const isShowSharePopover = ref(false);
const isEmptyStatement = computed(
- () => !currentTab.value || currentTab.value.statement === ""
+ () => !tabStore.currentTab || tabStore.currentTab.statement === ""
);
const selectedInstance = computed<Instance>(() => {
const ctx = connectionContext.value;
diff --git a/frontend/src/views/SqlEditor/EditorPanel/EditorPanel.vue b/frontend/src/views/SqlEditor/EditorPanel/EditorPanel.vue
index c92c22c235..986f3f1dfc 100644
--- a/frontend/src/views/SqlEditor/EditorPanel/EditorPanel.vue
+++ b/frontend/src/views/SqlEditor/EditorPanel/EditorPanel.vue
@@ -34,12 +34,11 @@ import {
useNamespacedActions,
} from "vuex-composition-helpers";
+import { useTabStore } from "@/store";
import {
SqlEditorState,
SqlEditorActions,
SqlEditorGetters,
- TabGetters,
- TabActions,
SheetActions,
} from "@/types";
import EditorAction from "./EditorAction.vue";
@@ -49,6 +48,8 @@ import ConnectionHolder from "./ConnectionHolder.vue";
import SaveSheetModal from "./SaveSheetModal.vue";
import { defaultTabName } from "../../../utils/tab";
+const tabStore = useTabStore();
+
const { isShowExecutingHint } = useNamespacedState<SqlEditorState>(
"sqlEditor",
["isShowExecutingHint"]
@@ -62,15 +63,11 @@ const { setSqlEditorState } = useNamespacedActions<SqlEditorActions>(
"sqlEditor",
["setSqlEditorState"]
);
-const { currentTab } = useNamespacedGetters<TabGetters>("tab", ["currentTab"]);
// actions
const { upsertSheet } = useNamespacedActions<SheetActions>("sheet", [
"upsertSheet",
]);
-const { updateCurrentTab } = useNamespacedActions<TabActions>("tab", [
- "updateCurrentTab",
-]);
const isShowSaveSheetModal = ref(false);
@@ -81,21 +78,24 @@ const handleClose = () => {
};
const handleSaveSheet = async (sheetName?: string) => {
- if (currentTab.value.name === defaultTabName.value && !sheetName) {
+ if (tabStore.currentTab.name === defaultTabName.value && !sheetName) {
isShowSaveSheetModal.value = true;
return;
}
isShowSaveSheetModal.value = false;
- const { name, statement, sheetId } = currentTab.value;
+ const { name, statement, sheetId } = tabStore.currentTab;
const sheet = await upsertSheet({
- id: sheetId,
- name: sheetName ? sheetName : name,
- statement,
+ sheet: {
+ id: sheetId,
+ name: sheetName ? sheetName : name,
+ statement,
+ },
+ currentTab: tabStore.currentTab,
});
- updateCurrentTab({
+ tabStore.updateCurrentTab({
sheetId: sheet.id,
isSaved: true,
name: sheetName ? sheetName : name,
diff --git a/frontend/src/views/SqlEditor/EditorPanel/ExecuteHint.vue b/frontend/src/views/SqlEditor/EditorPanel/ExecuteHint.vue
index 4abb45929a..1d1b66b01f 100644
--- a/frontend/src/views/SqlEditor/EditorPanel/ExecuteHint.vue
+++ b/frontend/src/views/SqlEditor/EditorPanel/ExecuteHint.vue
@@ -57,9 +57,10 @@ import {
useNamespacedState,
} from "vuex-composition-helpers";
+import { useTabStore } from "@/store";
import { UNKNOWN_ID } from "@/types";
-import type { SqlEditorState, SqlEditorGetters, TabGetters } from "@/types";
+import type { SqlEditorState, SqlEditorGetters } from "@/types";
import {
parseSQL,
transformSQL,
@@ -73,6 +74,7 @@ const emit = defineEmits<{
const router = useRouter();
const store = useStore();
const { t } = useI18n();
+const tabStore = useTabStore();
const { findProjectIdByDatabaseId } = useNamespacedGetters<SqlEditorGetters>(
"sqlEditor",
@@ -81,10 +83,9 @@ const { findProjectIdByDatabaseId } = useNamespacedGetters<SqlEditorGetters>(
const { connectionContext } = useNamespacedState<SqlEditorState>("sqlEditor", [
"connectionContext",
]);
-const { currentTab } = useNamespacedGetters<TabGetters>("tab", ["currentTab"]);
const sqlStatement = computed(
- () => currentTab.value.selectedStatement || currentTab.value.statement
+ () => tabStore.currentTab.selectedStatement || tabStore.currentTab.statement
);
const getParsedStatement = () => {
diff --git a/frontend/src/views/SqlEditor/EditorPanel/QueryEditor.vue b/frontend/src/views/SqlEditor/EditorPanel/QueryEditor.vue
index cb40646768..3fd005813d 100644
--- a/frontend/src/views/SqlEditor/EditorPanel/QueryEditor.vue
+++ b/frontend/src/views/SqlEditor/EditorPanel/QueryEditor.vue
@@ -13,31 +13,26 @@
import { debounce } from "lodash-es";
import { computed, defineEmits } from "vue";
import { useStore } from "vuex";
-import {
- useNamespacedActions,
- useNamespacedGetters,
- useNamespacedState,
-} from "vuex-composition-helpers";
+import { useNamespacedState } from "vuex-composition-helpers";
+import { useTabStore } from "@/store";
import { useExecuteSQL } from "@/composables/useExecuteSQL";
-import { TabActions, TabGetters, SqlEditorState } from "@/types";
+import { SqlEditorState } from "@/types";
const emit = defineEmits<{
(e: "save-sheet", content?: string): void;
}>();
const store = useStore();
-const { currentTab } = useNamespacedGetters<TabGetters>("tab", ["currentTab"]);
+const tabStore = useTabStore();
+
const { connectionContext } = useNamespacedState<SqlEditorState>("sqlEditor", [
"connectionContext",
]);
-const { updateCurrentTab } = useNamespacedActions<TabActions>("tab", [
- "updateCurrentTab",
-]);
const { execute } = useExecuteSQL();
-const sqlCode = computed(() => currentTab.value.statement);
+const sqlCode = computed(() => tabStore.currentTab.statement);
const selectedInstance = computed(() => {
const ctx = connectionContext.value;
return store.getters["instance/instanceById"](ctx.instanceId);
@@ -60,14 +55,14 @@ const selectedLanguage = computed(() => {
});
const handleChange = debounce((value: string) => {
- updateCurrentTab({
+ tabStore.updateCurrentTab({
statement: value,
isSaved: false,
});
}, 300);
const handleChangeSelection = debounce((value: string) => {
- updateCurrentTab({
+ tabStore.updateCurrentTab({
selectedStatement: value,
});
}, 300);
diff --git a/frontend/src/views/SqlEditor/EditorPanel/SaveSheetModal.vue b/frontend/src/views/SqlEditor/EditorPanel/SaveSheetModal.vue
index f326ce3946..116931ab36 100644
--- a/frontend/src/views/SqlEditor/EditorPanel/SaveSheetModal.vue
+++ b/frontend/src/views/SqlEditor/EditorPanel/SaveSheetModal.vue
@@ -19,17 +19,17 @@
<script lang="ts" setup>
import { ref, nextTick, defineEmits } from "vue";
-import { useNamespacedGetters } from "vuex-composition-helpers";
-import { TabGetters } from "../../../types";
+
+import { useTabStore } from "@/store";
const emit = defineEmits<{
(e: "close"): void;
(e: "save-sheet", content: string): void;
}>();
-const { currentTab } = useNamespacedGetters<TabGetters>("tab", ["currentTab"]);
+const tabStore = useTabStore();
-const sheetName = ref(currentTab.value.name);
+const sheetName = ref(tabStore.currentTab.name);
const sheetNameInputRef = ref();
nextTick(() => {
diff --git a/frontend/src/views/SqlEditor/EditorPanel/SharePopover.vue b/frontend/src/views/SqlEditor/EditorPanel/SharePopover.vue
index 0ee5b03fed..c9a4b1e571 100644
--- a/frontend/src/views/SqlEditor/EditorPanel/SharePopover.vue
+++ b/frontend/src/views/SqlEditor/EditorPanel/SharePopover.vue
@@ -65,7 +65,7 @@
<NButton
class="w-20"
:type="copied ? 'success' : 'primary'"
- :disabled="!currentTab.isSaved"
+ :disabled="!tabStore.currentTab.isSaved"
@click="handleCopy"
>
<heroicons-solid:check v-if="copied" class="h-4 w-4" />
@@ -87,8 +87,8 @@ import {
} from "vuex-composition-helpers";
import slug from "slug";
+import { useTabStore } from "@/store";
import {
- TabGetters,
SqlEditorState,
SheetGetters,
SheetActions,
@@ -101,6 +101,7 @@ const emit = defineEmits<{
const store = useStore();
const { t } = useI18n();
+const tabStore = useTabStore();
const accessOptions = computed<AccessOption[]>(() => {
return [
@@ -125,7 +126,6 @@ const accessOptions = computed<AccessOption[]>(() => {
const { connectionContext } = useNamespacedState<SqlEditorState>("sqlEditor", [
"connectionContext",
]);
-const { currentTab } = useNamespacedGetters<TabGetters>("tab", ["currentTab"]);
const { currentSheet, isCreator } = useNamespacedGetters<SheetGetters>(
"sheet",
["currentSheet", "isCreator"]
@@ -136,6 +136,9 @@ const { patchSheetById } = useNamespacedActions<SheetActions>("sheet", [
const ctx = connectionContext.value;
+const sheet = computed(() => currentSheet.value(tabStore.currentTab));
+const creator = computed(() => isCreator.value(tabStore.currentTab));
+
const host = window.location.host;
const connectionSlug = [
slug(ctx.instanceName as string),
@@ -148,18 +151,18 @@ const currentAccess = ref<AccessOption>(accessOptions.value[0]);
const isShowAccessPopover = ref(false);
const creatorAccessStyle = computed(() => {
- return isCreator.value
+ return creator.value
? "cursor-pointer hover:bg-accent hover:text-white"
: "bg-transparent text-gray-300 cursor-not-allowed";
});
const creatorAccessDescStyle = computed(() => {
- return isCreator.value ? "text-gray-400" : "text-gray-300 cursor-not-allowed";
+ return creator.value ? "text-gray-400" : "text-gray-300 cursor-not-allowed";
});
const updateSheet = () => {
- if (currentTab.value.sheetId) {
+ if (tabStore.currentTab.sheetId) {
patchSheetById({
- id: currentTab.value.sheetId,
+ id: tabStore.currentTab.sheetId,
visibility: currentAccess.value.value,
});
}
@@ -167,14 +170,16 @@ const updateSheet = () => {
const handleChangeAccess = (option: AccessOption) => {
// only creator can change access
- if (isCreator.value) {
+ if (creator.value) {
currentAccess.value = option;
updateSheet();
}
isShowAccessPopover.value = false;
};
-const sheetSlug = `${slug(currentTab.value.name)}_${currentTab.value.sheetId}`;
+const sheetSlug = `${slug(tabStore.currentTab.name)}_${
+ tabStore.currentTab.sheetId
+}`;
const sharedTabLink = ref(`${host}/sql-editor/${connectionSlug}/${sheetSlug}`);
const { copy, copied } = useClipboard({
@@ -192,8 +197,8 @@ const handleCopy = async () => {
};
onMounted(() => {
- if (currentSheet.value) {
- const { visibility } = currentSheet.value;
+ if (sheet.value) {
+ const { visibility } = sheet.value;
const idx = accessOptions.value.findIndex(
(item) => item.value === visibility
);
diff --git a/frontend/src/views/SqlEditor/SqlEditor.vue b/frontend/src/views/SqlEditor/SqlEditor.vue
index 0e223218b4..380a1fa013 100644
--- a/frontend/src/views/SqlEditor/SqlEditor.vue
+++ b/frontend/src/views/SqlEditor/SqlEditor.vue
@@ -32,11 +32,13 @@ import EditorPanel from "./EditorPanel/EditorPanel.vue";
import TabListContainer from "./TabListContainer.vue";
import TablePanel from "./TablePanel/TablePanel.vue";
import GettingStarted from "./GettingStarted.vue";
+import { useTabStore } from "@/store";
const store = useStore();
+const tabStore = useTabStore();
-const hasTabs = computed(() => store.getters["tab/hasTabs"]);
-const paneKey = computed(() => store.getters["tab/currentTab"].id);
+const hasTabs = computed(() => tabStore.hasTabs);
+const paneKey = computed(() => tabStore.currentTab.id);
const isDisconnected = computed(
() => store.getters["sqlEditor/isDisconnected"]
);
diff --git a/frontend/src/views/SqlEditor/TabListContainer.vue b/frontend/src/views/SqlEditor/TabListContainer.vue
index a117a057b9..3a21239015 100644
--- a/frontend/src/views/SqlEditor/TabListContainer.vue
+++ b/frontend/src/views/SqlEditor/TabListContainer.vue
@@ -11,10 +11,10 @@
@wheel="handleScollTabList"
>
<div
- v-for="tab in tabList"
+ v-for="tab in tabStore.tabList"
:key="tab.id"
class="tab-list-tab"
- :class="{ active: tab.id === currentTabId }"
+ :class="{ active: tab.id === tabStore.currentTabId }"
:style="scrollState.style"
@click="handleSelectTab(tab)"
@mouseover="enterTabId = tab.id"
@@ -44,17 +44,17 @@
</div>
<span v-else class="flex items-center space-x-2">
<heroicons-outline:user-group
- v-if="currentSheet.visibility === 'PROJECT'"
+ v-if="sheet.visibility === 'PROJECT'"
class="w-4 h-4"
/>
<heroicons-outline:globe
- v-if="currentSheet.visibility === 'PUBLIC'"
+ v-if="sheet.visibility === 'PUBLIC'"
class="w-4 h-4"
/>
<span>{{ tab.name }}</span>
</span>
</div>
- <template v-if="enterTabId === tab.id && tabList.length > 1">
+ <template v-if="enterTabId === tab.id && tabStore.tabList.length > 1">
<span
class="suffix close hover:bg-gray-200 rounded-sm"
@click.prevent.stop="handleRemoveTab(tab)"
@@ -68,7 +68,11 @@
<carbon:dot-mark class="h-4 w-4" />
</span>
</template>
- <template v-else-if="tab.id === currentTabId && tabList.length > 1">
+ <template
+ v-else-if="
+ tab.id === tabStore.currentTabId && tabStore.tabList.length > 1
+ "
+ >
<span
class="suffix close hover:bg-gray-200 rounded-sm"
@click.prevent="handleRemoveTab(tab)"
@@ -131,49 +135,28 @@ import { useI18n } from "vue-i18n";
import { useStore } from "vuex";
import {
useNamespacedGetters,
- useNamespacedState,
useNamespacedActions,
} from "vuex-composition-helpers";
import { useDialog } from "naive-ui";
-import {
- TabInfo,
- SqlEditorGetters,
- TabGetters,
- TabState,
- TabActions,
- SheetGetters,
- SheetActions,
-} from "@/types";
+import { useTabStore } from "@/store";
+import { TabInfo, SqlEditorGetters, SheetGetters, SheetActions } from "@/types";
import { getDefaultTab } from "@/utils/tab";
import { useSQLEditorConnection } from "@/composables/useSQLEditorConnection";
-// getters map
-const { currentTab, hasTabs } = useNamespacedGetters<TabGetters>("tab", [
- "currentTab",
- "hasTabs",
-]);
+const tabStore = useTabStore();
+
const { isDisconnected } = useNamespacedGetters<SqlEditorGetters>("sqlEditor", [
"isDisconnected",
]);
+
const { currentSheet } = useNamespacedGetters<SheetGetters>("sheet", [
"currentSheet",
]);
-// state map
-const { currentTabId, tabList } = useNamespacedState<TabState>("tab", [
- "currentTabId",
- "tabList",
-]);
+const sheet = computed(() => currentSheet.value(tabStore.currentTab));
// actions map
-const { addTab, removeTab, setCurrentTabId, updateCurrentTab } =
- useNamespacedActions<TabActions>("tab", [
- "addTab",
- "removeTab",
- "setCurrentTabId",
- "updateCurrentTab",
- ]);
const { patchSheetById } = useNamespacedActions<SheetActions>("sheet", [
"patchSheetById",
]);
@@ -184,7 +167,7 @@ const { setConnectionContextFromCurrentTab } = useSQLEditorConnection();
const dialog = useDialog();
const enterTabId = ref("");
-const selectedTab = computed(() => currentTabId.value);
+const selectedTab = computed(() => tabStore.currentTabId);
// edit label state
const labelState = reactive({
currentLabelName: "",
@@ -193,7 +176,7 @@ const labelState = reactive({
const labelInputRef = ref<HTMLInputElement>();
const localTabList = computed(() => {
- return tabList.value.map((tab: TabInfo) => {
+ return tabStore.tabList.map((tab: TabInfo) => {
return {
label: tab.name,
value: tab.id,
@@ -220,9 +203,9 @@ const recalculateScrollWidth = () => {
};
const updateSheetName = () => {
- if (currentTab.value.sheetId) {
+ if (tabStore.currentTab.sheetId) {
patchSheetById({
- id: currentTab.value.sheetId,
+ id: tabStore.currentTab.sheetId,
name: labelState.currentLabelName,
});
}
@@ -239,7 +222,7 @@ const handleEditLabel = (tab: TabInfo) => {
const handleTryChangeLabel = () => {
if (labelState.editingTabId) {
if (labelState.currentLabelName !== "") {
- updateCurrentTab({
+ tabStore.updateCurrentTab({
name: labelState.currentLabelName,
});
@@ -267,13 +250,11 @@ const handleCancelChangeLabel = () => {
};
const handleSelectTab = async (tab: TabInfo) => {
- setCurrentTabId(tab.id);
+ tabStore.setCurrentTabId(tab.id);
setConnectionContextFromCurrentTab();
};
const handleAddTab = () => {
- if (isDisconnected.value) return;
-
- addTab(getDefaultTab());
+ tabStore.addTab(getDefaultTab());
nextTick(recalculateScrollWidth);
};
@@ -299,11 +280,11 @@ const handleRemoveTab = async (tab: TabInfo) => {
}
async function remove() {
- await removeTab(tab);
- const tabsLength = tabList.value.length;
+ await tabStore.removeTab(tab);
+ const tabsLength = tabStore.tabList.length;
if (tabsLength > 0) {
- handleSelectTab(tabList.value[tabsLength - 1]);
+ handleSelectTab(tabStore.tabList[tabsLength - 1]);
}
nextTick(() => {
recalculateScrollWidth();
@@ -311,7 +292,7 @@ const handleRemoveTab = async (tab: TabInfo) => {
}
};
const handleSelectTabFromPopselect = (tabId: string) => {
- setCurrentTabId(tabId);
+ tabStore.setCurrentTabId(tabId);
setConnectionContextFromCurrentTab();
};
@@ -322,8 +303,8 @@ const handleScollTabList = debounce((e: WheelEvent) => {
}, 333);
onMounted(async () => {
- if (!hasTabs.value) {
- addTab(getDefaultTab());
+ if (!tabStore.hasTabs) {
+ tabStore.addTab(getDefaultTab());
recalculateScrollWidth();
}
});
diff --git a/frontend/src/views/SqlEditor/TablePanel/TableView.vue b/frontend/src/views/SqlEditor/TablePanel/TableView.vue
index a6f0ec58d6..eddc5dd084 100644
--- a/frontend/src/views/SqlEditor/TablePanel/TableView.vue
+++ b/frontend/src/views/SqlEditor/TablePanel/TableView.vue
@@ -66,29 +66,27 @@
import { computed, reactive, ref } from "vue";
import { useI18n } from "vue-i18n";
import { useResizeObserver } from "@vueuse/core";
-import {
- useNamespacedGetters,
- useNamespacedState,
-} from "vuex-composition-helpers";
+import { useNamespacedState } from "vuex-composition-helpers";
import { unparse } from "papaparse";
import { isEmpty } from "lodash-es";
import dayjs from "dayjs";
-import { TabGetters, SqlEditorState } from "@/types";
+import { useTabStore } from "@/store";
+
+import { SqlEditorState } from "@/types";
interface State {
search: string;
}
const { t } = useI18n();
+const tabStore = useTabStore();
const { isExecuting } = useNamespacedState<SqlEditorState>("sqlEditor", [
"isExecuting",
]);
-const { currentTab } = useNamespacedGetters<TabGetters>("tab", ["currentTab"]);
-
-const queryResult = computed(() => currentTab.value.queryResult || null);
+const queryResult = computed(() => tabStore.currentTab.queryResult || null);
const state = reactive<State>({
search: "",