sentryAppExternalIssueActions.tsx 5.2 KB

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