Skip to content

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

Add notification store

Tianzhou Chenae427cc0b783
diff --git a/frontend/package.json b/frontend/package.json
index c952b733e6..2a1ecf3b9c 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -8,6 +8,7 @@
},
"dependencies": {
"@tailwindcss/forms": "^0.2.1",
+ "@types/uuid": "^8.3.0",
"autoprefixer": "^9",
"axios": "^0.20.0",
"faker": "^5.1.0",
@@ -19,6 +20,7 @@
"postcss-focus-visible": "^5.0.0",
"slug": "^4.0.2",
"tailwindcss": "npm:@tailwindcss/postcss7-compat",
+ "uuid": "^8.3.2",
"vue": "^3.0.0",
"vue-router": "^4.0.0-0",
"vuex": "^4.0.0-beta.4"
diff --git a/frontend/src/store/index.ts b/frontend/src/store/index.ts
index b529f3417d..1bb344151a 100644
--- a/frontend/src/store/index.ts
+++ b/frontend/src/store/index.ts
@@ -1,4 +1,6 @@
import { createStore, Store } from "vuex";
+
+// Following states are persisted in database
import activity from "./modules/activity";
import bookmark from "./modules/bookmark";
import project from "./modules/project";
@@ -9,8 +11,13 @@ import repository from "./modules/repository";
import auth from "./modules/auth";
import group from "./modules/group";
import pipeline from "./modules/pipeline";
+
+// Following states are persisted in local storage
import uistate from "./modules/uistate";
+
+// Following states are only stored in memory
import router from "./modules/router";
+import notification from "./modules/notification";
const debug = process.env.NODE_ENV !== "production";
@@ -28,6 +35,7 @@ export const store: Store<any> = createStore({
pipeline,
uistate,
router,
+ notification,
},
strict: debug,
});
diff --git a/frontend/src/store/modules/notification.ts b/frontend/src/store/modules/notification.ts
new file mode 100644
index 0000000000..2cb8b04be8
--- /dev/null
+++ b/frontend/src/store/modules/notification.ts
@@ -0,0 +1,75 @@
+import {
+ Notification,
+ NewNotification,
+ NotificationFilter,
+ NotificationState,
+} from "../../types";
+import { v1 as uuidv1 } from "uuid";
+
+const state: () => NotificationState = () => ({
+ notificationByModule: new Map(),
+});
+
+function findNotification(
+ state: NotificationState,
+ filter: NotificationFilter
+): Notification | null {
+ const list = state.notificationByModule.get(filter.module);
+ if (list) {
+ for (const notification of list) {
+ if (notification.action == filter.action) {
+ return notification;
+ }
+ }
+ }
+ return null;
+}
+
+const getters = {};
+
+const actions = {
+ pushNotification({ commit }: any, newNotification: NewNotification) {
+ const notification: Notification = {
+ id: uuidv1(),
+ createdTs: Date.now(),
+ ...newNotification,
+ };
+ commit("appendNotification", notification);
+ },
+ peekNotification({ state }: any, filter: NotificationFilter) {
+ return findNotification(state, filter);
+ },
+ popNotification({ state, commit }: any, filter: NotificationFilter) {
+ const notification = findNotification(state, filter);
+ commit("removeNotification", notification);
+ return notification;
+ },
+};
+
+const mutations = {
+ appendNotification(state: NotificationState, notification: Notification) {
+ const list = state.notificationByModule.get(notification.module);
+ if (list) {
+ list.push(notification);
+ } else {
+ state.notificationByModule.set(notification.module, [notification]);
+ }
+ },
+ removeNotification(state: NotificationState, notification: Notification) {
+ const list = state.notificationByModule.get(notification.module);
+ if (list) {
+ const i = list.indexOf(notification);
+ if (i > -1) {
+ list.splice(i, 1);
+ }
+ }
+ },
+};
+
+export default {
+ namespaced: true,
+ state,
+ getters,
+ actions,
+ mutations,
+};
diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.ts
index 55bd11839d..791f223906 100644
--- a/frontend/src/types/bytebase.ts
+++ b/frontend/src/types/bytebase.ts
@@ -19,7 +19,7 @@ export type DataSourceId = {
instanceId: InstanceId;
};
-// Models
+// Persistent State Models
export type User = ResourceObject & {};
export type NewUser = Omit<User, "id">;
@@ -121,6 +121,29 @@ export type SignupInfo = Omit<
"id"
>;
+// UI State Models
+export type RouterSlug = {
+ pipelineId?: PipelineId;
+ instanceSlug?: string;
+};
+
+export type Notification = {
+ id: string;
+ createdTs: number;
+ module: string;
+ action: string;
+ userInfo?: any;
+};
+
+// "id" and "createdTs" is auto generated upon the notification store
+// receives.
+export type NewNotification = Omit<Notification, "id" | "createdTs">;
+
+export type NotificationFilter = {
+ module: string;
+ action: string;
+};
+
// Store
export interface AuthState {
currentUser: User | null;
@@ -166,8 +189,6 @@ export interface RepositoryState {
repositoryByProject: Map<ProjectId, Repository>;
}
-// UI
-export interface RouterSlug {
- pipelineId?: PipelineId;
- instanceSlug?: string;
+export interface NotificationState {
+ notificationByModule: Map<string, Notification[]>;
}
diff --git a/frontend/yarn.lock b/frontend/yarn.lock
index f930a00470..69166a2600 100644
--- a/frontend/yarn.lock
+++ b/frontend/yarn.lock
@@ -279,6 +279,11 @@
resolved "https://registry.yarnpkg.com/@types/slug/-/slug-0.9.1.tgz#16dbf8b77d73e0a09ce51a96400878f33806ab32"
integrity sha512-zR/u8WFQ4/6uCIikjI00a5uB084XjgEGNRAvM4a1BL39Bw9yEiDQFiPS2DgJ8lPDkR2Qd/vZ26dCR9XqlKbDqQ==
+"@types/uuid@^8.3.0":
+ version "8.3.0"
+ resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.0.tgz#215c231dff736d5ba92410e6d602050cce7e273f"
+ integrity sha512-eQ9qFW/fhfGJF8WKHGEHZEyVWfZxrT+6CLIJGBcZPfxUh/+BnEj+UCGYMlr9qZuX/2AltsvwrGqp0LhEW8D0zQ==
+
version "3.0.0"
resolved "https://registry.yarnpkg.com/@vue/compiler-core/-/compiler-core-3.0.0.tgz#25e4f079cf6c39f83bad23700f814c619105a0f2"
@@ -2494,6 +2499,11 @@ util-deprecate@^1.0.2:
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
integrity sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=
+uuid@^8.3.2:
+ version "8.3.2"
+ resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
+ integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==
+
vary@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"