import React, { useState, useEffect } from 'react'; import { Box, Button, TextField, Typography, IconButton, InputAdornment, Tooltip, CircularProgress, } from '@mui/material'; import { Visibility, VisibilityOff, ContentCopy } 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 ApiKeyField = styled(TextField)` width: 400px; margin: 20px 0; `; const HiddenText = styled(Typography)` color: #888; font-style: italic; `; const ApiKey = () => { const [apiKey, setApiKey] = 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('http://localhost:8080/auth/api-key'); setApiKey(data.api_key); notify('info', `Fetc API Key: ${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', `Genrate API Key: ${data.api_key}`); } catch (error) { console.error('Error generating API key', error); } finally { setLoading(false); } }; const copyToClipboard = () => { if (apiKey) { navigator.clipboard.writeText(apiKey); setCopySuccess(true); setTimeout(() => setCopySuccess(false), 2000); } }; const toggleShowKey = () => { setShowKey(!showKey); }; if (loading) return ; return ( Manage Your API Key {apiKey ? ( <> {showKey ? : } ), }} /> {copySuccess && ( )} ) : ( <> You haven't generated an API key yet. )} ); }; export default ApiKey;