notificationSettings.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  67. to={`/settings/account/notifications/${NOTIFICATION_SETTINGS_PATHNAMES[type]}/`}
  68. />
  69. </IconWrapper>
  70. </FieldWrapper>
  71. );
  72. };
  73. const legacyFields = SELF_NOTIFICATION_SETTINGS_TYPES.map(
  74. type => NOTIFICATION_SETTING_FIELDS[type] as FieldObject
  75. );
  76. // use 0 as stale time because we change the values elsewhere
  77. const {
  78. data: initialLegacyData,
  79. isPending,
  80. isError,
  81. isSuccess,
  82. refetch,
  83. } = useApiQuery<{[key: string]: string}>(['/users/me/notifications/'], {
  84. staleTime: 0,
  85. });
  86. return (
  87. <Fragment>
  88. <SentryDocumentTitle title={t('Notifications')} />
  89. <SettingsPageHeader title={t('Notifications')} />
  90. <TextBlock>
  91. {t('Personal notifications sent by email or an integration.')}
  92. </TextBlock>
  93. {isError && <LoadingError onRetry={refetch} />}
  94. <PanelNoBottomMargin>
  95. <PanelHeader>{t('Notification')}</PanelHeader>
  96. <PanelBody>{notificationFields.map(renderOneSetting)}</PanelBody>
  97. </PanelNoBottomMargin>
  98. <BottomFormWrapper>
  99. {isPending && (
  100. <Panel>
  101. {new Array(2).fill(0).map((_, idx) => (
  102. <PanelItem key={idx}>
  103. <Placeholder height="38px" />
  104. </PanelItem>
  105. ))}
  106. </Panel>
  107. )}
  108. {isSuccess && (
  109. <Form
  110. saveOnBlur
  111. apiMethod="PUT"
  112. apiEndpoint="/users/me/notifications/"
  113. initialData={initialLegacyData}
  114. >
  115. <JsonForm fields={legacyFields} />
  116. </Form>
  117. )}
  118. </BottomFormWrapper>
  119. <AlertLink to="/settings/account/emails" icon={<IconMail />}>
  120. {t('Looking to add or remove an email address? Use the emails panel.')}
  121. </AlertLink>
  122. </Fragment>
  123. );
  124. }
  125. export default withOrganizations(NotificationSettings);
  126. const FieldLabel = styled('div')`
  127. font-size: ${p => p.theme.fontSizeMedium};
  128. `;
  129. const FieldHelp = styled('div')`
  130. font-size: ${p => p.theme.fontSizeSmall};
  131. color: ${p => p.theme.gray300};
  132. `;
  133. const FieldWrapper = styled('div')`
  134. display: grid;
  135. grid-template-columns: 1fr min-content;
  136. padding: ${p => p.theme.grid * 2}px;
  137. border-bottom: 1px solid ${p => p.theme.border};
  138. `;
  139. const IconWrapper = styled('div')`
  140. display: flex;
  141. margin: auto;
  142. cursor: pointer;
  143. `;
  144. const BottomFormWrapper = styled('div')`
  145. ${Panel} {
  146. border-top-left-radius: 0;
  147. border-top-right-radius: 0;
  148. border-top: 0;
  149. }
  150. `;
  151. const PanelNoBottomMargin = styled(Panel)`
  152. margin-bottom: 0;
  153. border-bottom: 0;
  154. border-bottom-left-radius: 0;
  155. border-bottom-right-radius: 0;
  156. `;