diff --git a/frontend/src/Setting.tsx b/frontend/src/Setting.tsx index 56423797..83cf06bf 100644 --- a/frontend/src/Setting.tsx +++ b/frontend/src/Setting.tsx @@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react'; import { useSelector, useDispatch } from 'react-redux'; import ArrowLeft from './assets/arrow-left.svg'; import ArrowRight from './assets/arrow-right.svg'; +import Exit from './assets/exit.svg'; import Trash from './assets/trash.svg'; import { selectPrompt, @@ -12,16 +13,13 @@ import { import { Doc } from './preferences/preferenceApi'; import { useDarkTheme } from './hooks'; import Dropdown from './components/Dropdown'; -type PromptProps = { - prompts: { name: string; id: string; type: string }[]; - selectedPrompt: { name: string; id: string; type: string }; - onSelectPrompt: (name: string, id: string, type: string) => void; - setPrompts: (prompts: { name: string; id: string; type: string }[]) => void; - apiHost: string; -}; +const apiHost = import.meta.env.VITE_API_HOST || 'https://docsapi.arc53.com'; +const embeddingsName = + import.meta.env.VITE_EMBEDDINGS_NAME || + 'huggingface_sentence-transformers/all-mpnet-base-v2'; const Setting: React.FC = () => { - const tabs = ['General', 'Prompts', 'Documents']; + const tabs = ['General', 'Prompts', 'Documents', 'API Keys']; //const tabs = ['General', 'Prompts', 'Documents', 'Widgets']; const [activeTab, setActiveTab] = useState('General'); @@ -35,7 +33,6 @@ const Setting: React.FC = () => { const dispatch = useDispatch(); - const apiHost = import.meta.env.VITE_API_HOST || 'https://docsapi.arc53.com'; const [widgetScreenshot, setWidgetScreenshot] = useState(null); const updateWidgetScreenshot = (screenshot: File | null) => { @@ -55,7 +52,6 @@ const Setting: React.FC = () => { console.error(error); } }; - fetchPrompts(); }, []); @@ -167,7 +163,6 @@ const Setting: React.FC = () => { dispatch(setPrompt({ name: name, id: id, type: type })) } setPrompts={setPrompts} - apiHost={apiHost} /> ); case 'Documents': @@ -184,6 +179,8 @@ const Setting: React.FC = () => { onWidgetScreenshotChange={updateWidgetScreenshot} // Add this line /> ); + case 'API Keys': + return ; default: return null; } @@ -226,13 +223,18 @@ const General: React.FC = () => { }; export default Setting; +type PromptProps = { + prompts: { name: string; id: string; type: string }[]; + selectedPrompt: { name: string; id: string; type: string }; + onSelectPrompt: (name: string, id: string, type: string) => void; + setPrompts: (prompts: { name: string; id: string; type: string }[]) => void; +}; const Prompts: React.FC = ({ prompts, selectedPrompt, onSelectPrompt, setPrompts, - apiHost, }) => { const handleSelectPrompt = ({ name, @@ -468,7 +470,6 @@ const AddPromptModal: React.FC = ({ ); }; - type DocumentsProps = { documents: Doc[] | null; handleDeleteDocument: (index: number, document: Doc) => void; @@ -480,10 +481,10 @@ const Documents: React.FC = ({ }) => { return (
-
+
{/*

Documents

*/} -
+
@@ -617,7 +618,268 @@ const AddDocumentModal: React.FC = ({ ); }; +const APIKeys: React.FC = () => { + const [isCreateModalOpen, setCreateModal] = useState(false); + const [isSaveKeyModalOpen, setSaveKeyModal] = useState(false); + const [newKey, setNewKey] = useState(''); + const [apiKeys, setApiKeys] = useState< + { name: string; key: string; source: string; id: string }[] + >([]); + const handleDeleteKey = (id: string) => { + fetch(`${apiHost}/api/delete_api_key`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ id }), + }) + .then((response) => { + if (!response.ok) { + throw new Error('Failed to delete API Key'); + } + return response.json(); + }) + .then((data) => { + data.status === 'ok' && + setApiKeys((previous) => previous.filter((elem) => elem.id !== id)); + }) + .catch((error) => { + console.error(error); + }); + }; + useEffect(() => { + fetchAPIKeys(); + }, []); + const fetchAPIKeys = async () => { + try { + const response = await fetch(`${apiHost}/api/get_api_keys`); + if (!response.ok) { + throw new Error('Failed to fetch API Keys'); + } + const apiKeys = await response.json(); + setApiKeys(apiKeys); + } catch (error) { + console.log(error); + } + }; + const createAPIKey = (payload: { name: string; source: string }) => { + fetch(`${apiHost}/api/create_api_key`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(payload), + }) + .then((response) => { + if (!response.ok) { + throw new Error('Failed to create API Key'); + } + return response.json(); + }) + .then((data) => { + setApiKeys([...apiKeys, data]); + setCreateModal(false); //close the create key modal + setNewKey(data.key); + setSaveKeyModal(true); // render the newly created key + fetchAPIKeys(); + }) + .catch((error) => { + console.error(error); + }); + }; + return ( +
+
+
+ +
+ {isCreateModalOpen && ( + setCreateModal(false)} + createAPIKey={createAPIKey} + /> + )} + {isSaveKeyModalOpen && ( + setSaveKeyModal(false)} + /> + )} +
+
+
+ + + + + + + + + + {apiKeys?.map((element, index) => ( + + + + + + + ))} + +
Name + Source document + API Key
{element.name}{element.source}{element.key} + Delete handleDeleteKey(element.id)} + /> +
+
+
+
+
+ ); +}; +type SaveAPIKeyModalProps = { + apiKey: string; + close: () => void; +}; +const SaveAPIKeyModal: React.FC = ({ apiKey, close }) => { + const [isCopied, setIsCopied] = useState(false); + const handleCopyKey = () => { + navigator.clipboard.writeText(apiKey); + setIsCopied(true); + }; + return ( +
+
+ +

Please save your Key

+

+ This is the only time your key will be shown. +

+
+
+

API Key

+ {apiKey} +
+ +
+ +
+
+ ); +}; +type CreateAPIKeyModalProps = { + close: () => void; + createAPIKey: (payload: { name: string; source: string }) => void; +}; +const CreateAPIKeyModal: React.FC = ({ + close, + createAPIKey, +}) => { + const [APIKeyName, setAPIKeyName] = useState(''); + const [sourcePath, setSourcePath] = useState<{ + label: string; + value: string; + } | null>(null); + const docs = useSelector(selectSourceDocs); + const extractDocPaths = () => + docs + ? docs + .filter((doc) => doc.model === embeddingsName) + .map((doc: Doc) => { + let namePath = doc.name; + if (doc.language === namePath) { + namePath = '.project'; + } + let docPath = 'default'; + if (doc.location === 'local') { + docPath = 'local' + '/' + doc.name + '/'; + } else if (doc.location === 'remote') { + docPath = + doc.language + + '/' + + namePath + + '/' + + doc.version + + '/' + + doc.model + + '/'; + } + return { + label: doc.name, + value: docPath, + }; + }) + : []; + + return ( +
+
+ + + Create New API Key + +
+ + API Key Name + + setAPIKeyName(e.target.value)} + /> +
+
+ + setSourcePath(selection) + } + options={extractDocPaths()} + /> +
+ +
+
+ ); +}; const Widgets: React.FC<{ widgetScreenshot: File | null; onWidgetScreenshotChange: (screenshot: File | null) => void; diff --git a/frontend/src/components/Dropdown.tsx b/frontend/src/components/Dropdown.tsx index 5654b430..3ede2f2f 100644 --- a/frontend/src/components/Dropdown.tsx +++ b/frontend/src/components/Dropdown.tsx @@ -7,18 +7,22 @@ function Dropdown({ onSelect, showDelete, onDelete, + placeholder, }: { options: | string[] | { name: string; id: string; type: string }[] | { label: string; value: string }[]; - selectedValue: string | { label: string; value: string }; + selectedValue: string | { label: string; value: string } | null; onSelect: | ((value: string) => void) | ((value: { name: string; id: string; type: string }) => void) | ((value: { label: string; value: string }) => void); showDelete?: boolean; onDelete?: (value: string) => void; + placeholder?: string; + className?: string; + width?: string; }) { const [isOpen, setIsOpen] = useState(false); return ( @@ -31,7 +35,7 @@ function Dropdown({ > {isOpen && ( -
+
{options.map((option: any, index) => (