Files
Checkmate/src/Components/NotificationIntegrationModal/Hooks/useNotification.js
T

116 lines
3.4 KiB
JavaScript
Raw Normal View History

import { useState } from 'react';
import { toast } from 'react-toastify';
import { useTranslation } from "react-i18next";
import { networkService } from '../../../Utils/NetworkService';
2025-03-11 19:12:00 -07:00
// Define constants for notification types to avoid magic values
const NOTIFICATION_TYPES = {
SLACK: 'slack',
DISCORD: 'discord',
TELEGRAM: 'telegram',
WEBHOOK: 'webhook'
};
// Define constants for field IDs
const FIELD_IDS = {
WEBHOOK: 'webhook',
TOKEN: 'token',
CHAT_ID: 'chatId',
URL: 'url'
};
/**
* Custom hook for notification-related operations
*/
const useNotifications = () => {
const [loading, setLoading] = useState(false);
const [error, setError] = useState(undefined);
const { t } = useTranslation();
/**
* Send a test notification
* @param {string} type - The notification type (slack, discord, telegram, webhook)
* @param {object} config - Configuration object with necessary params
*/
const sendTestNotification = async (type, config) => {
setLoading(true);
setError(undefined);
// Validation based on notification type
let payload = { platform: type };
let isValid = true;
let errorMessage = '';
switch(type) {
2025-03-11 19:12:00 -07:00
case NOTIFICATION_TYPES.SLACK:
payload.webhookUrl = config.webhook;
2025-03-11 19:12:00 -07:00
if (typeof payload.webhookUrl === 'undefined' || payload.webhookUrl === '') {
isValid = false;
2025-03-16 14:04:56 -07:00
errorMessage = t('notifications.slack.webhookRequired');
}
break;
2025-03-11 19:12:00 -07:00
case NOTIFICATION_TYPES.DISCORD:
payload.webhookUrl = config.webhook;
2025-03-11 19:12:00 -07:00
if (typeof payload.webhookUrl === 'undefined' || payload.webhookUrl === '') {
isValid = false;
2025-03-16 14:04:56 -07:00
errorMessage = t('notifications.discord.webhookRequired');
}
break;
2025-03-11 19:12:00 -07:00
case NOTIFICATION_TYPES.TELEGRAM:
payload.botToken = config.token;
payload.chatId = config.chatId;
2025-03-11 19:12:00 -07:00
if (typeof payload.botToken === 'undefined' || payload.botToken === '' ||
typeof payload.chatId === 'undefined' || payload.chatId === '') {
isValid = false;
2025-03-16 14:04:56 -07:00
errorMessage = t('notifications.telegram.fieldsRequired');
}
break;
2025-03-11 19:12:00 -07:00
case NOTIFICATION_TYPES.WEBHOOK:
payload.webhookUrl = config.url;
2025-03-11 19:14:42 -07:00
payload.platform = NOTIFICATION_TYPES.SLACK;
2025-03-11 19:12:00 -07:00
if (typeof payload.webhookUrl === 'undefined' || payload.webhookUrl === '') {
isValid = false;
2025-03-16 14:04:56 -07:00
errorMessage = t('notifications.webhook.urlRequired');
}
break;
default:
isValid = false;
2025-03-16 14:04:56 -07:00
errorMessage = t('notifications.unsupportedType');
}
// If validation fails, show error and return
2025-03-11 19:12:00 -07:00
if (isValid === false) {
toast.error(errorMessage);
setLoading(false);
return;
}
try {
const response = await networkService.axiosInstance.post('/notifications/test-webhook', payload);
2025-03-11 19:12:00 -07:00
if (response.data.success === true) {
2025-03-16 14:04:56 -07:00
toast.success(t('notifications.testSuccess'));
} else {
2025-03-16 14:04:56 -07:00
throw new Error(response.data.msg || t('notifications.testFailed'));
}
} catch (error) {
2025-03-16 14:04:56 -07:00
const errorMsg = error.response?.data?.msg || error.message || t('notifications.networkError');
toast.error(`${t('notifications.testFailed')}: ${errorMsg}`);
setError(errorMsg);
} finally {
setLoading(false);
}
};
2025-03-16 13:12:13 -07:00
return [
loading,
error,
sendTestNotification
2025-03-16 13:12:13 -07:00
];
};
export default useNotifications;