2021-12-04 12:30:03 +01:00
|
|
|
import React, { useState, useRef, useEffect } from 'react';
|
|
|
|
import * as CodeMirror from 'codemirror';
|
|
|
|
import StyledWrapper from './StyledWrapper';
|
|
|
|
|
|
|
|
const QueryResult = ({data, isLoading, width}) => {
|
|
|
|
const [cmEditor, setCmEditor] = useState(null);
|
|
|
|
const editor = useRef();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (editor.current && !cmEditor) {
|
|
|
|
const _cmEditor = CodeMirror.fromTextArea(editor.current, {
|
|
|
|
value: '',
|
2021-12-05 16:45:20 +01:00
|
|
|
lineNumbers: true,
|
|
|
|
matchBrackets: true,
|
|
|
|
autoCloseBrackets: true,
|
2022-01-25 22:36:46 +01:00
|
|
|
mode: "application/ld+json",
|
2021-12-05 16:45:20 +01:00
|
|
|
foldGutter: true,
|
|
|
|
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
|
|
|
|
lineWrapping: true
|
2021-12-04 12:30:03 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
setCmEditor(_cmEditor);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(editor.current && cmEditor && data && !isLoading) {
|
|
|
|
cmEditor.setValue(JSON.stringify(data, null, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if(cmEditor) {
|
|
|
|
cmEditor.toTextArea();
|
|
|
|
setCmEditor(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [editor.current, cmEditor, data]);
|
|
|
|
|
|
|
|
return (
|
2022-01-26 16:01:49 +01:00
|
|
|
<StyledWrapper className="mt-4 px-3 w-full" style={{maxWidth: width}}>
|
2022-01-25 22:30:21 +01:00
|
|
|
<div className="h-full">
|
2021-12-04 12:30:03 +01:00
|
|
|
<textarea
|
2022-01-26 16:01:49 +01:00
|
|
|
id="query-result"
|
2021-12-04 12:30:03 +01:00
|
|
|
ref={editor}
|
|
|
|
className="cm-editor"
|
|
|
|
>
|
|
|
|
</textarea>
|
|
|
|
</div>
|
|
|
|
</StyledWrapper>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default QueryResult;
|