feat: better notifications for api key page

This commit is contained in:
Rohit
2025-02-10 15:06:21 +05:30
parent 85132c3d50
commit ed5e96b476

View File

@@ -83,11 +83,36 @@ const ApiKeyManager = () => {
setLoading(true);
try {
const { data } = await axios.post(`${apiUrl}/auth/generate-api-key`);
setApiKey(data.api_key);
notify('success', t('apikey.notifications.generate_success'));
if (data.ok && data.api_key) {
setApiKey(data.api_key);
notify('success', t('apikey.notifications.success.generate'));
}
} catch (error: any) {
notify('error', t('apikey.notifications.generate_error', { error: error.message }));
const status = error.response?.status;
let errorKey = 'unknown';
switch (status) {
case 401:
errorKey = 'unauthorized';
break;
case 403:
errorKey = 'limit_reached';
break;
case 500:
errorKey = 'server';
break;
default:
if (error.message?.includes('Network Error')) {
errorKey = 'network';
}
}
notify(
'error',
t(`apikey.notifications.errors.generate.${errorKey}`, {
error: error.response?.data?.message || error.message
})
);
} finally {
setLoading(false);
}
@@ -96,22 +121,54 @@ const ApiKeyManager = () => {
const deleteApiKey = async () => {
setLoading(true);
try {
await axios.delete(`${apiUrl}/auth/delete-api-key`);
setApiKey(null);
notify('success', t('apikey.notifications.delete_success'));
const response = await axios.delete(`${apiUrl}/auth/delete-api-key`);
if (response.data.ok) {
setApiKey(null);
notify('success', t('apikey.notifications.success.delete'));
}
} catch (error: any) {
notify('error', t('apikey.notifications.delete_error', { error: error.message }));
const status = error.response?.status;
let errorKey = 'unknown';
switch (status) {
case 401:
errorKey = 'unauthorized';
break;
case 404:
errorKey = 'not_found';
break;
case 500:
errorKey = 'server';
break;
default:
if (error.message?.includes('Network Error')) {
errorKey = 'network';
}
}
notify(
'error',
t(`apikey.notifications.errors.delete.${errorKey}`, {
error: error.response?.data?.message || error.message
})
);
} finally {
setLoading(false);
}
};
const copyToClipboard = () => {
if (apiKey) {
navigator.clipboard.writeText(apiKey);
const copyToClipboard = async () => {
if (!apiKey) return;
try {
await navigator.clipboard.writeText(apiKey);
setCopySuccess(true);
notify('success', t('apikey.notifications.success.copy'));
// Reset copy success state after 2 seconds
setTimeout(() => setCopySuccess(false), 2000);
notify('info', t('apikey.notifications.copy_success'));
} catch (error) {
notify('error', t('apikey.notifications.errors.copy.failed'));
}
};