sentryAppExternalIssueActions.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  4. import {openModal} from 'sentry/actionCreators/modal';
  5. import {deleteExternalIssue} from 'sentry/actionCreators/platformExternalIssues';
  6. import {Client} from 'sentry/api';
  7. import {IntegrationLink} from 'sentry/components/issueSyncListElement';
  8. import SentryAppComponentIcon from 'sentry/components/sentryAppComponentIcon';
  9. import {IconAdd, IconClose} from 'sentry/icons';
  10. import {t, tct} from 'sentry/locale';
  11. import space from 'sentry/styles/space';
  12. import {
  13. Group,
  14. PlatformExternalIssue,
  15. SentryAppComponent,
  16. SentryAppInstallation,
  17. } from 'sentry/types';
  18. import {Event} from 'sentry/types/event';
  19. import {recordInteraction} from 'sentry/utils/recordSentryAppInteraction';
  20. import withApi from 'sentry/utils/withApi';
  21. import SentryAppExternalIssueModal from './sentryAppExternalIssueModal';
  22. type Props = {
  23. api: Client;
  24. event: Event;
  25. group: Group;
  26. sentryAppComponent: SentryAppComponent;
  27. sentryAppInstallation: SentryAppInstallation;
  28. externalIssue?: PlatformExternalIssue;
  29. };
  30. type State = {
  31. action: 'create' | 'link';
  32. externalIssue?: PlatformExternalIssue;
  33. };
  34. class SentryAppExternalIssueActions extends Component<Props, State> {
  35. state: State = {
  36. action: 'create',
  37. externalIssue: this.props.externalIssue,
  38. };
  39. componentDidUpdate(prevProps: Props) {
  40. if (this.props.externalIssue !== prevProps.externalIssue) {
  41. this.updateExternalIssue(this.props.externalIssue);
  42. }
  43. }
  44. updateExternalIssue(externalIssue?: PlatformExternalIssue) {
  45. this.setState({externalIssue});
  46. }
  47. doOpenModal = (e?: React.MouseEvent) => {
  48. // Only show the modal when we don't have a linked issue
  49. if (this.state.externalIssue) {
  50. return;
  51. }
  52. const {group, event, sentryAppComponent, sentryAppInstallation} = this.props;
  53. recordInteraction(
  54. sentryAppComponent.sentryApp.slug,
  55. 'sentry_app_component_interacted',
  56. {
  57. componentType: 'issue-link',
  58. }
  59. );
  60. e?.preventDefault();
  61. openModal(
  62. deps => (
  63. <SentryAppExternalIssueModal
  64. {...deps}
  65. {...{group, event, sentryAppComponent, sentryAppInstallation}}
  66. onSubmitSuccess={this.onSubmitSuccess}
  67. />
  68. ),
  69. {allowClickClose: false}
  70. );
  71. };
  72. deleteIssue = () => {
  73. const {api, group} = this.props;
  74. const {externalIssue} = this.state;
  75. externalIssue &&
  76. deleteExternalIssue(api, group.id, externalIssue.id)
  77. .then(_data => {
  78. this.setState({externalIssue: undefined});
  79. addSuccessMessage(t('Successfully unlinked issue.'));
  80. })
  81. .catch(_error => {
  82. addErrorMessage(t('Unable to unlink issue.'));
  83. });
  84. };
  85. onAddRemoveClick = () => {
  86. const {externalIssue} = this.state;
  87. if (!externalIssue) {
  88. this.doOpenModal();
  89. } else {
  90. this.deleteIssue();
  91. }
  92. };
  93. onSubmitSuccess = (externalIssue: PlatformExternalIssue) => {
  94. this.setState({externalIssue});
  95. };
  96. render() {
  97. const {sentryAppComponent} = this.props;
  98. const {externalIssue} = this.state;
  99. const name = sentryAppComponent.sentryApp.name;
  100. let url = '#';
  101. let displayName: React.ReactNode | string = tct('Link [name] Issue', {name});
  102. if (externalIssue) {
  103. url = externalIssue.webUrl;
  104. displayName = externalIssue.displayName;
  105. }
  106. return (
  107. <IssueLinkContainer>
  108. <IssueLink>
  109. <StyledSentryAppComponentIcon sentryAppComponent={sentryAppComponent} />
  110. <IntegrationLink onClick={this.doOpenModal} href={url}>
  111. {displayName}
  112. </IntegrationLink>
  113. </IssueLink>
  114. <StyledIcon onClick={this.onAddRemoveClick}>
  115. {!!externalIssue ? <IconClose /> : <IconAdd />}
  116. </StyledIcon>
  117. </IssueLinkContainer>
  118. );
  119. }
  120. }
  121. const StyledSentryAppComponentIcon = styled(SentryAppComponentIcon)`
  122. color: ${p => p.theme.textColor};
  123. width: ${space(3)};
  124. height: ${space(3)};
  125. cursor: pointer;
  126. flex-shrink: 0;
  127. `;
  128. const IssueLink = styled('div')`
  129. display: flex;
  130. align-items: center;
  131. min-width: 0;
  132. `;
  133. const IssueLinkContainer = styled('div')`
  134. line-height: 0;
  135. display: flex;
  136. align-items: center;
  137. justify-content: space-between;
  138. margin-bottom: 16px;
  139. `;
  140. const StyledIcon = styled('span')`
  141. color: ${p => p.theme.textColor};
  142. cursor: pointer;
  143. `;
  144. export default withApi(SentryAppExternalIssueActions);