import React, { useState, useEffect } from 'react'; import { Box, Button, Typography, IconButton, CircularProgress, Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Tooltip, } from '@mui/material'; import { ContentCopy, Visibility, Delete } from '@mui/icons-material'; import styled from 'styled-components'; import axios from 'axios'; import { useGlobalInfoStore } from '../../context/globalInfo'; const Container = styled(Box)` display: flex; flex-direction: column; align-items: center; margin-top: 50px; `; const ApiKeyManager = () => { const [apiKey, setApiKey] = useState(null); const [apiKeyName, setApiKeyName] = useState('Maxun API Key'); 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('http://localhost:8080/auth/api-key'); setApiKey(data.api_key); } catch (error) { console.error('Error fetching API key', error); } finally { setLoading(false); } }; fetchApiKey(); }, []); const generateApiKey = async () => { setLoading(true); try { const { data } = await axios.post('http://localhost:8080/auth/generate-api-key'); setApiKey(data.api_key); notify('info', `Generated API Key: ${data.api_key}`); } catch (error) { console.error('Error generating API key', error); } finally { setLoading(false); } }; const deleteApiKey = async () => { setLoading(true); try { await axios.delete('http://localhost:8080/auth/delete-api-key'); setApiKey(null); notify('success', 'API Key deleted successfully'); } catch (error) { console.error('Error deleting API key', error); } finally { setLoading(false); } }; const copyToClipboard = () => { if (apiKey) { navigator.clipboard.writeText(apiKey); setCopySuccess(true); setTimeout(() => setCopySuccess(false), 2000); } }; if (loading) return ; return ( Manage Your API Key {apiKey ? ( API Key Name API Key Actions {apiKeyName} {showKey ? apiKey : '****************'} setShowKey(!showKey)}>
{copySuccess && ( Copied to Clipboard )}
) : ( <> You haven't generated an API key yet. )}
); }; export default ApiKeyManager;