import React, { useState, useEffect } from 'react'; import { Box, Button, Typography, IconButton, CircularProgress, Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Tooltip, Paper, } from '@mui/material'; import { ContentCopy, Visibility, VisibilityOff, Delete } from '@mui/icons-material'; import styled from 'styled-components'; import axios from 'axios'; import { useGlobalInfoStore } from '../../context/globalInfo'; import { apiUrl } from '../../apiConfig'; import { useTranslation } from 'react-i18next'; const Container = styled(Box)` display: flex; flex-direction: column; align-items: center; margin-top: 50px; margin-left: 50px; `; const ApiKeyManager = () => { const { t } = useTranslation(); const [apiKey, setApiKey] = useState(null); const [apiKeyName, setApiKeyName] = useState(t('apikey.default_name')); const [loading, setLoading] = useState(true); const [showKey, setShowKey] = useState(false); const [copySuccess, setCopySuccess] = useState(false); const { notify } = useGlobalInfoStore(); useEffect(() => { const fetchApiKey = async () => { try { const { data } = await axios.get(`${apiUrl}/auth/api-key`); setApiKey(data.api_key); notify('success', t('apikey.notifications.success.fetch')); } catch (error: any) { const status = error.response?.status; let errorKey = 'unknown'; switch (status) { case 401: errorKey = 'unauthorized'; break; case 404: errorKey = 'not_found'; break; case 500: errorKey = 'server'; break; default: if (error.message?.includes('Network Error')) { errorKey = 'network'; } } notify( 'error', t(`apikey.notifications.errors.fetch.${errorKey}`, { error: error.response?.data?.message || error.message }) ); } finally { setLoading(false); } }; fetchApiKey(); }, []); const generateApiKey = async () => { setLoading(true); try { const { data } = await axios.post(`${apiUrl}/auth/generate-api-key`); if (data.ok && data.api_key) { setApiKey(data.api_key); notify('success', t('apikey.notifications.success.generate')); } } catch (error: any) { const status = error.response?.status; let errorKey = 'unknown'; switch (status) { case 401: errorKey = 'unauthorized'; break; case 403: errorKey = 'limit_reached'; break; case 500: errorKey = 'server'; break; default: if (error.message?.includes('Network Error')) { errorKey = 'network'; } } notify( 'error', t(`apikey.notifications.errors.generate.${errorKey}`, { error: error.response?.data?.message || error.message }) ); } finally { setLoading(false); } }; const deleteApiKey = async () => { setLoading(true); try { const response = await axios.delete(`${apiUrl}/auth/delete-api-key`); if (response.data.ok) { setApiKey(null); notify('success', t('apikey.notifications.success.delete')); } } catch (error: any) { const status = error.response?.status; let errorKey = 'unknown'; switch (status) { case 401: errorKey = 'unauthorized'; break; case 404: errorKey = 'not_found'; break; case 500: errorKey = 'server'; break; default: if (error.message?.includes('Network Error')) { errorKey = 'network'; } } notify( 'error', t(`apikey.notifications.errors.delete.${errorKey}`, { error: error.response?.data?.message || error.message }) ); } finally { setLoading(false); } }; const copyToClipboard = async () => { if (!apiKey) return; try { await navigator.clipboard.writeText(apiKey); setCopySuccess(true); notify('success', t('apikey.notifications.success.copy')); // Reset copy success state after 2 seconds setTimeout(() => setCopySuccess(false), 2000); } catch (error) { notify('error', t('apikey.notifications.errors.copy.failed')); } }; if (loading) { return ( ); } return ( {t('apikey.title')} {apiKey ? ( {t('apikey.table.name')} {t('apikey.table.key')} {t('apikey.table.actions')} {apiKeyName} {showKey ? `${apiKey?.substring(0, 10)}...` : '**********'} setShowKey(!showKey)}> {showKey ? : }
) : ( <> {t('apikey.no_key_message')} )}
); }; export default ApiKeyManager;