Skip to content

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

feat: add InputWithTemplate component (#1082)

ecmadao7912a1e305e1
diff --git a/frontend/src/bbkit/BBBadge.vue b/frontend/src/bbkit/BBBadge.vue
new file mode 100644
index 0000000000..c636f9fe76
--- /dev/null
+++ b/frontend/src/bbkit/BBBadge.vue
@@ -0,0 +1,53 @@
+<template>
+ <span
+ :class="[
+ 'inline-flex rounded-full items-center py-0.5 px-2.5 text-sm font-medium',
+ `bg-${color}-100 text-${color}-800`,
+ canRemove ? 'pr-1' : '',
+ ]"
+ >
+ {{ text }}
+ <button
+ v-if="canRemove"
+ type="button"
+ :class="[
+ 'flex-shrink-0 ml-1 p-0.5 rounded-full inline-flex items-center justify-center focus:outline-none',
+ `text-${color}-400 hover:bg-${color}-200 hover:text-${color}-500 focus:bg-${color}-500`,
+ ]"
+ @click="$emit('remove')"
+ >
+ <heroicons-outline:x class="h-3 w-3" />
+ </button>
+ </span>
+</template>
+
+<script lang="ts" setup>
+import { computed, withDefaults } from "vue";
+import { BBAttentionStyle } from "./types";
+
+const props = withDefaults(
+ defineProps<{
+ text: string;
+ canRemove?: boolean;
+ style?: BBAttentionStyle;
+ }>(),
+ {
+ style: "INFO",
+ text: "",
+ canRemove: true,
+ }
+);
+
+// eslint-disable-next-line vue/return-in-computed-property
+const color = computed(() => {
+ switch (props.style) {
+ case "INFO":
+ return "indigo";
+ case "WARN":
+ return "yellow";
+ case "CRITICAL":
+ return "red";
+ }
+});
+const emit = defineEmits(["remove"]);
+</script>
diff --git a/frontend/src/components/InputWithTemplate/AutoWidthInput.vue b/frontend/src/components/InputWithTemplate/AutoWidthInput.vue
new file mode 100644
index 0000000000..c0106aaf6d
--- /dev/null
+++ b/frontend/src/components/InputWithTemplate/AutoWidthInput.vue
@@ -0,0 +1,70 @@
+<template>
+ <div>
+ <div ref="divRef" class="fixed invisible text-base leading-6">
+ {{ state.data }}
+ </div>
+ <input
+ ref="inputRef"
+ v-model="state.data"
+ :style="`width: ${state.width}px;`"
+ class="px-0 m-0 py-1 cleared-input outline-none"
+ type="text"
+ @keyup="(e) => $emit('keyup', e)"
+ />
+ </div>
+</template>
+
+<script lang="ts" setup>
+import { watch, reactive, ref, watchEffect, nextTick } from "vue";
+
+const props = defineProps({
+ value: {
+ default: "",
+ type: String,
+ },
+ maxWidth: {
+ default: 0,
+ type: Number,
+ },
+});
+
+const emit = defineEmits(["change", "keyup"]);
+
+const state = reactive<{ data: string; width: number }>({
+ data: props.value,
+ width: 0,
+});
+
+const inputRef = ref<HTMLInputElement>();
+const divRef = ref<HTMLDivElement>();
+
+watch(
+ () => state.data,
+ (val) => {
+ nextTick(updateWidth);
+ emit("change", val);
+ }
+);
+
+watch(
+ () => props.value,
+ (val) => {
+ state.data = val;
+ nextTick(updateWidth);
+ }
+);
+
+const updateWidth = () => {
+ const width = divRef.value?.offsetWidth ?? state.width;
+ state.width = Math.min(props.maxWidth ?? width, width);
+};
+
+watchEffect(updateWidth);
+</script>
+
+<style scoped>
+.cleared-input,
+.cleared-input:focus {
+ @apply shadow-none ring-0 border-0 border-none;
+}
+</style>
diff --git a/frontend/src/components/InputWithTemplate/InputWithTemplate.vue b/frontend/src/components/InputWithTemplate/InputWithTemplate.vue
new file mode 100644
index 0000000000..f74323b53c
--- /dev/null
+++ b/frontend/src/components/InputWithTemplate/InputWithTemplate.vue
@@ -0,0 +1,243 @@
+<template>
+ <div class="border border-gray-300 rounded">
+ <div class="flex flex-wrap gap-4 p-3 bg-gray-50 rounded">
+ <div
+ v-for="template in templates"
+ :key="template.id"
+ class="px-4 py-1 rounded text-sm font-sm font-normal border border-gray-300 bg-gray-100 cursor-pointer tooltip-wrapper"
+ @click="() => onTemplateAdd(template)"
+ >
+ {{ template.id }}
+ <span class="tooltip whitespace-nowrap">{{
+ template.description
+ }}</span>
+ </div>
+ </div>
+ <div class="p-2 border-t border-gray-300">
+ <div ref="containerRef" class="flex flex-wrap items-center gap-1">
+ <div v-for="(data, i) in state.templateInputs" :key="i">
+ <BBBadge
+ v-if="data.type == 'template'"
+ :text="data.value"
+ @remove="() => onTemplateRemove(i)"
+ />
+ <AutoWidthInput
+ v-else
+ :value="data.value"
+ :max-width="state.inputMaxWidth"
+ @keyup="(e) => onKeyup(i, e)"
+ @change="(val) => onTemplateChange(i, val)"
+ />
+ </div>
+ <input
+ ref="inputRef"
+ v-model="state.inputData"
+ class="flex-1 px-0 m-0 py-1 cleared-input outline-none"
+ type="text"
+ @keydown.delete="onInputDataDeleteEnter"
+ @keyup.delete="onInputDataDeleteLeave"
+ />
+ </div>
+ </div>
+ </div>
+</template>
+
+<script lang="ts" setup>
+import {
+ reactive,
+ watch,
+ watchEffect,
+ ref,
+ PropType,
+ onUnmounted,
+ onMounted,
+} from "vue";
+import { Template, TemplateInput, InputType } from "./types";
+import { getTemplateInputs, templateInputsToString } from "./utils";
+
+interface LocalState {
+ inputData: string;
+ inputMaxWidth: number;
+ templateInputs: TemplateInput[];
+}
+
+const props = defineProps({
+ value: {
+ default: "",
+ type: String,
+ },
+ templates: {
+ require: true,
+ default: () => [],
+ type: Array as PropType<Template[]>,
+ },
+});
+
+const emit = defineEmits(["change"]);
+
+const templateInputs = getTemplateInputs(props.value, props.templates);
+let inputData = "";
+
+if (
+ templateInputs.length > 0 &&
+ templateInputs[templateInputs.length - 1].type === InputType.String
+) {
+ inputData = templateInputs.pop()?.value ?? inputData;
+}
+
+const state = reactive<LocalState>({
+ inputData,
+ inputMaxWidth: 0,
+ templateInputs,
+});
+
+watch(
+ () => state.templateInputs,
+ (val) => {
+ emit("change", `${templateInputsToString(val)}${state.inputData}`);
+ },
+ { deep: true }
+);
+
+watch(
+ () => state.inputData,
+ (val) => {
+ emit("change", `${templateInputsToString(state.templateInputs)}${val}`);
+ }
+);
+
+const containerRef = ref<HTMLDivElement>();
+const inputRef = ref<HTMLInputElement>();
+
+watchEffect(() => {
+ if (containerRef.value) {
+ state.inputMaxWidth = containerRef.value.offsetWidth;
+ }
+});
+
+const onWindowResize = () => {
+ if (containerRef && containerRef.value) {
+ state.inputMaxWidth = containerRef.value.offsetWidth;
+ }
+};
+
+const onInputDataDeleteEnter = (e: KeyboardEvent) => {
+ if (!state.inputData && state.templateInputs.length > 0) {
+ const last = state.templateInputs.slice(-1)[0];
+ if (last.type === InputType.Template) {
+ state.templateInputs.pop();
+ }
+ }
+};
+
+const onInputDataDeleteLeave = (e: KeyboardEvent) => {
+ if (!state.inputData && state.templateInputs.length > 0) {
+ const last = state.templateInputs.slice(-1)[0];
+ if (last && last.type === InputType.String) {
+ state.inputData = state.templateInputs.pop()?.value ?? state.inputData;
+ }
+ }
+};
+
+const onKeyup = (i: number, e: KeyboardEvent) => {
+ const data = state.templateInputs[i];
+ if (!data) {
+ return;
+ }
+
+ if (e.key !== "Delete" || data.value !== "") {
+ return;
+ }
+
+ onTemplateRemove(i);
+};
+
+const onTemplateChange = (i: number, data: string) => {
+ const target = state.templateInputs[i];
+ if (!target) {
+ return;
+ }
+
+ state.templateInputs = [
+ ...state.templateInputs.slice(0, i),
+ {
+ value: data,
+ type: target.type,
+ },
+ ...state.templateInputs.slice(i + 1),
+ ];
+};
+
+const onTemplateAdd = (template: Template) => {
+ if (state.inputData) {
+ // If the last input contains user's input, we also need to add it
+ state.templateInputs.push({
+ value: state.inputData,
+ type: InputType.String,
+ });
+ }
+
+ state.templateInputs.push({
+ value: template.id,
+ type: InputType.Template,
+ });
+
+ state.inputData = "";
+ if (inputRef && inputRef.value) {
+ inputRef.value.focus();
+ }
+};
+
+const onTemplateRemove = (i: number) => {
+ state.templateInputs = [
+ ...state.templateInputs.slice(0, i),
+ ...state.templateInputs.slice(i + 1),
+ ];
+
+ if (state.templateInputs.length === 0) {
+ return;
+ }
+
+ const index = i - 1;
+ if (index < 0 || index >= state.templateInputs.length) {
+ return;
+ }
+
+ const template = state.templateInputs[index];
+ if (template.type !== InputType.String) {
+ return;
+ }
+
+ if (i === state.templateInputs.length) {
+ // If the last value is string, we need to extract it into the last input.
+ state.inputData = `${state.templateInputs.pop()?.value ?? ""}${
+ state.inputData
+ }`;
+ } else if (state.templateInputs[i].type === InputType.String) {
+ // Join the adjacent string value
+ state.templateInputs = [
+ ...state.templateInputs.slice(0, index),
+ {
+ value: `${template.value}${state.templateInputs[i].value}`,
+ type: InputType.String,
+ },
+ ...state.templateInputs.slice(i + 1),
+ ];
+ }
+};
+
+onMounted(() => {
+ window.addEventListener("resize", onWindowResize);
+});
+
+onUnmounted(() => {
+ window.removeEventListener("resize", onWindowResize);
+});
+</script>
+
+<style scoped>
+.cleared-input,
+.cleared-input:focus {
+ @apply shadow-none ring-0 border-0 border-none;
+}
+</style>
diff --git a/frontend/src/components/InputWithTemplate/index.ts b/frontend/src/components/InputWithTemplate/index.ts
new file mode 100644
index 0000000000..f98d1b09fe
--- /dev/null
+++ b/frontend/src/components/InputWithTemplate/index.ts
@@ -0,0 +1,3 @@
+import InputWithTemplate from "./InputWithTemplate.vue";
+
+export { InputWithTemplate };
diff --git a/frontend/src/components/InputWithTemplate/types.ts b/frontend/src/components/InputWithTemplate/types.ts
new file mode 100644
index 0000000000..04d00a9bb2
--- /dev/null
+++ b/frontend/src/components/InputWithTemplate/types.ts
@@ -0,0 +1,14 @@
+export interface Template {
+ id: string;
+ description?: string;
+}
+
+export enum InputType {
+ String = "string",
+ Template = "template",
+}
+
+export interface TemplateInput {
+ value: string;
+ type: InputType;
+}
diff --git a/frontend/src/components/InputWithTemplate/utils.ts b/frontend/src/components/InputWithTemplate/utils.ts
new file mode 100644
index 0000000000..d7af8f31ff
--- /dev/null
+++ b/frontend/src/components/InputWithTemplate/utils.ts
@@ -0,0 +1,99 @@
+import { Template, TemplateInput, InputType } from "./types";
+
+const TEMPLATE_BRACKET_LEFT = "{{";
+const TEMPLATE_BRACKET_RIGHT = "}}";
+
+// getTemplateInputs will convert the string value into TemplateInput array.
+// For example:
+// "abc{{template}}" -> [{value: "abc", type: "string"}, {value: "template", type: "template"}]
+// "abc{{not_template}}{{template}}" -> [{value: "abc{{not_template}}", type: "string"}, {value: "template", type: "template"}]
+// "abc{{not_template}}{{template}}}}" -> [{value: "abc{{not_template}}", type: "string"}, {value: "template", type: "template"}, {value: "}}", type: "string"}]
+// "{{abc{{}}{{template}}}}" -> [{value: "{{abc{{}}", type: "string"}, {value: "template", type: "template"}, {value: "}}", type: "string"}]
+export const getTemplateInputs = (
+ value: string,
+ templates: Template[]
+): TemplateInput[] => {
+ let start = 0;
+ let end = 0;
+ const res: TemplateInput[] = [];
+ const templateSet = new Set<string>(templates.map((t) => t.id));
+
+ while (end <= value.length - 1) {
+ if (
+ value.slice(end, end + 2) === TEMPLATE_BRACKET_RIGHT &&
+ value.slice(start, start + 2) === TEMPLATE_BRACKET_LEFT
+ ) {
+ // When the end pointer meet the "}}" and the start pointer is "{{"
+ // we can extract the string slice as template or normal string.
+ const str = value.slice(start + 2, end);
+ if (templateSet.has(str)) {
+ res.push({
+ value: str,
+ type: InputType.Template,
+ });
+ } else {
+ res.push({
+ value: `${TEMPLATE_BRACKET_LEFT}${str}${TEMPLATE_BRACKET_RIGHT}`,
+ type: InputType.String,
+ });
+ }
+ end += 2;
+ start = end;
+ } else if (value.slice(end, end + 2) === TEMPLATE_BRACKET_LEFT) {
+ // When the end pointer meet the "{{"
+ // we should reset the position of the start pointer.
+ res.push({
+ value: value.slice(start, end),
+ type: InputType.String,
+ });
+ start = end;
+ end += 2;
+ } else {
+ end += 1;
+ }
+ }
+
+ if (start < end) {
+ res.push({
+ value: value.slice(start, end),
+ type: InputType.String,
+ });
+ }
+
+ // Join the adjacent string value
+ return res.reduce((result, data) => {
+ if (data.type === InputType.Template) {
+ return [...result, data];
+ }
+
+ let str = data.value;
+
+ if (
+ result.length > 0 &&
+ result[result.length - 1].type === InputType.String
+ ) {
+ str = `${result.pop()?.value ?? ""}${str}`;
+ }
+
+ return [
+ ...result,
+ {
+ value: str,
+ type: InputType.String,
+ },
+ ];
+ }, [] as TemplateInput[]);
+};
+
+// templateInputsToString will convert TemplateInput array into string
+// For example:
+// [{value: "abc", type: "string"}, {value: "template", type: "template"}] -> "abc{{template}}"
+export const templateInputsToString = (inputs: TemplateInput[]): string => {
+ return inputs
+ .map((input) =>
+ input.type === InputType.String
+ ? input.value
+ : `${TEMPLATE_BRACKET_LEFT}${input.value}${TEMPLATE_BRACKET_RIGHT}`
+ )
+ .join("");
+};
diff --git a/frontend/tailwind.config.js b/frontend/tailwind.config.js
index c83dd54277..2b8631d751 100644
--- a/frontend/tailwind.config.js
+++ b/frontend/tailwind.config.js
@@ -11,8 +11,8 @@ module.exports = {
safelist: [
// "w-xxx" is used by BBTab
// "pl-xxx" is used by BBOutline
- // "bg-xxx", "text-xxx" is used by BBAttention TaskCheckBadgeBar
- // "hover:bg-xxx" is used by TaskCheckBadgeBar
+ // "bg-xxx", "text-xxx" is used by BBAttention TaskCheckBadgeBar BBBadge
+ // "hover:bg-xxx" is used by TaskCheckBadgeBar BBBadge
// "grid-cols-xxx" is used by AnomalyCenterDashboard
{ pattern: /^w-/ },
{ pattern: /^pl-/ },
@@ -20,6 +20,8 @@ module.exports = {
{ pattern: /^bg-blue-/, variants: ["hover"] },
{ pattern: /^bg-yellow-/, variants: ["hover"] },
{ pattern: /^bg-red-/, variants: ["hover"] },
+ { pattern: /^bg-indigo-/, variants: ["hover"] },
+ { pattern: /^text-indigo-/ },
{ pattern: /^text-gray-/ },
{ pattern: /^text-blue-/ },
{ pattern: /^text-yellow-/ },