2025-03-11 18:37:14 -07:00
|
|
|
import { useState } from 'react';
|
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
import { networkService } from '../../../Utils/NetworkService';
|
2025-03-20 08:29:14 -07:00
|
|
|
import { createToast } from '../../../Utils/toastUtils';
|
2025-03-11 18:37:14 -07:00
|
|
|
|
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'
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-11 18:37:14 -07:00
|
|
|
/**
|
|
|
|
|
* Custom hook for notification-related operations
|
|
|
|
|
*/
|
|
|
|
|
const useNotifications = () => {
|
|
|
|
|
const [loading, setLoading] = useState(false);
|
2025-03-16 13:15:25 -07:00
|
|
|
const [error, setError] = useState(undefined);
|
2025-03-11 18:37:14 -07:00
|
|
|
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);
|
2025-03-16 13:15:25 -07:00
|
|
|
setError(undefined);
|
2025-03-11 18:37:14 -07:00
|
|
|
|
|
|
|
|
// 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:
|
2025-03-11 18:37:14 -07:00
|
|
|
payload.webhookUrl = config.webhook;
|
2025-03-11 19:12:00 -07:00
|
|
|
if (typeof payload.webhookUrl === 'undefined' || payload.webhookUrl === '') {
|
2025-03-11 18:37:14 -07:00
|
|
|
isValid = false;
|
2025-03-16 14:04:56 -07:00
|
|
|
errorMessage = t('notifications.slack.webhookRequired');
|
2025-03-11 18:37:14 -07:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2025-03-11 19:12:00 -07:00
|
|
|
case NOTIFICATION_TYPES.DISCORD:
|
2025-03-11 18:37:14 -07:00
|
|
|
payload.webhookUrl = config.webhook;
|
2025-03-11 19:12:00 -07:00
|
|
|
if (typeof payload.webhookUrl === 'undefined' || payload.webhookUrl === '') {
|
2025-03-11 18:37:14 -07:00
|
|
|
isValid = false;
|
2025-03-16 14:04:56 -07:00
|
|
|
errorMessage = t('notifications.discord.webhookRequired');
|
2025-03-11 18:37:14 -07:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2025-03-11 19:12:00 -07:00
|
|
|
case NOTIFICATION_TYPES.TELEGRAM:
|
2025-03-11 18:37:14 -07:00
|
|
|
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 === '') {
|
2025-03-11 18:37:14 -07:00
|
|
|
isValid = false;
|
2025-03-16 14:04:56 -07:00
|
|
|
errorMessage = t('notifications.telegram.fieldsRequired');
|
2025-03-11 18:37:14 -07:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2025-03-11 19:12:00 -07:00
|
|
|
case NOTIFICATION_TYPES.WEBHOOK:
|
2025-03-11 18:37:14 -07:00
|
|
|
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 === '') {
|
2025-03-11 18:37:14 -07:00
|
|
|
isValid = false;
|
2025-03-16 14:04:56 -07:00
|
|
|
errorMessage = t('notifications.webhook.urlRequired');
|
2025-03-11 18:37:14 -07:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
isValid = false;
|
2025-03-16 14:04:56 -07:00
|
|
|
errorMessage = t('notifications.unsupportedType');
|
2025-03-11 18:37:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If validation fails, show error and return
|
2025-03-11 19:12:00 -07:00
|
|
|
if (isValid === false) {
|
2025-03-20 08:29:14 -07:00
|
|
|
createToast({
|
|
|
|
|
body: errorMessage,
|
|
|
|
|
variant: "error"
|
|
|
|
|
});
|
2025-03-11 18:37:14 -07:00
|
|
|
setLoading(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2025-03-16 14:46:17 -07:00
|
|
|
const response = await networkService.testNotification({
|
|
|
|
|
platform: type,
|
|
|
|
|
payload: payload
|
|
|
|
|
});
|
2025-03-11 18:37:14 -07:00
|
|
|
|
2025-03-11 19:12:00 -07:00
|
|
|
if (response.data.success === true) {
|
2025-03-20 08:29:14 -07:00
|
|
|
createToast({
|
|
|
|
|
body: t('notifications.testSuccess'),
|
|
|
|
|
variant: "info"
|
|
|
|
|
});
|
2025-03-11 18:37:14 -07:00
|
|
|
} else {
|
2025-03-16 14:04:56 -07:00
|
|
|
throw new Error(response.data.msg || t('notifications.testFailed'));
|
2025-03-11 18:37:14 -07:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
2025-03-16 14:04:56 -07:00
|
|
|
const errorMsg = error.response?.data?.msg || error.message || t('notifications.networkError');
|
2025-03-20 08:29:14 -07:00
|
|
|
createToast({
|
|
|
|
|
body: `${t('notifications.testFailed')}: ${errorMsg}`,
|
|
|
|
|
variant: "error"
|
|
|
|
|
});
|
2025-03-11 18:37:14 -07:00
|
|
|
setError(errorMsg);
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-16 13:12:13 -07:00
|
|
|
return [
|
2025-03-11 18:37:14 -07:00
|
|
|
loading,
|
|
|
|
|
error,
|
|
|
|
|
sendTestNotification
|
2025-03-16 13:12:13 -07:00
|
|
|
];
|
2025-03-11 18:37:14 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default useNotifications;
|