Made tabs configure ready.

This commit is contained in:
Skorpios committed 2025-03-07 17:25:28 -08:00
1 parent e6e20d507e
commit 92df32cd82
1 file changed
+165 -261
@@ -14,25 +14,98 @@ import TextInput from "../Inputs/TextInput";
import Checkbox from "../Inputs/Checkbox"; import Checkbox from "../Inputs/Checkbox";
import TabPanel from "./TabPanel"; import TabPanel from "./TabPanel";
// Configuration for notification tabs
const NOTIFICATION_TYPES = [
{
id: 'slack',
label: 'Slack',
description: 'To enable Slack notifications, create a Slack app and enable incoming webhooks. After that, simply provide the webhook URL here.',
fields: [
{
id: 'webhook',
label: 'Webhook URL',
placeholder: 'https://hooks.slack.com/services/...',
type: 'text'
}
]
},
{
id: 'discord',
label: 'Discord',
description: 'To send data to a Discord channel from Checkmate via Discord notifications using webhooks, you can use Discord\'s incoming Webhooks feature.',
fields: [
{
id: 'webhook',
label: 'Discord Webhook URL',
placeholder: 'https://discord.com/api/webhooks/...',
type: 'text'
}
]
},
{
id: 'telegram',
label: 'Telegram',
description: 'To enable Telegram notifications, create a Telegram bot using BotFather, an official bot for creating and managing Telegram bots. Then, get the API token and chat ID and write them down here.',
fields: [
{
id: 'token',
label: 'Your bot token',
placeholder: '123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11',
type: 'text'
},
{
id: 'chatId',
label: 'Your Chat ID',
placeholder: '-1001234567890',
type: 'text'
}
]
},
{
id: 'webhook',
label: 'Webhooks',
description: 'You can set up a custom webhook to receive notifications when incidents occur.',
fields: [
{
id: 'url',
label: 'Webhook URL',
placeholder: 'https://your-server.com/webhook',
type: 'text'
}
]
}
];
const NotificationIntegrationModal = ({ const NotificationIntegrationModal = ({
open, open,
onClose, onClose,
monitor, monitor,
setMonitor setMonitor,
// Optional prop to configure available notification types
notificationTypes = NOTIFICATION_TYPES
}) => { }) => {
const theme = useTheme(); const theme = useTheme();
const [tabValue, setTabValue] = useState(0); const [tabValue, setTabValue] = useState(0);
const [integrations, setIntegrations] = useState({
slack: monitor?.notifications?.some(n => n.type === "slack") || false, // Initialize integrations state based on available notification types
slackWebhook: monitor?.notifications?.find(n => n.type === "slack")?.webhook || "", const initializeIntegrationsState = () => {
discord: monitor?.notifications?.some(n => n.type === "discord") || false, const state = {};
discordWebhook: monitor?.notifications?.find(n => n.type === "discord")?.webhook || "",
telegram: monitor?.notifications?.some(n => n.type === "telegram") || false, notificationTypes.forEach(type => {
telegramToken: monitor?.notifications?.find(n => n.type === "telegram")?.token || "", // Add enabled flag for each notification type
telegramChatId: monitor?.notifications?.find(n => n.type === "telegram")?.chatId || "", state[type.id] = monitor?.notifications?.some(n => n.type === type.id) || false;
webhook: monitor?.notifications?.some(n => n.type === "webhook") || false,
webhookUrl: monitor?.notifications?.find(n => n.type === "webhook")?.url || "", // Add state for each field in the notification type
}); type.fields.forEach(field => {
const fieldKey = `${type.id}${field.id.charAt(0).toUpperCase() + field.id.slice(1)}`;
state[fieldKey] = monitor?.notifications?.find(n => n.type === type.id)?.[field.id] || "";
});
});
return state;
};
const [integrations, setIntegrations] = useState(initializeIntegrationsState());
const handleChangeTab = (event, newValue) => { const handleChangeTab = (event, newValue) => {
setTabValue(newValue); setTabValue(newValue);
@@ -61,39 +134,30 @@ const NotificationIntegrationModal = ({
//notifications array for selected integrations //notifications array for selected integrations
const notifications = [...(monitor?.notifications || [])]; const notifications = [...(monitor?.notifications || [])];
const existingTypes = ["slack", "discord", "telegram", "webhook"]; // Get all notification types IDs
const existingTypes = notificationTypes.map(type => type.id);
// Filter out notifications that are configurable in this modal
const filteredNotifications = notifications.filter( const filteredNotifications = notifications.filter(
notification => !existingTypes.includes(notification.type) notification => !existingTypes.includes(notification.type)
); );
if (integrations.slack) { // Add each enabled notification with its configured fields
filteredNotifications.push({ notificationTypes.forEach(type => {
type: "slack", if (integrations[type.id]) {
webhook: integrations.slackWebhook const notificationObject = {
}); type: type.id
} };
if (integrations.discord) { // Add each field value to the notification object
filteredNotifications.push({ type.fields.forEach(field => {
type: "discord", const fieldKey = `${type.id}${field.id.charAt(0).toUpperCase() + field.id.slice(1)}`;
webhook: integrations.discordWebhook notificationObject[field.id] = integrations[fieldKey];
}); });
}
filteredNotifications.push(notificationObject);
if (integrations.telegram) { }
filteredNotifications.push({ });
type: "telegram",
token: integrations.telegramToken,
chatId: integrations.telegramChatId
});
}
if (integrations.webhook) {
filteredNotifications.push({
type: "webhook",
url: integrations.webhookUrl
});
}
// Update monitor with new notifications // Update monitor with new notifications
setMonitor(prev => ({ setMonitor(prev => ({
@@ -141,234 +205,74 @@ const NotificationIntegrationModal = ({
onChange={handleChangeTab} onChange={handleChangeTab}
aria-label="Notification tabs" aria-label="Notification tabs"
> >
<Tab label="Slack" orientation="vertical" /> {notificationTypes.map((type, index) => (
<Tab label="Discord" orientation="vertical" /> <Tab key={type.id} label={type.label} orientation="vertical" />
<Tab label="Telegram" orientation="vertical" /> ))}
<Tab label="Webhooks" orientation="vertical" />
</Tabs> </Tabs>
</Box> </Box>
{/* Right side content */} {/* Right side content */}
<Box sx={{ flex: 1, pl: theme.spacing(7.5) }}> <Box sx={{ flex: 1, pl: theme.spacing(7.5) }}>
{/* Slack Tab */} {notificationTypes.map((type, index) => (
<TabPanel value={tabValue} index={0}> <TabPanel key={type.id} value={tabValue} index={index}>
<Typography variant="subtitle1" component="h4" sx={{ <Typography variant="subtitle1" component="h4" sx={{
fontWeight: 'bold',
color: theme.palette.primary.contrastTextSecondary
}}>Slack</Typography>
<Typography sx={{
mt: theme.spacing(0.5),
mb: theme.spacing(1.5),
color: theme.palette.primary.contrastTextTertiary
}}>
To enable Slack notifications, create a Slack app and enable incoming webhooks. After that, simply provide the webhook URL here.
</Typography>
<Box sx={{ pl: theme.spacing(1.5) }}>
<Checkbox
id="enable-slack"
label="Enable Slack notifications"
isChecked={integrations.slack}
onChange={(e) => handleIntegrationChange('slack', e.target.checked)}
/>
</Box>
<Box sx={{ mt: theme.spacing(1) }}>
<Typography sx={{
mb: theme.spacing(2),
fontWeight: 'bold', fontWeight: 'bold',
color: theme.palette.primary.contrastTextSecondary color: theme.palette.primary.contrastTextSecondary
}}>Webhook URL</Typography> }}>{type.label}</Typography>
<TextInput
id="slack-webhook"
type="text"
placeholder="https://hooks.slack.com/services/..."
value={integrations.slackWebhook}
onChange={(e) => handleInputChange('slackWebhook', e.target.value)}
disabled={!integrations.slack}
/>
</Box>
<Box sx={{ mt: theme.spacing(1) }}>
<Button
variant="text"
color="info"
onClick={() => handleTestNotification('slack')}
disabled={!integrations.slack || !integrations.slackWebhook}
>
Test notification
</Button>
</Box>
</TabPanel>
{/* Discord Tab */}
<TabPanel value={tabValue} index={1}>
<Typography variant="subtitle1" component="h4" sx={{
fontWeight: 'bold',
color: theme.palette.primary.contrastTextSecondary
}}>Discord</Typography>
<Typography sx={{
mt: theme.spacing(0.5),
mb: theme.spacing(1.5),
color: theme.palette.primary.contrastTextTertiary
}}>
To send data to a Discord channel from Checkmate via Discord notifications using webhooks, you can use Discord's incoming Webhooks feature.
</Typography>
<Box sx={{ pl: theme.spacing(1.5) }}>
<Checkbox
id="enable-discord"
label="Enable Discord notifications"
isChecked={integrations.discord}
onChange={(e) => handleIntegrationChange('discord', e.target.checked)}
/>
</Box>
<Box sx={{ mt: theme.spacing(1) }}>
<Typography sx={{ <Typography sx={{
mb: theme.spacing(2), mt: theme.spacing(0.5),
fontWeight: 'bold', mb: theme.spacing(1.5),
color: theme.palette.primary.contrastTextSecondary color: theme.palette.primary.contrastTextTertiary
}}>Discord Webhook URL</Typography> }}>
<TextInput {type.description}
id="discord-webhook" </Typography>
type="text"
placeholder="https://discord.com/api/webhooks/..." <Box sx={{ pl: theme.spacing(1.5) }}>
value={integrations.discordWebhook} <Checkbox
onChange={(e) => handleInputChange('discordWebhook', e.target.value)} id={`enable-${type.id}`}
disabled={!integrations.discord} label={`Enable ${type.label} notifications`}
/> isChecked={integrations[type.id]}
</Box> onChange={(e) => handleIntegrationChange(type.id, e.target.checked)}
/>
<Box sx={{ mt: theme.spacing(1) }}> </Box>
<Button
variant="text" {type.fields.map(field => {
color="info" const fieldKey = `${type.id}${field.id.charAt(0).toUpperCase() + field.id.slice(1)}`;
onClick={() => handleTestNotification('discord')}
disabled={!integrations.discord || !integrations.discordWebhook} return (
> <Box key={field.id} sx={{ mt: theme.spacing(1) }}>
Test notification <Typography sx={{
</Button> mb: theme.spacing(2),
</Box> fontWeight: 'bold',
</TabPanel> color: theme.palette.primary.contrastTextSecondary
}}>{field.label}</Typography>
{/* Telegram Tab */} <TextInput
<TabPanel value={tabValue} index={2}> id={`${type.id}-${field.id}`}
<Typography variant="subtitle1" component="h4" sx={{ type={field.type}
fontWeight: 'bold', placeholder={field.placeholder}
color: theme.palette.primary.contrastTextSecondary value={integrations[fieldKey]}
}}>Telegram</Typography> onChange={(e) => handleInputChange(fieldKey, e.target.value)}
<Typography sx={{ disabled={!integrations[type.id]}
mt: theme.spacing(0.5), />
mb: theme.spacing(1.5), </Box>
color: theme.palette.primary.contrastTextTertiary );
}}> })}
To enable Telegram notifications, create a Telegram bot using BotFather, an official bot for creating and managing Telegram bots. Then, get the API token and chat ID and write them down here.
</Typography> <Box sx={{ mt: theme.spacing(1) }}>
<Button
<Box sx={{ pl: theme.spacing(1.5) }}> variant="text"
<Checkbox color="info"
id="enable-telegram" onClick={() => handleTestNotification(type.id)}
label="Enable Telegram notifications" disabled={!integrations[type.id] || !type.fields.every(field => {
isChecked={integrations.telegram} const fieldKey = `${type.id}${field.id.charAt(0).toUpperCase() + field.id.slice(1)}`;
onChange={(e) => handleIntegrationChange('telegram', e.target.checked)} return integrations[fieldKey];
/> })}
</Box> >
Test notification
<Box sx={{ mt: theme.spacing(1) }}> </Button>
<Typography sx={{ </Box>
mb: theme.spacing(2), </TabPanel>
fontWeight: 'bold', ))}
color: theme.palette.primary.contrastTextSecondary
}}>Your bot token</Typography>
<TextInput
id="telegram-token"
type="text"
placeholder="123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"
value={integrations.telegramToken}
onChange={(e) => handleInputChange('telegramToken', e.target.value)}
disabled={!integrations.telegram}
/>
</Box>
<Box sx={{ mt: theme.spacing(1) }}>
<Typography sx={{
mb: theme.spacing(2),
fontWeight: 'bold',
color: theme.palette.primary.contrastTextSecondary
}}>Your Chat ID</Typography>
<TextInput
id="telegram-chat-id"
type="text"
placeholder="-1001234567890"
value={integrations.telegramChatId}
onChange={(e) => handleInputChange('telegramChatId', e.target.value)}
disabled={!integrations.telegram}
/>
</Box>
<Box sx={{ mt: theme.spacing(1) }}>
<Button
variant="text"
color="info"
onClick={() => handleTestNotification('telegram')}
disabled={!integrations.telegram || !integrations.telegramToken || !integrations.telegramChatId}
>
Test notification
</Button>
</Box>
</TabPanel>
{/* Webhooks Tab */}
<TabPanel value={tabValue} index={3}>
<Typography variant="subtitle1" component="h4" sx={{
fontWeight: 'bold',
color: theme.palette.primary.contrastTextSecondary
}}>Webhooks</Typography>
<Typography sx={{
mt: theme.spacing(0.5),
mb: theme.spacing(1.5),
color: theme.palette.primary.contrastTextTertiary
}}>
You can set up a custom webhook to receive notifications when incidents occur.
</Typography>
<Box sx={{ pl: theme.spacing(1.5) }}>
<Checkbox
id="enable-webhook"
label="Enable Webhook notifications"
isChecked={integrations.webhook}
onChange={(e) => handleIntegrationChange('webhook', e.target.checked)}
/>
</Box>
<Box sx={{ mt: theme.spacing(1) }}>
<Typography sx={{
mb: theme.spacing(2),
fontWeight: 'bold',
color: theme.palette.primary.contrastTextSecondary
}}>Webhook URL</Typography>
<TextInput
id="webhook-url"
type="text"
placeholder="https://your-server.com/webhook"
value={integrations.webhookUrl}
onChange={(e) => handleInputChange('webhookUrl', e.target.value)}
disabled={!integrations.webhook}
/>
</Box>
<Box sx={{ mt: theme.spacing(1) }}>
<Button
variant="text"
color="info"
onClick={() => handleTestNotification('webhook')}
disabled={!integrations.webhook || !integrations.webhookUrl}
>
Test notification
</Button>
</Box>
</TabPanel>
</Box> </Box>
</Box> </Box>
</DialogContent> </DialogContent>