import React, { useState, useEffect, useCallback } 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'; 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] = useState(false); const [keyToDelete, setKeyToDelete] = useState<{ id: string; name: string; } | null>(null); const setLoadingWithMinDuration = useCallback((isLoading: boolean) => { if (isLoading) { setLoading(true); } else { setTimeout(() => { setLoading(false); }, 2000); } }, []); const handleFetchKeys = useCallback(async () => { setLoadingWithMinDuration(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 { setLoadingWithMinDuration(false); } }, [setLoadingWithMinDuration]); const handleDeleteKey = useCallback( (id: string) => { setLoadingWithMinDuration(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(() => { setLoadingWithMinDuration(false); }); }, [setLoadingWithMinDuration], ); const handleCreateKey = useCallback( (payload: { name: string; source?: string; retriever?: string; prompt_id: string; chunks: string; }) => { setLoadingWithMinDuration(true); userService .createAPIKey(payload) .then((response) => { if (!response.ok) { throw new Error('Failed to create API Key'); } return response.json(); }) .then((data) => { setApiKeys((prevKeys) => [...prevKeys, data]); setCreateModal(false); setNewKey(data.key); setSaveKeyModal(true); handleFetchKeys(); }) .catch((error) => { console.error(error); }) .finally(() => { setLoadingWithMinDuration(false); }); }, [handleFetchKeys, setLoadingWithMinDuration], ); useEffect(() => { handleFetchKeys(); }, [handleFetchKeys]); const confirmDelete = () => { if (keyToDelete) { handleDeleteKey(keyToDelete.id); } }; return (
{isCreateModalOpen && ( setCreateModal(false)} /> )} {isSaveKeyModalOpen && ( setSaveKeyModal(false)} /> )} {keyToDelete && ( setKeyToDelete(null)} submitLabel={t('modals.deleteConv.delete')} handleSubmit={confirmDelete} handleCancel={() => setKeyToDelete(null)} /> )}
{loading ? ( ) : (
{!apiKeys?.length && ( )} {Array.isArray(apiKeys) && apiKeys.map((element, index) => ( ))}
{t('settings.apiKeys.name')} {t('settings.apiKeys.sourceDoc')} {t('settings.apiKeys.key')}
{t('settings.apiKeys.noData')}
{element.name} {element.source} {element.key} {`Delete setKeyToDelete({ id: element.id, name: element.name, }) } />
)}
); }