2024-10-03 02:47:29 +05:30
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
|
import {
|
|
|
|
|
Box,
|
|
|
|
|
Button,
|
|
|
|
|
Typography,
|
|
|
|
|
IconButton,
|
2024-10-03 04:52:29 +05:30
|
|
|
CircularProgress,
|
2024-10-03 05:06:11 +05:30
|
|
|
Table,
|
|
|
|
|
TableBody,
|
|
|
|
|
TableCell,
|
|
|
|
|
TableContainer,
|
|
|
|
|
TableHead,
|
|
|
|
|
TableRow,
|
|
|
|
|
Tooltip,
|
2024-10-03 05:09:23 +05:30
|
|
|
Paper,
|
2024-10-03 02:47:29 +05:30
|
|
|
} from '@mui/material';
|
2024-10-03 05:06:11 +05:30
|
|
|
import { ContentCopy, Visibility, Delete } from '@mui/icons-material';
|
2024-10-03 02:47:29 +05:30
|
|
|
import styled from 'styled-components';
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
import { useGlobalInfoStore } from '../../context/globalInfo';
|
2024-11-01 08:25:33 +05:30
|
|
|
import { apiUrl } from '../../apiConfig';
|
2024-10-03 02:47:29 +05:30
|
|
|
|
|
|
|
|
const Container = styled(Box)`
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
align-items: center;
|
|
|
|
|
margin-top: 50px;
|
2024-10-03 05:09:23 +05:30
|
|
|
margin-left: 50px;
|
2024-10-03 02:47:29 +05:30
|
|
|
`;
|
|
|
|
|
|
2024-10-03 03:29:38 +05:30
|
|
|
const ApiKeyManager = () => {
|
2024-10-03 02:47:29 +05:30
|
|
|
const [apiKey, setApiKey] = useState<string | null>(null);
|
2024-10-03 05:06:11 +05:30
|
|
|
const [apiKeyName, setApiKeyName] = useState<string>('Maxun API Key');
|
2024-10-03 02:47:29 +05:30
|
|
|
const [loading, setLoading] = useState<boolean>(true);
|
2024-10-03 05:06:11 +05:30
|
|
|
const [showKey, setShowKey] = useState<boolean>(false);
|
2024-10-03 02:47:29 +05:30
|
|
|
const [copySuccess, setCopySuccess] = useState<boolean>(false);
|
2024-10-03 02:51:08 +05:30
|
|
|
const { notify } = useGlobalInfoStore();
|
2024-10-03 02:47:29 +05:30
|
|
|
|
2024-11-15 16:16:01 +05:30
|
|
|
|
|
|
|
|
|
2024-11-15 22:26:36 +05:30
|
|
|
|
|
|
|
|
|
2024-10-03 02:47:29 +05:30
|
|
|
useEffect(() => {
|
|
|
|
|
const fetchApiKey = async () => {
|
|
|
|
|
try {
|
2024-11-01 08:25:33 +05:30
|
|
|
const { data } = await axios.get(`${apiUrl}/auth/api-key`);
|
2024-10-03 02:47:29 +05:30
|
|
|
setApiKey(data.api_key);
|
2024-10-03 05:27:15 +05:30
|
|
|
} catch (error: any) {
|
|
|
|
|
notify('error', `Failed to fetch API Key - ${error.message}`);
|
2024-10-03 02:47:29 +05:30
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fetchApiKey();
|
2024-11-15 22:26:36 +05:30
|
|
|
|
2024-10-03 02:47:29 +05:30
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const generateApiKey = async () => {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
try {
|
2024-11-01 08:25:33 +05:30
|
|
|
const { data } = await axios.post(`${apiUrl}/auth/generate-api-key`);
|
2024-10-03 02:47:29 +05:30
|
|
|
setApiKey(data.api_key);
|
2024-11-15 22:26:36 +05:30
|
|
|
|
2024-10-03 05:10:15 +05:30
|
|
|
notify('success', `Generated API Key successfully`);
|
2024-10-03 05:27:15 +05:30
|
|
|
} catch (error: any) {
|
|
|
|
|
notify('error', `Failed to generate API Key - ${error.message}`);
|
2024-10-03 02:47:29 +05:30
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-03 05:06:11 +05:30
|
|
|
const deleteApiKey = async () => {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
try {
|
2024-11-01 08:25:33 +05:30
|
|
|
await axios.delete(`${apiUrl}/auth/delete-api-key`);
|
2024-10-03 05:06:11 +05:30
|
|
|
setApiKey(null);
|
|
|
|
|
notify('success', 'API Key deleted successfully');
|
2024-10-03 05:27:15 +05:30
|
|
|
} catch (error: any) {
|
|
|
|
|
notify('error', `Failed to delete API Key - ${error.message}`);
|
2024-10-03 05:06:11 +05:30
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-03 02:47:29 +05:30
|
|
|
const copyToClipboard = () => {
|
|
|
|
|
if (apiKey) {
|
|
|
|
|
navigator.clipboard.writeText(apiKey);
|
|
|
|
|
setCopySuccess(true);
|
|
|
|
|
setTimeout(() => setCopySuccess(false), 2000);
|
2024-10-03 05:11:00 +05:30
|
|
|
notify('info', 'Copied to clipboard');
|
2024-10-03 02:47:29 +05:30
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-11-29 22:32:30 +05:30
|
|
|
if (loading) {
|
|
|
|
|
return (
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
display: 'flex',
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
height: '100vh',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<CircularProgress />
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-10-03 02:47:29 +05:30
|
|
|
|
2024-10-03 01:56:52 +05:30
|
|
|
return (
|
2024-10-23 06:49:56 +05:30
|
|
|
<Container sx={{ alignSelf: 'flex-start' }}>
|
|
|
|
|
<Typography variant="h6" gutterBottom component="div" style={{ marginBottom: '20px' }}>
|
2024-10-11 03:15:21 +05:30
|
|
|
Manage Your API Key
|
|
|
|
|
</Typography>
|
2024-10-03 02:47:29 +05:30
|
|
|
{apiKey ? (
|
2024-10-03 05:19:28 +05:30
|
|
|
<TableContainer component={Paper} sx={{ width: '100%', overflow: 'hidden' }}>
|
2024-10-03 05:06:11 +05:30
|
|
|
<Table>
|
|
|
|
|
<TableHead>
|
|
|
|
|
<TableRow>
|
|
|
|
|
<TableCell>API Key Name</TableCell>
|
|
|
|
|
<TableCell>API Key</TableCell>
|
|
|
|
|
<TableCell>Actions</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
</TableHead>
|
|
|
|
|
<TableBody>
|
|
|
|
|
<TableRow>
|
|
|
|
|
<TableCell>{apiKeyName}</TableCell>
|
2024-10-03 05:24:18 +05:30
|
|
|
<TableCell>{showKey ? `${apiKey?.substring(0, 10)}...` : '***************'}</TableCell>
|
2024-10-03 05:06:11 +05:30
|
|
|
<TableCell>
|
2024-10-11 02:54:14 +05:30
|
|
|
<Tooltip title="Copy">
|
2024-10-03 05:06:11 +05:30
|
|
|
<IconButton onClick={copyToClipboard}>
|
|
|
|
|
<ContentCopy />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Tooltip>
|
2024-10-11 02:54:14 +05:30
|
|
|
<Tooltip title={showKey ? 'Hide' : 'Show'}>
|
2024-10-03 05:06:11 +05:30
|
|
|
<IconButton onClick={() => setShowKey(!showKey)}>
|
|
|
|
|
<Visibility />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Tooltip>
|
2024-10-11 02:54:14 +05:30
|
|
|
<Tooltip title="Delete">
|
2024-10-23 06:51:47 +05:30
|
|
|
<IconButton onClick={deleteApiKey} color="error">
|
2024-10-03 05:06:11 +05:30
|
|
|
<Delete />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
</TableBody>
|
|
|
|
|
</Table>
|
|
|
|
|
</TableContainer>
|
2024-10-03 02:47:29 +05:30
|
|
|
) : (
|
2024-10-03 05:06:11 +05:30
|
|
|
<>
|
2024-10-03 04:53:28 +05:30
|
|
|
<Typography>You haven't generated an API key yet.</Typography>
|
2024-10-11 02:54:30 +05:30
|
|
|
<Button onClick={generateApiKey} variant="contained" color="primary" sx={{ marginTop: '15px' }}>
|
2024-10-03 02:47:29 +05:30
|
|
|
Generate API Key
|
|
|
|
|
</Button>
|
2024-10-03 05:06:11 +05:30
|
|
|
</>
|
2024-10-03 02:47:29 +05:30
|
|
|
)}
|
|
|
|
|
</Container>
|
|
|
|
|
);
|
|
|
|
|
};
|
2024-10-03 01:56:52 +05:30
|
|
|
|
2024-10-03 05:06:31 +05:30
|
|
|
export default ApiKeyManager;
|