notificationSettings.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import AlertLink from 'sentry/components/alertLink';
  4. import {LinkButton} from 'sentry/components/button';
  5. import Form from 'sentry/components/forms/form';
  6. import JsonForm from 'sentry/components/forms/jsonForm';
  7. import type {FieldObject} from 'sentry/components/forms/types';
  8. import LoadingError from 'sentry/components/loadingError';
  9. import Panel from 'sentry/components/panels/panel';
  10. import PanelBody from 'sentry/components/panels/panelBody';
  11. import PanelHeader from 'sentry/components/panels/panelHeader';
  12. import PanelItem from 'sentry/components/panels/panelItem';
  13. import Placeholder from 'sentry/components/placeholder';
  14. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  15. import {IconMail, IconSettings} from 'sentry/icons';
  16. import {t} from 'sentry/locale';
  17. import type {Organization} from 'sentry/types/organization';
  18. import {useApiQuery} from 'sentry/utils/queryClient';
  19. import withOrganizations from 'sentry/utils/withOrganizations';
  20. import {
  21. NOTIFICATION_FEATURE_MAP,
  22. NOTIFICATION_SETTINGS_PATHNAMES,
  23. NOTIFICATION_SETTINGS_TYPES,
  24. SELF_NOTIFICATION_SETTINGS_TYPES,
  25. } from 'sentry/views/settings/account/notifications/constants';
  26. import {NOTIFICATION_SETTING_FIELDS} from 'sentry/views/settings/account/notifications/fields2';
  27. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  28. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  29. interface NotificationSettingsProps {
  30. organizations: Organization[];
  31. }
  32. function NotificationSettings({organizations}: NotificationSettingsProps) {
  33. const checkFeatureFlag = (flag: string) => {
  34. return organizations.some(org => org.features?.includes(flag));
  35. };
  36. const notificationFields = NOTIFICATION_SETTINGS_TYPES.filter(type => {
  37. const notificationFlag = NOTIFICATION_FEATURE_MAP[type];
  38. if (Array.isArray(notificationFlag)) {
  39. return notificationFlag.some(flag => checkFeatureFlag(flag));
  40. }
  41. if (notificationFlag) {
  42. return checkFeatureFlag(notificationFlag);
  43. }
  44. return true;
  45. });
  46. const renderOneSetting = (type: string) => {
  47. // TODO(isabella): Once GA, remove this
  48. const field = NOTIFICATION_SETTING_FIELDS[type];
  49. if (type === 'quota' && checkFeatureFlag('spend-visibility-notifications')) {
  50. field.label = t('Spend');
  51. field.help = t('Notifications that help avoid surprise invoices.');
  52. }
  53. return (
  54. <FieldWrapper key={type}>
  55. <div>
  56. <FieldLabel>{field.label as React.ReactNode}</FieldLabel>
  57. <FieldHelp>{field.help as React.ReactNode}</FieldHelp>
  58. </div>
  59. <IconWrapper>
  60. <LinkButton
  61. icon={<IconSettings size="sm" />}
  62. size="sm"
  63. borderless
  64. aria-label={t('Notification Settings')}
  65. data-test-id="fine-tuning"
  66. to={`/settings/account/notifications/${NOTIFICATION_SETTINGS_PATHNAMES[type]}/`}
  67. />
  68. </IconWrapper>
  69. </FieldWrapper>
  70. );
  71. };
  72. const legacyFields = SELF_NOTIFICATION_SETTINGS_TYPES.map(
  73. type => NOTIFICATION_SETTING_FIELDS[type] as FieldObject
  74. );
  75. // use 0 as stale time because we change the values elsewhere
  76. const {
  77. data: initialLegacyData,
  78. isLoading,
  79. isError,
  80. isSuccess,
  81. refetch,
  82. } = useApiQuery<{[key: string]: string}>(['/users/me/notifications/'], {
  83. staleTime: 0,
  84. });
  85. return (
  86. <Fragment>
  87. <SentryDocumentTitle title={t('Notifications')} />
  88. <SettingsPageHeader title={t('Notifications')} />
  89. <TextBlock>
  90. {t('Personal notifications sent by email or an integration.')}
  91. </TextBlock>
  92. {isError && <LoadingError onRetry={refetch} />}
  93. <PanelNoBottomMargin>
  94. <PanelHeader>{t('Notification')}</PanelHeader>
  95. <PanelBody>{notificationFields.map(renderOneSetting)}</PanelBody>
  96. </PanelNoBottomMargin>
  97. <BottomFormWrapper>
  98. {isLoading && (
  99. <Panel>
  100. {new Array(2).fill(0).map((_, idx) => (
  101. <PanelItem key={idx}>
  102. <Placeholder height="38px" />
  103. </PanelItem>
  104. ))}
  105. </Panel>
  106. )}
  107. {isSuccess && (
  108. <Form
  109. saveOnBlur
  110. apiMethod="PUT"
  111. apiEndpoint="/users/me/notifications/"
  112. initialData={initialLegacyData}
  113. >
  114. <JsonForm fields={legacyFields} />
  115. </Form>
  116. )}
  117. </BottomFormWrapper>
  118. <AlertLink to="/settings/account/emails" icon={<IconMail />}>
  119. {t('Looking to add or remove an email address? Use the emails panel.')}
  120. </AlertLink>
  121. </Fragment>
  122. );
  123. }
  124. export default withOrganizations(NotificationSettings);
  125. const FieldLabel = styled('div')`
  126. font-size: ${p => p.theme.fontSizeMedium};
  127. `;
  128. const FieldHelp = styled('div')`
  129. font-size: ${p => p.theme.fontSizeSmall};
  130. color: ${p => p.theme.gray300};
  131. `;
  132. const FieldWrapper = styled('div')`
  133. display: grid;
  134. grid-template-columns: 1fr min-content;
  135. padding: ${p => p.theme.grid * 2}px;
  136. border-bottom: 1px solid ${p => p.theme.border};
  137. `;
  138. const IconWrapper = styled('div')`
  139. display: flex;
  140. margin: auto;
  141. cursor: pointer;
  142. `;
  143. const BottomFormWrapper = styled('div')`
  144. ${Panel} {
  145. border-top-left-radius: 0;
  146. border-top-right-radius: 0;
  147. border-top: 0;
  148. }
  149. `;
  150. const PanelNoBottomMargin = styled(Panel)`
  151. margin-bottom: 0;
  152. border-bottom: 0;
  153. border-bottom-left-radius: 0;
  154. border-bottom-right-radius: 0;
  155. `;