Skip to content

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

feat: sidebar recent database list (#7982)

Liu Ji2fe2eb32ff66
diff --git a/frontend/src/components/DatabaseListSidePanel.vue b/frontend/src/components/DatabaseListSidePanel.vue
index afae6e1716..cb87dc7578 100644
--- a/frontend/src/components/DatabaseListSidePanel.vue
+++ b/frontend/src/components/DatabaseListSidePanel.vue
@@ -1,31 +1,21 @@
<template>
- <template v-for="(env, index) in databaseListByEnvironment" :key="index">
- <BBOutline
- :id="env.id"
- :title="env.name"
- :item-list="env.childList"
- :allow-collapse="false"
- :outline-item-class="'pt-0.5 pb-0.5'"
- />
- </template>
- <template v-for="(prj, index) in tenantDatabaseListByProject" :key="index">
- <BBOutline
- :id="prj.id"
- :title="prj.name"
- :item-list="prj.childList"
- :allow-collapse="false"
- :outline-item-class="'pt-0.5 pb-0.5'"
- />
- </template>
+ <BBOutline
+ id="bb.recent-databases"
+ :title="$t('database.recent')"
+ :item-list="recentDatabaseItemList"
+ :allow-collapse="false"
+ outline-item-class="pt-0.5 pb-0.5"
+ />
</template>
<script lang="ts" setup>
import { Action, defineAction, useRegisterActions } from "@bytebase/vue-kbar";
-import { cloneDeep } from "lodash-es";
-import { computed, h, watchEffect } from "vue";
+import { useDebounce, useStorage } from "@vueuse/core";
+import { cloneDeep, uniqBy } from "lodash-es";
+import { computed, h, watch, watchEffect } from "vue";
import { useI18n } from "vue-i18n";
-import { useRouter } from "vue-router";
-import type { BBOutlineItem } from "@/bbkit/types";
+import { useRoute, useRouter } from "vue-router";
+import { BBOutline, type BBOutlineItem } from "@/bbkit";
import {
useEnvironmentV1List,
useDatabaseV1Store,
@@ -34,7 +24,11 @@ import {
useDBGroupStore,
useProjectV1ListByCurrentUser,
} from "@/store";
-import { DEFAULT_PROJECT_V1_NAME, UNKNOWN_USER_NAME } from "@/types";
+import {
+ DEFAULT_PROJECT_V1_NAME,
+ UNKNOWN_ID,
+ UNKNOWN_USER_NAME,
+} from "@/types";
import { State } from "@/types/proto/v1/common";
import {
PolicyResourceType,
@@ -47,6 +41,9 @@ import {
projectV1Slug,
isDatabaseV1Accessible,
extractProjectResourceName,
+ uidFromSlug,
+ keyBy,
+ extractUserUID,
} from "@/utils";
import DatabaseGroupIcon from "./DatabaseGroupIcon.vue";
import EngineIcon from "./Icon/EngineIcon.vue";
@@ -59,11 +56,78 @@ const currentUserV1 = useCurrentUserV1();
const rawEnvironmentList = useEnvironmentV1List();
const { projectList } = useProjectV1ListByCurrentUser();
+type RecentVisitDatabase = {
+ uid: string;
+};
+
+const useRecentDatabaseList = () => {
+ const me = useCurrentUserV1();
+ const STORAGE_KEY_PREFIX = "bb.ui.recent-database-list";
+ const MAX_HISTORY = 10;
+ // The format of storage key if {HARDCODED_PREFIX}.#{USER_UID}
+ // to provide separate storage for each user in a same browser.
+ const KEY = `${STORAGE_KEY_PREFIX}.#${extractUserUID(me.value.name)}`;
+
+ const route = useRoute();
+ const recentList = useStorage(KEY, [] as RecentVisitDatabase[]);
+ const uid = computed(() => {
+ if (route.matched.length === 0) {
+ return undefined;
+ }
+ if (route.name === "workspace.database.detail") {
+ const slug = route.params.databaseSlug;
+ if (!slug || typeof slug !== "string") {
+ return undefined;
+ }
+ const uid = uidFromSlug(slug);
+ if (!uid || uid === String(UNKNOWN_ID)) {
+ return undefined;
+ }
+ return uid;
+ }
+ return undefined;
+ });
+
+ watch(
+ // Debounce the listener so we can skip internal immediate redirection
+ useDebounce(uid, 50),
+ (uid) => {
+ if (!uid) return;
+ if (uid === String(UNKNOWN_ID)) return;
+ const list = [...recentList.value];
+ const index = list.findIndex((item) => {
+ return item.uid === uid;
+ });
+ if (index >= 0) {
+ // current page exists in the history already
+ // pull it out before next step
+ list.splice(index, 1);
+ }
+ // then prepend the latest item to the queue
+ list.unshift({ uid });
+
+ // ensure the queue's length
+ // should be no more than (MAX_HISTORY)
+ while (list.length > MAX_HISTORY) {
+ list.pop();
+ }
+ recentList.value = uniqBy(list, (item) => item.uid);
+ },
+ {
+ immediate: true,
+ }
+ );
+
+ return recentList;
+};
+
// Reserve the environment list, put "Prod" to the top.
const environmentList = computed(() =>
cloneDeep(rawEnvironmentList.value).reverse()
);
+const recentDatabaseList = useRecentDatabaseList();
+
// Prepare policy list for checking if user has access to the database.
const preparePolicyList = () => {
usePolicyV1Store().fetchPolicies({
@@ -97,6 +161,26 @@ const databaseList = computed(() => {
);
});
+const recentDatabaseItemList = computed(() => {
+ const databaseMap = keyBy(databaseList.value, (db) => db.uid);
+ const recentDatabaseItemList: BBOutlineItem[] = [];
+ recentDatabaseList.value.forEach((item) => {
+ const db = databaseMap.get(item.uid);
+ if (db) {
+ recentDatabaseItemList.push({
+ id: `bb.database.${db.uid}`,
+ name: `${db.databaseName} (${db.instanceEntity.title})`,
+ link: `/db/${databaseV1Slug(db)}`,
+ prefix: h(EngineIcon, {
+ class: "shrink-0",
+ engine: db.instanceEntity.engine,
+ }),
+ });
+ }
+ });
+ return recentDatabaseItemList;
+});
+
const databaseListByEnvironment = computed(() => {
const envToDbMap: Map<string, BBOutlineItem[]> = new Map();
for (const environment of environmentList.value) {
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 636f7906d4..dd1f974e49 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -1437,7 +1437,8 @@
"transfer-database-from-to": "Transfer database from source project to target project",
"classification": {
"self": "Classification"
- }
+ },
+ "recent": "Recent"
},
"repository": {
"our-webhook-link": "The webhook created by Bytebase can be found at {webhookLink}.",
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index 5bc278875c..7a70d1a764 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -1436,7 +1436,8 @@
"transfer-database-from-to": "Transferir la base de datos del proyecto de origen al proyecto de destino",
"classification": {
"self": "Clasificación"
- }
+ },
+ "recent": "Reciente"
},
"repository": {
"branch-observe-file-change": "La rama donde Bytebase observa el cambio de archivo.",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 56ebcd1be8..ec50ea472f 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -1432,7 +1432,8 @@
"transfer-database-from-to": "将数据库从源项目转移到目标项目",
"classification": {
"self": "分类分级"
- }
+ },
+ "recent": "最近访问"
},
"repository": {
"branch-observe-file-change": "Bytebase 跟踪文件变更的分支。",
diff --git a/frontend/src/utils/collections.ts b/frontend/src/utils/collections.ts
index 1e69384466..b29807e2f2 100644
--- a/frontend/src/utils/collections.ts
+++ b/frontend/src/utils/collections.ts
@@ -10,3 +10,11 @@ export const groupBy = <T, K>(
return map;
}, new Map<K, T[]>());
};
+
+export const keyBy = <T, K>(array: T[], keyOf: (item: T) => K): Map<K, T> => {
+ return array.reduce((map, item) => {
+ const key = keyOf(item);
+ map.set(key, item);
+ return map;
+ }, new Map<K, T>());
+};