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: 70px; margin-right: 70px; `; const ApiKeyManager = () => { const { t } = useTranslation(); const [apiKey, setApiKey] = useState(null); const [apiKeyName, setApiKeyName] = useState(t('apikey.default_name')); const [apiKeyCreatedAt, setApiKeyCreatedAt] = useState(null); 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); setApiKeyCreatedAt(data.api_key_created_at); } catch (error: any) { notify('error', t('apikey.notifications.fetch_error', { error: error.message })); } finally { setLoading(false); } }; fetchApiKey(); }, []); const generateApiKey = async () => { setLoading(true); try { const { data } = await axios.post(`${apiUrl}/auth/generate-api-key`); setApiKey(data.api_key); setApiKeyCreatedAt(data.api_key_created_at); notify('success', t('apikey.notifications.generate_success')); } catch (error: any) { notify('error', t('apikey.notifications.generate_error', { error: error.message })); } finally { setLoading(false); } }; const deleteApiKey = async () => { setLoading(true); try { await axios.delete(`${apiUrl}/auth/delete-api-key`); setApiKey(null); setApiKeyCreatedAt(null); notify('success', t('apikey.notifications.delete_success')); } catch (error: any) { notify('error', t('apikey.notifications.delete_error', { error: error.message })); } finally { setLoading(false); } }; const copyToClipboard = () => { if (apiKey) { navigator.clipboard.writeText(apiKey); setCopySuccess(true); setTimeout(() => setCopySuccess(false), 2000); notify('info', t('apikey.notifications.copy_success')); } }; if (loading) { return ( ); } return ( Start by creating an API key below. Then, test your API or read the API documentation for setup instructions. {t('apikey.title')} {apiKey ? ( {t('apikey.table.name')} {t('apikey.table.key')} Created On {t('apikey.table.actions')} {apiKeyName} {showKey ? `${apiKey?.substring(0, 10)}...` : '**********'} {apiKeyCreatedAt && ( {new Date(apiKeyCreatedAt).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric', })} )} setShowKey(!showKey)}> {showKey ? : }
) : ( <> {t('apikey.no_key_message')} )}
); } export default ApiKeyManager;