Merge pull request #2189 from bluewave-labs/hotfix/notifications

hotfix: missing dependency array
This commit is contained in:
Alexander Holliday authored and GitHub committed 2025-05-04 22:27:35 -07:00
commit 2ab39f8fcd
1 file changed
+105 -91
@@ -20,18 +20,18 @@ import useNotifications from "../Hooks/useNotification";
// Define constants for notification types to avoid magic values // Define constants for notification types to avoid magic values
const NOTIFICATION_TYPES = { const NOTIFICATION_TYPES = {
SLACK: 'slack', SLACK: "slack",
DISCORD: 'discord', DISCORD: "discord",
TELEGRAM: 'telegram', TELEGRAM: "telegram",
WEBHOOK: 'webhook' WEBHOOK: "webhook",
}; };
// Define constants for field IDs // Define constants for field IDs
const FIELD_IDS = { const FIELD_IDS = {
WEBHOOK: 'webhook', WEBHOOK: "webhook",
TOKEN: 'token', TOKEN: "token",
CHAT_ID: 'chatId', CHAT_ID: "chatId",
URL: 'url' URL: "url",
}; };
const NotificationIntegrationModal = ({ const NotificationIntegrationModal = ({
@@ -40,7 +40,7 @@ const NotificationIntegrationModal = ({
monitor, monitor,
setMonitor, setMonitor,
// Optional prop to configure available notification types // Optional prop to configure available notification types
notificationTypes = null notificationTypes = null,
}) => { }) => {
const { t } = useTranslation(); const { t } = useTranslation();
const theme = useTheme(); const theme = useTheme();
@@ -49,78 +49,81 @@ const NotificationIntegrationModal = ({
const [loading, _, sendTestNotification] = useNotifications(); const [loading, _, sendTestNotification] = useNotifications();
// Helper to get the field state key with error handling // Helper to get the field state key with error handling
const getFieldKey = useCallback((typeId, fieldId) => { const getFieldKey = useCallback(
if (typeof typeId !== 'string' || typeId === '') { (typeId, fieldId) => {
throw new Error(t('errorInvalidTypeId')); if (typeof typeId !== "string" || typeId === "") {
throw new Error(t("errorInvalidTypeId"));
} }
if (typeof fieldId !== 'string' || fieldId === '') { if (typeof fieldId !== "string" || fieldId === "") {
throw new Error(t('errorInvalidFieldId')); throw new Error(t("errorInvalidFieldId"));
} }
return `${typeId}${fieldId.charAt(0).toUpperCase() + fieldId.slice(1)}`; return `${typeId}${fieldId.charAt(0).toUpperCase() + fieldId.slice(1)}`;
}); },
[t]
);
// Define notification types // Define notification types
const DEFAULT_NOTIFICATION_TYPES = [ const DEFAULT_NOTIFICATION_TYPES = [
{ {
id: NOTIFICATION_TYPES.SLACK, id: NOTIFICATION_TYPES.SLACK,
label: t('notifications.slack.label'), label: t("notifications.slack.label"),
description: t('notifications.slack.description'), description: t("notifications.slack.description"),
fields: [ fields: [
{ {
id: FIELD_IDS.WEBHOOK, id: FIELD_IDS.WEBHOOK,
label: t('notifications.slack.webhookLabel'), label: t("notifications.slack.webhookLabel"),
placeholder: t('notifications.slack.webhookPlaceholder'), placeholder: t("notifications.slack.webhookPlaceholder"),
type: 'text' type: "text",
} },
] ],
}, },
{ {
id: NOTIFICATION_TYPES.DISCORD, id: NOTIFICATION_TYPES.DISCORD,
label: t('notifications.discord.label'), label: t("notifications.discord.label"),
description: t('notifications.discord.description'), description: t("notifications.discord.description"),
fields: [ fields: [
{ {
id: FIELD_IDS.WEBHOOK, id: FIELD_IDS.WEBHOOK,
label: t('notifications.discord.webhookLabel'), label: t("notifications.discord.webhookLabel"),
placeholder: t('notifications.discord.webhookPlaceholder'), placeholder: t("notifications.discord.webhookPlaceholder"),
type: 'text' type: "text",
} },
] ],
}, },
{ {
id: NOTIFICATION_TYPES.TELEGRAM, id: NOTIFICATION_TYPES.TELEGRAM,
label: t('notifications.telegram.label'), label: t("notifications.telegram.label"),
description: t('notifications.telegram.description'), description: t("notifications.telegram.description"),
fields: [ fields: [
{ {
id: FIELD_IDS.TOKEN, id: FIELD_IDS.TOKEN,
label: t('notifications.telegram.tokenLabel'), label: t("notifications.telegram.tokenLabel"),
placeholder: t('notifications.telegram.tokenPlaceholder'), placeholder: t("notifications.telegram.tokenPlaceholder"),
type: 'text' type: "text",
}, },
{ {
id: FIELD_IDS.CHAT_ID, id: FIELD_IDS.CHAT_ID,
label: t('notifications.telegram.chatIdLabel'), label: t("notifications.telegram.chatIdLabel"),
placeholder: t('notifications.telegram.chatIdPlaceholder'), placeholder: t("notifications.telegram.chatIdPlaceholder"),
type: 'text' type: "text",
} },
] ],
}, },
{ {
id: NOTIFICATION_TYPES.WEBHOOK, id: NOTIFICATION_TYPES.WEBHOOK,
label: t('notifications.webhook.label'), label: t("notifications.webhook.label"),
description: t('notifications.webhook.description'), description: t("notifications.webhook.description"),
fields: [ fields: [
{ {
id: FIELD_IDS.URL, id: FIELD_IDS.URL,
label: t('notifications.webhook.urlLabel'), label: t("notifications.webhook.urlLabel"),
placeholder: t('notifications.webhook.urlPlaceholder'), placeholder: t("notifications.webhook.urlPlaceholder"),
type: 'text' type: "text",
} },
] ],
} },
]; ];
// Use provided notification types or default to our translated ones // Use provided notification types or default to our translated ones
@@ -207,22 +210,22 @@ const NotificationIntegrationModal = ({
}; };
const handleIntegrationChange = (type, checked) => { const handleIntegrationChange = (type, checked) => {
setIntegrations(prev => ({ setIntegrations((prev) => ({
...prev, ...prev,
[type]: checked [type]: checked,
})); }));
}; };
const handleInputChange = (type, value) => { const handleInputChange = (type, value) => {
setIntegrations(prev => ({ setIntegrations((prev) => ({
...prev, ...prev,
[type]: value [type]: value,
})); }));
}; };
const handleTestNotification = async (type) => { const handleTestNotification = async (type) => {
// Get the notification type details // Get the notification type details
const notificationType = activeNotificationTypes.find(t => t.id === type); const notificationType = activeNotificationTypes.find((t) => t.id === type);
if (typeof notificationType === "undefined") { if (typeof notificationType === "undefined") {
return; return;
@@ -232,7 +235,7 @@ const NotificationIntegrationModal = ({
const config = {}; const config = {};
// Add each field value to the config object // Add each field value to the config object
notificationType.fields.forEach(field => { notificationType.fields.forEach((field) => {
const fieldKey = getFieldKey(type, field.id); const fieldKey = getFieldKey(type, field.id);
config[field.id] = integrations[fieldKey]; config[field.id] = integrations[fieldKey];
}); });
@@ -247,42 +250,42 @@ const handleSave = () => {
const notifications = [...(monitor?.notifications || [])]; const notifications = [...(monitor?.notifications || [])];
// Get all notification types IDs // Get all notification types IDs
const existingTypes = activeNotificationTypes.map(type => type.id); const existingTypes = activeNotificationTypes.map((type) => type.id);
// Filter out notifications that are configurable in this modal // Filter out notifications that are configurable in this modal
const filteredNotifications = notifications.filter( const filteredNotifications = notifications.filter((notification) => {
notification => {
if (notification.platform) { if (notification.platform) {
return !existingTypes.includes(notification.platform); return !existingTypes.includes(notification.platform);
} }
return !existingTypes.includes(notification.type); return !existingTypes.includes(notification.type);
} });
);
// Add each enabled notification with its configured fields // Add each enabled notification with its configured fields
activeNotificationTypes.forEach(type => { activeNotificationTypes.forEach((type) => {
if (integrations[type.id]) { if (integrations[type.id]) {
let notificationObject = { let notificationObject = {
type: "webhook", type: "webhook",
platform: type.id, // Set platform to identify the specific service platform: type.id, // Set platform to identify the specific service
config: {} config: {},
}; };
// Configure based on notification type // Configure based on notification type
switch (type.id) { switch (type.id) {
case "slack": case "slack":
case "discord": case "discord":
notificationObject.config.webhookUrl = integrations[getFieldKey(type.id, 'webhook')]; notificationObject.config.webhookUrl =
integrations[getFieldKey(type.id, "webhook")];
break; break;
case "telegram": case "telegram":
notificationObject.config.botToken = integrations[getFieldKey(type.id, 'token')]; notificationObject.config.botToken =
notificationObject.config.chatId = integrations[getFieldKey(type.id, 'chatId')]; integrations[getFieldKey(type.id, "token")];
notificationObject.config.chatId =
integrations[getFieldKey(type.id, "chatId")];
break; break;
case "webhook": case "webhook":
notificationObject.config.webhookUrl = integrations[getFieldKey(type.id, 'url')]; notificationObject.config.webhookUrl =
integrations[getFieldKey(type.id, "url")];
break; break;
} }
@@ -291,9 +294,9 @@ const handleSave = () => {
}); });
// Update monitor with new notifications // Update monitor with new notifications
setMonitor(prev => ({ setMonitor((prev) => ({
...prev, ...prev,
notifications: filteredNotifications notifications: filteredNotifications,
})); }));
onClose(); onClose();
@@ -306,28 +309,31 @@ const handleSave = () => {
fullWidth fullWidth
maxWidth="md" maxWidth="md"
sx={{ sx={{
'& .MuiDialog-paper': { "& .MuiDialog-paper": {
width: `calc(80% - ${theme.spacing(40)})`, width: `calc(80% - ${theme.spacing(40)})`,
maxWidth: `${theme.breakpoints.values.md - 70}px` maxWidth: `${theme.breakpoints.values.md - 70}px`,
} },
}} }}
> >
<DialogContent> <DialogContent>
<Stack <Stack
direction="row" direction="row"
sx={{ sx={{
height: `calc(30vh - ${theme.spacing(20)})` height: `calc(30vh - ${theme.spacing(20)})`,
}}> }}
>
{/* Left sidebar with tabs */} {/* Left sidebar with tabs */}
<Box sx={{ <Box
sx={{
borderRight: 1, borderRight: 1,
borderColor: theme.palette.primary.lowContrast, borderColor: theme.palette.primary.lowContrast,
width: '30%', width: "30%",
maxWidth: theme.spacing(120), maxWidth: theme.spacing(120),
pr: theme.spacing(10) pr: theme.spacing(10),
}}> }}
>
<Typography variant="h2"> <Typography variant="h2">
{t('notifications.addOrEditNotifications')} {t("notifications.addOrEditNotifications")}
</Typography> </Typography>
<Tabs <Tabs
@@ -349,13 +355,19 @@ const handleSave = () => {
</Box> </Box>
{/* Right side content */} {/* Right side content */}
<Box sx={{ <Box
sx={{
flex: 1, flex: 1,
pl: theme.spacing(7.5), pl: theme.spacing(7.5),
overflowY: 'auto' overflowY: "auto",
}}> }}
>
{activeNotificationTypes.map((type, index) => ( {activeNotificationTypes.map((type, index) => (
<TabPanel key={type.id} value={tabValue} index={index}> <TabPanel
key={type.id}
value={tabValue}
index={index}
>
<TabComponent <TabComponent
type={type} type={type}
integrations={integrations} integrations={integrations}
@@ -369,25 +381,27 @@ const handleSave = () => {
</Box> </Box>
</Stack> </Stack>
</DialogContent> </DialogContent>
<DialogActions sx={{ <DialogActions
sx={{
p: theme.spacing(4), p: theme.spacing(4),
display: 'flex', display: "flex",
justifyContent: 'flex-end', justifyContent: "flex-end",
mb: theme.spacing(5), mb: theme.spacing(5),
mr: theme.spacing(5) mr: theme.spacing(5),
}}> }}
>
<Button <Button
variant="contained" variant="contained"
color="accent" color="accent"
onClick={handleSave} onClick={handleSave}
loading={loading} loading={loading}
sx={{ sx={{
width: 'auto', width: "auto",
minWidth: theme.spacing(60), minWidth: theme.spacing(60),
px: theme.spacing(8) px: theme.spacing(8),
}} }}
> >
{t('commonSave')} {t("commonSave")}
</Button> </Button>
</DialogActions> </DialogActions>
</Dialog> </Dialog>
@@ -399,7 +413,7 @@ NotificationIntegrationModal.propTypes = {
onClose: PropTypes.func.isRequired, onClose: PropTypes.func.isRequired,
monitor: PropTypes.object.isRequired, monitor: PropTypes.object.isRequired,
setMonitor: PropTypes.func.isRequired, setMonitor: PropTypes.func.isRequired,
notificationTypes: PropTypes.array notificationTypes: PropTypes.array,
}; };
export default NotificationIntegrationModal; export default NotificationIntegrationModal;