Skip to content

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

fix(sql-editor): restore the database label when the result bar widens (#21458)

Danny Xu039225f0b27a
diff --git a/frontend/src/modules/sql-editor/components/ResultView/ResultStatusBar.test.tsx b/frontend/src/modules/sql-editor/components/ResultView/ResultStatusBar.test.tsx
index 07b42f0a58..2351ba5173 100644
--- a/frontend/src/modules/sql-editor/components/ResultView/ResultStatusBar.test.tsx
+++ b/frontend/src/modules/sql-editor/components/ResultView/ResultStatusBar.test.tsx
@@ -58,7 +58,7 @@ globalThis.ResizeObserver = class ResizeObserver {
} as typeof ResizeObserver;
const setElementWidth = (
- element: HTMLElement,
+ element: Element,
width: { clientWidth: number; scrollWidth: number }
) => {
Object.defineProperty(element, "clientWidth", {
@@ -71,6 +71,34 @@ const setElementWidth = (
});
};
+// The element the bar measures: the truncating span rendered by EllipsisText.
+const statementTextOf = (statement: HTMLElement) => {
+ const text = statement.querySelector("span");
+ if (!text) throw new Error("statement text span not rendered");
+ return text;
+};
+
+// jsdom reports every rect as zero, so the bar measures no trailing controls
+// unless a test places them. Only `right` is read.
+const setRightEdge = (element: Element, right: number) => {
+ Object.defineProperty(element, "getBoundingClientRect", {
+ configurable: true,
+ value: () => ({ right, left: 0, width: right }) as DOMRect,
+ });
+};
+
+const flushResize = () => {
+ act(() => {
+ for (const callback of resizeCallbacks) {
+ callback([] as unknown as ResizeObserverEntry[], {
+ disconnect: vi.fn(),
+ observe: vi.fn(),
+ unobserve: vi.fn(),
+ });
+ }
+ });
+};
+
describe("ResultStatusBar", () => {
beforeEach(() => {
resizeCallbacks = [];
@@ -169,20 +197,14 @@ describe("ResultStatusBar", () => {
const statusLeft = screen.getByTestId("result-status-left");
const databaseLabel = screen.getByTestId("result-status-database");
const statement = screen.getByTestId("result-status-statement");
+ const statementText = statementTextOf(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(),
- });
- }
- });
+ setElementWidth(statement, { clientWidth: 310, scrollWidth: 310 });
+ setElementWidth(statementText, { clientWidth: 282, scrollWidth: 900 });
+
+ flushResize();
expect(databaseLabel.classList.contains("hidden")).toBe(true);
});
@@ -202,18 +224,113 @@ describe("ResultStatusBar", () => {
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(),
- });
- }
+ setElementWidth(statement, { clientWidth: 282, scrollWidth: 282 });
+ setElementWidth(statementTextOf(statement), {
+ clientWidth: 250,
+ scrollWidth: 250,
});
+ flushResize();
+
+ expect(databaseLabel.classList.contains("hidden")).toBe(false);
+ });
+
+ test("counts the copy control and the row gaps against the space left", () => {
+ const { rerender } = render(
+ <ResultStatusBar
+ database={database}
+ statement="SELECT * FROM employee WHERE id = 1"
+ queryTime="5 ms"
+ />
+ );
+
+ const statusLeft = screen.getByTestId("result-status-left");
+ const databaseLabel = screen.getByTestId("result-status-database");
+ const statement = screen.getByTestId("result-status-statement");
+ const statementText = statementTextOf(statement);
+ const copyButton = screen.getByRole("button", { name: "common.copy" });
+
+ // 250px of text, then a 4px gap and a 24px copy control; the label is 230px
+ // and sits 8px from the statement. The row needs 516px in all.
+ statusLeft.style.columnGap = "8px";
+ setElementWidth(databaseLabel, { clientWidth: 230, scrollWidth: 230 });
+ setElementWidth(statementText, { clientWidth: 250, scrollWidth: 250 });
+ setRightEdge(statementText, 250);
+ setRightEdge(copyButton, 278);
+
+ setElementWidth(statusLeft, { clientWidth: 500, scrollWidth: 500 });
+ flushResize();
+
+ // The text alone would fit beside the label in 500px; the copy control and
+ // the gaps are what push the row over, so the label has to yield.
+ expect(databaseLabel.classList.contains("hidden")).toBe(true);
+
+ setElementWidth(statusLeft, { clientWidth: 520, scrollWidth: 520 });
+ flushResize();
+
+ expect(databaseLabel.classList.contains("hidden")).toBe(false);
+
+ // An empty statement drops the copy control, so nothing trails the text.
+ rerender(
+ <ResultStatusBar database={database} statement="" queryTime="5 ms" />
+ );
+ setElementWidth(statementTextOf(statement), {
+ clientWidth: 0,
+ scrollWidth: 0,
+ });
+ setElementWidth(statusLeft, { clientWidth: 240, scrollWidth: 240 });
+ flushResize();
+
+ expect(databaseLabel.classList.contains("hidden")).toBe(false);
+ });
+
+ test("shows the database label again when the container widens back", () => {
+ 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");
+ const databaseLabel = screen.getByTestId("result-status-database");
+ const statement = screen.getByTestId("result-status-statement");
+ const statementText = statementTextOf(statement);
+
+ // Widths measured in Chromium at a 1440px viewport. The statement row
+ // stretches to fill whatever the label leaves, while the text span stays at
+ // its intrinsic 994px in every state below.
+ setElementWidth(statusLeft, { clientWidth: 1319, scrollWidth: 1319 });
+ setElementWidth(databaseLabel, { clientWidth: 233, scrollWidth: 233 });
+ setElementWidth(statement, { clientWidth: 1078, scrollWidth: 1078 });
+ setElementWidth(statementText, { clientWidth: 994, scrollWidth: 994 });
+
+ flushResize();
+
+ expect(databaseLabel.classList.contains("hidden")).toBe(false);
+
+ // Narrowed to a 600px viewport: the statement no longer fits beside the
+ // label, so the label gives up its room.
+ setElementWidth(statusLeft, { clientWidth: 479, scrollWidth: 479 });
+ setElementWidth(databaseLabel, { clientWidth: 216, scrollWidth: 216 });
+ setElementWidth(statement, { clientWidth: 256, scrollWidth: 256 });
+ setElementWidth(statementText, { clientWidth: 224, scrollWidth: 994 });
+
+ flushResize();
+
+ expect(databaseLabel.classList.contains("hidden")).toBe(true);
+
+ // Back to 1440px. The hidden label now measures zero and the statement row
+ // stretches across the whole width, so only the text span still reports
+ // what the statement actually needs.
+ setElementWidth(statusLeft, { clientWidth: 1319, scrollWidth: 1319 });
+ setElementWidth(databaseLabel, { clientWidth: 0, scrollWidth: 0 });
+ setElementWidth(statement, { clientWidth: 1319, scrollWidth: 1319 });
+ setElementWidth(statementText, { clientWidth: 994, scrollWidth: 994 });
+
+ flushResize();
+
expect(databaseLabel.classList.contains("hidden")).toBe(false);
});
});
diff --git a/frontend/src/modules/sql-editor/components/ResultView/ResultStatusBar.tsx b/frontend/src/modules/sql-editor/components/ResultView/ResultStatusBar.tsx
index 158b93ab81..09ec7914d8 100644
--- a/frontend/src/modules/sql-editor/components/ResultView/ResultStatusBar.tsx
+++ b/frontend/src/modules/sql-editor/components/ResultView/ResultStatusBar.tsx
@@ -17,6 +17,20 @@ export const formatQueryTime = (latency: QueryResult["latency"]): string => {
return `${totalSeconds.toFixed(2)} s`;
};
+const columnGapWidth = (element: HTMLElement) =>
+ parseFloat(getComputedStyle(element).columnGap) || 0;
+
+// Width taken by whatever trails the statement text, today the copy control and
+// the gap before it. The text span is sized to its content and never stretches,
+// so the trailing controls sit directly after it at every row width.
+const trailingWidth = (row: HTMLElement, text: Element | null) => {
+ const last = row.lastElementChild;
+ if (!text || !last || last === text) return 0;
+ return (
+ last.getBoundingClientRect().right - text.getBoundingClientRect().right
+ );
+};
+
type ResultStatusBarProps = Readonly<{
database: Database;
statement: string;
@@ -37,7 +51,7 @@ export function ResultStatusBar({
const statusLeftRef = useRef<HTMLDivElement>(null);
const databaseRef = useRef<HTMLDivElement>(null);
const statementRef = useRef<HTMLDivElement>(null);
- const databaseWidthRef = useRef(0);
+ const databaseFootprintRef = useRef(0);
const [hideDatabase, setHideDatabase] = useState(false);
useLayoutEffect(() => {
@@ -51,17 +65,24 @@ export function ResultStatusBar({
databaseLabel.getBoundingClientRect().width ||
databaseLabel.clientWidth;
if (databaseWidth > 0) {
- databaseWidthRef.current = databaseWidth;
+ // What showing the label costs the statement: its own width plus the
+ // gap it puts between itself and the statement.
+ databaseFootprintRef.current =
+ databaseWidth + columnGapWidth(statusLeft);
}
+ // Measure the truncating text span, not its row wrapper. The span is
+ // sized to its content, so its scrollWidth is the statement's full width
+ // whether or not the database label is in layout; the wrapper stretches
+ // to fill the row, so measuring it would keep a hidden label hidden at
+ // every width.
const statementText = statementLabel.querySelector("span");
- const statementWidth = Math.max(
- statementLabel.scrollWidth,
- statementText?.scrollWidth ?? 0
- );
+ const statementWidth =
+ (statementText?.scrollWidth ?? 0) +
+ trailingWidth(statementLabel, statementText);
setHideDatabase(
- databaseWidthRef.current > 0 &&
- statementWidth + databaseWidthRef.current > statusLeft.clientWidth
+ databaseFootprintRef.current > 0 &&
+ statementWidth + databaseFootprintRef.current > statusLeft.clientWidth
);
};