1
1
import { useState , useMemo } from "react" ;
2
2
import { useTranslation } from "react-i18next" ;
3
+ import PropTypes from "prop-types" ;
3
4
4
5
import {
5
6
Dialog ,
@@ -49,11 +50,11 @@ const NotificationIntegrationModal = ({
49
50
// Helper to get the field state key with error handling
50
51
const getFieldKey = ( typeId , fieldId ) => {
51
52
if ( typeof typeId !== 'string' || typeId === '' ) {
52
- throw new Error ( 'Invalid typeId provided to getFieldKey' ) ;
53
+ throw new Error ( t ( 'errorInvalidTypeId' ) ) ;
53
54
}
54
55
55
56
if ( typeof fieldId !== 'string' || fieldId === '' ) {
56
- throw new Error ( 'Invalid fieldId provided to getFieldKey' ) ;
57
+ throw new Error ( t ( 'errorInvalidFieldId' ) ) ;
57
58
}
58
59
59
60
return `${ typeId } ${ fieldId . charAt ( 0 ) . toUpperCase ( ) + fieldId . slice ( 1 ) } ` ;
@@ -182,43 +183,64 @@ const NotificationIntegrationModal = ({
182
183
await sendTestNotification ( type , config ) ;
183
184
} ;
184
185
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 => {
191
198
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
+ ) ;
196
206
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 ;
211
230
}
212
- } ) ;
231
+
232
+ filteredNotifications . push ( notificationObject ) ;
233
+ }
234
+ } ) ;
213
235
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
+ } ;
222
244
223
245
return (
224
246
< Dialog
@@ -320,4 +342,12 @@ const NotificationIntegrationModal = ({
320
342
) ;
321
343
} ;
322
344
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
+
323
353
export default NotificationIntegrationModal ;
0 commit comments