Skip to content

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

refactor(bbkit): refactor bbkit using modern vue APIs (part.1) (#509)

Liu Ji8a0ccb96c1a4
diff --git a/frontend/src/bbkit/BBAlert.vue b/frontend/src/bbkit/BBAlert.vue
index f11c5c4c55..56c540c2a1 100644
--- a/frontend/src/bbkit/BBAlert.vue
+++ b/frontend/src/bbkit/BBAlert.vue
@@ -117,73 +117,54 @@
</div>
</template>
-<script lang="ts">
-import { onMounted, onUnmounted, PropType } from "vue";
+<script lang="ts" setup>
+import { useEventListener } from "@vueuse/core";
+import { defineProps, withDefaults, defineEmits, computed } from "vue";
import { BBAlertStyle } from "./types";
-export default {
- name: "BBAlert",
- props: {
- style: {
- type: String as PropType<BBAlertStyle>,
- default: "INFO",
- },
- title: {
- required: true,
- type: String,
- },
- description: {
- type: String,
- default: "",
- },
- okText: {
- type: String,
- default: "bbkit.common.ok",
- },
- cancelText: {
- type: String,
- default: "bbkit.common.cancel",
- },
- inProgress: {
- type: Boolean,
- default: false,
- },
- // Any payload pass through to "ok" and "cancel" events
- payload: {},
- },
- emits: ["ok", "cancel"],
- setup(props, { emit }) {
- const cancel = () => {
- emit("cancel", props.payload);
- };
+type Payload = string | number | boolean | any;
- const escHandler = (e: KeyboardEvent) => {
- if (!props.inProgress && e.code == "Escape") {
- cancel();
- }
- };
+const props = withDefaults(
+ defineProps<{
+ style?: BBAlertStyle;
+ title: string;
+ description?: string;
+ okText?: string;
+ cancelText?: string;
+ inProgress?: boolean;
+ payload?: Payload; // Any payload pass through to "ok" and "cancel" events
+ }>(),
+ {
+ style: "INFO",
+ description: "",
+ okText: "bbkit.common.ok",
+ cancelText: "bbkit.common.cancel",
+ inProgress: false,
+ payload: undefined,
+ }
+);
- onMounted(() => {
- document.addEventListener("keydown", escHandler);
- });
+const emit = defineEmits<{
+ (event: "ok", payload: Payload): void;
+ (event: "cancel", payload: Payload): void;
+}>();
- onUnmounted(() => {
- document.removeEventListener("keydown", escHandler);
- });
-
- const buttonStyleMap: Record<string, string> = {
- INFO: "btn-primary",
- SUCCESS: "btn-success",
- WARN: "btn-primary",
- CRITICAL: "btn-danger",
- };
+const cancel = () => {
+ emit("cancel", props.payload);
+};
- const okButtonStyle = buttonStyleMap[props.style];
+useEventListener(document, "keydown", (e) => {
+ if (!props.inProgress && e.code == "Escape") {
+ cancel();
+ }
+});
- return {
- okButtonStyle,
- cancel,
- };
- },
+const buttonStyleMap: Record<string, string> = {
+ INFO: "btn-primary",
+ SUCCESS: "btn-success",
+ WARN: "btn-primary",
+ CRITICAL: "btn-danger",
};
+
+const okButtonStyle = computed(() => buttonStyleMap[props.style]);
</script>
diff --git a/frontend/src/bbkit/BBAttention.vue b/frontend/src/bbkit/BBAttention.vue
index 9bd8a32276..22ed43be7d 100644
--- a/frontend/src/bbkit/BBAttention.vue
+++ b/frontend/src/bbkit/BBAttention.vue
@@ -46,44 +46,38 @@
</div>
</template>
-<script lang="ts">
-import { computed, PropType } from "vue";
+<script lang="ts" setup>
+import { computed, defineProps, withDefaults, defineEmits } from "vue";
import { BBAttentionStyle } from "./types";
-export default {
- name: "BBAttention",
- props: {
- style: {
- type: String as PropType<BBAttentionStyle>,
- default: "INFO",
- },
- title: {
- default: "bbkit.attention.default",
- type: String,
- },
- description: {
- default: "",
- type: String,
- },
- actionText: {
- default: "",
- type: String,
- },
- },
- emits: ["click-action"],
- setup(props) {
- const color = computed(() => {
- switch (props.style) {
- case "INFO":
- return "blue";
- case "WARN":
- return "yellow";
- case "CRITICAL":
- return "red";
- }
- });
+const props = withDefaults(
+ defineProps<{
+ style?: BBAttentionStyle;
+ title?: string;
+ description?: string;
+ actionText?: string;
+ }>(),
+ {
+ style: "INFO",
+ title: "bbkit.attention.default",
+ description: "",
+ actionText: "",
+ }
+);
- return { color };
- },
-};
+defineEmits<{
+ (event: "click-action"): void;
+}>();
+
+// eslint-disable-next-line vue/return-in-computed-property
+const color = computed(() => {
+ switch (props.style) {
+ case "INFO":
+ return "blue";
+ case "WARN":
+ return "yellow";
+ case "CRITICAL":
+ return "red";
+ }
+});
</script>
diff --git a/frontend/src/bbkit/BBAvatar.vue b/frontend/src/bbkit/BBAvatar.vue
index 97d11ab18c..4c0f5b0b1f 100644
--- a/frontend/src/bbkit/BBAvatar.vue
+++ b/frontend/src/bbkit/BBAvatar.vue
@@ -11,9 +11,8 @@
</div>
</template>
-<script lang="ts">
-import { computed, PropType } from "vue";
-
+<script lang="ts" setup>
+import { computed, defineProps, withDefaults } from "vue";
import { hashCode } from "./BBUtil";
import { BBAvatarSizeType } from "./types";
@@ -50,77 +49,64 @@ const fontStyleClassMap: Map<BBAvatarSizeType, string> = new Map([
["HUGE", "3rem"], // text-5xl
]);
-export default {
- name: "BBAvatar",
- props: {
- username: {
- type: String,
- default: "",
- },
- size: {
- type: String as PropType<BBAvatarSizeType>,
- default: "NORMAL",
- },
- rounded: {
- type: Boolean,
- default: true,
- },
- backgroundColor: {
- type: String,
- },
- },
- setup(props) {
- const initials = computed(() => {
- if (props.username == "?") {
- return "?";
- }
+const props = withDefaults(
+ defineProps<{
+ username?: string;
+ size: BBAvatarSizeType;
+ rounded?: boolean;
+ backgroundColor?: string;
+ }>(),
+ {
+ username: "",
+ size: "NORMAL",
+ rounded: true,
+ backgroundColor: "",
+ }
+);
- let parts = props.username.split(/[ -]/);
- let initials = "";
- for (var i = 0; i < parts.length; i++) {
- for (var j = 0; j < parts[i].length; j++) {
- // Skip non-alphabet leading letters
- if (/[a-zA-Z0-9]/.test(parts[i].charAt(j))) {
- initials += parts[i].charAt(j);
- break;
- }
- }
- }
- if (initials.length <= 2) {
- return initials.toUpperCase();
- }
- // If there are more than 2 initials, we will pick the first and last initial.
- // Displaying > 2 letters in a circle doesn't look good.
- return (initials[0] + initials[initials.length - 1]).toUpperCase();
- });
+const initials = computed(() => {
+ if (props.username == "?") {
+ return "?";
+ }
- const backgroundColor = computed(() => {
- return (
- props.backgroundColor ||
- BACKGROUND_COLOR_LIST[
- (hashCode(props.username) & 0xfffffff) % BACKGROUND_COLOR_LIST.length
- ]
- );
- });
+ let parts = props.username.split(/[ -]/);
+ let initials = "";
+ for (var i = 0; i < parts.length; i++) {
+ for (var j = 0; j < parts[i].length; j++) {
+ // Skip non-alphabet leading letters
+ if (/[a-zA-Z0-9]/.test(parts[i].charAt(j))) {
+ initials += parts[i].charAt(j);
+ break;
+ }
+ }
+ }
+ if (initials.length <= 2) {
+ return initials.toUpperCase();
+ }
+ // If there are more than 2 initials, we will pick the first and last initial.
+ // Displaying > 2 letters in a circle doesn't look good.
+ return (initials[0] + initials[initials.length - 1]).toUpperCase();
+});
- const style = computed(() => {
- return {
- borderRadius: props.rounded ? "50%" : 0,
- backgroundColor: backgroundColor.value,
- color: "white",
- "font-size": fontStyleClassMap.get(props.size),
- };
- });
+const backgroundColor = computed(() => {
+ return (
+ props.backgroundColor ||
+ BACKGROUND_COLOR_LIST[
+ (hashCode(props.username) & 0xfffffff) % BACKGROUND_COLOR_LIST.length
+ ]
+ );
+});
- const textClass = computed(() => {
- return sizeClassMap.get(props.size);
- });
+const style = computed(() => {
+ return {
+ borderRadius: props.rounded ? "50%" : 0,
+ backgroundColor: backgroundColor.value,
+ color: "white",
+ "font-size": fontStyleClassMap.get(props.size),
+ };
+});
- return {
- initials,
- textClass,
- style,
- };
- },
-};
+const textClass = computed(() => {
+ return sizeClassMap.get(props.size);
+});
</script>
diff --git a/frontend/src/bbkit/BBButtonAdd.vue b/frontend/src/bbkit/BBButtonAdd.vue
index 2c1760ec1f..fe781e4cde 100644
--- a/frontend/src/bbkit/BBButtonAdd.vue
+++ b/frontend/src/bbkit/BBButtonAdd.vue
@@ -7,10 +7,10 @@
</button>
</template>
-<script lang="ts">
-export default {
- name: "BBButtonAdd",
- props: {},
- emits: ["add"],
-};
+<script lang="ts" setup>
+import { defineEmits } from "vue";
+
+defineEmits<{
+ (event: "add"): void;
+}>();
</script>
diff --git a/frontend/src/bbkit/BBButtonConfirm.vue b/frontend/src/bbkit/BBButtonConfirm.vue
index 2fa5846c7d..9d10a0054b 100644
--- a/frontend/src/bbkit/BBButtonConfirm.vue
+++ b/frontend/src/bbkit/BBButtonConfirm.vue
@@ -40,47 +40,34 @@
</BBAlert>
</template>
-<script lang="ts">
-import { PropType, reactive } from "vue";
+<script lang="ts" setup>
+import { reactive, defineProps, withDefaults, defineEmits } from "vue";
import { BBButtonConfirmStyle } from "./types";
-export default {
- name: "BBButtonConfirm",
- props: {
- style: {
- default: "DELETE",
- type: String as PropType<BBButtonConfirmStyle>,
- },
- buttonText: {
- default: "",
- type: String,
- },
- requireConfirm: {
- default: false,
- type: Boolean,
- },
- okText: {
- default: "Delete",
- type: String,
- },
- confirmTitle: {
- default: "Are you sure to delete?",
- type: String,
- },
- confirmDescription: {
- default: "You cannot undo this action",
- type: String,
- },
- },
- emits: ["confirm"],
- setup() {
- const state = reactive({
- showModal: false,
- });
+withDefaults(
+ defineProps<{
+ style?: BBButtonConfirmStyle;
+ buttonText?: string;
+ requireConfirm?: boolean;
+ okText?: string;
+ confirmTitle?: string;
+ confirmDescription?: string;
+ }>(),
+ {
+ style: "DELETE",
+ buttonText: "",
+ requireConfirm: false,
+ okText: "Delete",
+ confirmTitle: "Are you sure to delete?",
+ confirmDescription: "You cannot undo this action",
+ }
+);
- return {
- state,
- };
- },
-};
+defineEmits<{
+ (event: "confirm"): void;
+}>();
+
+const state = reactive({
+ showModal: false,
+});
</script>
diff --git a/frontend/src/bbkit/BBCheckbox.vue b/frontend/src/bbkit/BBCheckbox.vue
index 149f34b681..7917b0e652 100644
--- a/frontend/src/bbkit/BBCheckbox.vue
+++ b/frontend/src/bbkit/BBCheckbox.vue
@@ -3,15 +3,7 @@
<div class="flex items-center h-5">
<input
type="checkbox"
- class="
- h-4
- w-4
- text-accent
- rounded
- disabled:cursor-not-allowed
- border-control-border
- focus:ring-accent
- "
+ class="h-4 w-4 text-accent rounded disabled:cursor-not-allowed border-control-border focus:ring-accent"
:disabled="disabled"
:checked="value"
@input="
@@ -39,39 +31,33 @@
</div>
</template>
-<script lang="ts">
-import { ref, watch } from "vue";
+<script lang="ts" setup>
+import { ref, watch, defineProps, withDefaults, defineEmits } from "vue";
+
+const props = withDefaults(
+ defineProps<{
+ title?: string;
+ label?: string;
+ value?: boolean;
+ disabled?: boolean;
+ }>(),
+ {
+ title: "",
+ label: "",
+ value: true,
+ disabled: false,
+ }
+);
+
+defineEmits<{
+ (event: "toggle", on: boolean): void;
+}>();
-export default {
- name: "BBCheckbox",
- props: {
- title: {
- default: "",
- type: String,
- },
- label: {
- default: "",
- type: String,
- },
- value: {
- default: true,
- type: Boolean,
- },
- disabled: {
- default: false,
- type: Boolean,
- },
- },
- emits: ["toggle"],
- setup(props) {
- const on = ref(props.value);
- watch(
- () => props.value,
- (cur) => {
- on.value = cur;
- }
- );
- return { on };
- },
-};
+const on = ref(props.value);
+watch(
+ () => props.value,
+ (cur) => {
+ on.value = cur;
+ }
+);
</script>
diff --git a/frontend/src/bbkit/BBContextMenu.vue b/frontend/src/bbkit/BBContextMenu.vue
index 495c419f86..b57556c1e0 100644
--- a/frontend/src/bbkit/BBContextMenu.vue
+++ b/frontend/src/bbkit/BBContextMenu.vue
@@ -12,15 +12,15 @@
<template>
<div class="z-50 absolute right-0 rounded-md shadow-lg">
<!--
- Profile dropdown panel, show/hide based on dropdown state.
+ Profile dropdown panel, show/hide based on dropdown state.
- Entering: "transition ease-out duration-100"
- From: "transform opacity-0 scale-95"
- To: "transform opacity-100 scale-100"
- Leaving: "transition ease-in duration-75"
- From: "transform opacity-100 scale-100"
- To: "transform opacity-0 scale-95"
- -->
+ Entering: "transition ease-out duration-100"
+ From: "transform opacity-0 scale-95"
+ To: "transform opacity-100 scale-100"
+ Leaving: "transition ease-in duration-75"
+ From: "transform opacity-100 scale-100"
+ To: "transform opacity-0 scale-95"
+ -->
<transition
enter-active-class="transition ease-out duration-100"
enter-class="transform opacity-0 scale-95"
@@ -30,7 +30,7 @@
leave-to-class="transform opacity-0 scale-95"
>
<div
- v-show="isOpen"
+ v-show="state.isOpen"
v-click-inside-outside="close"
@contextmenu.capture.prevent
>
@@ -40,52 +40,46 @@
aria-orientation="vertical"
aria-labelledby="user-menu"
>
- <slot :context="context" />
+ <slot :context="state.context" />
</div>
</div>
</transition>
</div>
</template>
-<script lang="ts">
-import { defineComponent } from "vue";
+<script lang="ts" setup>
+import { reactive, defineExpose } from "vue";
+import vClickInsideOutside from "./directives/click-inside-outside";
-import clickInsideOutside from "./directives/click-inside-outside";
+type Context = string | number | boolean | any;
-export default defineComponent({
- name: "BBContextMenu",
- directives: {
- "click-inside-outside": clickInsideOutside,
- },
- data: function () {
- return {
- isOpen: false,
- context: {} as any,
- };
- },
- methods: {
- open(evt: any, context: any) {
- // This is a hack, upon intial click to bring up the context menu, it will
- // trigger both open and close. The latter is triggered because the click
- // is outside of the menu which hasn't brought up yet. So here, we use
- // setTimeout to schedule open after close.
- setTimeout(() => {
- this.isOpen = true;
- this.context = context;
- }, 1);
- },
- close() {
- // Close the menu regardless of whether the click is inside the menu.
- this.isOpen = false;
- this.context = null;
- },
- toggle(evt: any, context: any) {
- if (!this.isOpen) {
- this.open(evt, context);
- } else {
- this.close();
- }
- },
- },
+const state = reactive({
+ isOpen: false,
+ context: {} as Context,
});
+
+const open = (evt: any, context: Context) => {
+ // This is a hack, upon initial click to bring up the context menu, it will
+ // trigger both open and close. The latter is triggered because the click
+ // is outside of the menu which hasn't brought up yet. So here, we use
+ // setTimeout to schedule open after close.
+ requestAnimationFrame(() => {
+ state.isOpen = true;
+ state.context = context;
+ });
+};
+const close = () => {
+ // Close the menu regardless of whether the click is inside the menu.
+ state.isOpen = false;
+ state.context = null;
+};
+const toggle = (evt: any, context: Context) => {
+ if (!state.isOpen) {
+ open(evt, context);
+ } else {
+ close();
+ }
+};
+
+defineExpose({ open, close, toggle });
</script>