From 612e75037086c59a4c1bfb2ea917ef0dbf98a297 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Mon, 11 Mar 2024 03:03:52 +0000 Subject: [PATCH] Add configuration for open ai key --- .../GenerateRoadmap/OpenAISettings.tsx | 38 +++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/components/GenerateRoadmap/OpenAISettings.tsx b/src/components/GenerateRoadmap/OpenAISettings.tsx index fcf46b101..ced58b7cb 100644 --- a/src/components/GenerateRoadmap/OpenAISettings.tsx +++ b/src/components/GenerateRoadmap/OpenAISettings.tsx @@ -8,6 +8,7 @@ import { import { cn } from '../../lib/classname.ts'; import { CloseIcon } from '../ReactIcons/CloseIcon.tsx'; import { useToast } from '../../hooks/use-toast.ts'; +import { httpPost } from '../../lib/http.ts'; type OpenAISettingsProps = { onClose: () => void; @@ -20,6 +21,8 @@ export function OpenAISettings(props: OpenAISettingsProps) { const [hasError, setHasError] = useState(false); const [openaiApiKey, setOpenaiApiKey] = useState(''); + const [isLoading, setIsLoading] = useState(false); + const toast = useToast(); useEffect(() => { @@ -50,7 +53,7 @@ export function OpenAISettings(props: OpenAISettingsProps) {
{ + onSubmit={async (e) => { e.preventDefault(); setHasError(false); @@ -67,6 +70,20 @@ export function OpenAISettings(props: OpenAISettingsProps) { return; } + setIsLoading(true); + const { response, error } = await httpPost( + `${import.meta.env.PUBLIC_API_URL}/v1-validate-openai-key`, + { + key: normalizedKey, + }, + ); + + if (error) { + setHasError(true); + setIsLoading(false); + return; + } + // Save the API key to cookies saveOpenAPIKey(normalizedKey); toast.success('OpenAI API key saved'); @@ -110,11 +127,26 @@ export function OpenAISettings(props: OpenAISettingsProps) {

)} + {!defaultOpenAIKey && ( + + )} {defaultOpenAIKey && (