A NRQL editor with basic syntax highlighting. The editor includes an account picker. The query and account id are returned when the user hits the Enter key in the query editor, or clicks the button within the editor.
Import the component:
import { NrqlEditor } from '@newrelic/nr-labs-components';
Use the component in your code:
<NrqlEditor
query="SELECT count(*) FROM Transaction FACET appName"
accountId={0}
onSave={res => console.log('onSave response', res)}
placeholder="NRQL query goes here"
saveButtonText="Preview"
/>
query
(string) - the initial query to be displayed and edited (default isSELECT * FROM Transaction
)accountId
(numeric) - the default account id for the account pickeronSave
(function) - a callback function that will be called when a user saves their changes =placeholder
(string) - placeholder textsaveButtonText
(string) - the text to display for the save button (default isRun
)
When a user saves the query, either by hitting Enter or pressing the save button, the function passed to onSave
gets called with a query object containing the nrql
and the accountId
. If no account has been selected, accountId
will be undefined
.
import React, { useEffect, useState } from 'react';
import { NrqlEditor } from '@newrelic/nr-labs-components';
function MyComponent() {
const [queryObject, setQueryObject] = useState({
query: null,
accountId: null,
});
useEffect(() => console.log('query:', queryObject.query.replace(/\s/g, ' ')), [queryObject.query]);
useEffect(() => console.log('accountId:', queryObject.accountId), [queryObject.accountId]);
return (
<div>
<h1>NRQL Editor</h1>
<NrqlEditor
query={queryObject.query}
accountId={queryObject.accountId}
onSave={({ query, accountId }) => setQueryObject({ query, accountId })}
placeholder="Enter a NRQL query..."
saveButtonText="Save"
/>
</div>
);
}