notificationSettingsV2.tsx 4.6 KB

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