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, Delete } from '@mui/icons-material'; import styled from 'styled-components'; import axios from 'axios'; import { useGlobalInfoStore } from '../../context/globalInfo'; import { apiUrl } from '../../apiConfig'; const Container = styled(Box)` display: flex; flex-direction: column; align-items: center; margin-top: 50px; margin-left: 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(`${apiUrl}/auth/api-key`); setApiKey(data.api_key); } catch (error: any) { notify('error', `Failed to fetch API Key - ${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); notify('success', `Generated API Key successfully`); } catch (error: any) { notify('error', `Failed to generate API Key - ${error.message}`); } finally { setLoading(false); } }; const deleteApiKey = async () => { setLoading(true); try { await axios.delete(`${apiUrl}/auth/delete-api-key`); setApiKey(null); notify('success', 'API Key deleted successfully'); } catch (error: any) { notify('error', `Failed to delete API Key - ${error.message}`); } finally { setLoading(false); } }; const copyToClipboard = () => { if (apiKey) { navigator.clipboard.writeText(apiKey); setCopySuccess(true); setTimeout(() => setCopySuccess(false), 2000); notify('info', 'Copied to clipboard'); } }; if (loading) { return ( ); } return ( Manage Your API Key {apiKey ? ( API Key Name API Key Actions {apiKeyName} {showKey ? `${apiKey?.substring(0, 10)}...` : '***************'} setShowKey(!showKey)}>
) : ( <> You haven't generated an API key yet. )}
); }; export default ApiKeyManager;