Skip to content

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

chore: optimize history search (#20574)

ecmadao9034899e6b6a
diff --git a/frontend/src/react/components/sql-editor/HistoryPane.test.tsx b/frontend/src/react/components/sql-editor/HistoryPane.test.tsx
index 3f62c897ed..f498c8221e 100644
--- a/frontend/src/react/components/sql-editor/HistoryPane.test.tsx
+++ b/frontend/src/react/components/sql-editor/HistoryPane.test.tsx
@@ -124,20 +124,22 @@ vi.mock("@/views/sql-editor/events", () => ({
},
}));
-vi.mock("@/react/components/ui/search-input", () => ({
- SearchInput: ({
+// HistorySearchInput is exercised in its own test; here it's stubbed to a
+// plain input wired to the string-based onChange.
+vi.mock("./HistorySearchInput", () => ({
+ HistorySearchInput: ({
value,
onChange,
placeholder,
}: {
value: string;
- onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
+ onChange: (value: string) => void;
placeholder?: string;
}) => (
<input
data-testid="search-input"
value={value}
- onChange={onChange}
+ onChange={(e) => onChange(e.target.value)}
placeholder={placeholder}
/>
),
diff --git a/frontend/src/react/components/sql-editor/HistoryPane.tsx b/frontend/src/react/components/sql-editor/HistoryPane.tsx
index b26cc4f5f6..38d862a861 100644
--- a/frontend/src/react/components/sql-editor/HistoryPane.tsx
+++ b/frontend/src/react/components/sql-editor/HistoryPane.tsx
@@ -11,7 +11,6 @@ import {
import { useTranslation } from "react-i18next";
import { HighlightLabelText } from "@/react/components/HighlightLabelText";
import { Button } from "@/react/components/ui/button";
-import { SearchInput } from "@/react/components/ui/search-input";
import { cn } from "@/react/lib/utils";
import { router } from "@/react/router";
import { SQL_EDITOR_QUERY_HISTORY_MODULE } from "@/react/router/handles";
@@ -30,6 +29,7 @@ import { DEBOUNCE_SEARCH_DELAY, getDateForPbTimestampProtoEs } from "@/types";
import type { QueryHistory } from "@/types/proto-es/v1/sql_service_pb";
import { extractProjectResourceName, extractQueryHistoryUID } from "@/utils";
import { sqlEditorEvents } from "@/views/sql-editor/events";
+import { HistorySearchInput } from "./HistorySearchInput";
/**
* React migration of frontend/src/views/sql-editor/AsidePanel/HistoryPane/HistoryPane.vue.
@@ -259,11 +259,10 @@ export function HistoryPane() {
return (
<div className="relative w-full h-full flex flex-col justify-start items-start">
<div className="w-full px-1">
- <SearchInput
+ <HistorySearchInput
value={searchText}
- onChange={(e) => onSearchUpdate(e.target.value)}
+ onChange={onSearchUpdate}
placeholder={t("sql-editor.search-history-by-statement")}
- className="h-8"
/>
</div>
diff --git a/frontend/src/react/components/sql-editor/HistorySearchInput.test.tsx b/frontend/src/react/components/sql-editor/HistorySearchInput.test.tsx
new file mode 100644
index 0000000000..83f05e7e29
--- /dev/null
+++ b/frontend/src/react/components/sql-editor/HistorySearchInput.test.tsx
@@ -0,0 +1,78 @@
+import type { ReactElement } from "react";
+import { act } from "react";
+import { createRoot } from "react-dom/client";
+import { afterEach, describe, expect, test, vi } from "vitest";
+
+(
+ globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }
+).IS_REACT_ACT_ENVIRONMENT = true;
+
+vi.mock("react-i18next", () => ({
+ useTranslation: () => ({ t: (key: string) => key }),
+}));
+
+import { HistorySearchInput } from "./HistorySearchInput";
+
+const render = (el: ReactElement) => {
+ const container = document.createElement("div");
+ document.body.appendChild(container);
+ act(() => createRoot(container).render(el));
+ return container;
+};
+
+afterEach(() => {
+ document.body.innerHTML = "";
+});
+
+describe("HistorySearchInput", () => {
+ test("renders a textarea with the placeholder", () => {
+ const c = render(
+ <HistorySearchInput value="" onChange={() => {}} placeholder="search…" />
+ );
+ const ta = c.querySelector("textarea") as HTMLTextAreaElement;
+ expect(ta).not.toBeNull();
+ expect(ta.placeholder).toBe("search…");
+ });
+
+ test("typing calls onChange with the new value", () => {
+ const onChange = vi.fn();
+ const c = render(<HistorySearchInput value="" onChange={onChange} />);
+ const ta = c.querySelector("textarea") as HTMLTextAreaElement;
+ act(() => {
+ Object.defineProperty(ta, "value", { writable: true, value: "select" });
+ ta.dispatchEvent(new Event("input", { bubbles: true }));
+ });
+ expect(onChange).toHaveBeenCalledWith("select");
+ });
+
+ test("no clear button when empty", () => {
+ const c = render(<HistorySearchInput value="" onChange={() => {}} />);
+ expect(c.querySelector("[data-clear-search]")).toBeNull();
+ });
+
+ test("clear button appears with content and clears on click", () => {
+ const onChange = vi.fn();
+ const c = render(
+ <HistorySearchInput value="select 1" onChange={onChange} />
+ );
+ const clear = c.querySelector("[data-clear-search]") as HTMLButtonElement;
+ expect(clear).not.toBeNull();
+ act(() => clear.click());
+ expect(onChange).toHaveBeenCalledWith("");
+ });
+
+ test("Enter does not insert a newline (preventDefault)", () => {
+ const onChange = vi.fn();
+ const c = render(<HistorySearchInput value="" onChange={onChange} />);
+ const ta = c.querySelector("textarea") as HTMLTextAreaElement;
+ const evt = new KeyboardEvent("keydown", {
+ key: "Enter",
+ bubbles: true,
+ cancelable: true,
+ });
+ act(() => {
+ ta.dispatchEvent(evt);
+ });
+ expect(evt.defaultPrevented).toBe(true);
+ });
+});
diff --git a/frontend/src/react/components/sql-editor/HistorySearchInput.tsx b/frontend/src/react/components/sql-editor/HistorySearchInput.tsx
new file mode 100644
index 0000000000..fea859f56c
--- /dev/null
+++ b/frontend/src/react/components/sql-editor/HistorySearchInput.tsx
@@ -0,0 +1,80 @@
+import { Search, X } from "lucide-react";
+import { useLayoutEffect, useRef } from "react";
+import { useTranslation } from "react-i18next";
+import { cn } from "@/react/lib/utils";
+
+// Tallest the box grows before it starts scrolling internally (px).
+const MAX_HEIGHT = 120;
+
+interface Props {
+ value: string;
+ onChange: (value: string) => void;
+ placeholder?: string;
+ className?: string;
+}
+
+/**
+ * Query-history search box. Unlike the shared single-line `SearchInput`, this
+ * uses an auto-growing `<textarea>` so a long query soft-wraps and the box
+ * grows up to `MAX_HEIGHT` (then scrolls), plus a trailing clear button.
+ * The auto-grow follows the codebase's `scrollHeight` pattern (see
+ * `AgentInput` / `PromptInput`).
+ */
+export function HistorySearchInput({
+ value,
+ onChange,
+ placeholder,
+ className,
+}: Props) {
+ const { t } = useTranslation();
+ const textareaRef = useRef<HTMLTextAreaElement>(null);
+
+ useLayoutEffect(() => {
+ const el = textareaRef.current;
+ if (!el) return;
+ // Reset first so shrinking works, then clamp to the max.
+ el.style.height = "auto";
+ const next = Math.min(el.scrollHeight, MAX_HEIGHT);
+ el.style.height = `${next}px`;
+ el.style.overflowY = el.scrollHeight > MAX_HEIGHT ? "auto" : "hidden";
+ }, [value]);
+
+ return (
+ <div
+ className={cn(
+ "relative flex w-full rounded-xs border border-control-border bg-transparent transition-colors",
+ className
+ )}
+ >
+ <Search className="absolute left-2.5 top-2 h-4 w-4 text-control-placeholder pointer-events-none" />
+ <textarea
+ ref={textareaRef}
+ rows={1}
+ value={value}
+ onChange={(e) => onChange(e.target.value)}
+ // It's a search box, not a multi-line editor: long text soft-wraps,
+ // but Enter must not insert a newline.
+ onKeyDown={(e) => {
+ if (e.key === "Enter") e.preventDefault();
+ }}
+ placeholder={placeholder ?? t("common.type-to-search")}
+ className={cn(
+ "w-full resize-none appearance-none border-0 bg-transparent text-sm leading-5 text-main",
+ "py-1.5 pl-8 pr-8 outline-hidden focus:ring-0 focus:outline-hidden",
+ "placeholder:text-control-placeholder"
+ )}
+ />
+ {value && (
+ <button
+ type="button"
+ data-clear-search
+ aria-label={t("common.clear")}
+ onClick={() => onChange("")}
+ className="absolute right-1.5 top-1.5 flex h-5 w-5 items-center justify-center rounded-xs text-control-placeholder hover:bg-control-bg-hover hover:text-control"
+ >
+ <X className="h-3.5 w-3.5" />
+ </button>
+ )}
+ </div>
+ );
+}
diff --git a/frontend/src/utils/util.test.ts b/frontend/src/utils/util.test.ts
new file mode 100644
index 0000000000..92ef5fcb8d
--- /dev/null
+++ b/frontend/src/utils/util.test.ts
@@ -0,0 +1,39 @@
+import { describe, expect, test, vi } from "vitest";
+
+// util.ts transitively pulls in i18n-aware modules; stub the i18n surface so
+// importing the highlight helper doesn't require the full i18n runtime.
+vi.mock("react-i18next", () => ({
+ useTranslation: () => ({ t: (k: string) => k }),
+ initReactI18next: { type: "3rdParty", init: () => {} },
+}));
+
+import { getHighlightHTMLByRegExp } from "./util";
+
+describe("getHighlightHTMLByRegExp", () => {
+ test("highlights a plain keyword", () => {
+ const html = getHighlightHTMLByRegExp("SELECT 1", "select");
+ expect(html).toContain('<b class="text-accent">SELECT</b>');
+ });
+
+ test("highlights a keyword containing a double quote (HTML-escaped target)", () => {
+ // Regression: `"public"` must still highlight even though the target's
+ // quotes are HTML-escaped to &quot; before matching.
+ const html = getHighlightHTMLByRegExp('FROM "public".db', [
+ '"public".db',
+ ] as string[]);
+ // The matched substring (incl. the quoted identifier) is wrapped in <b>;
+ // before the fix this token was not highlighted at all.
+ expect(html).toContain("<b");
+ expect(html).toContain("public");
+ });
+
+ test("highlights multiple whitespace-split tokens including a quoted one", () => {
+ const html = getHighlightHTMLByRegExp('SELECT * FROM "public".db', [
+ "SELECT",
+ "*",
+ '"public".db',
+ ]);
+ // All three tokens get wrapped.
+ expect((html.match(/<b /g) ?? []).length).toBeGreaterThanOrEqual(3);
+ });
+});
diff --git a/frontend/src/utils/util.ts b/frontend/src/utils/util.ts
index fdc5b18612..a42de584b7 100644
--- a/frontend/src/utils/util.ts
+++ b/frontend/src/utils/util.ts
@@ -216,10 +216,14 @@ export function getHighlightHTMLByRegExp(
return escapedText;
}
- // Step 2: Build safe regex pattern and apply highlighting
+ // Step 2: Build safe regex pattern and apply highlighting.
+ // The keyword is HTML-escaped first so it matches `escapedText` (otherwise a
+ // keyword containing `"`, `'`, `<`, `>` or `&` — e.g. a quoted SQL identifier
+ // like `"public"` — would search for a literal char the escaped text no
+ // longer contains, and silently fail to highlight).
pattern = Array.isArray(pattern)
- ? pattern.map((kw) => escapeRegExp(kw)).join("|")
- : escapeRegExp(pattern);
+ ? pattern.map((kw) => escapeRegExp(escapeHtml(kw))).join("|")
+ : escapeRegExp(escapeHtml(pattern));
const flags = caseSensitive ? "g" : "gi";
const re = new RegExp(pattern, flags);
const highlighted = escapedText.replaceAll(