issueAlertNotificationOptions.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import {Fragment, useCallback, useEffect, useMemo, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import MultipleCheckbox from 'sentry/components/forms/controls/multipleCheckbox';
  4. import {t, tct} from 'sentry/locale';
  5. import {space} from 'sentry/styles/space';
  6. import {type IntegrationAction, IssueAlertActionType} from 'sentry/types/alerts';
  7. import type {OrganizationIntegration} from 'sentry/types/integrations';
  8. import {useApiQuery} from 'sentry/utils/queryClient';
  9. import useApi from 'sentry/utils/useApi';
  10. import useOrganization from 'sentry/utils/useOrganization';
  11. import SetupMessagingIntegrationButton, {
  12. MessagingIntegrationAnalyticsView,
  13. } from 'sentry/views/alerts/rules/issue/setupMessagingIntegrationButton';
  14. import MessagingIntegrationAlertRule from 'sentry/views/projectInstall/messagingIntegrationAlertRule';
  15. export const providerDetails = {
  16. slack: {
  17. name: t('Slack'),
  18. action: IssueAlertActionType.SLACK,
  19. placeholder: t('channel, e.g. #critical'),
  20. makeSentence: ({providerName, integrationName, target}) =>
  21. tct(
  22. 'Send [providerName] notification to the [integrationName] workspace to [target]',
  23. {
  24. providerName,
  25. integrationName,
  26. target,
  27. }
  28. ),
  29. },
  30. discord: {
  31. name: t('Discord'),
  32. action: IssueAlertActionType.DISCORD,
  33. placeholder: t('channel ID or URL'),
  34. makeSentence: ({providerName, integrationName, target}) =>
  35. tct(
  36. 'Send [providerName] notification to the [integrationName] server in the channel [target]',
  37. {
  38. providerName,
  39. integrationName,
  40. target,
  41. }
  42. ),
  43. },
  44. msteams: {
  45. name: t('MS Teams'),
  46. action: IssueAlertActionType.MS_TEAMS,
  47. placeholder: t('channel ID'),
  48. makeSentence: ({providerName, integrationName, target}) =>
  49. tct('Send [providerName] notification to the [integrationName] team to [target]', {
  50. providerName,
  51. integrationName,
  52. target,
  53. }),
  54. },
  55. };
  56. export const enum MultipleCheckboxOptions {
  57. EMAIL = 'email',
  58. INTEGRATION = 'integration',
  59. }
  60. export type IssueAlertNotificationProps = {
  61. actions: MultipleCheckboxOptions[];
  62. channel: string | undefined;
  63. integration: OrganizationIntegration | undefined;
  64. provider: string | undefined;
  65. providersToIntegrations: Record<string, OrganizationIntegration[]>;
  66. querySuccess: boolean;
  67. setActions: (action: MultipleCheckboxOptions[]) => void;
  68. setChannel: (channel: string | undefined) => void;
  69. setIntegration: (integration: OrganizationIntegration | undefined) => void;
  70. setProvider: (provider: string | undefined) => void;
  71. shouldRenderSetupButton: boolean;
  72. };
  73. export function useCreateNotificationAction() {
  74. const api = useApi();
  75. const organization = useOrganization();
  76. const messagingIntegrationsQuery = useApiQuery<OrganizationIntegration[]>(
  77. [`/organizations/${organization.slug}/integrations/?integrationType=messaging`],
  78. {staleTime: Infinity}
  79. );
  80. const providersToIntegrations = useMemo(() => {
  81. const map: Record<string, OrganizationIntegration[]> = {};
  82. if (messagingIntegrationsQuery.data) {
  83. for (const i of messagingIntegrationsQuery.data) {
  84. if (i.status === 'active') {
  85. const providerSlug = i.provider.slug;
  86. map[providerSlug] = map[providerSlug] ?? [];
  87. map[providerSlug].push(i);
  88. }
  89. }
  90. }
  91. return map;
  92. }, [messagingIntegrationsQuery.data]);
  93. const [actions, setActions] = useState<MultipleCheckboxOptions[]>([
  94. MultipleCheckboxOptions.EMAIL,
  95. ]);
  96. const [provider, setProvider] = useState<string | undefined>(undefined);
  97. const [integration, setIntegration] = useState<OrganizationIntegration | undefined>(
  98. undefined
  99. );
  100. const [channel, setChannel] = useState<string | undefined>(undefined);
  101. const [shouldRenderSetupButton, setShouldRenderSetupButton] = useState<boolean>(false);
  102. useEffect(() => {
  103. if (messagingIntegrationsQuery.isSuccess) {
  104. const providerKeys = Object.keys(providersToIntegrations);
  105. const firstProvider = providerKeys[0] ?? undefined;
  106. const firstIntegration = providersToIntegrations[firstProvider]?.[0] ?? undefined;
  107. setProvider(firstProvider);
  108. setIntegration(firstIntegration);
  109. setShouldRenderSetupButton(!firstProvider);
  110. }
  111. }, [messagingIntegrationsQuery.isSuccess, providersToIntegrations]);
  112. type Props = {
  113. actionMatch: string | undefined;
  114. conditions: {id: string; interval: string; value: string}[] | undefined;
  115. frequency: number | undefined;
  116. name: string | undefined;
  117. projectSlug: string;
  118. shouldCreateRule: boolean | undefined;
  119. };
  120. const createNotificationAction = useCallback(
  121. ({
  122. shouldCreateRule,
  123. projectSlug,
  124. name,
  125. conditions,
  126. actionMatch,
  127. frequency,
  128. }: Props) => {
  129. const isCreatingIntegrationNotification = actions.find(
  130. action => action === MultipleCheckboxOptions.INTEGRATION
  131. );
  132. if (
  133. !organization.features.includes(
  134. 'messaging-integration-onboarding-project-creation'
  135. ) ||
  136. !shouldCreateRule ||
  137. !isCreatingIntegrationNotification
  138. ) {
  139. return undefined;
  140. }
  141. let integrationAction: IntegrationAction;
  142. switch (provider) {
  143. case 'slack':
  144. integrationAction = {
  145. id: IssueAlertActionType.SLACK,
  146. workspace: integration?.id,
  147. channel: channel,
  148. };
  149. break;
  150. case 'discord':
  151. integrationAction = {
  152. id: IssueAlertActionType.DISCORD,
  153. server: integration?.id,
  154. channel_id: channel,
  155. };
  156. break;
  157. case 'msteams':
  158. integrationAction = {
  159. id: IssueAlertActionType.MS_TEAMS,
  160. team: integration?.id,
  161. channel: channel,
  162. };
  163. break;
  164. default:
  165. return undefined;
  166. }
  167. return api.requestPromise(`/projects/${organization.slug}/${projectSlug}/rules/`, {
  168. method: 'POST',
  169. data: {
  170. name,
  171. conditions,
  172. actions: [integrationAction],
  173. actionMatch,
  174. frequency,
  175. },
  176. });
  177. },
  178. [
  179. actions,
  180. api,
  181. provider,
  182. integration,
  183. channel,
  184. organization.features,
  185. organization.slug,
  186. ]
  187. );
  188. return {
  189. createNotificationAction,
  190. notificationProps: {
  191. actions,
  192. provider,
  193. integration,
  194. channel,
  195. setActions,
  196. setProvider,
  197. setIntegration,
  198. setChannel,
  199. providersToIntegrations,
  200. querySuccess: messagingIntegrationsQuery.isSuccess,
  201. shouldRenderSetupButton,
  202. },
  203. };
  204. }
  205. export default function IssueAlertNotificationOptions(
  206. notificationProps: IssueAlertNotificationProps
  207. ) {
  208. const {actions, setActions, querySuccess, shouldRenderSetupButton} = notificationProps;
  209. const shouldRenderNotificationConfigs = actions.some(
  210. v => v !== MultipleCheckboxOptions.EMAIL
  211. );
  212. if (!querySuccess) {
  213. return null;
  214. }
  215. return (
  216. <Fragment>
  217. <MultipleCheckbox
  218. name="notification"
  219. value={actions}
  220. onChange={values => setActions(values)}
  221. >
  222. <Wrapper>
  223. <MultipleCheckbox.Item value={MultipleCheckboxOptions.EMAIL} disabled>
  224. {t('Notify via email')}
  225. </MultipleCheckbox.Item>
  226. {!shouldRenderSetupButton && (
  227. <div>
  228. <MultipleCheckbox.Item value={MultipleCheckboxOptions.INTEGRATION}>
  229. {t('Notify via integration (Slack, Discord, MS Teams, etc.)')}
  230. </MultipleCheckbox.Item>
  231. {shouldRenderNotificationConfigs && (
  232. <MessagingIntegrationAlertRule {...notificationProps} />
  233. )}
  234. </div>
  235. )}
  236. </Wrapper>
  237. </MultipleCheckbox>
  238. {shouldRenderSetupButton && (
  239. <SetupMessagingIntegrationButton
  240. analyticsParams={{
  241. view: MessagingIntegrationAnalyticsView.ALERT_RULE_CREATION,
  242. }}
  243. />
  244. )}
  245. </Fragment>
  246. );
  247. }
  248. const Wrapper = styled('div')`
  249. display: flex;
  250. flex-direction: column;
  251. gap: ${space(1)};
  252. `;