2021-11-22 11:49:09 +05:30
|
|
|
import { Typography } from 'antd';
|
|
|
|
|
import get from 'api/channels/get';
|
|
|
|
|
import Spinner from 'components/Spinner';
|
2022-03-28 21:01:57 +05:30
|
|
|
import {
|
2023-08-15 21:19:05 +05:30
|
|
|
MsTeamsChannel,
|
|
|
|
|
MsTeamsType,
|
2022-05-03 11:28:00 +05:30
|
|
|
PagerChannel,
|
|
|
|
|
PagerType,
|
2022-03-28 21:01:57 +05:30
|
|
|
SlackChannel,
|
|
|
|
|
SlackType,
|
|
|
|
|
WebhookChannel,
|
|
|
|
|
WebhookType,
|
|
|
|
|
} from 'container/CreateAlertChannels/config';
|
2021-11-22 11:49:09 +05:30
|
|
|
import EditAlertChannels from 'container/EditAlertChannels';
|
2022-04-18 15:24:51 +05:30
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
import { useQuery } from 'react-query';
|
2022-03-24 12:06:57 +05:30
|
|
|
import { useParams } from 'react-router-dom';
|
2021-11-22 11:49:09 +05:30
|
|
|
|
2022-03-22 12:10:31 +05:30
|
|
|
function ChannelsEdit(): JSX.Element {
|
2021-11-22 11:49:09 +05:30
|
|
|
const { id } = useParams<Params>();
|
2022-04-18 15:24:51 +05:30
|
|
|
const { t } = useTranslation();
|
2021-11-22 11:49:09 +05:30
|
|
|
|
2022-04-18 15:24:51 +05:30
|
|
|
const { isLoading, isError, data } = useQuery(['getChannel', id], {
|
|
|
|
|
queryFn: () =>
|
|
|
|
|
get({
|
|
|
|
|
id,
|
|
|
|
|
}),
|
2021-11-22 11:49:09 +05:30
|
|
|
});
|
|
|
|
|
|
2022-04-18 15:24:51 +05:30
|
|
|
if (isError) {
|
|
|
|
|
return <Typography>{data?.error || t('something_went_wrong')}</Typography>;
|
2021-11-22 11:49:09 +05:30
|
|
|
}
|
|
|
|
|
|
2022-04-18 15:24:51 +05:30
|
|
|
if (isLoading || !data?.payload) {
|
2021-11-22 11:49:09 +05:30
|
|
|
return <Spinner tip="Loading Channels..." />;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-18 15:24:51 +05:30
|
|
|
const { data: ChannelData } = data.payload;
|
2021-11-22 11:49:09 +05:30
|
|
|
|
2022-04-18 15:24:51 +05:30
|
|
|
const value = JSON.parse(ChannelData);
|
2021-11-22 11:49:09 +05:30
|
|
|
|
2022-05-03 11:28:00 +05:30
|
|
|
const prepChannelConfig = (): {
|
|
|
|
|
type: string;
|
2023-08-15 21:19:05 +05:30
|
|
|
channel: SlackChannel & WebhookChannel & PagerChannel & MsTeamsChannel;
|
2022-05-03 11:28:00 +05:30
|
|
|
} => {
|
2023-08-15 21:19:05 +05:30
|
|
|
let channel: SlackChannel & WebhookChannel & PagerChannel & MsTeamsChannel = {
|
|
|
|
|
name: '',
|
|
|
|
|
};
|
2022-05-03 11:28:00 +05:30
|
|
|
if (value && 'slack_configs' in value) {
|
|
|
|
|
const slackConfig = value.slack_configs[0];
|
|
|
|
|
channel = slackConfig;
|
|
|
|
|
return {
|
|
|
|
|
type: SlackType,
|
|
|
|
|
channel,
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-08-15 21:19:05 +05:30
|
|
|
|
|
|
|
|
if (value && 'msteams_configs' in value) {
|
|
|
|
|
const msteamsConfig = value.msteams_configs[0];
|
|
|
|
|
channel = msteamsConfig;
|
|
|
|
|
return {
|
|
|
|
|
type: MsTeamsType,
|
|
|
|
|
channel,
|
|
|
|
|
};
|
|
|
|
|
}
|
2022-05-03 11:28:00 +05:30
|
|
|
if (value && 'pagerduty_configs' in value) {
|
|
|
|
|
const pagerConfig = value.pagerduty_configs[0];
|
|
|
|
|
channel = pagerConfig;
|
|
|
|
|
channel.details = JSON.stringify(pagerConfig.details);
|
|
|
|
|
channel.detailsArray = { ...pagerConfig.details };
|
|
|
|
|
return {
|
|
|
|
|
type: PagerType,
|
|
|
|
|
channel,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (value && 'webhook_configs' in value) {
|
|
|
|
|
const webhookConfig = value.webhook_configs[0];
|
|
|
|
|
channel = webhookConfig;
|
|
|
|
|
channel.api_url = webhookConfig.url;
|
2021-11-22 11:49:09 +05:30
|
|
|
|
2022-05-03 11:28:00 +05:30
|
|
|
if ('http_config' in webhookConfig) {
|
|
|
|
|
const httpConfig = webhookConfig.http_config;
|
|
|
|
|
if ('basic_auth' in httpConfig) {
|
|
|
|
|
channel.username = webhookConfig.http_config?.basic_auth?.username;
|
|
|
|
|
channel.password = webhookConfig.http_config?.basic_auth?.password;
|
|
|
|
|
} else if ('authorization' in httpConfig) {
|
|
|
|
|
channel.password = webhookConfig.http_config?.authorization?.credentials;
|
|
|
|
|
}
|
2022-03-28 21:01:57 +05:30
|
|
|
}
|
2022-05-03 11:28:00 +05:30
|
|
|
return {
|
|
|
|
|
type: WebhookType,
|
|
|
|
|
channel,
|
|
|
|
|
};
|
2022-03-28 21:01:57 +05:30
|
|
|
}
|
2022-05-03 11:28:00 +05:30
|
|
|
return {
|
|
|
|
|
type: SlackType,
|
|
|
|
|
channel,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const target = prepChannelConfig();
|
2022-04-18 15:24:51 +05:30
|
|
|
|
2021-11-22 11:49:09 +05:30
|
|
|
return (
|
|
|
|
|
<EditAlertChannels
|
|
|
|
|
{...{
|
|
|
|
|
initialValue: {
|
2022-05-03 11:28:00 +05:30
|
|
|
...target.channel,
|
|
|
|
|
type: target.type,
|
2021-11-22 11:49:09 +05:30
|
|
|
name: value.name,
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2022-03-22 12:10:31 +05:30
|
|
|
}
|
2021-11-22 11:49:09 +05:30
|
|
|
interface Params {
|
|
|
|
|
id: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default ChannelsEdit;
|