notificationSettings.tsx 5.0 KB

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