Skip to content

Commit 7cb8590

Browse files
authored
Merge pull request #1982 from bluewave-labs/feat/monitor-notification-integration
Add notifications to our existing monitors
2 parents 0dde925 + 5c6ea20 commit 7cb8590

File tree

3 files changed

+69
-37
lines changed

3 files changed

+69
-37
lines changed

src/Components/NotificationIntegrationModal/Components/NotificationIntegrationModal.jsx

+65-35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useState, useMemo } from "react";
22
import { useTranslation } from "react-i18next";
3+
import PropTypes from "prop-types";
34

45
import {
56
Dialog,
@@ -49,11 +50,11 @@ const NotificationIntegrationModal = ({
4950
// Helper to get the field state key with error handling
5051
const getFieldKey = (typeId, fieldId) => {
5152
if (typeof typeId !== 'string' || typeId === '') {
52-
throw new Error('Invalid typeId provided to getFieldKey');
53+
throw new Error(t('errorInvalidTypeId'));
5354
}
5455

5556
if (typeof fieldId !== 'string' || fieldId === '') {
56-
throw new Error('Invalid fieldId provided to getFieldKey');
57+
throw new Error(t('errorInvalidFieldId'));
5758
}
5859

5960
return `${typeId}${fieldId.charAt(0).toUpperCase() + fieldId.slice(1)}`;
@@ -182,43 +183,64 @@ const NotificationIntegrationModal = ({
182183
await sendTestNotification(type, config);
183184
};
184185

185-
const handleSave = () => {
186-
//notifications array for selected integrations
187-
const notifications = [...(monitor?.notifications || [])];
188-
189-
// Get all notification types IDs
190-
const existingTypes = activeNotificationTypes.map(type => type.id);
186+
// In NotificationIntegrationModal.jsx, update the handleSave function:
187+
188+
const handleSave = () => {
189+
// Get existing notifications
190+
const notifications = [...(monitor?.notifications || [])];
191+
192+
// Get all notification types IDs
193+
const existingTypes = activeNotificationTypes.map(type => type.id);
194+
195+
// Filter out notifications that are configurable in this modal
196+
const filteredNotifications = notifications.filter(
197+
notification => {
191198

192-
// Filter out notifications that are configurable in this modal
193-
const filteredNotifications = notifications.filter(
194-
notification => !existingTypes.includes(notification.type)
195-
);
199+
if (notification.platform) {
200+
return !existingTypes.includes(notification.platform);
201+
}
202+
203+
return !existingTypes.includes(notification.type);
204+
}
205+
);
196206

197-
// Add each enabled notification with its configured fields
198-
activeNotificationTypes.forEach(type => {
199-
if (integrations[type.id]) {
200-
const notificationObject = {
201-
type: type.id
202-
};
203-
204-
// Add each field value to the notification object
205-
type.fields.forEach(field => {
206-
const fieldKey = getFieldKey(type.id, field.id);
207-
notificationObject[field.id] = integrations[fieldKey];
208-
});
209-
210-
filteredNotifications.push(notificationObject);
207+
// Add each enabled notification with its configured fields
208+
activeNotificationTypes.forEach(type => {
209+
if (integrations[type.id]) {
210+
211+
let notificationObject = {
212+
type: "webhook",
213+
platform: type.id, // Set platform to identify the specific service
214+
config: {}
215+
};
216+
217+
// Configure based on notification type
218+
switch(type.id) {
219+
case "slack":
220+
case "discord":
221+
notificationObject.config.webhookUrl = integrations[getFieldKey(type.id, 'webhook')];
222+
break;
223+
case "telegram":
224+
notificationObject.config.botToken = integrations[getFieldKey(type.id, 'token')];
225+
notificationObject.config.chatId = integrations[getFieldKey(type.id, 'chatId')];
226+
break;
227+
case "webhook":
228+
notificationObject.config.webhookUrl = integrations[getFieldKey(type.id, 'url')];
229+
break;
211230
}
212-
});
231+
232+
filteredNotifications.push(notificationObject);
233+
}
234+
});
213235

214-
// Update monitor with new notifications
215-
setMonitor(prev => ({
216-
...prev,
217-
notifications: filteredNotifications
218-
}));
219-
220-
onClose();
221-
};
236+
// Update monitor with new notifications
237+
setMonitor(prev => ({
238+
...prev,
239+
notifications: filteredNotifications
240+
}));
241+
242+
onClose();
243+
};
222244

223245
return (
224246
<Dialog
@@ -320,4 +342,12 @@ const NotificationIntegrationModal = ({
320342
);
321343
};
322344

345+
NotificationIntegrationModal.propTypes = {
346+
open: PropTypes.bool.isRequired,
347+
onClose: PropTypes.func.isRequired,
348+
monitor: PropTypes.object.isRequired,
349+
setMonitor: PropTypes.func.isRequired,
350+
notificationTypes: PropTypes.array
351+
};
352+
323353
export default NotificationIntegrationModal;

src/Pages/Uptime/Create/index.jsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -415,15 +415,15 @@ const CreateMonitor = () => {
415415
onChange={(event) => handleNotifications(event, "email")}
416416
/>
417417

418-
<Box mt={theme.spacing(2)}>
418+
{/* <Box mt={theme.spacing(2)}>
419419
<Button
420420
variant="contained"
421421
color="accent"
422422
onClick={handleOpenNotificationModal}
423423
>
424424
{t("notifications.integrationButton")}
425425
</Button>
426-
</Box>
426+
</Box> */}
427427
</Stack>
428428
</ConfigBox>
429429
<ConfigBox>

src/locales/gb.json

+2
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@
184184
"unsupportedType": "Unsupported notification type",
185185
"networkError": "Network error occurred"
186186
},
187+
"errorInvalidTypeId": "Invalid notification type provided",
188+
"errorInvalidFieldId": "Invalid field ID provided",
187189
"testLocale": "testLocale",
188190
"add": "Add",
189191
"monitors": "monitors",

0 commit comments

Comments
 (0)