2021-12-04 12:30:03 +01:00
|
|
|
import React, { useState, useRef, useEffect } from 'react';
|
|
|
|
import StyledWrapper from './StyledWrapper';
|
|
|
|
|
2022-03-07 13:22:35 +01:00
|
|
|
let CodeMirror;
|
|
|
|
const SERVER_RENDERED = typeof navigator === 'undefined' || global['PREVENT_CODEMIRROR_RENDER'] === true;
|
|
|
|
|
|
|
|
if (!SERVER_RENDERED) {
|
|
|
|
CodeMirror = require('codemirror');
|
|
|
|
}
|
|
|
|
|
2022-03-14 22:43:26 +01:00
|
|
|
const QueryResult = ({data, width}) => {
|
2021-12-04 12:30:03 +01:00
|
|
|
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"],
|
2022-03-15 22:59:55 +01:00
|
|
|
readOnly: true,
|
2021-12-05 16:45:20 +01:00
|
|
|
lineWrapping: true
|
2021-12-04 12:30:03 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
setCmEditor(_cmEditor);
|
|
|
|
}
|
|
|
|
|
2022-03-14 22:43:26 +01:00
|
|
|
if(editor.current && cmEditor && data) {
|
2021-12-04 12:30:03 +01:00
|
|
|
cmEditor.setValue(JSON.stringify(data, null, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if(cmEditor) {
|
|
|
|
cmEditor.toTextArea();
|
|
|
|
setCmEditor(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [editor.current, cmEditor, data]);
|
|
|
|
|
2022-03-15 22:59:55 +01:00
|
|
|
|
2021-12-04 12:30:03 +01:00
|
|
|
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;
|