Skip to content

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

refactor: re-implement inbox store module with pinia (#1085)

Liu Ji875702da87ac
diff --git a/frontend/src/components/InboxList.vue b/frontend/src/components/InboxList.vue
index 0bdf1d645f..9939acb668 100644
--- a/frontend/src/components/InboxList.vue
+++ b/frontend/src/components/InboxList.vue
@@ -52,7 +52,7 @@
</template>
<script lang="ts">
-import { computed, defineComponent, PropType } from "vue";
+import { defineComponent, PropType } from "vue";
import PrincipalAvatar from "../components/PrincipalAvatar.vue";
import {
ActivityIssueCommentCreatePayload,
@@ -65,13 +65,12 @@ import {
Activity,
Inbox,
} from "../types";
-import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { isEmpty } from "lodash-es";
import { issueActivityActionSentence } from "../utils";
import { useI18n } from "vue-i18n";
import dayjs from "dayjs";
-import { useCurrentUser } from "@/store";
+import { useCurrentUser, useInboxStore } from "@/store";
export default defineComponent({
name: "InboxList",
@@ -84,7 +83,7 @@ export default defineComponent({
},
setup() {
const { t } = useI18n();
- const store = useStore();
+ const inboxStore = useInboxStore();
const router = useRouter();
const currentUser = useCurrentUser();
@@ -196,18 +195,15 @@ export default defineComponent({
const clickInbox = (inbox: Inbox) => {
if (inbox.status == "UNREAD") {
- store
- .dispatch("inbox/patchInbox", {
+ inboxStore
+ .patchInbox({
inboxId: inbox.id,
inboxPatch: {
status: "READ",
},
})
.then(() => {
- store.dispatch(
- "inbox/fetchInboxSummaryByUser",
- currentUser.value.id
- );
+ inboxStore.fetchInboxSummaryByUser(currentUser.value.id);
});
}
const link = actionLink(inbox.activity);
diff --git a/frontend/src/store/modules/inbox.ts b/frontend/src/store/modules/inbox.ts
deleted file mode 100644
index 252ff6d73a..0000000000
--- a/frontend/src/store/modules/inbox.ts
+++ /dev/null
@@ -1,165 +0,0 @@
-import axios from "axios";
-import {
- Activity,
- Inbox,
- InboxId,
- InboxPatch,
- InboxState,
- InboxSummary,
- PrincipalId,
- ResourceIdentifier,
- ResourceObject,
- unknown,
-} from "../../types";
-import { useActivityStore } from "../pinia-modules";
-
-function convert(
- inbox: ResourceObject,
- includedList: ResourceObject[],
- rootGetters: any
-): Inbox {
- const activityId = (inbox.relationships!.activity.data as ResourceIdentifier)
- .id;
- let activity: Activity = unknown("ACTIVITY") as Activity;
- activity.id = parseInt(activityId);
-
- const activityStore = useActivityStore();
- for (const item of includedList || []) {
- if (item.type == "activity" && item.id == activityId) {
- activity = activityStore.convert(item, includedList);
- }
- }
-
- return {
- ...(inbox.attributes as Omit<Inbox, "id">),
- id: parseInt(inbox.id),
- activity,
- };
-}
-
-const state: () => InboxState = () => ({
- inboxListByUser: new Map(),
- inboxSummaryByUser: new Map(),
-});
-
-const getters = {
- inboxListByUser:
- (state: InboxState) =>
- (userId: PrincipalId): Inbox[] => {
- return state.inboxListByUser.get(userId) || [];
- },
-
- inboxSummaryByUser:
- (state: InboxState) =>
- (userId: PrincipalId): InboxSummary => {
- return (
- state.inboxSummaryByUser.get(userId) || {
- hasUnread: false,
- hasUnreadError: false,
- }
- );
- },
-};
-
-const actions = {
- async fetchInboxListByUser(
- { commit, rootGetters }: any,
- {
- userId,
- readCreatedAfterTs,
- }: { userId: PrincipalId; readCreatedAfterTs?: number }
- ) {
- let url = `/api/inbox/user/${userId}`;
- if (readCreatedAfterTs) {
- url += `?created=${readCreatedAfterTs}`;
- }
- const data = (await axios.get(url)).data;
- const inboxList = data.data.map((inbox: ResourceObject) => {
- return convert(inbox, data.included, rootGetters);
- });
-
- commit("setInboxListByUser", { userId, inboxList });
- return inboxList;
- },
-
- async fetchInboxSummaryByUser({ commit }: any, userId: PrincipalId) {
- const inboxSummary = (await axios.get(`/api/inbox/user/${userId}/summary`))
- .data;
-
- commit("setInboxSummaryByUser", { userId, inboxSummary });
- return inboxSummary;
- },
-
- async patchInbox(
- { commit, rootGetters }: any,
- { inboxId, inboxPatch }: { inboxId: InboxId; inboxPatch: InboxPatch }
- ) {
- const data = (
- await axios.patch(`/api/inbox/${inboxId}`, {
- data: {
- type: "inboxPatch",
- attributes: inboxPatch,
- },
- })
- ).data;
- const updatedInbox = convert(data.data, data.included, rootGetters);
-
- commit("updateInboxById", { inboxId, inbox: updatedInbox });
-
- return updatedInbox;
- },
-};
-
-const mutations = {
- setInboxListByUser(
- state: InboxState,
- {
- userId,
- inboxList,
- }: {
- userId: PrincipalId;
- inboxList: Inbox[];
- }
- ) {
- state.inboxListByUser.set(userId, inboxList);
- },
-
- setInboxSummaryByUser(
- state: InboxState,
- {
- userId,
- inboxSummary,
- }: {
- userId: PrincipalId;
- inboxSummary: InboxSummary;
- }
- ) {
- state.inboxSummaryByUser.set(userId, inboxSummary);
- },
-
- updateInboxById(
- state: InboxState,
- {
- inboxId,
- inbox,
- }: {
- inboxId: InboxId;
- inbox: Inbox;
- }
- ) {
- for (const [_, inboxList] of state.inboxListByUser) {
- const i = inboxList.findIndex((item: Inbox) => item.id == inboxId);
- if (i >= 0) {
- inboxList[i] = inbox;
- }
- }
- },
-};
-
-export default {
- namespaced: true,
- state,
- getters,
- actions,
- mutations,
-};
diff --git a/frontend/src/store/pinia-modules/inbox.ts b/frontend/src/store/pinia-modules/inbox.ts
new file mode 100644
index 0000000000..c7da40cdc8
--- /dev/null
+++ b/frontend/src/store/pinia-modules/inbox.ts
@@ -0,0 +1,129 @@
+import axios from "axios";
+import { defineStore } from "pinia";
+import {
+ Activity,
+ Inbox,
+ InboxId,
+ InboxPatch,
+ InboxState,
+ InboxSummary,
+ PrincipalId,
+ ResourceIdentifier,
+ ResourceObject,
+ unknown,
+} from "../../types";
+import { useActivityStore } from "./activity";
+
+function convert(inbox: ResourceObject, includedList: ResourceObject[]): Inbox {
+ const activityId = (inbox.relationships!.activity.data as ResourceIdentifier)
+ .id;
+ let activity: Activity = unknown("ACTIVITY") as Activity;
+ activity.id = parseInt(activityId);
+
+ const activityStore = useActivityStore();
+ for (const item of includedList || []) {
+ if (item.type == "activity" && item.id == activityId) {
+ activity = activityStore.convert(item, includedList);
+ }
+ }
+
+ return {
+ ...(inbox.attributes as Omit<Inbox, "id">),
+ id: parseInt(inbox.id),
+ activity,
+ };
+}
+
+export const useInboxStore = defineStore("inbox", {
+ state: (): InboxState => ({
+ inboxListByUser: new Map(),
+ inboxSummaryByUser: new Map(),
+ }),
+ actions: {
+ getInboxListByUser(userId: PrincipalId): Inbox[] {
+ return this.inboxListByUser.get(userId) || [];
+ },
+ getInboxSummaryByUser(userId: PrincipalId): InboxSummary {
+ return (
+ this.inboxSummaryByUser.get(userId) || {
+ hasUnread: false,
+ hasUnreadError: false,
+ }
+ );
+ },
+ setInboxListByUser({
+ userId,
+ inboxList,
+ }: {
+ userId: PrincipalId;
+ inboxList: Inbox[];
+ }) {
+ this.inboxListByUser.set(userId, inboxList);
+ },
+ setInboxSummaryByUser({
+ userId,
+ inboxSummary,
+ }: {
+ userId: PrincipalId;
+ inboxSummary: InboxSummary;
+ }) {
+ this.inboxSummaryByUser.set(userId, inboxSummary);
+ },
+ updateInboxById({ inboxId, inbox }: { inboxId: InboxId; inbox: Inbox }) {
+ for (const [_, inboxList] of this.inboxListByUser) {
+ const i = inboxList.findIndex((item: Inbox) => item.id == inboxId);
+ if (i >= 0) {
+ inboxList[i] = inbox;
+ }
+ }
+ },
+ async fetchInboxListByUser({
+ userId,
+ readCreatedAfterTs,
+ }: {
+ userId: PrincipalId;
+ readCreatedAfterTs?: number;
+ }) {
+ let url = `/api/inbox/user/${userId}`;
+ if (readCreatedAfterTs) {
+ url += `?created=${readCreatedAfterTs}`;
+ }
+ const data = (await axios.get(url)).data;
+ const inboxList = data.data.map((inbox: ResourceObject) => {
+ return convert(inbox, data.included);
+ });
+
+ this.setInboxListByUser({ userId, inboxList });
+ return inboxList;
+ },
+ async fetchInboxSummaryByUser(userId: PrincipalId) {
+ const inboxSummary = (
+ await axios.get(`/api/inbox/user/${userId}/summary`)
+ ).data;
+
+ this.setInboxSummaryByUser({ userId, inboxSummary });
+ return inboxSummary;
+ },
+ async patchInbox({
+ inboxId,
+ inboxPatch,
+ }: {
+ inboxId: InboxId;
+ inboxPatch: InboxPatch;
+ }) {
+ const data = (
+ await axios.patch(`/api/inbox/${inboxId}`, {
+ data: {
+ type: "inboxPatch",
+ attributes: inboxPatch,
+ },
+ })
+ ).data;
+ const updatedInbox = convert(data.data, data.included);
+
+ this.updateInboxById({ inboxId, inbox: updatedInbox });
+
+ return updatedInbox;
+ },
+ },
+});
diff --git a/frontend/src/store/pinia-modules/index.ts b/frontend/src/store/pinia-modules/index.ts
index 9d556cf6ef..0cfc78ddfc 100644
--- a/frontend/src/store/pinia-modules/index.ts
+++ b/frontend/src/store/pinia-modules/index.ts
@@ -11,6 +11,7 @@ export * from "./deployment";
export * from "./environment";
export * from "./gitlab";
export * from "./issueSubscriber";
+export * from "./inbox";
export * from "./instance";
export * from "./label";
export * from "./member";
diff --git a/frontend/src/views/DashboardHeader.vue b/frontend/src/views/DashboardHeader.vue
index 300518f810..14354446ea 100644
--- a/frontend/src/views/DashboardHeader.vue
+++ b/frontend/src/views/DashboardHeader.vue
@@ -191,11 +191,10 @@
import { defineAction, useRegisterActions } from "@bytebase/vue-kbar";
import { computed, reactive, watchEffect, defineComponent } from "vue";
import { useRouter } from "vue-router";
-import { useStore } from "vuex";
import { useI18n } from "vue-i18n";
import ProfileDropdown from "../components/ProfileDropdown.vue";
-import { InboxSummary, UNKNOWN_ID } from "../types";
+import { UNKNOWN_ID } from "../types";
import { brandingLogoSettingName } from "../types/setting";
import { isDBAOrOwner, isDev } from "../utils";
import { useLanguage } from "../composables/useLanguage";
@@ -204,6 +203,7 @@ import {
useAuthStore,
useSettingStore,
useSubscriptionStore,
+ useInboxStore,
} from "@/store";
import { storeToRefs } from "pinia";
@@ -216,8 +216,8 @@ export default defineComponent({
components: { ProfileDropdown },
setup() {
const { t, availableLocales } = useI18n();
- const store = useStore();
const authStore = useAuthStore();
+ const inboxStore = useInboxStore();
const settingStore = useSettingStore();
const subscriptionStore = useSubscriptionStore();
const router = useRouter();
@@ -255,14 +255,14 @@ export default defineComponent({
const prepareInboxSummary = () => {
// It will also be called when user logout
if (currentUser.value.id != UNKNOWN_ID) {
- store.dispatch("inbox/fetchInboxSummaryByUser", currentUser.value.id);
+ inboxStore.fetchInboxSummaryByUser(currentUser.value.id);
}
};
watchEffect(prepareInboxSummary);
- const inboxSummary = computed((): InboxSummary => {
- return store.getters["inbox/inboxSummaryByUser"](currentUser.value.id);
+ const inboxSummary = computed(() => {
+ return inboxStore.getInboxSummaryByUser(currentUser.value.id);
});
const switchToOwner = () => {
diff --git a/frontend/src/views/Inbox.vue b/frontend/src/views/Inbox.vue
index c54f4316a2..e3d4a96c8f 100644
--- a/frontend/src/views/Inbox.vue
+++ b/frontend/src/views/Inbox.vue
@@ -35,11 +35,10 @@
<script lang="ts">
import { reactive, watchEffect } from "vue";
-import { useStore } from "vuex";
import InboxList from "../components/InboxList.vue";
import { Inbox, UNKNOWN_ID } from "../types";
import { useRouter } from "vue-router";
-import { useCurrentUser } from "@/store";
+import { useCurrentUser, useInboxStore } from "@/store";
// We alway fetch all "UNREAD" items. But for "READ" items, by default, we only fetch the most recent 7 days.
// And each time clicking "View older" will extend 7 days further.
@@ -55,7 +54,7 @@ export default {
name: "Inbox",
components: { InboxList },
setup() {
- const store = useStore();
+ const inboxStore = useInboxStore();
const router = useRouter();
const state = reactive<LocalState>({
@@ -71,8 +70,8 @@ export default {
const prepareInboxList = () => {
// It will also be called when user logout
if (currentUser.value.id != UNKNOWN_ID) {
- store
- .dispatch("inbox/fetchInboxListByUser", {
+ inboxStore
+ .fetchInboxListByUser({
userId: currentUser.value.id,
readCreatedAfterTs: state.readCreatedAfterTs,
})
@@ -98,8 +97,8 @@ export default {
const inboxList = state.unreadList.map((item) => item);
inboxList.forEach((item: Inbox) => {
- store
- .dispatch("inbox/patchInbox", {
+ inboxStore
+ .patchInbox({
inboxId: item.id,
inboxPatch: {
status: "READ",
@@ -114,10 +113,7 @@ export default {
}
count--;
if (count == 0) {
- store.dispatch(
- "inbox/fetchInboxSummaryByUser",
- currentUser.value.id
- );
+ inboxStore.fetchInboxSummaryByUser(currentUser.value.id);
}
state.readList.push(item);
});
diff --git a/frontend/src/views/SqlEditor/EditorHeader.vue b/frontend/src/views/SqlEditor/EditorHeader.vue
index 25e75e6b5f..4f5510eaaa 100644
--- a/frontend/src/views/SqlEditor/EditorHeader.vue
+++ b/frontend/src/views/SqlEditor/EditorHeader.vue
@@ -97,12 +97,15 @@ import { computed, reactive, watchEffect, defineComponent } from "vue";
import { useRouter } from "vue-router";
import { useI18n } from "vue-i18n";
import { useLocalStorage } from "@vueuse/core";
-import { useStore } from "vuex";
-
import ProfileDropdown from "@/components/ProfileDropdown.vue";
-import { InboxSummary, UNKNOWN_ID } from "@/types";
+import { UNKNOWN_ID } from "@/types";
import { isDBAOrOwner, isDev } from "@/utils";
-import { hasFeature, useCurrentUser, useSubscriptionStore } from "@/store";
+import {
+ hasFeature,
+ useCurrentUser,
+ useInboxStore,
+ useSubscriptionStore,
+} from "@/store";
import { storeToRefs } from "pinia";
interface LocalState {
@@ -114,7 +117,7 @@ export default defineComponent({
components: { ProfileDropdown },
setup() {
const { t, availableLocales, locale } = useI18n();
- const store = useStore();
+ const inboxStore = useInboxStore();
const subscriptionStore = useSubscriptionStore();
const router = useRouter();
@@ -140,14 +143,14 @@ export default defineComponent({
const prepareInboxSummary = () => {
// It will also be called when user logout
if (currentUser.value.id != UNKNOWN_ID) {
- store.dispatch("inbox/fetchInboxSummaryByUser", currentUser.value.id);
+ inboxStore.fetchInboxSummaryByUser(currentUser.value.id);
}
};
watchEffect(prepareInboxSummary);
- const inboxSummary = computed((): InboxSummary => {
- return store.getters["inbox/inboxSummaryByUser"](currentUser.value.id);
+ const inboxSummary = computed(() => {
+ return inboxStore.getInboxSummaryByUser(currentUser.value.id);
});
const kbarActions = computed(() => [