Skip to content

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

fix: cut off the statement (#20796)

ecmadaof2c0eae15bca
diff --git a/frontend/src/react/components/sql-editor/ResultView/ResultStatusBar.test.tsx b/frontend/src/react/components/sql-editor/ResultView/ResultStatusBar.test.tsx
new file mode 100644
index 0000000000..d14c2c7822
--- /dev/null
+++ b/frontend/src/react/components/sql-editor/ResultView/ResultStatusBar.test.tsx
@@ -0,0 +1,198 @@
+import { render, screen } from "@testing-library/react";
+import { act } from "react";
+import { beforeEach, describe, expect, test, vi } from "vitest";
+import { Engine } from "@/types/proto-es/v1/common_pb";
+import type { Database } from "@/types/proto-es/v1/database_service_pb";
+import { ResultStatusBar, RichDatabaseName } from "./ResultStatusBar";
+
+vi.mock("react-i18next", () => ({
+ initReactI18next: {
+ init: vi.fn(),
+ type: "3rdParty",
+ },
+ useTranslation: () => ({ t: (key: string) => key }),
+}));
+
+vi.mock("@/react/components/EngineIcon", () => ({
+ EngineIcon: () => <span data-testid="engine-icon" />,
+}));
+
+vi.mock("@/react/components/ui/tooltip", () => ({
+ Tooltip: ({ children }: { children: React.ReactNode }) => <>{children}</>,
+}));
+
+vi.mock("@/react/stores/app", () => ({
+ useAppStore: {
+ getState: () => ({
+ notify: vi.fn(),
+ }),
+ },
+}));
+
+vi.mock("@/utils/v1/database", () => ({
+ extractDatabaseResourceName: () => ({
+ instance: "instances/prod",
+ database: "instances/prod/databases/very-long-database-name",
+ instanceName: "prod",
+ databaseName: "very-long-database-name",
+ }),
+ getDatabaseEnvironment: () => ({ title: "Prod" }),
+ getInstanceResource: () => ({
+ name: "instances/prod",
+ title: "bytebase-3.17.11-selfhost",
+ engine: Engine.POSTGRES,
+ }),
+}));
+
+vi.mock("@/utils/v1/instance", () => ({
+ instanceV1Name: () => "bytebase-3.17.11-selfhost",
+}));
+
+const database = {
+ name: "instances/prod/databases/very-long-database-name",
+ project: "projects/prod",
+ effectiveEnvironment: "environments/prod",
+} as Database;
+
+let resizeCallbacks: ResizeObserverCallback[] = [];
+
+globalThis.ResizeObserver = class ResizeObserver {
+ constructor(callback: ResizeObserverCallback) {
+ resizeCallbacks.push(callback);
+ }
+ observe() {}
+ unobserve() {}
+ disconnect() {}
+} as typeof ResizeObserver;
+
+const setElementWidth = (
+ element: HTMLElement,
+ width: { clientWidth: number; scrollWidth: number }
+) => {
+ Object.defineProperty(element, "clientWidth", {
+ configurable: true,
+ value: width.clientWidth,
+ });
+ Object.defineProperty(element, "scrollWidth", {
+ configurable: true,
+ value: width.scrollWidth,
+ });
+};
+
+describe("ResultStatusBar", () => {
+ beforeEach(() => {
+ resizeCallbacks = [];
+ });
+
+ test("lets the database label shrink while the statement truncates", () => {
+ render(
+ <ResultStatusBar
+ database={database}
+ statement="SELECT db.environment as env, db.instance as ins FROM db JOIN project on db.project = project.resource_id WHERE project.resource_id = 'a' AND db.deleted = false"
+ queryTime="4 ms"
+ />
+ );
+
+ const statusLeft = screen.getByTestId("result-status-left");
+ expect(statusLeft.className).toContain("min-w-0");
+ expect(statusLeft.className).toContain("flex-1");
+
+ const databaseLabel = screen.getByTestId("result-status-database");
+ expect(databaseLabel.className).toContain("min-w-0");
+ expect(databaseLabel.className).toContain("overflow-hidden");
+ expect(databaseLabel.className).toContain("whitespace-nowrap");
+
+ const statement = screen.getByTestId("result-status-statement");
+ expect(statement.className).toContain("min-w-0");
+ expect(statement.className).toContain("flex-1");
+ expect(statement.className).not.toContain("max-w-3xl");
+ expect(statement.textContent).toContain("SELECT db.environment");
+ });
+
+ test("does not cap rich database names outside the status bar", () => {
+ render(<RichDatabaseName database={database} />);
+
+ const databaseLabel = screen.getByTestId("result-status-database");
+ expect(databaseLabel.className).not.toContain("max-w-[45%]");
+ });
+
+ test("lets the statement use available space while keeping copy attached", () => {
+ render(
+ <ResultStatusBar
+ database={database}
+ statement="SELECT db.environment as env, db.instance as ins FROM db JOIN project on db.project = project.resource_id WHERE project.resource_id = 'a' AND 2=2 AND 3=3 LIMIT 50"
+ queryTime="7 ms"
+ />
+ );
+
+ const statement = screen.getByTestId("result-status-statement");
+ const copyButton = screen.getByRole("button", { name: "common.copy" });
+ const statementText = statement.querySelector("span");
+
+ expect(statement.className).toContain("flex-1");
+ expect(statement.className).not.toContain("max-w-3xl");
+ expect(statementText?.classList.contains("flex-1")).toBe(false);
+ expect(copyButton.className).toContain("shrink-0");
+ });
+
+ test("hides the database label when it would constrain the statement", () => {
+ render(
+ <ResultStatusBar
+ database={database}
+ statement="EXPLAIN SELECT db.environment as env, db.instance as ins FROM db JOIN project on db.project = project.resource_id WHERE project.resource_id = 'a' AND db.deleted = false"
+ queryTime="6 ms"
+ showVisualizeButton
+ />
+ );
+
+ const statusLeft = screen.getByTestId("result-status-left");
+ const databaseLabel = screen.getByTestId("result-status-database");
+ const statement = screen.getByTestId("result-status-statement");
+
+ setElementWidth(statusLeft, { clientWidth: 520, scrollWidth: 520 });
+ setElementWidth(databaseLabel, { clientWidth: 210, scrollWidth: 210 });
+ setElementWidth(statement, { clientWidth: 310, scrollWidth: 900 });
+
+ act(() => {
+ for (const callback of resizeCallbacks) {
+ callback([] as unknown as ResizeObserverEntry[], {
+ disconnect: vi.fn(),
+ observe: vi.fn(),
+ unobserve: vi.fn(),
+ });
+ }
+ });
+
+ expect(databaseLabel.classList.contains("hidden")).toBe(true);
+ });
+
+ test("keeps a long database label when the capped width still fits", () => {
+ render(
+ <ResultStatusBar
+ database={database}
+ statement="SELECT 1"
+ queryTime="3 ms"
+ />
+ );
+
+ const statusLeft = screen.getByTestId("result-status-left");
+ const databaseLabel = screen.getByTestId("result-status-database");
+ const statement = screen.getByTestId("result-status-statement");
+
+ setElementWidth(statusLeft, { clientWidth: 520, scrollWidth: 520 });
+ setElementWidth(databaseLabel, { clientWidth: 230, scrollWidth: 900 });
+ setElementWidth(statement, { clientWidth: 250, scrollWidth: 250 });
+
+ act(() => {
+ for (const callback of resizeCallbacks) {
+ callback([] as unknown as ResizeObserverEntry[], {
+ disconnect: vi.fn(),
+ observe: vi.fn(),
+ unobserve: vi.fn(),
+ });
+ }
+ });
+
+ expect(databaseLabel.classList.contains("hidden")).toBe(false);
+ });
+});
diff --git a/frontend/src/react/components/sql-editor/ResultView/ResultStatusBar.tsx b/frontend/src/react/components/sql-editor/ResultView/ResultStatusBar.tsx
new file mode 100644
index 0000000000..d38a85db86
--- /dev/null
+++ b/frontend/src/react/components/sql-editor/ResultView/ResultStatusBar.tsx
@@ -0,0 +1,160 @@
+import { ChevronRightIcon } from "lucide-react";
+import { type Ref, useLayoutEffect, useRef, useState } from "react";
+import { useTranslation } from "react-i18next";
+import { EngineIcon } from "@/react/components/EngineIcon";
+import { Button } from "@/react/components/ui/button";
+import { CopyButton } from "@/react/components/ui/copy-button";
+import { EllipsisText } from "@/react/components/ui/ellipsis-text";
+import { cn } from "@/react/lib/utils";
+import type { Database } from "@/types/proto-es/v1/database_service_pb";
+import {
+ extractDatabaseResourceName,
+ getDatabaseEnvironment,
+ getInstanceResource,
+} from "@/utils/v1/database";
+import { instanceV1Name } from "@/utils/v1/instance";
+
+type ResultStatusBarProps = Readonly<{
+ database: Database;
+ statement: string;
+ queryTime: string;
+ showVisualizeButton?: boolean;
+ onVisualizeExplain?: () => void;
+}>;
+
+export function ResultStatusBar({
+ database,
+ statement,
+ queryTime,
+ showVisualizeButton = false,
+ onVisualizeExplain,
+}: ResultStatusBarProps) {
+ const { t } = useTranslation();
+ const statusLeftRef = useRef<HTMLDivElement>(null);
+ const databaseRef = useRef<HTMLDivElement>(null);
+ const statementRef = useRef<HTMLDivElement>(null);
+ const databaseWidthRef = useRef(0);
+ const [hideDatabase, setHideDatabase] = useState(false);
+
+ useLayoutEffect(() => {
+ const update = () => {
+ const statusLeft = statusLeftRef.current;
+ const databaseLabel = databaseRef.current;
+ const statementLabel = statementRef.current;
+ if (!statusLeft || !databaseLabel || !statementLabel) return;
+
+ const databaseWidth =
+ databaseLabel.getBoundingClientRect().width ||
+ databaseLabel.clientWidth;
+ if (databaseWidth > 0) {
+ databaseWidthRef.current = databaseWidth;
+ }
+
+ const statementText = statementLabel.querySelector("span");
+ const statementWidth = Math.max(
+ statementLabel.scrollWidth,
+ statementText?.scrollWidth ?? 0
+ );
+ setHideDatabase(
+ databaseWidthRef.current > 0 &&
+ statementWidth + databaseWidthRef.current > statusLeft.clientWidth
+ );
+ };
+
+ update();
+ const observer = new ResizeObserver(update);
+ if (statusLeftRef.current) observer.observe(statusLeftRef.current);
+ if (databaseRef.current) observer.observe(databaseRef.current);
+ if (statementRef.current) observer.observe(statementRef.current);
+ return () => observer.disconnect();
+ }, [statement]);
+
+ return (
+ <div className="w-full min-w-0 flex items-center justify-between text-xs mt-1 gap-x-4 text-control-light">
+ <div
+ ref={statusLeftRef}
+ className="flex min-w-0 flex-1 items-center gap-x-2 overflow-hidden"
+ data-testid="result-status-left"
+ >
+ <RichDatabaseName
+ ref={databaseRef}
+ database={database}
+ hidden={hideDatabase}
+ className="max-w-[45%] shrink"
+ />
+ <div
+ ref={statementRef}
+ className="flex min-w-0 flex-1 items-center gap-x-1"
+ data-testid="result-status-statement"
+ >
+ <EllipsisText
+ text={statement}
+ className="min-w-0 max-w-full truncate"
+ />
+ <CopyButton
+ content={statement}
+ size="xs"
+ appearance="secondary"
+ className="h-auto shrink-0 px-0 text-control-light hover:bg-transparent hover:text-control"
+ />
+ </div>
+ </div>
+ <div className="flex shrink-0 items-center gap-x-2">
+ {showVisualizeButton && (
+ <Button
+ size="sm"
+ appearance="link"
+ className="h-auto px-0 text-xs"
+ onClick={onVisualizeExplain}
+ >
+ {t("sql-editor.visualize-explain")}
+ </Button>
+ )}
+ <span>
+ {t("sql-editor.query-time")}: {queryTime}
+ </span>
+ </div>
+ </div>
+ );
+}
+
+/**
+ * Inline simplified renderer mirroring the visible output of Vue
+ * `RichDatabaseName` with default props: engine icon + instance title +
+ * chevron + environment + database. Skips the popover-on-hover branch
+ * (only used by `tooltip="instance"` callers; result-view doesn't set it).
+ */
+export function RichDatabaseName({
+ ref,
+ database,
+ hidden = false,
+ className,
+}: Readonly<{
+ ref?: Ref<HTMLDivElement>;
+ database: Database;
+ hidden?: boolean;
+ className?: string;
+}>) {
+ const instance = getInstanceResource(database);
+ const environment = getDatabaseEnvironment(database);
+ const { databaseName } = extractDatabaseResourceName(database.name);
+ return (
+ <div
+ ref={ref}
+ className={cn(
+ "flex min-w-0 items-center gap-x-1 overflow-hidden whitespace-nowrap",
+ className,
+ hidden && "hidden"
+ )}
+ data-testid="result-status-database"
+ >
+ <EngineIcon engine={instance.engine} className="size-4 shrink-0" />
+ <span className="truncate">{instanceV1Name(instance)}</span>
+ <ChevronRightIcon className="size-3 shrink-0" />
+ <span className="flex min-w-0 items-center gap-x-1 overflow-hidden">
+ <span className="truncate text-control-light">{environment.title}</span>
+ <span className="truncate">{databaseName}</span>
+ </span>
+ </div>
+ );
+}
diff --git a/frontend/src/react/components/sql-editor/ResultView/SingleResultView.tsx b/frontend/src/react/components/sql-editor/ResultView/SingleResultView.tsx
index c71eff46c1..ba47905995 100644
--- a/frontend/src/react/components/sql-editor/ResultView/SingleResultView.tsx
+++ b/frontend/src/react/components/sql-editor/ResultView/SingleResultView.tsx
@@ -4,7 +4,6 @@ import {
ArrowDownIcon,
ArrowUpIcon,
ChevronDownIcon,
- ChevronRightIcon,
CopyIcon,
InfoIcon,
XIcon,
@@ -28,7 +27,6 @@ import {
DataExportButton,
type DataExportRequest,
} from "@/react/components/DataExportButton";
-import { EngineIcon } from "@/react/components/EngineIcon";
import { Alert } from "@/react/components/ui/alert";
import { Button } from "@/react/components/ui/button";
import {
@@ -37,12 +35,10 @@ import {
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/react/components/ui/dropdown-menu";
-import { EllipsisText } from "@/react/components/ui/ellipsis-text";
import { Switch } from "@/react/components/ui/switch";
import { Tooltip } from "@/react/components/ui/tooltip";
import { useExecuteSQL } from "@/react/hooks/useExecuteSQL";
import { useSQLEditorQueryDataPolicy } from "@/react/hooks/useSQLEditorBridge";
-import { writeTextToClipboard } from "@/react/lib/clipboard";
import { cn } from "@/react/lib/utils";
import { useAppStore } from "@/react/stores/app";
import { useSQLEditorEditorState } from "@/react/stores/sqlEditor/editor";
@@ -66,18 +62,14 @@ import {
} from "@/utils/sqlResult";
import { STORAGE_KEY_SQL_EDITOR_NOSQL_TABLE_VIEW } from "@/utils/storage-keys";
import { isNullOrUndefined } from "@/utils/util";
-import {
- extractDatabaseResourceName,
- getDatabaseEnvironment,
- getInstanceResource,
-} from "@/utils/v1/database";
-import { instanceV1Name } from "@/utils/v1/instance";
+import { getInstanceResource } from "@/utils/v1/database";
import { compareQueryRowValues, extractSQLRowValuePlain } from "@/utils/v1/sql";
import { SQLResultViewProvider, useSelectionContext } from "./context";
import { formatAsCSV, formatAsSQL, formatAsText } from "./copy-formats";
import { DetailPanel } from "./DetailPanel";
import { EmptyView } from "./EmptyView";
import { ErrorView } from "./ErrorView";
+import { ResultStatusBar, RichDatabaseName } from "./ResultStatusBar";
import { SelectionCopyTooltips } from "./SelectionCopyTooltips";
import type { ResultTableColumn, ResultTableRow, SortState } from "./types";
import {
@@ -761,33 +753,13 @@ function SingleResultViewInner({
</div>
{/* Status bar */}
- <div className="w-full flex items-center justify-between text-xs mt-1 gap-x-4 text-control-light">
- <div className="flex items-center gap-x-2">
- <RichDatabaseName database={database} />
- <div className="flex items-center gap-x-1 min-w-0">
- <EllipsisText
- text={result.statement ?? ""}
- className="truncate"
- />
- <CopyInlineButton text={result.statement ?? ""} />
- </div>
- </div>
- <div className="flex shrink-0 items-center gap-x-2">
- {showVisualizeButton && (
- <Button
- size="sm"
- appearance="link"
- className="h-auto px-0 text-xs"
- onClick={visualizeExplain}
- >
- {t("sql-editor.visualize-explain")}
- </Button>
- )}
- <span>
- {t("sql-editor.query-time")}: {queryTime}
- </span>
- </div>
- </div>
+ <ResultStatusBar
+ database={database}
+ statement={result.statement ?? ""}
+ queryTime={queryTime}
+ showVisualizeButton={showVisualizeButton}
+ onVisualizeExplain={visualizeExplain}
+ />
</>
)}
@@ -828,29 +800,6 @@ function useLocalStorageBoolean(
return [value, update];
}
-/**
- * Inline simplified renderer mirroring the visible output of Vue
- * `RichDatabaseName` with default props: engine icon + instance title +
- * chevron + environment + database. Skips the popover-on-hover branch
- * (only used by `tooltip="instance"` callers; result-view doesn't set it).
- */
-function RichDatabaseName({ database }: { database: Database }) {
- const instance = getInstanceResource(database);
- const environment = getDatabaseEnvironment(database);
- const { databaseName } = extractDatabaseResourceName(database.name);
- return (
- <div className="flex flex-row justify-start items-center gap-x-1">
- <EngineIcon engine={instance.engine} className="size-4" />
- <span>{instanceV1Name(instance)}</span>
- <ChevronRightIcon className="size-3" />
- <span className="flex flex-row items-center gap-x-1">
- <span className="text-control-light">{environment.title}</span>
- <span>{databaseName}</span>
- </span>
- </div>
- );
-}
-
/**
* Inline simplified `DatabaseInfo` for the export drawer's form-prefix slot.
* Vue version showed `[engine] instance > [env] database`; we mirror that.
@@ -865,28 +814,6 @@ function DatabaseInfo({ database }: { database: Database }) {
);
}
-function CopyInlineButton({ text }: { text: string }) {
- const [copied, setCopied] = useState(false);
- const handleCopy = async () => {
- if (await writeTextToClipboard(text)) {
- setCopied(true);
- window.setTimeout(() => setCopied(false), 1500);
- } else {
- // ignore
- }
- };
- return (
- <button
- type="button"
- onClick={handleCopy}
- className="text-control-light hover:text-control"
- aria-label="copy"
- >
- <CopyIcon className={cn("size-3", copied && "text-accent")} />
- </button>
- );
-}
-
function getExplainTokenFromResult(
result: QueryResult,
engine: Engine