|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import Container from 'react-bootstrap/Container'; |
| 3 | +import Row from 'react-bootstrap/Row'; |
| 4 | +import Col from 'react-bootstrap/Col'; |
| 5 | +import Stack from 'react-bootstrap/Stack'; |
| 6 | +import Form from 'react-bootstrap/Form'; |
| 7 | +import SyntaxHighlighter from 'react-syntax-highlighter'; |
| 8 | +import { atomOneLight } from 'react-syntax-highlighter/dist/esm/styles/hljs'; |
| 9 | +import { convertRequests, loadSchema, listFormats } from "@elastic/request-converter"; |
| 10 | + |
1 | 11 | function App() {
|
| 12 | + const formats = listFormats().sort(); |
| 13 | + const [hasSchema, setHasSchema] = useState(false); |
| 14 | + const [source, setSource] = useState('GET /_search'); |
| 15 | + const [code, setCode] = useState('Loading...'); |
| 16 | + const [language, setLanguage] = useState(formats[0]); |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + document.getElementById("source").focus(); |
| 20 | + }, []); |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + (async () => { |
| 24 | + const response = await fetch('https://raw.githubusercontent.com/elastic/elasticsearch-specification/refs/heads/main/output/schema/schema.json'); |
| 25 | + if (response.ok) { |
| 26 | + loadSchema(await response.json()); |
| 27 | + setHasSchema(true); |
| 28 | + } |
| 29 | + })(); |
| 30 | + }, []); |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + (async () => { |
| 34 | + if (hasSchema) { |
| 35 | + let c: string = ""; |
| 36 | + try { |
| 37 | + c = await convertRequests(source, language, {complete: true, printResponse: true}) as string; |
| 38 | + } |
| 39 | + catch (err) { |
| 40 | + } |
| 41 | + if (c) { |
| 42 | + setCode(c as string); |
| 43 | + } |
| 44 | + } |
| 45 | + })(); |
| 46 | + }, [hasSchema, source, language]); |
| 47 | + |
| 48 | + const onRequestChanged = (ev: React.ChangeEventHandler<HTMLSelectElement>) => { |
| 49 | + setSource(ev.target.value); |
| 50 | + ev.target.style.height = ev.target.scrollHeight + "px"; |
| 51 | + }; |
| 52 | + |
2 | 53 | return (
|
3 |
| - <p>App</p> |
| 54 | + <Container id="main-container"> |
| 55 | + <h1>Elasticsearch Request Converter Demo</h1> |
| 56 | + <Row> |
| 57 | + <Col className="col-6"> |
| 58 | + Source Request |
| 59 | + </Col> |
| 60 | + <Col className="col-6"> |
| 61 | + <Stack direction="horizontal" className="heading"> |
| 62 | + <p>Converted Code</p> |
| 63 | + <p className="spacer"></p> |
| 64 | + <Form.Select id="language-choice" size="sm" defaultValue={language} onChange={ev => setLanguage(ev.currentTarget.value)}> |
| 65 | + {formats.map(fmt => <option key={fmt} value={fmt}>{fmt}</option>)} |
| 66 | + </Form.Select> |
| 67 | + </Stack> |
| 68 | + </Col> |
| 69 | + </Row> |
| 70 | + <Form id="main-form"> |
| 71 | + <Row id="main-row"> |
| 72 | + <Col className="col-6"> |
| 73 | + <Form.Control as="textarea" id="source" value={source} onChange={onRequestChanged} /> |
| 74 | + </Col> |
| 75 | + <Col className="col-6"> |
| 76 | + <SyntaxHighlighter wrapLongLines={true} language={language} style={atomOneLight}>{code}</SyntaxHighlighter> |
| 77 | + </Col> |
| 78 | + </Row> |
| 79 | + </Form> |
| 80 | + </Container> |
4 | 81 | )
|
5 | 82 | }
|
6 | 83 |
|
|
0 commit comments