relayWrapper.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import {Fragment} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import isEqual from 'lodash/isEqual';
  4. import omit from 'lodash/omit';
  5. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  6. import {openModal} from 'sentry/actionCreators/modal';
  7. import {updateOrganization} from 'sentry/actionCreators/organizations';
  8. import {Button} from 'sentry/components/button';
  9. import ExternalLink from 'sentry/components/links/externalLink';
  10. import {IconAdd} from 'sentry/icons';
  11. import {t, tct} from 'sentry/locale';
  12. import type {Organization} from 'sentry/types/organization';
  13. import type {Relay, RelayActivity} from 'sentry/types/relay';
  14. import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  15. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  16. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  17. import PermissionAlert from 'sentry/views/settings/organization/permissionAlert';
  18. import Add from './modals/add';
  19. import Edit from './modals/edit';
  20. import EmptyState from './emptyState';
  21. import List from './list';
  22. const RELAY_DOCS_LINK = 'https://getsentry.github.io/relay/';
  23. type Props = {
  24. organization: Organization;
  25. } & RouteComponentProps<{}, {}>;
  26. type State = {
  27. relayActivities: Array<RelayActivity>;
  28. relays: Array<Relay>;
  29. } & DeprecatedAsyncView['state'];
  30. class RelayWrapper extends DeprecatedAsyncView<Props, State> {
  31. componentDidUpdate(prevProps: Props, prevState: State) {
  32. if (!isEqual(prevState.relays, this.state.relays)) {
  33. // Fetch fresh activities
  34. this.fetchData();
  35. updateOrganization({...prevProps.organization, trustedRelays: this.state.relays});
  36. }
  37. super.componentDidUpdate(prevProps, prevState);
  38. }
  39. getTitle() {
  40. return t('Relay');
  41. }
  42. getDefaultState() {
  43. return {
  44. ...super.getDefaultState(),
  45. relays: this.props.organization.trustedRelays,
  46. };
  47. }
  48. getEndpoints(): ReturnType<DeprecatedAsyncView['getEndpoints']> {
  49. const {organization} = this.props;
  50. return [['relayActivities', `/organizations/${organization.slug}/relay_usage/`]];
  51. }
  52. setRelays(trustedRelays: Array<Relay>) {
  53. this.setState({relays: trustedRelays});
  54. }
  55. handleDelete = (publicKey: Relay['publicKey']) => async () => {
  56. const {relays} = this.state;
  57. const trustedRelays = relays
  58. .filter(relay => relay.publicKey !== publicKey)
  59. .map(relay => omit(relay, ['created', 'lastModified']));
  60. try {
  61. const response = await this.api.requestPromise(
  62. `/organizations/${this.props.organization.slug}/`,
  63. {
  64. method: 'PUT',
  65. data: {trustedRelays},
  66. }
  67. );
  68. addSuccessMessage(t('Successfully deleted Relay public key'));
  69. this.setRelays(response.trustedRelays);
  70. } catch {
  71. addErrorMessage(t('An unknown error occurred while deleting Relay public key'));
  72. }
  73. };
  74. successfullySaved(response: Organization, successMessage: string) {
  75. addSuccessMessage(successMessage);
  76. this.setRelays(response.trustedRelays);
  77. }
  78. handleOpenEditDialog = (publicKey: Relay['publicKey']) => () => {
  79. const editRelay = this.state.relays.find(relay => relay.publicKey === publicKey);
  80. if (!editRelay) {
  81. return;
  82. }
  83. openModal(modalProps => (
  84. <Edit
  85. {...modalProps}
  86. savedRelays={this.state.relays}
  87. api={this.api}
  88. orgSlug={this.props.organization.slug}
  89. relay={editRelay}
  90. onSubmitSuccess={response => {
  91. this.successfullySaved(response, t('Successfully updated Relay public key'));
  92. }}
  93. />
  94. ));
  95. };
  96. handleOpenAddDialog = () => {
  97. openModal(modalProps => (
  98. <Add
  99. {...modalProps}
  100. savedRelays={this.state.relays}
  101. api={this.api}
  102. orgSlug={this.props.organization.slug}
  103. onSubmitSuccess={response => {
  104. this.successfullySaved(response, t('Successfully added Relay public key'));
  105. }}
  106. />
  107. ));
  108. };
  109. handleRefresh = () => {
  110. // Fetch fresh activities
  111. this.fetchData();
  112. };
  113. renderContent(disabled: boolean) {
  114. const {relays, relayActivities, loading} = this.state;
  115. if (loading) {
  116. return this.renderLoading();
  117. }
  118. if (!relays.length) {
  119. return <EmptyState />;
  120. }
  121. return (
  122. <List
  123. relays={relays}
  124. relayActivities={relayActivities}
  125. onEdit={this.handleOpenEditDialog}
  126. onRefresh={this.handleRefresh}
  127. onDelete={this.handleDelete}
  128. disabled={disabled}
  129. />
  130. );
  131. }
  132. renderBody() {
  133. const {organization} = this.props;
  134. const disabled = !organization.access.includes('org:write');
  135. return (
  136. <Fragment>
  137. <SettingsPageHeader
  138. title={t('Relay')}
  139. action={
  140. <Button
  141. title={
  142. disabled ? t('You do not have permission to register keys') : undefined
  143. }
  144. priority="primary"
  145. size="sm"
  146. icon={<IconAdd isCircled />}
  147. onClick={this.handleOpenAddDialog}
  148. disabled={disabled}
  149. >
  150. {t('Register Key')}
  151. </Button>
  152. }
  153. />
  154. <PermissionAlert />
  155. <TextBlock>
  156. {tct(
  157. 'Sentry Relay offers enterprise-grade data security by providing a standalone service that acts as a middle layer between your application and sentry.io. Go to [link:Relay Documentation] for setup and details.',
  158. {link: <ExternalLink href={RELAY_DOCS_LINK} />}
  159. )}
  160. </TextBlock>
  161. {this.renderContent(disabled)}
  162. </Fragment>
  163. );
  164. }
  165. }
  166. export default RelayWrapper;