Skip to content

Commit

Permalink
Add onFocus handler and Keyboard handler to Chat Prompt Editor
Browse files Browse the repository at this point in the history
  • Loading branch information
CoenWarmer committed Aug 2, 2023
1 parent bcb7fcd commit a28a5db
Showing 1 changed file with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import React, { useEffect, useState } from 'react';
import React, { Ref, useCallback, useEffect, useRef, useState } from 'react';
import {
EuiButtonIcon,
EuiButtonEmpty,
Expand All @@ -14,6 +14,7 @@ import {
EuiFlexItem,
EuiSpacer,
EuiPanel,
keys,
} from '@elastic/eui';
import { CodeEditor } from '@kbn/kibana-react-plugin/public';
import { i18n } from '@kbn/i18n';
Expand All @@ -39,6 +40,8 @@ export function ChatPromptEditor({ onSubmit, disabled, loading }: ChatPromptEdit

const { model, initialJsonString } = useJsonEditorModel(selectedFunction);

const ref = useRef<HTMLInputElement>(null);

useEffect(() => {
setFunctionPayload(initialJsonString);
}, [initialJsonString, selectedFunction]);
Expand All @@ -56,7 +59,7 @@ export function ChatPromptEditor({ onSubmit, disabled, loading }: ChatPromptEdit
setFunctionPayload('');
};

const handleSubmit = async () => {
const handleSubmit = useCallback(async () => {
const currentPrompt = prompt;
const currentPayload = functionPayload;

Expand Down Expand Up @@ -86,7 +89,27 @@ export function ChatPromptEditor({ onSubmit, disabled, loading }: ChatPromptEdit
} catch (_) {
setPrompt(currentPrompt);
}
};
}, [functionPayload, onSubmit, prompt, selectedFunction]);

useEffect(() => {
const keyboardListener = (event: KeyboardEvent) => {
if (event.key === keys.ENTER) {
handleSubmit();
}
};

window.addEventListener('keyup', keyboardListener);

return () => {
window.removeEventListener('keyup', keyboardListener);
};
}, [handleSubmit]);

useEffect(() => {
if (ref.current) {
ref.current.focus();
}
});

return (
<EuiFlexGroup gutterSize="s" responsive={false}>
Expand Down Expand Up @@ -169,6 +192,7 @@ export function ChatPromptEditor({ onSubmit, disabled, loading }: ChatPromptEdit
placeholder={i18n.translate('xpack.observabilityAiAssistant.prompt.placeholder', {
defaultMessage: 'Press ‘$’ for function recommendations',
})}
inputRef={ref}
onChange={handleChange}
onSubmit={handleSubmit}
/>
Expand Down

0 comments on commit a28a5db

Please sign in to comment.