Skip to content

Commit

Permalink
fix: update nrql editor to use react-simple-code-editor
Browse files Browse the repository at this point in the history
  • Loading branch information
amit-y committed Sep 12, 2024
1 parent 551c8eb commit ccce329
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- NrqlEditor - replace existing editor with react-simple-code-editor

## [1.23.2] - 2024-09-05

### Fixed
Expand Down
18 changes: 17 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"sass": "^1.57.1"
},
"dependencies": {
"dayjs": "^1.11.7"
"dayjs": "^1.11.7",
"react-simple-code-editor": "^0.14.1"
}
}
46 changes: 13 additions & 33 deletions src/components/nrql-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useCallback, useEffect, useState } from 'react';
import PropTypes from 'prop-types';

import { AccountPicker } from 'nr1';
import Editor from 'react-simple-code-editor';

import patterns from './patterns';

Expand Down Expand Up @@ -33,36 +34,15 @@ const NrqlEditor = ({
}) => {
const [nrql, setNrql] = useState('');
const [selectedAccountId, setSelectedAccountId] = useState();
const [displayNode, setDisplayNode] = useState();

useEffect(() => setSelectedAccountId(accountId), [accountId]);

useEffect(() => setNrql(query), [query]);

const scrollHandler = useCallback(
({ target: { scrollTop = 0 } = {} } = {}) => {
if (displayNode) displayNode.scrollTop = scrollTop;
},
[displayNode]
);

const displayNodeHandler = useCallback((node) => setDisplayNode(node), []);

const saveHandler = useCallback(() => {
if (onSave) onSave({ query: nrql, accountId: selectedAccountId });
}, [nrql, selectedAccountId, onSave]);

const keyDownHandler = useCallback(
(e) => {
const { keyCode, shiftKey } = e;
if (keyCode === 13 && !shiftKey) {
e.preventDefault();
saveHandler();
}
},
[saveHandler]
);

return (
<div className={styles['nrql-editor']}>
<div className={styles['account-picker']}>
Expand All @@ -72,20 +52,20 @@ const NrqlEditor = ({
/>
</div>
<div className={styles['color-coded-nrql']}>
<div className={styles.editor}>
<textarea
className={`u-unstyledInput ${styles.entry}`}
autoComplete="off"
autoCorrect="off"
spellCheck="false"
<div>
<Editor
value={nrql}
onChange={({ target: { value } = {} } = {}) => setNrql(value)}
onScroll={scrollHandler}
onKeyDown={keyDownHandler}
onValueChange={(code) => setNrql(code)}
highlight={(code) => lexer(code)}
textareaClassName="u-unstyledInput"
padding={8}
style={{
fontFamily: 'monospace',
fontSize: 16,
color: '#E9ECEC',
lineHeight: '19px',
}}
/>
<pre ref={displayNodeHandler} className={styles.display}>
<code dangerouslySetInnerHTML={{ __html: lexer(nrql) }} />
</pre>
</div>
</div>
<div className={styles.actions}>
Expand Down

0 comments on commit ccce329

Please sign in to comment.