Skip to content

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

feat: show guide dialog (#1630)

boojackf7b3d9cac3ad
diff --git a/frontend/src/bbkit/BBDialog.vue b/frontend/src/bbkit/BBDialog.vue
index d92115f5bf..c541f983b5 100644
--- a/frontend/src/bbkit/BBDialog.vue
+++ b/frontend/src/bbkit/BBDialog.vue
@@ -20,6 +20,7 @@
<button
type="button"
class="btn-primary py-2 px-4"
+ data-label="bb-modal-confirm-button"
@click.prevent="onPositiveClick"
>
{{ positiveText || $t("common.confirm") }}
diff --git a/frontend/src/components/AlterSchemaPrepForm/GhostDialog.vue b/frontend/src/components/AlterSchemaPrepForm/GhostDialog.vue
index 03297b5438..096e6db054 100644
--- a/frontend/src/components/AlterSchemaPrepForm/GhostDialog.vue
+++ b/frontend/src/components/AlterSchemaPrepForm/GhostDialog.vue
@@ -3,6 +3,7 @@
ref="dialog"
:title="$t('issue.migration-mode.title')"
:positive-text="$t('common.next')"
+ data-label="bb-migration-mode-dialog"
>
<div class="w-[28rem] space-y-4 pl-8 pr-2 pb-4">
<div class="flex items-start space-x-2">
diff --git a/frontend/src/components/DatabaseTable.vue b/frontend/src/components/DatabaseTable.vue
index 56174d4746..d17d934241 100644
--- a/frontend/src/components/DatabaseTable.vue
+++ b/frontend/src/components/DatabaseTable.vue
@@ -7,6 +7,7 @@
:right-bordered="bordered"
:top-bordered="bordered"
:bottom-bordered="bordered"
+ data-label="bb-database-table"
@click-row="clickDatabase"
>
<template #body="{ rowData: database }: { rowData: Database }">
diff --git a/frontend/src/components/QuickActionPanel.vue b/frontend/src/components/QuickActionPanel.vue
index 9e4e5fd765..78039ed3af 100644
--- a/frontend/src/components/QuickActionPanel.vue
+++ b/frontend/src/components/QuickActionPanel.vue
@@ -89,7 +89,11 @@
v-if="quickAction == 'quickaction.bb.database.schema.update'"
class="flex flex-col items-center w-28"
>
- <button class="btn-icon-primary p-3" @click.prevent="alterSchema">
+ <button
+ class="btn-icon-primary p-3"
+ data-label="bb-alter-schema-button"
+ @click.prevent="alterSchema"
+ >
<heroicons-outline:pencil-alt class="w-6 h-6" />
</button>
<h3 class="mt-1 text-center text-base font-normal text-main">
@@ -178,6 +182,7 @@
class="relative overflow-hidden"
:title="state.modalTitle"
:subtitle="state.modalSubtitle"
+ :data-label="`bb-${kebabCase(state.modalTitle)}-modal`"
@close="state.showModal = false"
>
<template v-if="state.quickActionType == 'quickaction.bb.project.create'">
@@ -246,6 +251,7 @@
<script lang="ts">
import { defineComponent, reactive, PropType, computed } from "vue";
import { useRouter } from "vue-router";
+import { kebabCase } from "lodash-es";
import ProjectCreate from "../components/ProjectCreate.vue";
import CreateInstanceForm from "../components/CreateInstanceForm.vue";
import AlterSchemaPrepForm from "./AlterSchemaPrepForm/";
@@ -444,6 +450,7 @@ export default defineComponent({
requestDatabase,
createEnvironment,
reorderEnvironment,
+ kebabCase,
};
},
});
diff --git a/frontend/src/main.ts b/frontend/src/main.ts
index 8deefff1af..bba855ac81 100644
--- a/frontend/src/main.ts
+++ b/frontend/src/main.ts
@@ -35,6 +35,7 @@ import dataSourceType from "./directives/data-source-type";
import App from "./App.vue";
import "./assets/css/inter.css";
import "./assets/css/tailwind.css";
+import "./plugins/guide/style.css";
console.debug("dev:", isDev());
console.debug("release:", isRelease());
diff --git a/frontend/src/plugins/guide/dialog.ts b/frontend/src/plugins/guide/dialog.ts
new file mode 100644
index 0000000000..edf2b5a796
--- /dev/null
+++ b/frontend/src/plugins/guide/dialog.ts
@@ -0,0 +1,127 @@
+import { getElementBounding } from "./utils";
+import * as storage from "./storage";
+import { merge } from "lodash-es";
+
+const getTargetElementBySelectors = (selectors: string[][]) => {
+ let targetElement = document.body;
+ for (const selector of selectors) {
+ try {
+ targetElement = document.body.querySelector(
+ selector.join(" ")
+ ) as HTMLElement;
+ } catch (error) {
+ // do nth
+ }
+
+ if (targetElement) {
+ break;
+ }
+ }
+ return targetElement;
+};
+
+export const showGuideDialog = async (guideStep: any) => {
+ removeGuideDialog();
+ const targetElement = await waitForTargetElement(guideStep.selectors);
+ if (targetElement) {
+ renderHighlightWrapper(targetElement);
+ renderGuideDialog(targetElement, guideStep.title, guideStep.description);
+ }
+};
+
+const renderHighlightWrapper = (targetElement: HTMLElement) => {
+ const highlightWrapper = document.createElement("div");
+ highlightWrapper.className = "bb-guide-highlight-wrapper";
+ document.body.appendChild(highlightWrapper);
+ const bounding = getElementBounding(targetElement);
+ highlightWrapper.style.top = `${bounding.top}px`;
+ highlightWrapper.style.left = `${bounding.left}px`;
+ highlightWrapper.style.width = `${bounding.width}px`;
+ highlightWrapper.style.height = `${bounding.height}px`;
+};
+
+const renderGuideDialog = (
+ targetElement: HTMLElement,
+ title: string,
+ description: string
+) => {
+ const guideDialogDiv = document.createElement("div");
+ guideDialogDiv.className = "bb-guide-dialog";
+ const bounding = getElementBounding(targetElement);
+ guideDialogDiv.style.top = `${bounding.top + bounding.height}px`;
+ guideDialogDiv.style.left = `${bounding.left}px`;
+ const titleElement = document.createElement("p");
+ titleElement.className = "bb-guide-title-text";
+ titleElement.innerText = title;
+ guideDialogDiv.appendChild(titleElement);
+ const descriptionElement = document.createElement("p");
+ descriptionElement.className = "bb-guide-description-text";
+ descriptionElement.innerText = description;
+ guideDialogDiv.appendChild(descriptionElement);
+
+ const buttonsContainer = document.createElement("div");
+ buttonsContainer.className = "bb-guide-btns-container";
+ const prevButton = document.createElement("button");
+ prevButton.className = "button";
+ prevButton.innerText = "Back";
+ const nextButton = document.createElement("button");
+ nextButton.className = "button";
+ nextButton.innerText = "Next";
+ buttonsContainer.appendChild(prevButton);
+ buttonsContainer.appendChild(nextButton);
+ guideDialogDiv.appendChild(buttonsContainer);
+
+ prevButton.onclick = () => {
+ // ...how to do prev?
+ };
+
+ nextButton.onclick = () => {
+ targetElement.click();
+ };
+
+ // TOOD(steven): support more action type: input value change...
+ targetElement.addEventListener("click", () => {
+ const { guide } = storage.get(["guide"]);
+ storage.set({
+ guide: merge(guide, {
+ stepIndex: (guide?.stepIndex ?? 0) + 1,
+ }),
+ });
+ storage.emitStorageChangedEvent();
+ });
+
+ document.body.appendChild(guideDialogDiv);
+};
+
+const waitForTargetElement = (selectors: string[][]): Promise<HTMLElement> => {
+ return new Promise((resolve) => {
+ let targetElement = getTargetElementBySelectors(selectors);
+ if (targetElement) {
+ return resolve(targetElement);
+ }
+
+ const observer = new MutationObserver(() => {
+ targetElement = getTargetElementBySelectors(selectors);
+ if (targetElement) {
+ observer.disconnect();
+ return resolve(targetElement);
+ }
+ });
+
+ observer.observe(document.body, {
+ childList: true,
+ subtree: true,
+ });
+ });
+};
+
+export const removeGuideDialog = () => {
+ document.body.querySelectorAll(".bb-guide-dialog")?.forEach((element) => {
+ element.remove();
+ });
+ document.body
+ .querySelectorAll(".bb-guide-highlight-wrapper")
+ ?.forEach((element) => {
+ element.remove();
+ });
+};
diff --git a/frontend/src/plugins/guide/index.ts b/frontend/src/plugins/guide/index.ts
index 30c2305c47..ed49e655f1 100644
--- a/frontend/src/plugins/guide/index.ts
+++ b/frontend/src/plugins/guide/index.ts
@@ -1,10 +1,17 @@
import initGuideListeners from "./listener";
+import * as storage from "./storage";
// initial guide listeners when window loaded
window.addEventListener(
"load",
() => {
- initGuideListeners();
+ const params = new URLSearchParams(window.location.search);
+ const paramGuide = params.get("guide");
+ const { guide: storageGuide } = storage.get(["guide"]);
+
+ if (paramGuide || storageGuide) {
+ initGuideListeners();
+ }
},
{
once: true,
diff --git a/frontend/src/plugins/guide/listener.ts b/frontend/src/plugins/guide/listener.ts
index c8f2789f52..7e62e4cdfe 100644
--- a/frontend/src/plugins/guide/listener.ts
+++ b/frontend/src/plugins/guide/listener.ts
@@ -1,43 +1,55 @@
import { isEqual } from "lodash-es";
import { fetchGuideDataWithName } from "./data";
+import { removeGuideDialog, showGuideDialog } from "./dialog";
import * as storage from "./storage";
+const refreshLocalStorageGuideData = async () => {
+ const params = new URLSearchParams(window.location.search);
+ const guideName = params.get("guide");
+ const { guide } = storage.get(["guide"]);
+
+ if (guideName && guideName !== guide?.name) {
+ const guideData = await fetchGuideDataWithName(guideName);
+
+ if (guideData) {
+ storage.set({
+ guide: {
+ name: guideName,
+ stepIndex: 0,
+ },
+ });
+ }
+ }
+};
+
+const tryToShowGuideDialog = async () => {
+ const { guide } = storage.get(["guide"]);
+
+ if (guide) {
+ const guideData = await fetchGuideDataWithName(guide.name);
+
+ // remove useless type guideData
+ guideData.steps = guideData.steps.filter(
+ (s: any) => s.type !== "setViewport" && s.type !== "navigate"
+ );
+
+ showGuideDialog(guideData.steps[guide.stepIndex]);
+ } else {
+ removeGuideDialog();
+ }
+};
+
// initLocationListener using mutation observer to detect DOM changes for mocking location changed.
const initLocationListener = () => {
let prevLocationString = "";
const observer = new MutationObserver(async () => {
+ await refreshLocalStorageGuideData();
const locationString = window.location.toString();
if (!isEqual(prevLocationString, locationString)) {
prevLocationString = locationString;
- let { guide } = storage.get(["guide"]);
-
- const params = new URLSearchParams(window.location.search);
- const guideName = params.get("guide");
-
- if (guideName && guideName !== guide?.name) {
- const guideData = await fetchGuideDataWithName(guideName);
-
- if (guideData) {
- const tempGuide = {
- name: guideName,
- startRoute: "",
- stepIndex: 0,
- };
-
- guide = tempGuide;
- storage.set({
- guide: tempGuide,
- });
- }
- }
-
- if (guide) {
- const guideData = await fetchGuideDataWithName(guide.name);
- console.log("guideData", guideData);
- // ...do something with the guide and guideData
- }
+ tryToShowGuideDialog();
}
});
@@ -49,8 +61,9 @@ const initLocationListener = () => {
// initStorageListener detecting storage changes
const initStorageListener = () => {
- window.addEventListener("storage", () => {
+ window.addEventListener("storage", async () => {
console.log("storage changed");
+ tryToShowGuideDialog();
});
};
diff --git a/frontend/src/plugins/guide/storage.ts b/frontend/src/plugins/guide/storage.ts
index cefb36f860..e80101c922 100644
--- a/frontend/src/plugins/guide/storage.ts
+++ b/frontend/src/plugins/guide/storage.ts
@@ -49,3 +49,12 @@ export function remove(keys: StorageKey[]) {
}
}
}
+
+export function emitStorageChangedEvent() {
+ const iframeEl = document.createElement("iframe");
+ iframeEl.style.display = "none";
+ document.body.appendChild(iframeEl);
+
+ iframeEl.contentWindow?.localStorage.setItem("t", Date.now().toString());
+ iframeEl.remove();
+}
diff --git a/frontend/src/plugins/guide/style.css b/frontend/src/plugins/guide/style.css
new file mode 100644
index 0000000000..2edda9b772
--- /dev/null
+++ b/frontend/src/plugins/guide/style.css
@@ -0,0 +1,27 @@
+.bb-guide-highlight-wrapper {
+ @apply absolute rounded pointer-events-none;
+ box-shadow: rgb(33 33 33 / 80%) 0px 0px 1px 2px;
+ z-index: 9999999;
+}
+
+.bb-guide-dialog {
+ @apply absolute bg-white w-72 mt-1 text-black p-2 px-3 rounded-lg text-left;
+ box-shadow: 0 3px 30px rgb(33 33 33 / 30%);
+ z-index: 10000000;
+}
+
+.bb-guide-title-text {
+ @apply text-lg text-black leading-6 py-1 mb-1;
+}
+
+.bb-guide-description-text {
+ @apply text-base text-gray-700;
+}
+
+.bb-guide-btns-container {
+ @apply mt-3 w-full flex flex-row justify-between items-center;
+}
+
+.bb-guide-btns-container > button {
+ @apply px-2 py-1 border text-sm rounded hover:opacity-80 hover:border-gray-400;
+}
diff --git a/frontend/src/plugins/guide/utils.ts b/frontend/src/plugins/guide/utils.ts
new file mode 100644
index 0000000000..e9733d4605
--- /dev/null
+++ b/frontend/src/plugins/guide/utils.ts
@@ -0,0 +1,75 @@
+import { assign } from "lodash-es";
+
+export const getStylePropertyValue = (
+ element: HTMLElement,
+ propertyName: string
+) => {
+ const propertyValue = window
+ .getComputedStyle(element)
+ .getPropertyValue(propertyName);
+
+ return propertyValue;
+};
+
+export const isElementFixed = (element: HTMLElement): boolean => {
+ const parentNode = element.parentNode;
+
+ if (!parentNode || parentNode.nodeName === "HTML") {
+ return false;
+ }
+
+ if (getStylePropertyValue(element, "position") === "fixed") {
+ return true;
+ }
+
+ return isElementFixed(parentNode as HTMLElement);
+};
+
+export const getElementBounding = (
+ element: HTMLElement,
+ relativeEl?: HTMLElement
+) => {
+ const scrollTop =
+ window.pageYOffset ||
+ document.documentElement.scrollTop ||
+ document.body.scrollTop;
+ const scrollLeft =
+ window.pageXOffset ||
+ document.documentElement.scrollLeft ||
+ document.body.scrollLeft;
+
+ relativeEl = relativeEl || document.body;
+
+ const elementRect = element.getBoundingClientRect();
+ const relativeElRect = relativeEl.getBoundingClientRect();
+ const relativeElPosition = getStylePropertyValue(relativeEl, "position");
+
+ const bounding = {
+ width: elementRect.width,
+ height: elementRect.height,
+ };
+
+ if (
+ (relativeEl.tagName !== "BODY" && relativeElPosition === "relative") ||
+ relativeElPosition === "sticky"
+ ) {
+ // when the container of our target element is _not_ body and has either "relative" or "sticky" position, we should not
+ // consider the scroll position but we need to include the relative x/y of the container element
+ return assign(bounding, {
+ top: elementRect.top - relativeElRect.top,
+ left: elementRect.left - relativeElRect.left,
+ });
+ }
+
+ if (isElementFixed(element)) {
+ return assign(bounding, {
+ top: elementRect.top,
+ left: elementRect.left,
+ });
+ }
+
+ return assign(bounding, {
+ top: elementRect.top + scrollTop,
+ left: elementRect.left + scrollLeft,
+ });
+};
diff --git a/frontend/static/guide/schema-migration.json b/frontend/static/guide/schema-migration.json
index 779208d7f2..e0d46430d3 100644
--- a/frontend/static/guide/schema-migration.json
+++ b/frontend/static/guide/schema-migration.json
@@ -4,15 +4,27 @@
{},
{
"title": "Click the Alter Schema button",
- "description": "",
- "url": "http://localhost:3000/"
+ "description": "Alter schema is a main feature of Bytebase",
+ "url": "http://localhost:3000",
+ "selectors": [["[data-label='bb-alter-schema-button']"]]
},
{
- "title": "Select one of your database",
- "description": ""
+ "title": "Select the database",
+ "description": "",
+ "selectors": [
+ [
+ "[data-label='bb-alter-schema-modal'] [data-label='bb-database-table'] tbody tr"
+ ]
+ ]
},
{
- "title": "Select Normal migration and click Next"
+ "title": "Choose Normal migration and click Next",
+ "description": "",
+ "selectors": [
+ [
+ "[data-label='bb-migration-mode-dialog'] [data-label='bb-modal-confirm-button']"
+ ]
+ ]
},
{
"title": "Input the `DROP COLUMN` SQL string in the editor",