Files
parcer/src/components/api/ApiKey.tsx

165 lines
4.9 KiB
TypeScript
Raw Normal View History

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';
2025-02-08 15:44:36 +05:30
import { ContentCopy, Visibility, VisibilityOff, 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-12-20 21:13:44 +05:30
import { useTranslation } from 'react-i18next';
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-12-20 21:13:44 +05:30
const { t } = useTranslation();
2024-10-03 02:47:29 +05:30
const [apiKey, setApiKey] = useState<string | null>(null);
2024-12-20 21:13:44 +05:30
const [apiKeyName, setApiKeyName] = useState<string>(t('apikey.default_name'));
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
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) {
2025-02-24 21:45:16 +05:30
notify('error', t('apikey.notifications.fetch_error', { error: error.message }));
2024-10-03 02:47:29 +05:30
} finally {
setLoading(false);
}
};
fetchApiKey();
2024-11-29 22:33:23 +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`);
2025-02-24 21:45:16 +05:30
setApiKey(data.api_key);
notify('success', t('apikey.notifications.success.generate'));
2024-10-03 05:27:15 +05:30
} catch (error: any) {
2025-02-24 21:45:16 +05:30
notify('error', t('apikey.notifications.generate_error', { error: 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 {
2025-02-24 21:45:16 +05:30
await axios.delete(`${apiUrl}/auth/delete-api-key`);
setApiKey(null);
notify('success', t('apikey.notifications.delete_success'));
2024-10-03 05:27:15 +05:30
} catch (error: any) {
2025-02-24 21:45:16 +05:30
notify('error', t('apikey.notifications.delete_error', { error: error.message }));
2024-10-03 05:06:11 +05:30
} finally {
setLoading(false);
}
};
2025-02-24 21:45:16 +05:30
const copyToClipboard = () => {
if (apiKey) {
navigator.clipboard.writeText(apiKey);
2024-10-03 02:47:29 +05:30
setCopySuccess(true);
setTimeout(() => setCopySuccess(false), 2000);
2025-02-24 21:45:16 +05:30
notify('info', t('apikey.notifications.copy_success'));
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',
width: '100vw',
2024-11-29 22:32:30 +05:30
}}
>
<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-12-20 21:13:44 +05:30
{t('apikey.title')}
2024-10-11 03:15:21 +05:30
</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>
2024-12-20 21:13:44 +05:30
<TableCell>{t('apikey.table.name')}</TableCell>
<TableCell>{t('apikey.table.key')}</TableCell>
<TableCell>{t('apikey.table.actions')}</TableCell>
2024-10-03 05:06:11 +05:30
</TableRow>
</TableHead>
<TableBody>
<TableRow>
<TableCell>{apiKeyName}</TableCell>
2024-12-31 21:26:53 +05:30
<TableCell>
<Box sx={{ fontFamily: 'monospace', width: '10ch' }}>
2024-12-31 21:27:12 +05:30
{showKey ? `${apiKey?.substring(0, 10)}...` : '**********'}
2024-12-31 21:26:53 +05:30
</Box>
</TableCell>
2024-10-03 05:06:11 +05:30
<TableCell>
2024-12-20 21:13:44 +05:30
<Tooltip title={t('apikey.actions.copy')}>
2024-10-03 05:06:11 +05:30
<IconButton onClick={copyToClipboard}>
<ContentCopy />
</IconButton>
</Tooltip>
2024-12-20 21:13:44 +05:30
<Tooltip title={showKey ? t('apikey.actions.hide') : t('apikey.actions.show')}>
2024-10-03 05:06:11 +05:30
<IconButton onClick={() => setShowKey(!showKey)}>
{showKey ? <VisibilityOff /> : <Visibility />}
2024-10-03 05:06:11 +05:30
</IconButton>
</Tooltip>
2024-12-20 21:13:44 +05:30
<Tooltip title={t('apikey.actions.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-12-20 21:13:44 +05:30
<Typography>{t('apikey.no_key_message')}</Typography>
2024-10-11 02:54:30 +05:30
<Button onClick={generateApiKey} variant="contained" color="primary" sx={{ marginTop: '15px' }}>
2024-12-20 21:13:44 +05:30
{t('apikey.generate_button')}
2024-10-03 02:47:29 +05:30
</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;