notificationSettings.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 (notificationFlag) {
  39. return checkFeatureFlag(notificationFlag);
  40. }
  41. return true;
  42. });
  43. const renderOneSetting = (type: string) => {
  44. // TODO(isabella): Once GA, remove this
  45. const field = NOTIFICATION_SETTING_FIELDS[type];
  46. if (type === 'quota' && checkFeatureFlag('spend-visibility-notifications')) {
  47. field.label = t('Spend');
  48. field.help = t('Notifications that help avoid surprise invoices.');
  49. }
  50. return (
  51. <FieldWrapper key={type}>
  52. <div>
  53. <FieldLabel>{field.label as React.ReactNode}</FieldLabel>
  54. <FieldHelp>{field.help as React.ReactNode}</FieldHelp>
  55. </div>
  56. <IconWrapper>
  57. <LinkButton
  58. icon={<IconSettings size="sm" />}
  59. size="sm"
  60. borderless
  61. aria-label={t('Notification Settings')}
  62. data-test-id="fine-tuning"
  63. to={`/settings/account/notifications/${NOTIFICATION_SETTINGS_PATHNAMES[type]}/`}
  64. />
  65. </IconWrapper>
  66. </FieldWrapper>
  67. );
  68. };
  69. const legacyFields = SELF_NOTIFICATION_SETTINGS_TYPES.map(
  70. type => NOTIFICATION_SETTING_FIELDS[type] as FieldObject
  71. );
  72. // use 0 as stale time because we change the values elsewhere
  73. const {
  74. data: initialLegacyData,
  75. isLoading,
  76. isError,
  77. isSuccess,
  78. refetch,
  79. } = useApiQuery<{[key: string]: string}>(['/users/me/notifications/'], {
  80. staleTime: 0,
  81. });
  82. return (
  83. <Fragment>
  84. <SentryDocumentTitle title={t('Notifications')} />
  85. <SettingsPageHeader title={t('Notifications')} />
  86. <TextBlock>
  87. {t('Personal notifications sent by email or an integration.')}
  88. </TextBlock>
  89. {isError && <LoadingError onRetry={refetch} />}
  90. <PanelNoBottomMargin>
  91. <PanelHeader>{t('Notification')}</PanelHeader>
  92. <PanelBody>{notificationFields.map(renderOneSetting)}</PanelBody>
  93. </PanelNoBottomMargin>
  94. <BottomFormWrapper>
  95. {isLoading && (
  96. <Panel>
  97. {new Array(2).fill(0).map((_, idx) => (
  98. <PanelItem key={idx}>
  99. <Placeholder height="38px" />
  100. </PanelItem>
  101. ))}
  102. </Panel>
  103. )}
  104. {isSuccess && (
  105. <Form
  106. saveOnBlur
  107. apiMethod="PUT"
  108. apiEndpoint="/users/me/notifications/"
  109. initialData={initialLegacyData}
  110. >
  111. <JsonForm fields={legacyFields} />
  112. </Form>
  113. )}
  114. </BottomFormWrapper>
  115. <AlertLink to="/settings/account/emails" icon={<IconMail />}>
  116. {t('Looking to add or remove an email address? Use the emails panel.')}
  117. </AlertLink>
  118. </Fragment>
  119. );
  120. }
  121. export default withOrganizations(NotificationSettings);
  122. const FieldLabel = styled('div')`
  123. font-size: ${p => p.theme.fontSizeMedium};
  124. `;
  125. const FieldHelp = styled('div')`
  126. font-size: ${p => p.theme.fontSizeSmall};
  127. color: ${p => p.theme.gray300};
  128. `;
  129. const FieldWrapper = styled('div')`
  130. display: grid;
  131. grid-template-columns: 1fr min-content;
  132. padding: ${p => p.theme.grid * 2}px;
  133. border-bottom: 1px solid ${p => p.theme.border};
  134. `;
  135. const IconWrapper = styled('div')`
  136. display: flex;
  137. margin: auto;
  138. cursor: pointer;
  139. `;
  140. const BottomFormWrapper = styled('div')`
  141. ${Panel} {
  142. border-top-left-radius: 0;
  143. border-top-right-radius: 0;
  144. border-top: 0;
  145. }
  146. `;
  147. const PanelNoBottomMargin = styled(Panel)`
  148. margin-bottom: 0;
  149. border-bottom: 0;
  150. border-bottom-left-radius: 0;
  151. border-bottom-right-radius: 0;
  152. `;