Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit ffaec73

Browse files
committed
First try to fix line editing jumps
1 parent f977ff8 commit ffaec73

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

src/subapps/admin/components/ViewForm/ElasticSearchQueryForm.tsx

+26-17
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Button, Card, Empty, Form, List } from 'antd';
77
import * as codemirror from 'codemirror';
88
import 'codemirror/addon/display/placeholder';
99
import 'codemirror/mode/javascript/javascript';
10-
import { useEffect, useRef, useState } from 'react';
10+
import { FC, MutableRefObject, useEffect, useRef, useState } from 'react';
1111
import { UnControlled as CodeMirror } from 'react-codemirror2';
1212
import ReactJson from 'react-json-view';
1313

@@ -28,7 +28,7 @@ export type NexusESError = {
2828
};
2929

3030
// TODO this needs to be broken into Input, Result, and Form components.
31-
const ElasticSearchQueryForm: React.FunctionComponent<{
31+
const ElasticSearchQueryForm: FC<{
3232
query: object;
3333
response?: ElasticSearchViewQueryResponse<any> | null;
3434
busy: boolean;
@@ -49,36 +49,45 @@ const ElasticSearchQueryForm: React.FunctionComponent<{
4949
onQueryChange,
5050
onChangePageSize,
5151
}): JSX.Element => {
52-
const [initialQuery, setInitialQuery] = useState('');
52+
const [_initialQuery, setInitialQuery] = useState('');
5353
const [valid, setValid] = useState(true);
54-
const [value, setValue] = useState<string>();
54+
// TODO Clean multiple states
55+
const [value, _setValue] = useState<string>();
5556
const [pageSize, setPageSize] = useState<number>(size);
5657
const editor = useRef<codemirror.Editor>();
5758
const wrapper = useRef(null);
5859

60+
const [editorValue, setEditorValue] = useState(''); // New state to manage the editor content
61+
5962
useEffect(() => {
60-
// only on first render!
6163
const formattedInitialQuery = JSON.stringify(query, null, 2);
6264
setInitialQuery(formattedInitialQuery);
63-
}, []);
64-
65-
const data =
66-
response && response.hits.hits.map(result => result._source || []);
67-
const total =
68-
(response && response.hits.total && response.hits.total.value) || 0;
69-
const totalPages = Math.ceil(total / size);
70-
const current = Math.floor((totalPages / total) * from + 1);
65+
setEditorValue(formattedInitialQuery); // Initialize the editor value with the formatted query
66+
}, [query]); // Dependency on `query` to update initial value when the prop changes
7167

7268
const handleChange = (_: any, __: any, value: string) => {
69+
setEditorValue(value); // Update editor value directly
7370
try {
7471
JSON.parse(value);
75-
setValue(value);
7672
setValid(true);
7773
} catch (error) {
7874
setValid(false);
7975
}
8076
};
8177

78+
useEffect(() => {
79+
// only on first render!
80+
const formattedInitialQuery = JSON.stringify(query, null, 2);
81+
setInitialQuery(formattedInitialQuery);
82+
}, []);
83+
84+
const data =
85+
response && response.hits.hits.map(result => result._source || []);
86+
const total =
87+
(response && response.hits.total && response.hits.total.value) || 0;
88+
const totalPages = Math.ceil(total / size);
89+
const current = Math.floor((totalPages / total) * from + 1);
90+
8291
const changePageSize = (_: number, size: number) => {
8392
setPageSize(size);
8493
onChangePageSize(size);
@@ -106,7 +115,7 @@ const ElasticSearchQueryForm: React.FunctionComponent<{
106115
</div>
107116
</div>
108117
<CodeMirror
109-
value={initialQuery}
118+
value={editorValue}
110119
options={{
111120
mode: { name: 'javascript', json: true },
112121
theme: 'base16-light',
@@ -115,12 +124,12 @@ const ElasticSearchQueryForm: React.FunctionComponent<{
115124
}}
116125
onChange={handleChange}
117126
editorDidMount={editorElement => {
118-
(editor as React.MutableRefObject<
127+
(editor as MutableRefObject<
119128
codemirror.Editor
120129
>).current = editorElement;
121130
}}
122131
editorWillUnmount={() => {
123-
const editorWrapper = (editor as React.MutableRefObject<
132+
const editorWrapper = (editor as MutableRefObject<
124133
CodeMirror.Editor
125134
>).current.getWrapperElement();
126135
if (editor) editorWrapper.remove();

src/subapps/admin/components/ViewForm/view-form.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
margin: 0 auto;
77
}
88
.code {
9+
margin-bottom: $default-pad;
910
max-height: 600;
1011
overflow: 'scroll';
11-
margin-bottom: $default-pad;
1212
}
1313
.ant-card-body {
1414
overflow: scroll;

src/subapps/admin/containers/ElasticSearchQuery.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { useState, FC } from 'react';
2-
import { useNexusContext } from '@bbp/react-nexus';
31
import {
42
DEFAULT_ELASTIC_SEARCH_VIEW_ID,
53
ElasticSearchViewQueryResponse,
64
} from '@bbp/nexus-sdk/es';
5+
import { useNexusContext } from '@bbp/react-nexus';
6+
import { FC, useState } from 'react';
77

8+
import { useQuery } from 'react-query';
89
import ElasticSearchQueryForm, {
910
NexusESError,
1011
} from '../components/ViewForm/ElasticSearchQueryForm';
11-
import { useQuery } from 'react-query';
1212

1313
const DEFAULT_PAGE_SIZE = 5;
1414
const DEFAULT_QUERY = {

0 commit comments

Comments
 (0)