Skip to content

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

refactor: kbar actions (#9531)

Liu Jia8272e2b5c37
diff --git a/frontend/src/components/KBar/KBarWrapper.vue b/frontend/src/components/KBar/KBarWrapper.vue
index 9bc887a65c..b25921484c 100644
--- a/frontend/src/components/KBar/KBarWrapper.vue
+++ b/frontend/src/components/KBar/KBarWrapper.vue
@@ -87,14 +87,6 @@ export default defineComponent({
keywords: "navigation",
perform: () => router.push({ name: "workspace.home" }),
}),
- defineAction({
- id: "bb.navigation.anomaly-center",
- name: "Anomaly Center",
- shortcut: ["g", "a", "c"],
- section: t("kbar.navigation"),
- keywords: "navigation",
- perform: () => router.push({ name: "workspace.anomaly-center" }),
- }),
]);
return {
diff --git a/frontend/src/components/KBar/useDatabaseActions.ts b/frontend/src/components/KBar/useDatabaseActions.ts
new file mode 100644
index 0000000000..0b52ba838f
--- /dev/null
+++ b/frontend/src/components/KBar/useDatabaseActions.ts
@@ -0,0 +1,115 @@
+import { Action, defineAction, useRegisterActions } from "@bytebase/vue-kbar";
+import { computed, unref } from "vue";
+import { useI18n } from "vue-i18n";
+import { useRouter } from "vue-router";
+import {
+ useCurrentUserV1,
+ useDatabaseV1Store,
+ useEnvironmentV1List,
+} from "@/store";
+import {
+ ComposedDatabase,
+ ComposedProject,
+ DEFAULT_PROJECT_V1_NAME,
+ MaybeRef,
+} from "@/types";
+import { State } from "@/types/proto/v1/common";
+import {
+ isDatabaseV1Accessible,
+ groupBy,
+ environmentV1Name,
+ databaseV1Slug,
+ sortDatabaseV1List,
+} from "@/utils";
+
+export const useDatabaseActions = (
+ databaseList: MaybeRef<ComposedDatabase[]>
+) => {
+ const { t } = useI18n();
+ const router = useRouter();
+ const me = useCurrentUserV1();
+ const environmentList = useEnvironmentV1List();
+
+ const accessibleDatabaseList = computed(() => {
+ return unref(databaseList)
+ .filter((db) => db.syncState === State.ACTIVE)
+ .filter((db) => isDatabaseV1Accessible(db, me.value));
+ });
+
+ const sortedDatabaseList = computed(() => {
+ return sortDatabaseV1List(accessibleDatabaseList.value);
+ });
+
+ const databaseListByEnvironment = computed(() => {
+ const databasesByEnv = groupBy(
+ sortedDatabaseList.value,
+ (db) => db.effectiveEnvironment
+ );
+
+ return environmentList.value
+ .filter((environment) => {
+ const group = databasesByEnv.get(environment.name) ?? [];
+ return group.length > 0;
+ })
+ .map((environment) => {
+ const databases = databasesByEnv.get(environment.name)!;
+ return {
+ id: `bb.env.${environment.uid}`,
+ name: environmentV1Name(environment),
+ children: databases.map((db) => ({
+ id: `bb.database.${db.uid}`,
+ name: `${db.databaseName} (${db.instanceEntity.title})`,
+ link: `/db/${databaseV1Slug(db)}`,
+ })),
+ };
+ });
+ });
+
+ const kbarActions = computed((): Action[] => {
+ const actions = databaseListByEnvironment.value.flatMap((group) =>
+ group.children!.map((item) =>
+ defineAction({
+ // `item.id` is namespaced already
+ // so here `id` looks like
+ // "bb.database.7001" for non-tenant databases
+ // "bb.project.3007.database.db3" for tenant databases
+ id: item.id,
+ section: t("common.databases"),
+ name: item.name,
+ // `group.name` is also a keyword to provide better search
+ // e.g. "blog" under "staged" now can be searched by "bl st"
+ // also "blog" under "HR system" (a project) can be searched by "bl hr"
+ keywords: `database db ${group.name}`,
+ data: {
+ tags: [group.name],
+ },
+ perform: () => {
+ router.push(item.link!);
+ },
+ })
+ )
+ );
+ return actions;
+ });
+ useRegisterActions(kbarActions);
+};
+
+export const useProjectDatabaseActions = (
+ project: MaybeRef<ComposedProject>
+) => {
+ const projectDatabaseList = computed(() => {
+ return useDatabaseV1Store().databaseListByProject(unref(project).name);
+ });
+ useDatabaseActions(projectDatabaseList);
+};
+
+export const useGlobalDatabaseActions = () => {
+ const me = useCurrentUserV1();
+ // Use this to make the list reactive when project is transferred.
+ const databaseList = computed(() => {
+ return useDatabaseV1Store()
+ .databaseListByUser(me.value)
+ .filter((db) => db.project !== DEFAULT_PROJECT_V1_NAME);
+ });
+ useDatabaseActions(databaseList);
+};
diff --git a/frontend/src/components/KBar/utils.ts b/frontend/src/components/KBar/utils.ts
index 3d23029f94..2f94fa1d23 100644
--- a/frontend/src/components/KBar/utils.ts
+++ b/frontend/src/components/KBar/utils.ts
@@ -1,4 +1,5 @@
import { Action, CompareFn } from "@bytebase/vue-kbar";
+import { findLastIndex } from "lodash-es";
const MAX_RANKING = Infinity;
@@ -6,6 +7,7 @@ export const ACTION_RANKINGS = [
"bb.recently_visited.",
"bb.quickaction.",
"bb.navigation.",
+ "bb.navigation.global.",
"bb.project.",
"bb.database.",
"bb.preferences.",
@@ -23,7 +25,7 @@ export const compareAction: CompareFn = (a, b) => {
};
function getActionRankingById(action: Action) {
- const rank = ACTION_RANKINGS.findIndex((prefix) =>
+ const rank = findLastIndex(ACTION_RANKINGS, (prefix) =>
action.id.startsWith(prefix)
);
// non-specified namespaces should always rank behind specified ones.
diff --git a/frontend/src/components/Project/ProjectSidebar.vue b/frontend/src/components/Project/ProjectSidebar.vue
index a587fc318d..aa206bb902 100644
--- a/frontend/src/components/Project/ProjectSidebar.vue
+++ b/frontend/src/components/Project/ProjectSidebar.vue
@@ -8,6 +8,7 @@
</template>
<script setup lang="ts">
+import { defineAction, useRegisterActions } from "@bytebase/vue-kbar";
import { computedAsync, useLocalStorage } from "@vueuse/core";
import { startCase } from "lodash-es";
import {
@@ -39,6 +40,7 @@ import {
} from "@/types";
import { TenantMode } from "@/types/proto/v1/project_service";
import { idFromSlug, projectSlugV1 } from "@/utils";
+import { useProjectDatabaseActions } from "../KBar/useDatabaseActions";
const projectHashList = [
"databases",
@@ -357,4 +359,41 @@ watch(
immediate: true,
}
);
+
+const navigationKbarActions = computed(() => {
+ const navigationItems = projectSidebarItemList.value.flatMap<{
+ path: ProjectHash;
+ title: string;
+ hide?: boolean;
+ }>((item) => {
+ if (item.children && item.children.length > 0) {
+ return item.children.map((child) => ({
+ path: child.path as ProjectHash,
+ title: child.title,
+ hide: child.hide,
+ }));
+ }
+ return [
+ {
+ path: item.path as ProjectHash,
+ title: item.title,
+ hide: item.hide,
+ },
+ ];
+ });
+
+ const actions = navigationItems.map((item) =>
+ defineAction({
+ id: `bb.navigation.project.${project.value.uid}.${item.path}`,
+ name: item.title,
+ section: t("kbar.navigation"),
+ keywords: [item.title.toLowerCase(), item.path].join(" "),
+ perform: () => onSelect(item.path),
+ })
+ );
+ return actions;
+});
+useRegisterActions(navigationKbarActions);
+
+useProjectDatabaseActions(project);
</script>
diff --git a/frontend/src/views/DashboardHeader.vue b/frontend/src/views/DashboardHeader.vue
index 7acfe16e3c..37278b895b 100644
--- a/frontend/src/views/DashboardHeader.vue
+++ b/frontend/src/views/DashboardHeader.vue
@@ -77,11 +77,12 @@ import { computed, reactive, watchEffect } from "vue";
import { useI18n } from "vue-i18n";
import { useRouter } from "vue-router";
import {
- useCurrentUser,
useSubscriptionV1Store,
useInboxV1Store,
+ useCurrentUserV1,
} from "@/store";
import { PlanType } from "@/types/proto/v1/subscription_service";
+import { extractUserUID } from "@/utils";
import BytebaseLogo from "../components/BytebaseLogo.vue";
import ProfileBrandingLogo from "../components/ProfileBrandingLogo.vue";
import ProfileDropdown from "../components/ProfileDropdown.vue";
@@ -113,13 +114,13 @@ const onClickSearchButton = () => {
handler.value.show();
};
-const currentUser = useCurrentUser();
+const me = useCurrentUserV1();
const { currentPlan } = storeToRefs(subscriptionStore);
const prepareInboxSummary = () => {
// It will also be called when user logout
- if (currentUser.value.id != UNKNOWN_ID) {
+ if (extractUserUID(me.value.name) !== String(UNKNOWN_ID)) {
inboxV1Store.fetchInboxSummary();
}
};
@@ -130,56 +131,24 @@ const inboxSummary = computed(() => {
return inboxV1Store.inboxSummary;
});
-const kbarActions = computed(() => [
- defineAction({
- id: "bb.navigation.projects",
- name: "Projects",
- shortcut: ["g", "p"],
- section: t("kbar.navigation"),
- keywords: "navigation",
- perform: () => router.push({ name: "workspace.project" }),
- }),
- defineAction({
- id: "bb.navigation.databases",
- name: "Databases",
- shortcut: ["g", "d"],
- section: t("kbar.navigation"),
- keywords: "navigation db",
- perform: () => router.push({ name: "workspace.database" }),
- }),
- defineAction({
- id: "bb.navigation.instances",
- name: "Instances",
- shortcut: ["g", "i"],
- section: t("kbar.navigation"),
- keywords: "navigation",
- perform: () => router.push({ name: "workspace.instance" }),
- }),
- defineAction({
- id: "bb.navigation.environments",
- name: "Environments",
- shortcut: ["g", "e"],
- section: t("kbar.navigation"),
- keywords: "navigation",
- perform: () => router.push({ name: "workspace.environment" }),
- }),
- defineAction({
- id: "bb.navigation.settings",
- name: "Settings",
- shortcut: ["g", "s"],
- section: t("kbar.navigation"),
- keywords: "navigation",
- perform: () => router.push({ name: "setting.workspace.member" }),
- }),
- defineAction({
- id: "bb.navigation.inbox",
- name: "Inbox",
- shortcut: ["g", "m"],
- section: t("kbar.navigation"),
- keywords: "navigation",
- perform: () => router.push({ name: "setting.inbox" }),
- }),
-]);
+const kbarActions = computed(() => {
+ return [
+ defineAction({
+ id: "bb.navigation.global.settings",
+ name: "Settings",
+ section: t("kbar.navigation"),
+ keywords: "navigation",
+ perform: () => router.push({ name: "setting.workspace.member" }),
+ }),
+ defineAction({
+ id: "bb.navigation.global.inbox",
+ name: "Inbox",
+ section: t("kbar.navigation"),
+ keywords: "navigation",
+ perform: () => router.push({ name: "setting.inbox" }),
+ }),
+ ];
+});
useRegisterActions(kbarActions);
const handleWantHelp = () => {
diff --git a/frontend/src/views/DashboardSidebar.vue b/frontend/src/views/DashboardSidebar.vue
index 8852527a0a..13e1916aeb 100644
--- a/frontend/src/views/DashboardSidebar.vue
+++ b/frontend/src/views/DashboardSidebar.vue
@@ -96,6 +96,7 @@
</template>
<script lang="ts" setup>
+import { Action, defineAction, useRegisterActions } from "@bytebase/vue-kbar";
import {
HomeIcon,
DatabaseIcon,
@@ -108,8 +109,10 @@ import {
SquareStackIcon,
} from "lucide-vue-next";
import { computed } from "vue";
-import { RouterLink, useRoute } from "vue-router";
+import { useI18n } from "vue-i18n";
+import { RouterLink, useRoute, useRouter } from "vue-router";
import BytebaseLogo from "@/components/BytebaseLogo.vue";
+import { useGlobalDatabaseActions } from "@/components/KBar/useDatabaseActions";
import {
useCurrentUserV1,
useCurrentUserIamPolicy,
@@ -117,8 +120,10 @@ import {
} from "@/store";
import { hasWorkspacePermissionV1 } from "../utils";
+const { t } = useI18n();
const currentUserV1 = useCurrentUserV1();
const route = useRoute();
+const router = useRouter();
// Only show sync schema if the user has permission to alter schema of at least one project.
const shouldShowSyncSchemaEntry = computed(() => {
@@ -147,4 +152,91 @@ const getRouteLinkClass = (prefix: string): string[] => {
}
return classes;
};
+
+const navigationKbarActions = computed(() => {
+ const actions: Action[] = [];
+ actions.push(
+ defineAction({
+ id: "bb.navigation.projects",
+ name: "Projects",
+ shortcut: ["g", "p"],
+ section: t("kbar.navigation"),
+ keywords: "navigation",
+ perform: () => router.push({ name: "workspace.project" }),
+ }),
+ defineAction({
+ id: "bb.navigation.databases",
+ name: "Databases",
+ shortcut: ["g", "d"],
+ section: t("kbar.navigation"),
+ keywords: "navigation db",
+ perform: () => router.push({ name: "workspace.database" }),
+ })
+ );
+
+ if (shouldShowInstanceEntry.value) {
+ actions.push(
+ defineAction({
+ id: "bb.navigation.instances",
+ name: "Instances",
+ shortcut: ["g", "i"],
+ section: t("kbar.navigation"),
+ keywords: "navigation",
+ perform: () => router.push({ name: "workspace.instance" }),
+ })
+ );
+ }
+ actions.push(
+ defineAction({
+ id: "bb.navigation.environments",
+ name: "Environments",
+ shortcut: ["g", "e"],
+ section: t("kbar.navigation"),
+ keywords: "navigation",
+ perform: () => router.push({ name: "workspace.environment" }),
+ })
+ );
+ if (shouldShowSyncSchemaEntry.value) {
+ actions.push(
+ defineAction({
+ id: "bb.navigation.sync-schema",
+ name: "Sync Schema",
+ shortcut: ["g", "s", "s"],
+ section: t("kbar.navigation"),
+ keywords: "sync schema",
+ perform: () => router.push({ name: "workspace.sync-schema" }),
+ })
+ );
+ }
+ actions.push(
+ defineAction({
+ id: "bb.navigation.slow-query",
+ name: "Slow Query",
+ section: t("kbar.navigation"),
+ shortcut: ["g", "s", "q"],
+ keywords: "slow query",
+ perform: () => router.push({ name: "workspace.slow-query" }),
+ }),
+ defineAction({
+ id: "bb.navigation.export-center",
+ name: "Export Center",
+ section: t("kbar.navigation"),
+ shortcut: ["g", "x", "c"],
+ keywords: "export center",
+ perform: () => router.push({ name: "workspace.export-center" }),
+ }),
+ defineAction({
+ id: "bb.navigation.anomaly-center",
+ name: "Anomaly Center",
+ shortcut: ["g", "a", "c"],
+ section: t("kbar.navigation"),
+ keywords: "anomaly center",
+ perform: () => router.push({ name: "workspace.anomaly-center" }),
+ })
+ );
+ return actions;
+});
+useRegisterActions(navigationKbarActions);
+
+useGlobalDatabaseActions();
</script>