Skip to content

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

refactor: re-implement router store module with pinia (#1016)

Liu Ji89c92ef10b03
diff --git a/frontend/src/components/Breadcrumb.vue b/frontend/src/components/Breadcrumb.vue
index e8082ea1fd..448c4c45c2 100644
--- a/frontend/src/components/Breadcrumb.vue
+++ b/frontend/src/components/Breadcrumb.vue
@@ -52,15 +52,9 @@ import { computed, ComputedRef, defineComponent } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { useI18n } from "vue-i18n";
-import {
- RouterSlug,
- Bookmark,
- UNKNOWN_ID,
- Principal,
- BookmarkCreate,
-} from "../types";
+import { Bookmark, UNKNOWN_ID, Principal, BookmarkCreate } from "../types";
import { idFromSlug } from "../utils";
-import { useUIStateStore } from "@/store";
+import { useRouterStore, useUIStateStore } from "@/store";
interface BreadcrumbItem {
name: string;
@@ -72,6 +66,7 @@ export default defineComponent({
components: {},
setup() {
const store = useStore();
+ const routerStore = useRouterStore();
const currentRoute = useRouter().currentRoute;
const { t } = useI18n();
@@ -93,9 +88,7 @@ export default defineComponent({
const allowBookmark = computed(() => currentRoute.value.meta.allowBookmark);
const breadcrumbList = computed(() => {
- const routeSlug: RouterSlug = store.getters["router/routeSlug"](
- currentRoute.value
- );
+ const routeSlug = routerStore.routeSlug(currentRoute.value);
const environmentSlug = routeSlug.environmentSlug;
const projectSlug = routeSlug.projectSlug;
const projectWebhookSlug = routeSlug.projectWebhookSlug;
diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts
index b7d33cfbac..e5e13c6143 100644
--- a/frontend/src/router/index.ts
+++ b/frontend/src/router/index.ts
@@ -12,7 +12,7 @@ import InstanceLayout from "../layouts/InstanceLayout.vue";
import SplashLayout from "../layouts/SplashLayout.vue";
import SqlEditorLayout from "../layouts/SqlEditorLayout.vue";
import { t } from "../plugins/i18n";
-import { store } from "../store";
+import { store, useRouterStore } from "../store";
import { Database, QuickActionType, Sheet } from "../types";
import { idFromSlug, isDBAOrOwner, isOwner } from "../utils";
// import PasswordReset from "../views/auth/PasswordReset.vue";
@@ -796,6 +796,7 @@ export const router = createRouter({
router.beforeEach((to, from, next) => {
console.debug("Router %s -> %s", from.name, to.name);
const tabStore = useTabStore();
+ const routerStore = useRouterStore();
const isLoggedIn = store.getters["auth/isLoggedIn"]();
@@ -805,7 +806,7 @@ router.beforeEach((to, from, next) => {
const toModule = to.name ? to.name.toString().split(".")[0] : HOME_MODULE;
if (toModule != fromModule) {
- store.dispatch("router/setBackPath", from.fullPath);
+ routerStore.setBackPath(from.fullPath);
}
// OAuth callback route is a relay to receive the OAuth callback and dispatch the corresponding OAuth event. It's called in the following scenarios:
@@ -915,7 +916,7 @@ router.beforeEach((to, from, next) => {
return;
}
- const routerSlug = store.getters["router/routeSlug"](to);
+ const routerSlug = routerStore.routeSlug(to);
const principalId = routerSlug.principalId;
const environmentSlug = routerSlug.environmentSlug;
const projectSlug = routerSlug.projectSlug;
diff --git a/frontend/src/store/pinia-modules/index.ts b/frontend/src/store/pinia-modules/index.ts
index 273f6e500b..c21844976a 100644
--- a/frontend/src/store/pinia-modules/index.ts
+++ b/frontend/src/store/pinia-modules/index.ts
@@ -2,6 +2,7 @@ export * from "./actuator";
export * from "./command";
export * from "./debug";
export * from "./label";
+export * from "./router";
export * from "./subscription";
export * from "./tab";
export * from "./uistate";
diff --git a/frontend/src/store/modules/router.ts b/frontend/src/store/pinia-modules/router.ts
similarity index 91%
rename from frontend/src/store/modules/router.ts
rename to frontend/src/store/pinia-modules/router.ts
index bb7c2ab6e5..5083134a6e 100644
--- a/frontend/src/store/modules/router.ts
+++ b/frontend/src/store/pinia-modules/router.ts
@@ -1,17 +1,22 @@
+import { defineStore } from "pinia";
import { RouteLocationNormalized } from "vue-router";
import { RouterSlug } from "../../types";
-// eslint-disable-next-line @typescript-eslint/no-empty-interface
-export interface RouterState {}
+export const useRouterStore = defineStore("router", {
+ // need not to initialize a state since we store everything into localStorage
+ // state: () => ({}),
-const getters = {
- backPath: (state: RouterState) => (): string => {
- return localStorage.getItem("ui.backPath") || "/";
+ getters: {
+ backPath: () => () => {
+ return localStorage.getItem("ui.backPath") || "/";
+ },
},
-
- routeSlug:
- (state: RouterState) =>
- (currentRoute: RouteLocationNormalized): RouterSlug => {
+ actions: {
+ setBackPath(backPath: string) {
+ localStorage.setItem("ui.backPath", backPath);
+ return backPath;
+ },
+ routeSlug(currentRoute: RouteLocationNormalized): RouterSlug {
{
// /u/:principalId
// Total 2 elements, 2nd element is the principal id
@@ -192,17 +197,5 @@ const getters = {
return {};
},
-};
-
-const actions = {
- setBackPath({ commit }: any, backPath: string) {
- localStorage.setItem("ui.backPath", backPath);
- return backPath;
},
-};
-
-export default {
- namespaced: true,
- getters,
- actions,
-};
+});
diff --git a/frontend/src/views/SettingSidebar.vue b/frontend/src/views/SettingSidebar.vue
index 9f3d90b376..4524f19537 100644
--- a/frontend/src/views/SettingSidebar.vue
+++ b/frontend/src/views/SettingSidebar.vue
@@ -92,6 +92,7 @@ import { computed, defineComponent, reactive } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { isOwner, isDev } from "../utils";
+import { useRouterStore } from "@/store";
interface LocalState {
collapseState: boolean;
@@ -107,6 +108,7 @@ export default defineComponent({
},
setup() {
const store = useStore();
+ const routerStore = useRouterStore();
const router = useRouter();
const state = reactive<LocalState>({
@@ -120,7 +122,7 @@ export default defineComponent({
});
const goBack = () => {
- router.push(store.getters["router/backPath"]());
+ router.push(routerStore.backPath());
};
const toggleCollapse = () => {