accountNotificationFineTuning.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import {Fragment} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import EmptyMessage from 'sentry/components/emptyMessage';
  5. import SelectField from 'sentry/components/forms/fields/selectField';
  6. import Form from 'sentry/components/forms/form';
  7. import ProjectBadge from 'sentry/components/idBadge/projectBadge';
  8. import Pagination from 'sentry/components/pagination';
  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 {t} from 'sentry/locale';
  13. import ConfigStore from 'sentry/stores/configStore';
  14. import {space} from 'sentry/styles/space';
  15. import {Organization, Project, UserEmail} from 'sentry/types';
  16. import parseLinkHeader from 'sentry/utils/parseLinkHeader';
  17. import withOrganizations from 'sentry/utils/withOrganizations';
  18. import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  19. import {
  20. ACCOUNT_NOTIFICATION_FIELDS,
  21. FineTuneField,
  22. } from 'sentry/views/settings/account/notifications/fields';
  23. import NotificationSettingsByType from 'sentry/views/settings/account/notifications/notificationSettingsByType';
  24. import {OrganizationSelectHeader} from 'sentry/views/settings/account/notifications/organizationSelectHeader';
  25. import {
  26. getNotificationTypeFromPathname,
  27. groupByOrganization,
  28. isGroupedByProject,
  29. } from 'sentry/views/settings/account/notifications/utils';
  30. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  31. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  32. const PanelBodyLineItem = styled(PanelBody)`
  33. font-size: 1rem;
  34. &:not(:last-child) {
  35. border-bottom: 1px solid ${p => p.theme.innerBorder};
  36. }
  37. `;
  38. const accountNotifications = [
  39. 'alerts',
  40. 'deploy',
  41. 'workflow',
  42. 'approval',
  43. 'quota',
  44. 'spikeProtection',
  45. 'reports',
  46. ];
  47. type ANBPProps = {
  48. field: FineTuneField;
  49. projects: Project[];
  50. };
  51. function AccountNotificationsByProject({projects, field}: ANBPProps) {
  52. const projectsByOrg = groupByOrganization(projects);
  53. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  54. const {title, description, ...fieldConfig} = field;
  55. // Display as select box in this view regardless of the type specified in the config
  56. const data = Object.values(projectsByOrg).map(org => ({
  57. name: org.organization.name,
  58. projects: org.projects.map(project => ({
  59. ...fieldConfig,
  60. // `name` key refers to field name
  61. // we use project.id because slugs are not unique across orgs
  62. name: project.id,
  63. label: (
  64. <ProjectBadge
  65. project={project}
  66. avatarSize={20}
  67. displayName={project.slug}
  68. avatarProps={{consistentWidth: true}}
  69. disableLink
  70. />
  71. ),
  72. })),
  73. }));
  74. return (
  75. <Fragment>
  76. {data.map(({name, projects: projectFields}) => (
  77. <div key={name}>
  78. {projectFields.map(f => (
  79. <PanelBodyLineItem key={f.name}>
  80. <SelectField
  81. defaultValue={f.defaultValue}
  82. name={f.name}
  83. options={f.options}
  84. label={f.label}
  85. />
  86. </PanelBodyLineItem>
  87. ))}
  88. </div>
  89. ))}
  90. </Fragment>
  91. );
  92. }
  93. type ANBOProps = {
  94. field: FineTuneField;
  95. organizations: Organization[];
  96. };
  97. function AccountNotificationsByOrganization({organizations, field}: ANBOProps) {
  98. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  99. const {title, description, ...fieldConfig} = field;
  100. // Display as select box in this view regardless of the type specified in the config
  101. const data = organizations.map(org => ({
  102. ...fieldConfig,
  103. // `name` key refers to field name
  104. // we use org.id to remain consistent project.id use (which is required because slugs are not unique across orgs)
  105. name: org.id,
  106. label: org.slug,
  107. }));
  108. return (
  109. <Fragment>
  110. {data.map(f => (
  111. <PanelBodyLineItem key={f.name}>
  112. <SelectField
  113. defaultValue={f.defaultValue}
  114. name={f.name}
  115. options={f.options}
  116. label={f.label}
  117. />
  118. </PanelBodyLineItem>
  119. ))}
  120. </Fragment>
  121. );
  122. }
  123. const AccountNotificationsByOrganizationContainer = withOrganizations(
  124. AccountNotificationsByOrganization
  125. );
  126. type Props = DeprecatedAsyncView['props'] &
  127. RouteComponentProps<{fineTuneType: string}, {}> & {
  128. organizations: Organization[];
  129. };
  130. type State = DeprecatedAsyncView['state'] & {
  131. emails: UserEmail[] | null;
  132. emailsByProject: Record<string, any> | null;
  133. notifications: Record<string, any> | null;
  134. projects: Project[] | null;
  135. };
  136. class AccountNotificationFineTuning extends DeprecatedAsyncView<Props, State> {
  137. getEndpoints(): ReturnType<DeprecatedAsyncView['getEndpoints']> {
  138. const {fineTuneType: pathnameType} = this.props.params;
  139. const fineTuneType = getNotificationTypeFromPathname(pathnameType);
  140. const endpoints: ReturnType<DeprecatedAsyncView['getEndpoints']> = [
  141. ['notifications', '/users/me/notifications/'],
  142. ];
  143. if (isGroupedByProject(fineTuneType)) {
  144. const organizationId = this.getOrganizationId();
  145. endpoints.push(['projects', `/projects/`, {query: {organizationId}}]);
  146. }
  147. // special logic for email
  148. if (fineTuneType === 'email') {
  149. endpoints.push(['emails', '/users/me/emails/']);
  150. endpoints.push(['emailsByProject', `/users/me/notifications/email/`]);
  151. }
  152. return endpoints;
  153. }
  154. // Return a sorted list of user's verified emails
  155. get emailChoices() {
  156. return (
  157. this.state.emails
  158. ?.filter(({isVerified}) => isVerified)
  159. ?.sort((a, b) => {
  160. // Sort by primary -> email
  161. if (a.isPrimary) {
  162. return -1;
  163. }
  164. if (b.isPrimary) {
  165. return 1;
  166. }
  167. return a.email < b.email ? -1 : 1;
  168. }) ?? []
  169. );
  170. }
  171. handleOrgChange = (organizationId: string) => {
  172. this.props.router.replace({
  173. ...this.props.location,
  174. query: {organizationId},
  175. });
  176. };
  177. getOrganizationId(): string | undefined {
  178. const {location, organizations} = this.props;
  179. const customerDomain = ConfigStore.get('customerDomain');
  180. const orgFromSubdomain = organizations.find(
  181. ({slug}) => slug === customerDomain?.subdomain
  182. )?.id;
  183. return location?.query?.organizationId ?? orgFromSubdomain ?? organizations[0]?.id;
  184. }
  185. renderBody() {
  186. const {params, organizations} = this.props;
  187. const {fineTuneType: pathnameType} = params;
  188. const fineTuneType = getNotificationTypeFromPathname(pathnameType);
  189. if (accountNotifications.includes(fineTuneType)) {
  190. return <NotificationSettingsByType notificationType={fineTuneType} />;
  191. }
  192. const {notifications, projects, emailsByProject, projectsPageLinks} = this.state;
  193. const isProject = isGroupedByProject(fineTuneType) && organizations.length > 0;
  194. const field = ACCOUNT_NOTIFICATION_FIELDS[fineTuneType];
  195. const {title, description} = field;
  196. const [stateKey] = isProject ? this.getEndpoints()[2] : [];
  197. const hasProjects = !!projects?.length;
  198. if (fineTuneType === 'email') {
  199. // Fetch verified email addresses
  200. field.options = this.emailChoices.map(({email}) => ({value: email, label: email}));
  201. }
  202. if (!notifications || (!emailsByProject && fineTuneType === 'email')) {
  203. return null;
  204. }
  205. const orgId = this.getOrganizationId();
  206. const paginationObject = parseLinkHeader(projectsPageLinks ?? '');
  207. const hasMore = paginationObject?.next?.results;
  208. const hasPrevious = paginationObject?.previous?.results;
  209. const mainContent = (
  210. <Fragment>
  211. {isProject && hasProjects && (
  212. <AccountNotificationsByProject projects={projects!} field={field} />
  213. )}
  214. {isProject && !hasProjects && (
  215. <EmptyMessage>{t('No projects found')}</EmptyMessage>
  216. )}
  217. {!isProject && <AccountNotificationsByOrganizationContainer field={field} />}
  218. </Fragment>
  219. );
  220. return (
  221. <div>
  222. <SettingsPageHeader title={title} />
  223. {description && <TextBlock>{description}</TextBlock>}
  224. <Panel>
  225. <StyledPanelHeader hasButtons={isProject}>
  226. {isProject ? (
  227. <Fragment>
  228. <OrganizationSelectHeader
  229. organizations={organizations}
  230. organizationId={orgId}
  231. handleOrgChange={this.handleOrgChange}
  232. />
  233. {this.renderSearchInput({
  234. placeholder: t('Search Projects'),
  235. url: `/projects/?organizationId=${orgId}`,
  236. stateKey,
  237. })}
  238. </Fragment>
  239. ) : (
  240. <Heading>{t('Organizations')}</Heading>
  241. )}
  242. </StyledPanelHeader>
  243. <PanelBody>
  244. {/* Only email needs the form to change the emmail */}
  245. {fineTuneType === 'email' && emailsByProject ? (
  246. <Form
  247. saveOnBlur
  248. apiMethod="PUT"
  249. apiEndpoint="/users/me/notifications/email/"
  250. initialData={emailsByProject}
  251. >
  252. {mainContent}
  253. </Form>
  254. ) : (
  255. mainContent
  256. )}
  257. </PanelBody>
  258. </Panel>
  259. {projects && (hasMore || hasPrevious) && (
  260. <Pagination pageLinks={projectsPageLinks} />
  261. )}
  262. </div>
  263. );
  264. }
  265. }
  266. const Heading = styled('div')`
  267. flex: 1;
  268. `;
  269. const StyledPanelHeader = styled(PanelHeader)`
  270. flex-wrap: wrap;
  271. gap: ${space(1)};
  272. & > form:last-child {
  273. flex-grow: 1;
  274. }
  275. `;
  276. export default withOrganizations(AccountNotificationFineTuning);