Merge pull request #1982 from bluewave-labs/feat/monitor-notification-integration

Add notifications to our existing monitors
This commit is contained in:
Alexander Holliday authored and GitHub committed 2025-03-24 12:48:04 -07:00
commit 7cb8590dbf
3 files changed
+66 -34

No files matched your search

@@ -1,5 +1,6 @@
import { useState, useMemo } from "react"; import { useState, useMemo } from "react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import PropTypes from "prop-types";
import { import {
Dialog, Dialog,
@@ -49,11 +50,11 @@ const NotificationIntegrationModal = ({
// Helper to get the field state key with error handling // Helper to get the field state key with error handling
const getFieldKey = (typeId, fieldId) => { const getFieldKey = (typeId, fieldId) => {
if (typeof typeId !== 'string' || typeId === '') { if (typeof typeId !== 'string' || typeId === '') {
throw new Error('Invalid typeId provided to getFieldKey'); throw new Error(t('errorInvalidTypeId'));
} }
if (typeof fieldId !== 'string' || fieldId === '') { if (typeof fieldId !== 'string' || fieldId === '') {
throw new Error('Invalid fieldId provided to getFieldKey'); throw new Error(t('errorInvalidFieldId'));
} }
return `${typeId}${fieldId.charAt(0).toUpperCase() + fieldId.slice(1)}`; return `${typeId}${fieldId.charAt(0).toUpperCase() + fieldId.slice(1)}`;
@@ -182,43 +183,64 @@ const NotificationIntegrationModal = ({
await sendTestNotification(type, config); await sendTestNotification(type, config);
}; };
const handleSave = () => { // In NotificationIntegrationModal.jsx, update the handleSave function:
//notifications array for selected integrations
const notifications = [...(monitor?.notifications || [])];
// Get all notification types IDs const handleSave = () => {
const existingTypes = activeNotificationTypes.map(type => type.id); // Get existing notifications
const notifications = [...(monitor?.notifications || [])];
// Filter out notifications that are configurable in this modal // Get all notification types IDs
const filteredNotifications = notifications.filter( const existingTypes = activeNotificationTypes.map(type => type.id);
notification => !existingTypes.includes(notification.type)
);
// Add each enabled notification with its configured fields // Filter out notifications that are configurable in this modal
activeNotificationTypes.forEach(type => { const filteredNotifications = notifications.filter(
if (integrations[type.id]) { notification => {
const notificationObject = {
type: type.id
};
// Add each field value to the notification object if (notification.platform) {
type.fields.forEach(field => { return !existingTypes.includes(notification.platform);
const fieldKey = getFieldKey(type.id, field.id);
notificationObject[field.id] = integrations[fieldKey];
});
filteredNotifications.push(notificationObject);
} }
});
// Update monitor with new notifications return !existingTypes.includes(notification.type);
setMonitor(prev => ({ }
...prev, );
notifications: filteredNotifications
}));
onClose(); // Add each enabled notification with its configured fields
}; activeNotificationTypes.forEach(type => {
if (integrations[type.id]) {
let notificationObject = {
type: "webhook",
platform: type.id, // Set platform to identify the specific service
config: {}
};
// Configure based on notification type
switch(type.id) {
case "slack":
case "discord":
notificationObject.config.webhookUrl = integrations[getFieldKey(type.id, 'webhook')];
break;
case "telegram":
notificationObject.config.botToken = integrations[getFieldKey(type.id, 'token')];
notificationObject.config.chatId = integrations[getFieldKey(type.id, 'chatId')];
break;
case "webhook":
notificationObject.config.webhookUrl = integrations[getFieldKey(type.id, 'url')];
break;
}
filteredNotifications.push(notificationObject);
}
});
// Update monitor with new notifications
setMonitor(prev => ({
...prev,
notifications: filteredNotifications
}));
onClose();
};
return ( return (
<Dialog <Dialog
@@ -320,4 +342,12 @@ const NotificationIntegrationModal = ({
); );
}; };
NotificationIntegrationModal.propTypes = {
open: PropTypes.bool.isRequired,
onClose: PropTypes.func.isRequired,
monitor: PropTypes.object.isRequired,
setMonitor: PropTypes.func.isRequired,
notificationTypes: PropTypes.array
};
export default NotificationIntegrationModal; export default NotificationIntegrationModal;
+2 -2
View File
@@ -415,7 +415,7 @@ const CreateMonitor = () => {
onChange={(event) => handleNotifications(event, "email")} onChange={(event) => handleNotifications(event, "email")}
/> />
<Box mt={theme.spacing(2)}> {/* <Box mt={theme.spacing(2)}>
<Button <Button
variant="contained" variant="contained"
color="accent" color="accent"
@@ -423,7 +423,7 @@ const CreateMonitor = () => {
> >
{t("notifications.integrationButton")} {t("notifications.integrationButton")}
</Button> </Button>
</Box> </Box> */}
</Stack> </Stack>
</ConfigBox> </ConfigBox>
<ConfigBox> <ConfigBox>
+2
View File
@@ -184,6 +184,8 @@
"unsupportedType": "Unsupported notification type", "unsupportedType": "Unsupported notification type",
"networkError": "Network error occurred" "networkError": "Network error occurred"
}, },
"errorInvalidTypeId": "Invalid notification type provided",
"errorInvalidFieldId": "Invalid field ID provided",
"testLocale": "testLocale", "testLocale": "testLocale",
"add": "Add", "add": "Add",
"monitors": "monitors", "monitors": "monitors",