Skip to content

Commit defbe1f

Browse files
knudttycursoragent
andauthored
Add raw SQL chart run shortcut (#2458)
Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: Aaron Knudtson <knudtty@users.noreply.github.com>
1 parent 6a80031 commit defbe1f

4 files changed

Lines changed: 43 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hyperdx/app': patch
3+
---
4+
5+
Add Cmd/Ctrl+Enter support for running raw SQL chart queries from the SQL editor.

‎packages/app/src/components/ChartEditor/RawSqlChartEditor.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ export default function RawSqlChartEditor({
262262
placeholder={placeholderSQl}
263263
tableConnections={tableConnections}
264264
additionalCompletions={additionalCompletions}
265+
onSubmit={onSubmit}
265266
/>
266267
<div className={resizeStyles.resizeYHandle} onMouseDown={startResize} />
267268
</Box>

‎packages/app/src/components/SQLEditor/SQLEditor.tsx‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useEffect, useRef } from 'react';
1+
import { useCallback, useEffect, useMemo, useRef } from 'react';
22
import { useController, UseControllerProps } from 'react-hook-form';
33
import { acceptCompletion } from '@codemirror/autocomplete';
44
import { TableConnection } from '@hyperdx/common-utils/dist/core/metadata';
@@ -7,6 +7,7 @@ import CodeMirror, {
77
Compartment,
88
EditorView,
99
keymap,
10+
Prec,
1011
ReactCodeMirrorRef,
1112
} from '@uiw/react-codemirror';
1213

@@ -30,8 +31,17 @@ type SQLEditorProps = {
3031
additionalCompletions?: SQLCompletion[];
3132
dateRange?: [Date, Date];
3233
timestampValueExpression?: string;
34+
onSubmit?: () => void;
3335
};
3436

37+
export const createRunQueryKeyBinding = (onSubmit: () => void) => ({
38+
key: 'Mod-Enter',
39+
run: () => {
40+
onSubmit();
41+
return true;
42+
},
43+
});
44+
3545
export default function SQLEditor({
3646
onChange,
3747
placeholder,
@@ -42,11 +52,20 @@ export default function SQLEditor({
4252
additionalCompletions,
4353
dateRange,
4454
timestampValueExpression,
55+
onSubmit,
4556
}: SQLEditorProps) {
4657
const { colorScheme } = useMantineColorScheme();
4758
const ref = useRef<ReactCodeMirrorRef>(null);
4859
const compartmentRef = useRef<Compartment>(new Compartment());
4960

61+
const runQueryKeymap = useMemo(
62+
() =>
63+
onSubmit == null
64+
? []
65+
: [Prec.highest(keymap.of([createRunQueryKeyBinding(onSubmit)]))],
66+
[onSubmit],
67+
);
68+
5069
const { data: fields } = useMultipleAllFields(tableConnections ?? [], {
5170
dateRange,
5271
timestampValueExpression,
@@ -111,6 +130,7 @@ export default function SQLEditor({
111130
upperCaseKeywords: true,
112131
}),
113132
),
133+
...runQueryKeymap,
114134
keymap.of([
115135
{
116136
key: 'Tab',
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { createRunQueryKeyBinding } from '../SQLEditor';
2+
3+
jest.mock('@/hooks/useMetadata', () => ({
4+
useMultipleAllFields: jest.fn().mockReturnValue({ data: [] }),
5+
}));
6+
7+
describe('SQLEditor', () => {
8+
it('creates a Cmd/Ctrl+Enter key binding that submits the query', () => {
9+
const onSubmit = jest.fn();
10+
const binding = createRunQueryKeyBinding(onSubmit);
11+
12+
expect(binding.key).toBe('Mod-Enter');
13+
expect(binding.run()).toBe(true);
14+
expect(onSubmit).toHaveBeenCalledTimes(1);
15+
});
16+
});

0 commit comments

Comments
 (0)