Skip to content

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

refactor: migrate bookmark store to pinia (#1031)

Yunwei Xiaof469032f301a
diff --git a/frontend/src/components/BookmarkListSidePanel.vue b/frontend/src/components/BookmarkListSidePanel.vue
index 604d71e119..5f9562b918 100644
--- a/frontend/src/components/BookmarkListSidePanel.vue
+++ b/frontend/src/components/BookmarkListSidePanel.vue
@@ -14,40 +14,39 @@
</template>
<script lang="ts">
-import { computed, watchEffect } from "vue";
+import { computed, watchEffect, defineComponent } from "vue";
import { useStore } from "vuex";
import { UNKNOWN_ID } from "../types";
import { Action, defineAction, useRegisterActions } from "@bytebase/vue-kbar";
import { useRouter } from "vue-router";
import { useI18n } from "vue-i18n";
+import { useBookmarkStore } from "@/store";
-export default {
+export default defineComponent({
name: "BookmarkListSidePanel",
setup() {
const { t } = useI18n();
const store = useStore();
const router = useRouter();
+ const bookmarkStore = useBookmarkStore();
const currentUser = computed(() => store.getters["auth/currentUser"]());
const prepareBookmarkList = () => {
// It will also be called when user logout
if (currentUser.value.id != UNKNOWN_ID) {
- store.dispatch(
- "bookmark/fetchBookmarkListByUser",
- currentUser.value.id
- );
+ bookmarkStore.fetchBookmarkListByUser(currentUser.value.id);
}
};
watchEffect(prepareBookmarkList);
const bookmarkList = computed(() =>
- store.getters["bookmark/bookmarkListByUser"](currentUser.value.id)
+ bookmarkStore.bookmarkListByUser(currentUser.value.id)
);
const deleteIndex = (index: number) => {
- store.dispatch("bookmark/deleteBookmark", bookmarkList.value[index]);
+ bookmarkStore.deleteBookmark(bookmarkList.value[index]);
};
const kbarActions = computed((): Action[] => {
@@ -72,5 +71,5 @@ export default {
deleteIndex,
};
},
-};
+});
</script>
diff --git a/frontend/src/components/Breadcrumb.vue b/frontend/src/components/Breadcrumb.vue
index 448c4c45c2..446e5c436d 100644
--- a/frontend/src/components/Breadcrumb.vue
+++ b/frontend/src/components/Breadcrumb.vue
@@ -54,7 +54,7 @@ import { useRouter } from "vue-router";
import { useI18n } from "vue-i18n";
import { Bookmark, UNKNOWN_ID, Principal, BookmarkCreate } from "../types";
import { idFromSlug } from "../utils";
-import { useRouterStore, useUIStateStore } from "@/store";
+import { useRouterStore, useUIStateStore, useBookmarkStore } from "@/store";
interface BreadcrumbItem {
name: string;
@@ -69,13 +69,14 @@ export default defineComponent({
const routerStore = useRouterStore();
const currentRoute = useRouter().currentRoute;
const { t } = useI18n();
+ const bookmarkStore = useBookmarkStore();
const currentUser: ComputedRef<Principal> = computed(() =>
store.getters["auth/currentUser"]()
);
const bookmark: ComputedRef<Bookmark> = computed(() =>
- store.getters["bookmark/bookmarkByUserAndLink"](
+ bookmarkStore.bookmarkByUserAndLink(
currentUser.value.id,
currentRoute.value.path
)
@@ -168,13 +169,13 @@ export default defineComponent({
const toggleBookmark = () => {
if (isBookmarked.value) {
- store.dispatch("bookmark/deleteBookmark", bookmark.value);
+ bookmarkStore.deleteBookmark(bookmark.value);
} else {
const newBookmark: BookmarkCreate = {
name: breadcrumbList.value[breadcrumbList.value.length - 1].name,
link: currentRoute.value.path,
};
- store.dispatch("bookmark/createBookmark", newBookmark).then(() => {
+ bookmarkStore.createBookmark(newBookmark).then(() => {
useUIStateStore().saveIntroStateByKey({
key: "bookmark.create",
newState: true,
diff --git a/frontend/src/store/modules/bookmark.ts b/frontend/src/store/modules/bookmark.ts
deleted file mode 100644
index 3cf21911e2..0000000000
--- a/frontend/src/store/modules/bookmark.ts
+++ /dev/null
@@ -1,133 +0,0 @@
-import axios from "axios";
-import {
- PrincipalId,
- Bookmark,
- BookmarkCreate,
- BookmarkState,
- ResourceObject,
- unknown,
-} from "../../types";
-import { getPrincipalFromIncludedList } from "./principal";
-
-function convert(
- bookmark: ResourceObject,
- includedList: ResourceObject[],
- rootGetters: any
-): Bookmark {
- return {
- ...(bookmark.attributes as Omit<Bookmark, "id" | "creator" | "updater">),
- id: parseInt(bookmark.id),
- creator: getPrincipalFromIncludedList(
- bookmark.relationships!.creator.data,
- includedList
- ),
- updater: getPrincipalFromIncludedList(
- bookmark.relationships!.updater.data,
- includedList
- ),
- };
-}
-
-const state: () => BookmarkState = () => ({
- bookmarkListByUser: new Map(),
-});
-
-const getters = {
- bookmarkListByUser:
- (state: BookmarkState) =>
- (userId: PrincipalId): Bookmark[] => {
- return state.bookmarkListByUser.get(userId) || [];
- },
- bookmarkByUserAndLink:
- (state: BookmarkState, getters: any) =>
- (userId: PrincipalId, link: string): Bookmark => {
- const list = getters["bookmarkListByUser"](userId);
- return (
- list.find((item: Bookmark) => item.link == link) ||
- (unknown("BOOKMARK") as Bookmark)
- );
- },
-};
-
-const actions = {
- async fetchBookmarkListByUser(
- { commit, rootGetters }: any,
- userId: PrincipalId
- ) {
- // API only returns bookmark for the requesting user.
- // User info is retrieved from the context.
- const data = (await axios.get(`/api/bookmark/user/${userId}`)).data;
- const bookmarkList = data.data.map((bookmark: ResourceObject) => {
- return convert(bookmark, data.included, rootGetters);
- });
- commit("setBookmarkListByPrincipalId", { userId, bookmarkList });
- return bookmarkList;
- },
-
- async createBookmark(
- { commit, rootGetters }: any,
- newBookmark: BookmarkCreate
- ) {
- const data = (
- await axios.post(`/api/bookmark`, {
- data: {
- type: "bookmark",
- attributes: newBookmark,
- },
- })
- ).data;
- const createdBookmark = convert(data.data, data.included, rootGetters);
-
- commit("appendBookmark", createdBookmark);
-
- return createdBookmark;
- },
-
- async deleteBookmark({ commit }: any, bookmark: Bookmark) {
- await axios.delete(`/api/bookmark/${bookmark.id}`);
-
- commit("deleteBookmark", bookmark);
- },
-};
-
-const mutations = {
- setBookmarkListByPrincipalId(
- state: BookmarkState,
- {
- userId,
- bookmarkList,
- }: {
- userId: PrincipalId;
- bookmarkList: Bookmark[];
- }
- ) {
- state.bookmarkListByUser.set(userId, bookmarkList);
- },
-
- appendBookmark(state: BookmarkState, bookmark: Bookmark) {
- const list = state.bookmarkListByUser.get(bookmark.creator.id);
- if (list) {
- list.push(bookmark);
- } else {
- state.bookmarkListByUser.set(bookmark.creator.id, [bookmark]);
- }
- },
-
- deleteBookmark(state: BookmarkState, bookmark: Bookmark) {
- const list = state.bookmarkListByUser.get(bookmark.creator.id);
- if (list) {
- const i = list.findIndex((item: Bookmark) => item.id == bookmark.id);
- if (i != -1) {
- list.splice(i, 1);
- }
- }
- },
-};
-
-export default {
- namespaced: true,
- state,
- getters,
- actions,
- mutations,
-};
diff --git a/frontend/src/store/pinia-modules/bookmark.ts b/frontend/src/store/pinia-modules/bookmark.ts
new file mode 100644
index 0000000000..a6e64f79ed
--- /dev/null
+++ b/frontend/src/store/pinia-modules/bookmark.ts
@@ -0,0 +1,106 @@
+import { defineStore } from "pinia";
+import axios from "axios";
+import {
+ PrincipalId,
+ Bookmark,
+ BookmarkCreate,
+ BookmarkState,
+ ResourceObject,
+ unknown,
+} from "../../types";
+import { getPrincipalFromIncludedList } from "@/store/modules/principal";
+
+function convert(
+ bookmark: ResourceObject,
+ includedList: ResourceObject[]
+): Bookmark {
+ return {
+ ...(bookmark.attributes as Omit<Bookmark, "id" | "creator" | "updater">),
+ id: parseInt(bookmark.id),
+ creator: getPrincipalFromIncludedList(
+ bookmark.relationships!.creator.data,
+ includedList
+ ),
+ updater: getPrincipalFromIncludedList(
+ bookmark.relationships!.updater.data,
+ includedList
+ ),
+ };
+}
+
+export const useBookmarkStore = defineStore("bookmark", {
+ state: (): BookmarkState => ({
+ bookmarkList: new Map(),
+ }),
+
+ actions: {
+ async fetchBookmarkListByUser(userId: PrincipalId) {
+ // API only returns bookmark for the requesting user.
+ // User info is retrieved from the context.
+ const data = (await axios.get(`/api/bookmark/user/${userId}`)).data;
+ const bookmarkList = data.data.map((bookmark: ResourceObject) => {
+ return convert(bookmark, data.included);
+ });
+ this.setBookmarkListByPrincipalId({ userId, bookmarkList });
+ return bookmarkList;
+ },
+
+ async createBookmrk(newBookmark: BookmarkCreate) {
+ const data = (
+ await axios.post(`/api/bookmark`, {
+ data: {
+ type: "bookmark",
+ attributes: newBookmark,
+ },
+ })
+ ).data;
+ const createdBookmark = convert(data.data, data.included);
+
+ this.appendBookmark(createdBookmark);
+
+ return createdBookmark;
+ },
+
+ setBookmarkListByPrincipalId({
+ userId,
+ bookmarkList,
+ }: {
+ userId: PrincipalId;
+ bookmarkList: Bookmark[];
+ }) {
+ this.bookmarkList.set(userId, bookmarkList);
+ },
+
+ appendBookmark(bookmark: Bookmark) {
+ const list = this.bookmarkList.get(bookmark.creator.id);
+ if (list) {
+ list.push(bookmark);
+ } else {
+ this.bookmarkList.set(bookmark.creator.id, [bookmark]);
+ }
+ },
+
+ async deleteBookmark(bookmark: Bookmark) {
+ await axios.delete(`/api/bookmark/${bookmark.id}`);
+
+ const list = this.bookmarkList.get(bookmark.creator.id);
+ if (list) {
+ const i = list.findIndex((item: Bookmark) => item.id == bookmark.id);
+ if (i != -1) {
+ list.splice(i, 1);
+ }
+ }
+ },
+
+ bookmarkListByUser(userId: PrincipalId): Bookmark[] {
+ return this.bookmarkList.get(userId) || [];
+ },
+ bookmarkByUserAndLink(userId: PrincipalId, link: string): Bookmark {
+ const list = this.bookmarkListByUser(userId);
+ return (
+ list.find((item: Bookmark) => item.link == link) ||
+ (unknown("BOOKMARK") as Bookmark)
+ );
+ },
+ },
+});
diff --git a/frontend/src/store/pinia-modules/index.ts b/frontend/src/store/pinia-modules/index.ts
index 359f9fbe93..136dc4931e 100644
--- a/frontend/src/store/pinia-modules/index.ts
+++ b/frontend/src/store/pinia-modules/index.ts
@@ -1,4 +1,5 @@
export * from "./actuator";
+export * from "./bookmark";
export * from "./command";
export * from "./debug";
export * from "./label";
diff --git a/frontend/src/types/store.ts b/frontend/src/types/store.ts
index 2a22911cd3..4a0b0a197b 100644
--- a/frontend/src/types/store.ts
+++ b/frontend/src/types/store.ts
@@ -48,7 +48,6 @@ import { ConnectionAtom, ConnectionContext } from "./sqlEditor";
import { TabInfo } from "./tab";
import instanceStore from "../store/modules/instance";
import sqlEditorStore from "../store/modules/sqlEditor";
-import tabStore from "../store/modules/tab";
import sheetStore from "../store/modules/sheet";
export interface ActuatorState {
@@ -77,7 +76,7 @@ export interface PrincipalState {
}
export interface BookmarkState {
- bookmarkListByUser: Map<PrincipalId, Bookmark[]>;
+ bookmarkList: Map<PrincipalId, Bookmark[]>;
}
export interface ActivityState {
@@ -212,9 +211,6 @@ export interface TabState {
tabList: TabInfo[];
currentTabId: string;
}
-export type TabGetters = typeof tabStore.getters;
-export type TabActions = typeof tabStore.actions;
-export type TabMutations = typeof tabStore.mutations;
export interface DeploymentState {
deploymentConfigByProjectId: Map<ProjectId, DeploymentConfig>;