Skip to content

Commit

Permalink
refactored code to use Form hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Samiul-TheSoccerFan committed Feb 9, 2024
1 parent f52cdaf commit a8a187b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 16 deletions.
10 changes: 10 additions & 0 deletions packages/kbn-ai-playground/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { ChatForm, ChatFormFields, MessageRole } from '../types';

import { MessageList } from './message_list/message_list';
import { QuestionInput } from './question_input';
import { OpenAIKey } from './open_ai_key';

import { TelegramIcon } from './telegram_icon';
import { InstructionsField } from '@kbn/ai-playground/components/instructions_field';
Expand Down Expand Up @@ -122,6 +123,15 @@ export const Chat = () => {
padding: euiTheme.size.l,
}}
>
<Controller
name={ChatFormFields.openAIKey}
control={control}
defaultValue=""
render={({ field }) => (
<OpenAIKey apiKey={field.value} onSave={field.onChange} />
)}
/>

<Controller
name={ChatFormFields.prompt}
control={control}
Expand Down
31 changes: 31 additions & 0 deletions packages/kbn-ai-playground/components/open_ai_key.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React, { useState } from "react";
import { OpenAIKeyCallout } from "./open_ai_key_callout";

import { OpenAIKeyFlyOut } from "./open_ai_key_flyout";

interface OpenAIKeyProps {
apiKey: string;
onSave: () => void;
}

export const OpenAIKey: React.FC<OpenAIKeyProps> = ({ apiKey, onSave }) => {
const [isOpenAIFlyOutOpen, setIsOpenAIFlyOutOpen] = useState<boolean>(false);

const onCloseOpenAIFlyOut = () => {
setIsOpenAIFlyOutOpen(!isOpenAIFlyOutOpen);
};

return (
<>
{isOpenAIFlyOutOpen && <OpenAIKeyFlyOut openAPIKey={apiKey} onSave={onSave} onClose={onCloseOpenAIFlyOut} />}
<OpenAIKeyCallout setIsOpenAIFlyOutOpen={setIsOpenAIFlyOutOpen} />
</>
);
}
22 changes: 13 additions & 9 deletions packages/kbn-ai-playground/components/open_ai_key_callout.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React from "react";

import { EuiButton, EuiCallOut } from "@elastic/eui";
import { css } from "@emotion/react";
import { i18n } from "@kbn/i18n";

import React, { Dispatch, SetStateAction } from "react";
import { i18n } from "@kbn/i18n";

interface ListProps {
setIsOpenAIFlyOutOpen: Dispatch<SetStateAction<boolean>>;
interface OpenAIKeyCalloutProps {
setIsOpenAIFlyOutOpen: (flyOut: boolean) => void;
}

export const OpenAIKeyCallout: React.FC<ListProps> = ({ setIsOpenAIFlyOutOpen }: ListProps) => {
export const OpenAIKeyCallout: React.FC<OpenAIKeyCalloutProps> = ({ setIsOpenAIFlyOutOpen }) => {
return (
<EuiCallOut
css={css`
width: 55%
`}
title={i18n.translate(
'aiPlayground.sidebar.openAICallout.headerText',
{
Expand Down
24 changes: 17 additions & 7 deletions packages/kbn-ai-playground/components/open_ai_key_flyout.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { EuiButton, EuiButtonEmpty, EuiFieldText, EuiFlexGroup, EuiFlexItem, EuiFlyout, EuiFlyoutBody, EuiFlyoutFooter, EuiFlyoutHeader, EuiFormRow, EuiLink, EuiSpacer, EuiText, EuiTitle } from "@elastic/eui";
import { i18n } from "@kbn/i18n";
import React, { useState } from "react";

export interface OpenAIKeyFlyOutProps {
openAPIKey: string;
onClose: () => void;
setOpenAIKey: (key: string) => void;
onSave: (key: string) => void;
}

export const OpenAIKeyFlyOut: React.FC<OpenAIKeyFlyOutProps> = ({ onClose, setOpenAIKey }: OpenAIKeyFlyOutProps) => {
const [apiKey, setApiKey] = useState<string>('');
export const OpenAIKeyFlyOut: React.FC<OpenAIKeyFlyOutProps> = ({ openAPIKey, onClose, onSave }) => {
const [apiKey, setApiKey] = useState<string>(openAPIKey);

const handleSave = () => {
onSave(apiKey);
onClose();
}

return (
<EuiFlyout onClose={onClose} size="m">
Expand Down Expand Up @@ -72,10 +85,7 @@ export const OpenAIKeyFlyOut: React.FC<OpenAIKeyFlyOutProps> = ({ onClose, setOp
isDisabled={!apiKey.trim()}
data-telemetry-id="entSearchAIPlayground-addingOpenAIKey-save"
fill
onClick={() => {
setOpenAIKey(apiKey.trim());
onClose();
}}
onClick={handleSave}
>
{i18n.translate(
'aiPlayground.sidebar.openAIFlyOut.saveButtonLabel',
Expand Down
2 changes: 2 additions & 0 deletions packages/kbn-ai-playground/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ export enum ChatFormFields {
question = 'question',
citations = 'citations',
prompt = 'prompt',
openAIKey = 'openAIKey',
}

export interface ChatForm {
[ChatFormFields.question]: string;
[ChatFormFields.prompt]: string;
[ChatFormFields.citations]: boolean;
[ChatFormFields.openAIKey]: string;
}

export interface AIPlaygroundPluginStartDeps {
Expand Down

0 comments on commit a8a187b

Please sign in to comment.