Skip to content

Commit 9a5bcb6

Browse files
authored
revert: removed changes done for cursor position jump fix (#9193)
1 parent 96cdf21 commit 9a5bcb6

File tree

2 files changed

+11
-80
lines changed

2 files changed

+11
-80
lines changed

frontend/src/components/QueryBuilderV2/QueryV2/QuerySearch/QuerySearch.tsx

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function QuerySearch({
9797
onRun?: (query: string) => void;
9898
}): JSX.Element {
9999
const isDarkMode = useIsDarkMode();
100-
const [query, setQuery] = useState<string>('');
100+
const [query, setQuery] = useState<string>(queryData.filter?.expression || '');
101101
const [valueSuggestions, setValueSuggestions] = useState<any[]>([]);
102102
const [activeKey, setActiveKey] = useState<string>('');
103103
const [isLoadingSuggestions, setIsLoadingSuggestions] = useState(false);
@@ -108,10 +108,6 @@ function QuerySearch({
108108
errors: [],
109109
});
110110

111-
const [cursorPos, setCursorPos] = useState({ line: 0, ch: 0 });
112-
const [isFocused, setIsFocused] = useState(false);
113-
const [hasInteractedWithQB, setHasInteractedWithQB] = useState(false);
114-
115111
const handleQueryValidation = (newQuery: string): void => {
116112
try {
117113
const validationResponse = validateQuery(newQuery);
@@ -131,28 +127,13 @@ function QuerySearch({
131127

132128
useEffect(() => {
133129
const newQuery = queryData.filter?.expression || '';
134-
// Only update query from external source when editor is not focused
135-
// When focused, just update the lastExternalQuery to track changes
130+
// Only mark as external change if the query actually changed from external source
136131
if (newQuery !== lastExternalQuery) {
137132
setQuery(newQuery);
138133
setIsExternalQueryChange(true);
139134
setLastExternalQuery(newQuery);
140135
}
141-
// eslint-disable-next-line react-hooks/exhaustive-deps
142-
}, [queryData.filter?.expression]);
143-
144-
useEffect(() => {
145-
// Update the query when the editor is blurred and the query has changed
146-
// Only call onChange if the editor has been focused before (not on initial mount)
147-
if (
148-
!isFocused &&
149-
hasInteractedWithQB &&
150-
query !== queryData.filter?.expression
151-
) {
152-
onChange(query);
153-
}
154-
// eslint-disable-next-line react-hooks/exhaustive-deps
155-
}, [isFocused]);
136+
}, [queryData.filter?.expression, lastExternalQuery]);
156137

157138
// Validate query when it changes externally (from queryData)
158139
useEffect(() => {
@@ -168,6 +149,9 @@ function QuerySearch({
168149

169150
const [showExamples] = useState(false);
170151

152+
const [cursorPos, setCursorPos] = useState({ line: 0, ch: 0 });
153+
const [isFocused, setIsFocused] = useState(false);
154+
171155
const [
172156
isFetchingCompleteValuesList,
173157
setIsFetchingCompleteValuesList,
@@ -181,9 +165,6 @@ function QuerySearch({
181165
const lastFetchedKeyRef = useRef<string>('');
182166
const lastValueRef = useRef<string>('');
183167
const isMountedRef = useRef<boolean>(true);
184-
const [shouldRunQueryPostUpdate, setShouldRunQueryPostUpdate] = useState(
185-
false,
186-
);
187168

188169
const { handleRunQuery } = useQueryBuilder();
189170

@@ -229,7 +210,6 @@ function QuerySearch({
229210

230211
return (): void => clearTimeout(timeoutId);
231212
},
232-
// eslint-disable-next-line react-hooks/exhaustive-deps
233213
[isFocused],
234214
);
235215

@@ -575,6 +555,7 @@ function QuerySearch({
575555

576556
const handleChange = (value: string): void => {
577557
setQuery(value);
558+
onChange(value);
578559
// Mark as internal change to avoid triggering external validation
579560
setIsExternalQueryChange(false);
580561
// Update lastExternalQuery to prevent external validation trigger
@@ -1238,25 +1219,6 @@ function QuerySearch({
12381219
</div>
12391220
);
12401221

1241-
// Effect to handle query run after update
1242-
useEffect(
1243-
() => {
1244-
// Only run the query post updating the filter expression.
1245-
// This runs the query in the next update cycle of react, when it's guaranteed that the query is updated.
1246-
// Because both the things are sequential and react batches the updates so it was still taking the old query.
1247-
if (shouldRunQueryPostUpdate) {
1248-
if (onRun && typeof onRun === 'function') {
1249-
onRun(query);
1250-
} else {
1251-
handleRunQuery();
1252-
}
1253-
setShouldRunQueryPostUpdate(false);
1254-
}
1255-
},
1256-
// eslint-disable-next-line react-hooks/exhaustive-deps
1257-
[shouldRunQueryPostUpdate, handleRunQuery, onRun],
1258-
);
1259-
12601222
return (
12611223
<div className="code-mirror-where-clause">
12621224
{editingMode && (
@@ -1331,7 +1293,6 @@ function QuerySearch({
13311293
theme={isDarkMode ? copilot : githubLight}
13321294
onChange={handleChange}
13331295
onUpdate={handleUpdate}
1334-
data-testid="query-where-clause-editor"
13351296
className={cx('query-where-clause-editor', {
13361297
isValid: validation.isValid === true,
13371298
hasErrors: validation.errors.length > 0,
@@ -1368,14 +1329,11 @@ function QuerySearch({
13681329
// and instead run a custom action
13691330
// Mod-Enter is usually Ctrl-Enter or Cmd-Enter based on OS
13701331
run: (): boolean => {
1371-
if (
1372-
onChange &&
1373-
typeof onChange === 'function' &&
1374-
query !== queryData.filter?.expression
1375-
) {
1376-
onChange(query);
1332+
if (onRun && typeof onRun === 'function') {
1333+
onRun(query);
1334+
} else {
1335+
handleRunQuery();
13771336
}
1378-
setShouldRunQueryPostUpdate(true);
13791337
return true;
13801338
},
13811339
},
@@ -1394,13 +1352,8 @@ function QuerySearch({
13941352
}}
13951353
onFocus={(): void => {
13961354
setIsFocused(true);
1397-
setHasInteractedWithQB(true);
13981355
}}
13991356
onBlur={handleBlur}
1400-
onCreateEditor={(view: EditorView): EditorView => {
1401-
editorRef.current = view;
1402-
return view;
1403-
}}
14041357
/>
14051358

14061359
{query && validation.isValid === false && !isFocused && (

frontend/src/components/QueryBuilderV2/QueryV2/__tests__/QuerySearch.test.tsx

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -222,28 +222,6 @@ describe('QuerySearch', () => {
222222
expect(screen.getByPlaceholderText(PLACEHOLDER_TEXT)).toBeInTheDocument();
223223
});
224224

225-
it('calls onChange on blur after user edits', async () => {
226-
const handleChange = jest.fn() as jest.MockedFunction<(v: string) => void>;
227-
const user = userEvent.setup({ pointerEventsCheck: 0 });
228-
229-
render(
230-
<QuerySearch
231-
onChange={handleChange}
232-
queryData={initialQueriesMap.metrics.builder.queryData[0]}
233-
dataSource={DataSource.METRICS}
234-
/>,
235-
);
236-
237-
const editor = screen.getByTestId(TESTID_EDITOR);
238-
await user.click(editor);
239-
await user.type(editor, SAMPLE_VALUE_TYPING_COMPLETE);
240-
// Blur triggers validation + onChange (only if focused at least once and value changed)
241-
editor.blur();
242-
243-
await waitFor(() => expect(handleChange).toHaveBeenCalledTimes(1));
244-
expect(handleChange.mock.calls[0][0]).toContain("service.name = 'frontend'");
245-
});
246-
247225
it('fetches key suggestions when typing a key (debounced)', async () => {
248226
jest.useFakeTimers();
249227
const advance = (ms: number): void => {

0 commit comments

Comments
 (0)