Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

Fix Cmd/ctrl + Enter in qb to run query (#10048)

Aditya Singh5ae7a464e675
diff --git a/frontend/src/components/QueryBuilderV2/QueryBuilderV2.tsx b/frontend/src/components/QueryBuilderV2/QueryBuilderV2.tsx
index e617ea152f..582cba3419 100644
--- a/frontend/src/components/QueryBuilderV2/QueryBuilderV2.tsx
+++ b/frontend/src/components/QueryBuilderV2/QueryBuilderV2.tsx
@@ -4,7 +4,7 @@ import { OPERATORS, PANEL_TYPES } from 'constants/queryBuilder';
import { Formula } from 'container/QueryBuilder/components/Formula';
import { QueryBuilderProps } from 'container/QueryBuilder/QueryBuilder.interfaces';
import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder';
-import { memo, useEffect, useMemo, useRef } from 'react';
+import { memo, useCallback, useEffect, useMemo, useRef } from 'react';
import { IBuilderTraceOperator } from 'types/api/queryBuilder/queryBuilderData';
import { DataSource } from 'types/common/queryBuilder';
@@ -33,6 +33,7 @@ export const QueryBuilderV2 = memo(function QueryBuilderV2({
addTraceOperator,
panelType,
initialDataSource,
+ handleRunQuery,
} = useQueryBuilder();
const containerRef = useRef(null);
@@ -157,10 +158,29 @@ export const QueryBuilderV2 = memo(function QueryBuilderV2({
[showTraceOperator, traceOperator, hasAtLeastOneTraceQuery],
);
+ const handleKeyDown = useCallback(
+ (e: React.KeyboardEvent<HTMLDivElement>): void => {
+ const target = e.target as HTMLElement | null;
+ const tagName = target?.tagName || '';
+
+ const isInputElement =
+ ['INPUT', 'TEXTAREA', 'SELECT'].includes(tagName) ||
+ (target?.getAttribute('contenteditable') || '').toLowerCase() === 'true';
+
+ // Allow input elements in qb to run the query when Cmd/Ctrl + Enter is pressed
+ if (isInputElement && (e.metaKey || e.ctrlKey) && e.key === 'Enter') {
+ e.preventDefault();
+ e.stopPropagation();
+ handleRunQuery();
+ }
+ },
+ [handleRunQuery],
+ );
+
return (
<QueryBuilderV2Provider>
<div className="query-builder-v2">
- <div className="qb-content-container">
+ <div className="qb-content-container" onKeyDownCapture={handleKeyDown}>
{!isMultiQueryAllowed ? (
<QueryV2
ref={containerRef}
diff --git a/frontend/src/components/QueryBuilderV2/QueryV2/QuerySearch/QuerySearch.tsx b/frontend/src/components/QueryBuilderV2/QueryV2/QuerySearch/QuerySearch.tsx
index de6f1223a1..b4c1104554 100644
--- a/frontend/src/components/QueryBuilderV2/QueryV2/QuerySearch/QuerySearch.tsx
+++ b/frontend/src/components/QueryBuilderV2/QueryV2/QuerySearch/QuerySearch.tsx
@@ -1379,8 +1379,6 @@ function QuerySearch({
run: (): boolean => {
if (onRun && typeof onRun === 'function') {
onRun(getCurrentExpression());
- } else {
- handleRunQuery();
}
return true;
},
diff --git a/frontend/src/components/QueryBuilderV2/QueryV2/TraceOperator/TraceOperatorEditor.tsx b/frontend/src/components/QueryBuilderV2/QueryV2/TraceOperator/TraceOperatorEditor.tsx
index 749e3f145e..9177a2b0a3 100644
--- a/frontend/src/components/QueryBuilderV2/QueryV2/TraceOperator/TraceOperatorEditor.tsx
+++ b/frontend/src/components/QueryBuilderV2/QueryV2/TraceOperator/TraceOperatorEditor.tsx
@@ -410,8 +410,6 @@ function TraceOperatorEditor({
run: (): boolean => {
if (onRun && typeof onRun === 'function') {
onRun(value);
- } else {
- handleRunQuery();
}
return true;
},
diff --git a/frontend/src/components/QueryBuilderV2/QueryV2/__tests__/QuerySearch.test.tsx b/frontend/src/components/QueryBuilderV2/QueryV2/__tests__/QuerySearch.test.tsx
index 8430272b5e..bf38fbb28d 100644
--- a/frontend/src/components/QueryBuilderV2/QueryV2/__tests__/QuerySearch.test.tsx
+++ b/frontend/src/components/QueryBuilderV2/QueryV2/__tests__/QuerySearch.test.tsx
@@ -270,44 +270,6 @@ describe('QuerySearch (Integration with Real CodeMirror)', () => {
await waitFor(() => expect(onRun).toHaveBeenCalled(), { timeout: 2000 });
});
- it('calls handleRunQuery when Mod-Enter without onRun', async () => {
- const mockedHandleRunQuery = handleRunQueryMock as jest.MockedFunction<
- () => void
- >;
- mockedHandleRunQuery.mockClear();
-
- render(
- <QuerySearch
- onChange={jest.fn() as jest.MockedFunction<(v: string) => void>}
- queryData={initialQueriesMap.logs.builder.queryData[0]}
- dataSource={DataSource.LOGS}
- />,
- );
-
- // Wait for CodeMirror to initialize
- await waitFor(() => {
- const editor = document.querySelector(CM_EDITOR_SELECTOR);
- expect(editor).toBeInTheDocument();
- });
-
- const editor = document.querySelector(CM_EDITOR_SELECTOR) as HTMLElement;
- await userEvent.click(editor);
- await userEvent.type(editor, SAMPLE_VALUE_TYPING_COMPLETE);
-
- // Use fireEvent for keyboard shortcuts as userEvent might not work well with CodeMirror
- const modKey = navigator.platform.includes('Mac') ? 'metaKey' : 'ctrlKey';
- fireEvent.keyDown(editor, {
- key: 'Enter',
- code: 'Enter',
- [modKey]: true,
- keyCode: 13,
- });
-
- await waitFor(() => expect(mockedHandleRunQuery).toHaveBeenCalled(), {
- timeout: 2000,
- });
- });
-
it('initializes CodeMirror with expression from queryData.filter.expression on mount', async () => {
const testExpression =
"http.status_code >= 500 AND service.name = 'frontend'";
diff --git a/frontend/src/components/QueryBuilderV2/QueryV2/__tests__/QueryV2.test.tsx b/frontend/src/components/QueryBuilderV2/QueryV2/__tests__/QueryV2.test.tsx
index 66d3209714..00b9cda4fd 100644
--- a/frontend/src/components/QueryBuilderV2/QueryV2/__tests__/QueryV2.test.tsx
+++ b/frontend/src/components/QueryBuilderV2/QueryV2/__tests__/QueryV2.test.tsx
@@ -3,14 +3,21 @@
import '@testing-library/jest-dom';
import { jest } from '@jest/globals';
+import { fireEvent, waitFor } from '@testing-library/react';
+import { PANEL_TYPES } from 'constants/queryBuilder';
import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder';
import { useQueryOperations } from 'hooks/queryBuilder/useQueryBuilderOperations';
import { render, screen, userEvent } from 'tests/test-utils';
-import { Having, IBuilderQuery } from 'types/api/queryBuilder/queryBuilderData';
+import {
+ Having,
+ IBuilderQuery,
+ Query,
+} from 'types/api/queryBuilder/queryBuilderData';
+import { EQueryType } from 'types/common/dashboard';
import { UseQueryOperations } from 'types/common/operations.types';
import { DataSource, QueryBuilderContextType } from 'types/common/queryBuilder';
-import { QueryV2 } from '../QueryV2';
+import { QueryBuilderV2 } from '../../QueryBuilderV2';
// Local mocks for domain-specific heavy child components
jest.mock(
@@ -20,13 +27,6 @@ jest.mock(
return <div>QueryAggregation</div>;
},
);
-jest.mock(
- '../QuerySearch/QuerySearch',
- () =>
- function () {
- return <div>QuerySearch</div>;
- },
-);
jest.mock(
'../MerticsAggregateSection/MetricsAggregateSection',
() =>
@@ -43,16 +43,87 @@ const mockedUseQueryOperations = jest.mocked(
useQueryOperations,
) as jest.MockedFunction<UseQueryOperations>;
-describe('QueryV2 - base render', () => {
+describe('QueryBuilderV2 + QueryV2 - base render', () => {
+ let handleRunQueryMock: jest.MockedFunction<() => void>;
+
beforeEach(() => {
const mockCloneQuery = jest.fn() as jest.MockedFunction<
(type: string, q: IBuilderQuery) => void
>;
+ handleRunQueryMock = jest.fn() as jest.MockedFunction<() => void>;
+ const baseQuery: IBuilderQuery = {
+ queryName: 'A',
+ dataSource: DataSource.LOGS,
+ aggregateOperator: '',
+ aggregations: [],
+ timeAggregation: '',
+ spaceAggregation: '',
+ temporality: '',
+ functions: [],
+ filter: undefined,
+ filters: { items: [], op: 'AND' },
+ groupBy: [],
+ expression: '',
+ disabled: false,
+ having: [] as Having[],
+ limit: 10,
+ stepInterval: null,
+ orderBy: [],
+ legend: 'A',
+ };
+
+ const currentQueryObj: Query = {
+ id: 'test',
+ unit: undefined,
+ queryType: EQueryType.CLICKHOUSE,
+ promql: [],
+ clickhouse_sql: [],
+ builder: {
+ queryData: [baseQuery],
+ queryFormulas: [],
+ queryTraceOperator: [],
+ },
+ };
+
+ const updateAllQueriesOperators: QueryBuilderContextType['updateAllQueriesOperators'] = (
+ q,
+ ) => q;
+ const updateQueriesData: QueryBuilderContextType['updateQueriesData'] = (q) =>
+ q;
mockedUseQueryBuilder.mockReturnValue(({
- // Only fields used by QueryV2
+ currentQuery: currentQueryObj,
+ stagedQuery: null,
+ lastUsedQuery: null,
+ setLastUsedQuery: jest.fn(),
+ supersetQuery: currentQueryObj,
+ setSupersetQuery: jest.fn(),
+ initialDataSource: null,
+ panelType: PANEL_TYPES.TABLE,
+ isEnabledQuery: true,
+ handleSetQueryData: jest.fn(),
+ handleSetTraceOperatorData: jest.fn(),
+ handleSetFormulaData: jest.fn(),
+ handleSetQueryItemData: jest.fn(),
+ handleSetConfig: jest.fn(),
+ removeQueryBuilderEntityByIndex: jest.fn(),
+ removeAllQueryBuilderEntities: jest.fn(),
+ removeQueryTypeItemByIndex: jest.fn(),
+ addNewBuilderQuery: jest.fn(),
+ addNewFormula: jest.fn(),
+ removeTraceOperator: jest.fn(),
+ addTraceOperator: jest.fn(),
cloneQuery: mockCloneQuery,
- panelType: null,
+ addNewQueryItem: jest.fn(),
+ redirectWithQueryBuilderData: jest.fn(),
+ handleRunQuery: handleRunQueryMock,
+ resetQuery: jest.fn(),
+ handleOnUnitsChange: jest.fn(),
+ updateAllQueriesOperators,
+ updateQueriesData,
+ initQueryBuilderData: jest.fn(),
+ isStagedQueryUpdated: jest.fn(() => false),
+ isDefaultQuery: jest.fn(() => false),
} as unknown) as QueryBuilderContextType);
mockedUseQueryOperations.mockReturnValue({
@@ -77,41 +148,8 @@ describe('QueryV2 - base render', () => {
jest.clearAllMocks();
});
- it('renders limit input when dataSource is logs', async () => {
- const baseQuery: IBuilderQuery = {
- queryName: 'A',
- dataSource: DataSource.LOGS,
- aggregateOperator: '',
- aggregations: [],
- timeAggregation: '',
- spaceAggregation: '',
- temporality: '',
- functions: [],
- filter: undefined,
- filters: { items: [], op: 'AND' },
- groupBy: [],
- expression: '',
- disabled: false,
- having: [] as Having[],
- limit: 10,
- stepInterval: null,
- orderBy: [],
- legend: 'A',
- };
-
- render(
- <QueryV2
- index={0}
- isAvailableToDisable
- query={baseQuery}
- version="v4"
- onSignalSourceChange={jest.fn() as jest.MockedFunction<(v: string) => void>}
- signalSourceChangeEnabled={false}
- queriesCount={1}
- showTraceOperator={false}
- hasTraceOperator={false}
- />,
- );
+ it('renders limit input when dataSource is logs', () => {
+ render(<QueryBuilderV2 panelType={PANEL_TYPES.TABLE} version="v4" />);
// Ensure the Limit add-on input is present and is of type number
const limitInput = screen.getByPlaceholderText(
@@ -121,13 +159,44 @@ describe('QueryV2 - base render', () => {
expect(limitInput).toHaveAttribute('type', 'number');
expect(limitInput).toHaveAttribute('name', 'limit');
expect(limitInput).toHaveAttribute('data-testid', 'input-Limit');
+ });
- // Clear the input and ensure it stays visible (post-mount should not auto-hide)
- const user = userEvent.setup();
- await user.click(limitInput);
- expect(limitInput.value).toBe('10');
- await user.keyboard('{Backspace>2}'); // press backspace twice
- expect(limitInput.value).toBe('');
- expect(limitInput).toBeInTheDocument();
+ it('Cmd+Enter on an input triggers handleRunQuery via container handler', async () => {
+ render(<QueryBuilderV2 panelType={PANEL_TYPES.TABLE} version="v4" />);
+
+ const limitInput = screen.getByPlaceholderText('Enter limit');
+ fireEvent.keyDown(limitInput, {
+ key: 'Enter',
+ code: 'Enter',
+ metaKey: true,
+ });
+
+ expect(handleRunQueryMock).toHaveBeenCalled();
+
+ const legendInput = screen.getByPlaceholderText('Write legend format');
+ fireEvent.keyDown(legendInput, {
+ key: 'Enter',
+ code: 'Enter',
+ metaKey: true,
+ });
+
+ expect(handleRunQueryMock).toHaveBeenCalled();
+
+ const CM_EDITOR_SELECTOR = '.cm-editor .cm-content';
+ // Wait for CodeMirror to initialize
+ await waitFor(() => {
+ const editor = document.querySelector(CM_EDITOR_SELECTOR);
+ expect(editor).toBeInTheDocument();
+ });
+
+ const editor = document.querySelector(CM_EDITOR_SELECTOR) as HTMLElement;
+ await userEvent.click(editor);
+ fireEvent.keyDown(editor, {
+ key: 'Enter',
+ code: 'Enter',
+ metaKey: true,
+ });
+
+ expect(handleRunQueryMock).toHaveBeenCalled();
});
});