pluginActions.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import {Component, Fragment} from 'react';
  2. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  3. import {ModalRenderProps, openModal} from 'sentry/actionCreators/modal';
  4. import {Client} from 'sentry/api';
  5. import IssueSyncListElement from 'sentry/components/issueSyncListElement';
  6. import NavTabs from 'sentry/components/navTabs';
  7. import {t, tct} from 'sentry/locale';
  8. import plugins from 'sentry/plugins';
  9. import {Group, Organization, Plugin, Project} from 'sentry/types';
  10. import withApi from 'sentry/utils/withApi';
  11. import withOrganization from 'sentry/utils/withOrganization';
  12. type PluginIssue = {
  13. issue_id: string;
  14. label: string;
  15. url: string;
  16. };
  17. type TitledPlugin = Plugin & {
  18. // issue serializer adds more fields
  19. // TODO: should be able to use name instead of title
  20. title: string;
  21. };
  22. type Props = {
  23. api: Client;
  24. group: Group;
  25. organization: Organization;
  26. plugin: TitledPlugin;
  27. project: Project;
  28. };
  29. type State = {
  30. issue: PluginIssue | null;
  31. pluginLoading: boolean;
  32. };
  33. class PluginActions extends Component<Props, State> {
  34. state: State = {
  35. issue: null,
  36. pluginLoading: false,
  37. };
  38. componentDidMount() {
  39. this.loadPlugin(this.props.plugin);
  40. }
  41. componentDidUpdate(prevProps: Props) {
  42. if (this.props.plugin.id !== prevProps.plugin.id) {
  43. this.loadPlugin(this.props.plugin);
  44. }
  45. }
  46. deleteIssue = () => {
  47. const plugin = {
  48. ...this.props.plugin,
  49. issue: null,
  50. };
  51. // override plugin.issue so that 'create/link' Modal
  52. // doesn't think the plugin still has an issue linked
  53. const endpoint = `/issues/${this.props.group.id}/plugins/${plugin.slug}/unlink/`;
  54. this.props.api.request(endpoint, {
  55. success: () => {
  56. this.loadPlugin(plugin);
  57. addSuccessMessage(t('Successfully unlinked issue.'));
  58. },
  59. error: () => {
  60. addErrorMessage(t('Unable to unlink issue'));
  61. },
  62. });
  63. };
  64. loadPlugin = (data: any) => {
  65. this.setState(
  66. {
  67. pluginLoading: true,
  68. },
  69. () => {
  70. plugins.load(data, () => {
  71. const issue = data.issue || null;
  72. this.setState({pluginLoading: false, issue});
  73. });
  74. }
  75. );
  76. };
  77. handleModalClose = (data?: any) =>
  78. this.setState({
  79. issue:
  80. data?.id && data?.link
  81. ? {issue_id: data.id, url: data.link, label: data.label}
  82. : null,
  83. });
  84. openModal = () => {
  85. const {issue} = this.state;
  86. const {project, group, organization} = this.props;
  87. const plugin = {...this.props.plugin, issue};
  88. openModal(
  89. deps => (
  90. <PluginActionsModal
  91. {...deps}
  92. project={project}
  93. group={group}
  94. organization={organization}
  95. plugin={plugin}
  96. onSuccess={this.handleModalClose}
  97. />
  98. ),
  99. {onClose: this.handleModalClose}
  100. );
  101. };
  102. render() {
  103. const {issue} = this.state;
  104. const plugin = {...this.props.plugin, issue};
  105. return (
  106. <IssueSyncListElement
  107. onOpen={this.openModal}
  108. externalIssueDisplayName={issue ? issue.label : null}
  109. externalIssueId={issue ? issue.issue_id : null}
  110. externalIssueLink={issue ? issue.url : null}
  111. onClose={this.deleteIssue}
  112. integrationType={plugin.id}
  113. />
  114. );
  115. }
  116. }
  117. type ModalProps = ModalRenderProps & {
  118. group: Group;
  119. onSuccess: (data: any) => void;
  120. organization: Organization;
  121. plugin: TitledPlugin & {issue: PluginIssue | null};
  122. project: Project;
  123. };
  124. type ModalState = {
  125. actionType: 'create' | 'link' | null;
  126. };
  127. class PluginActionsModal extends Component<ModalProps, ModalState> {
  128. state: ModalState = {
  129. actionType: 'create',
  130. };
  131. render() {
  132. const {Header, Body, group, project, organization, plugin, onSuccess} = this.props;
  133. const {actionType} = this.state;
  134. return (
  135. <Fragment>
  136. <Header closeButton>
  137. {tct('[name] Issue', {name: plugin.name || plugin.title})}
  138. </Header>
  139. <NavTabs underlined>
  140. <li className={actionType === 'create' ? 'active' : ''}>
  141. <a onClick={() => this.setState({actionType: 'create'})}>{t('Create')}</a>
  142. </li>
  143. <li className={actionType === 'link' ? 'active' : ''}>
  144. <a onClick={() => this.setState({actionType: 'link'})}>{t('Link')}</a>
  145. </li>
  146. </NavTabs>
  147. {actionType && (
  148. // need the key here so React will re-render
  149. // with new action prop
  150. <Body key={actionType}>
  151. {plugins.get(plugin).renderGroupActions({
  152. plugin,
  153. group,
  154. project,
  155. organization,
  156. actionType,
  157. onSuccess,
  158. })}
  159. </Body>
  160. )}
  161. </Fragment>
  162. );
  163. }
  164. }
  165. export {PluginActions};
  166. export default withApi(withOrganization(PluginActions));