Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
CoenWarmer committed May 14, 2024
1 parent e9bed9f commit 4af5f4b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"alerting",
"features"
],
"requiredBundles": ["kibanaReact", "kibanaUtils"],
"requiredBundles": ["kibanaReact"],
"optionalPlugins": ["cloud"],
"extraPublicDirs": []
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function PromptEditor({

const [hasFocus, setHasFocus] = useState(false);

const { lastUsedPrompts, addPrompt } = useLastUsedPrompts();
const { lastUsedPrompts, addLastUsedPrompt } = useLastUsedPrompts();

const initialInnerMessage = initialRole
? {
Expand Down Expand Up @@ -100,13 +100,13 @@ export function PromptEditor({
}
};

const handleSubmit = useCallback(async () => {
const handleSubmit = useCallback(() => {
if (loading || !innerMessage) {
return;
}

if (innerMessage.content) {
addPrompt(innerMessage.content);
addLastUsedPrompt(innerMessage.content);
}

const oldMessage = innerMessage;
Expand All @@ -130,7 +130,7 @@ export function PromptEditor({
setInnerMessage(oldMessage);
setMode(oldMessage.function_call?.name ? 'function' : 'prompt');
}
}, [addPrompt, innerMessage, loading, onSendTelemetry, onSubmit]);
}, [addLastUsedPrompt, innerMessage, loading, onSendTelemetry, onSubmit]);

// Submit on Enter
useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@

import { useKibana } from '@kbn/kibana-react-plugin/public';
import type { CoreStart } from '@kbn/core/public';
import { Storage } from '@kbn/kibana-utils-plugin/public';
import type { ObservabilityAIAssistantAppPluginStartDependencies } from '../types';

export type StartServices<TAdditionalServices> = CoreStart & {
plugins: { start: ObservabilityAIAssistantAppPluginStartDependencies };
storage: Storage;
} & TAdditionalServices & {};

const useTypedKibana = <AdditionalServices extends object = {}>() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { uniq } from 'lodash';
import { useCallback, useMemo } from 'react';
import { useLocalStorage } from './use_local_storage';

const AI_ASSISTANT_LAST_USED_PROMPT_STORAGE = 'kibana.ai-assistant.last-used-prompts';
Expand All @@ -16,7 +17,13 @@ export function useLastUsedPrompts() {
[]
);

const addPrompt = (prompt: string) => setPrompt(uniq([prompt, ...lastUsedPrompts]).slice(0, 5));
const addLastUsedPrompt = useCallback(
(prompt: string) => setPrompt(uniq([prompt, ...lastUsedPrompts]).slice(0, 5)),
[lastUsedPrompts, setPrompt]
);

return { lastUsedPrompts, addPrompt };
return useMemo(
() => ({ lastUsedPrompts, addLastUsedPrompt }),
[addLastUsedPrompt, lastUsedPrompts]
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { useState, useEffect, useMemo } from 'react';
import { useState, useEffect, useMemo, useCallback } from 'react';

export function useLocalStorage<T>(key: string, defaultValue: T) {
// This is necessary to fix a race condition issue.
Expand All @@ -17,14 +17,17 @@ export function useLocalStorage<T>(key: string, defaultValue: T) {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [key, storageUpdate, defaultValue]);

const saveToStorage = (value: T) => {
if (value === undefined) {
window.localStorage.removeItem(key);
} else {
window.localStorage.setItem(key, JSON.stringify(value));
setStorageUpdate(storageUpdate + 1);
}
};
const saveToStorage = useCallback(
(value: T) => {
if (value === undefined) {
window.localStorage.removeItem(key);
} else {
window.localStorage.setItem(key, JSON.stringify(value));
setStorageUpdate(storageUpdate + 1);
}
},
[key, storageUpdate]
);

useEffect(() => {
function onUpdate(event: StorageEvent) {
Expand All @@ -38,7 +41,7 @@ export function useLocalStorage<T>(key: string, defaultValue: T) {
};
}, [key, setStorageUpdate, storageUpdate]);

return [item, saveToStorage] as const;
return useMemo(() => [item, saveToStorage] as const, [item, saveToStorage]);
}

function getFromStorage<T>(keyName: string, defaultValue: T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import { EuiErrorBoundary } from '@elastic/eui';
import type { CoreStart, CoreTheme } from '@kbn/core/public';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import { Storage } from '@kbn/kibana-utils-plugin/public';
import { KibanaThemeProvider } from '@kbn/react-kibana-context-theme';
import { RedirectAppLinks } from '@kbn/shared-ux-link-redirect-app';
import React, { useMemo } from 'react';
Expand All @@ -16,8 +15,6 @@ import { ObservabilityAIAssistantAppServiceProvider } from '../context/observabi
import type { ObservabilityAIAssistantAppService } from '../service/create_app_service';
import type { ObservabilityAIAssistantAppPluginStartDependencies } from '../types';

const storage = new Storage(localStorage);

export function SharedProviders({
children,
coreStart,
Expand Down Expand Up @@ -45,7 +42,6 @@ export function SharedProviders({
plugins: {
start: pluginsStart,
},
storage,
}}
>
<RedirectAppLinks coreStart={coreStart}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@
"@kbn/serverless",
"@kbn/task-manager-plugin",
"@kbn/cloud-plugin",
"@kbn/observability-plugin",
"@kbn/kibana-utils-plugin"
"@kbn/observability-plugin"
],
"exclude": ["target/**/*"]
}

0 comments on commit 4af5f4b

Please sign in to comment.