palash-signoz 01bad0f18a
chore: eslint fix (#884)
* chore: eslint is updated

* chore: some eslint fixes are made

* chore: some more eslint fix are updated

* chore: some eslint fix is made

* chore: styled components type is added

* chore: some more eslint fix are made

* chore: some more eslint fix are updated

* chore: some more eslint fix are updated

* fix: eslint fixes

Co-authored-by: Pranshu Chittora <pranshu@signoz.io>
2022-03-24 12:06:57 +05:30

119 lines
3.2 KiB
TypeScript

import { Form, notification } from 'antd';
import createSlackApi from 'api/channels/createSlack';
import ROUTES from 'constants/routes';
import FormAlertChannels from 'container/FormAlertChannels';
import history from 'lib/history';
import React, { useCallback, useState } from 'react';
import { ChannelType, SlackChannel } from './config';
function CreateAlertChannels({
preType = 'slack',
}: CreateAlertChannelsProps): JSX.Element {
const [formInstance] = Form.useForm();
const [selectedConfig, setSelectedConfig] = useState<Partial<SlackChannel>>({
text: ` {{ range .Alerts -}}
*Alert:* {{ .Annotations.title }}{{ if .Labels.severity }} - {{ .Labels.severity }}{{ end }}
*Description:* {{ .Annotations.description }}
*Details:*
{{ range .Labels.SortedPairs }} • *{{ .Name }}:* {{ .Value }}
{{ end }}
{{ end }}`,
title: `[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .CommonLabels.alertname }} for {{ .CommonLabels.job }}
{{- if gt (len .CommonLabels) (len .GroupLabels) -}}
{{" "}}(
{{- with .CommonLabels.Remove .GroupLabels.Names }}
{{- range $index, $label := .SortedPairs -}}
{{ if $index }}, {{ end }}
{{- $label.Name }}="{{ $label.Value -}}"
{{- end }}
{{- end -}}
)
{{- end }}`,
});
const [savingState, setSavingState] = useState<boolean>(false);
const [notifications, NotificationElement] = notification.useNotification();
const [type, setType] = useState<ChannelType>(preType);
const onTypeChangeHandler = useCallback((value: string) => {
setType(value as ChannelType);
}, []);
const onTestHandler = useCallback(() => {
console.log('test');
}, []);
const onSlackHandler = useCallback(async () => {
try {
setSavingState(true);
const response = await createSlackApi({
api_url: selectedConfig?.api_url || '',
channel: selectedConfig?.channel || '',
name: selectedConfig?.name || '',
send_resolved: true,
text: selectedConfig?.text || '',
title: selectedConfig?.title || '',
});
if (response.statusCode === 200) {
notifications.success({
message: 'Success',
description: 'Successfully created the channel',
});
setTimeout(() => {
history.replace(ROUTES.SETTINGS);
}, 2000);
} else {
notifications.error({
message: 'Error',
description: response.error || 'Error while creating the channel',
});
}
setSavingState(false);
} catch (error) {
setSavingState(false);
}
}, [notifications, selectedConfig]);
const onSaveHandler = useCallback(
async (value: ChannelType) => {
if (value === 'slack') {
onSlackHandler();
}
},
[onSlackHandler],
);
return (
<FormAlertChannels
{...{
formInstance,
onTypeChangeHandler,
setSelectedConfig,
type,
onTestHandler,
onSaveHandler,
savingState,
NotificationElement,
title: 'New Notification Channels',
initialValue: {
type,
...selectedConfig,
},
}}
/>
);
}
interface CreateAlertChannelsProps {
preType?: ChannelType;
}
CreateAlertChannels.defaultProps = {
preType: undefined,
};
export default CreateAlertChannels;