import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import userService from '../api/services/userService'; import Trash from '../assets/trash.svg'; import CreateAPIKeyModal from '../modals/CreateAPIKeyModal'; import SaveAPIKeyModal from '../modals/SaveAPIKeyModal'; import ConfirmationModal from '../modals/ConfirmationModal'; import { APIKeyData } from './types'; import SkeletonLoader from '../components/SkeletonLoader'; import { useLoaderState } from '../hooks'; export default function APIKeys() { const { t } = useTranslation(); const [isCreateModalOpen, setCreateModal] = useState(false); const [isSaveKeyModalOpen, setSaveKeyModal] = useState(false); const [newKey, setNewKey] = useState(''); const [apiKeys, setApiKeys] = useState([]); const [loading, setLoading] = useLoaderState(true); const [keyToDelete, setKeyToDelete] = useState<{ id: string; name: string; } | null>(null); const handleFetchKeys = async () => { setLoading(true); try { const response = await userService.getAPIKeys(); if (!response.ok) { throw new Error('Failed to fetch API Keys'); } const apiKeys = await response.json(); setApiKeys(apiKeys); } catch (error) { console.log(error); } finally { setLoading(false); } }; const handleDeleteKey = (id: string) => { setLoading(true); userService .deleteAPIKey({ id }) .then((response) => { if (!response.ok) { throw new Error('Failed to delete API Key'); } return response.json(); }) .then((data) => { if (data.success === true) { setApiKeys((previous) => previous.filter((elem) => elem.id !== id)); } setKeyToDelete(null); }) .catch((error) => { console.error(error); }) .finally(() => { setLoading(false); }); }; const handleCreateKey = (payload: { name: string; source?: string; retriever?: string; prompt_id: string; chunks: string; }) => { setLoading(true); userService .createAPIKey(payload) .then((response) => { if (!response.ok) { throw new Error('Failed to create API Key'); } return response.json(); }) .then((data) => { setApiKeys([...apiKeys, data]); setCreateModal(false); setNewKey(data.key); setSaveKeyModal(true); handleFetchKeys(); }) .catch((error) => { console.error(error); }) .finally(() => { setLoading(false); }); }; React.useEffect(() => { handleFetchKeys(); }, []); return (

{t('settings.apiKeys.description')}

{loading ? ( ) : !apiKeys?.length ? ( ) : ( Array.isArray(apiKeys) && apiKeys.map((element) => ( )) )}
{t('settings.apiKeys.name')} {t('settings.apiKeys.sourceDoc')} {t('settings.apiKeys.key')} {t('settings.apiKeys.key')} Actions
{t('settings.apiKeys.noData')}
{element.name}
{element.source}
{element.key}
{isCreateModalOpen && ( setCreateModal(false)} /> )} {isSaveKeyModalOpen && ( setSaveKeyModal(false)} /> )} {keyToDelete && ( setKeyToDelete(null)} submitLabel={t('modals.deleteConv.delete')} handleSubmit={() => handleDeleteKey(keyToDelete.id)} handleCancel={() => setKeyToDelete(null)} /> )}
); }