issueSyncListElement.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import {Component} from 'react';
  2. import {ClassNames} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import capitalize from 'lodash/capitalize';
  5. import {Hovercard} from 'sentry/components/hovercard';
  6. import {IconAdd, IconClose} from 'sentry/icons';
  7. import space from 'sentry/styles/space';
  8. import {callIfFunction} from 'sentry/utils/callIfFunction';
  9. import {getIntegrationIcon} from 'sentry/utils/integrationUtil';
  10. type Props = {
  11. externalIssueDisplayName?: string | null;
  12. externalIssueId?: string | null;
  13. externalIssueKey?: string | null;
  14. externalIssueLink?: string | null;
  15. hoverCardBody?: React.ReactNode;
  16. hoverCardHeader?: React.ReactNode;
  17. integrationType?: string;
  18. onClose?: (externalIssueId?: string | null) => void;
  19. onOpen?: () => void;
  20. showHoverCard?: boolean;
  21. };
  22. class IssueSyncListElement extends Component<Props> {
  23. isLinked(): boolean {
  24. return !!(this.props.externalIssueLink && this.props.externalIssueId);
  25. }
  26. handleDelete = (): void => {
  27. callIfFunction(this.props.onClose, this.props.externalIssueId);
  28. };
  29. handleIconClick = () => {
  30. if (this.isLinked()) {
  31. this.handleDelete();
  32. } else if (this.props.onOpen) {
  33. this.props.onOpen();
  34. }
  35. };
  36. getIcon(): React.ReactNode {
  37. return getIntegrationIcon(this.props.integrationType);
  38. }
  39. getPrettyName(): string {
  40. const type = this.props.integrationType;
  41. switch (type) {
  42. case 'gitlab':
  43. return 'GitLab';
  44. case 'github':
  45. return 'GitHub';
  46. case 'github_enterprise':
  47. return 'GitHub Enterprise';
  48. case 'vsts':
  49. return 'Azure DevOps';
  50. case 'jira_server':
  51. return 'Jira Server';
  52. default:
  53. return capitalize(type);
  54. }
  55. }
  56. getLink(): React.ReactElement {
  57. return (
  58. <IntegrationLink
  59. href={this.props.externalIssueLink || undefined}
  60. onClick={!this.isLinked() ? this.props.onOpen : undefined}
  61. >
  62. {this.getText()}
  63. </IntegrationLink>
  64. );
  65. }
  66. getText(): React.ReactNode {
  67. if (this.props.children) {
  68. return this.props.children;
  69. }
  70. if (this.props.externalIssueDisplayName) {
  71. return this.props.externalIssueDisplayName;
  72. }
  73. if (this.props.externalIssueKey) {
  74. return this.props.externalIssueKey;
  75. }
  76. return `Link ${this.getPrettyName()} Issue`;
  77. }
  78. render() {
  79. return (
  80. <IssueSyncListElementContainer>
  81. <ClassNames>
  82. {({css}) => (
  83. <Hovercard
  84. containerClassName={css`
  85. display: flex;
  86. align-items: center;
  87. min-width: 0; /* flex-box overflow workaround */
  88. svg {
  89. flex-shrink: 0;
  90. }
  91. `}
  92. header={this.props.hoverCardHeader}
  93. body={this.props.hoverCardBody}
  94. bodyClassName="issue-list-body"
  95. forceVisible={this.props.showHoverCard}
  96. >
  97. {this.getIcon()}
  98. {this.getLink()}
  99. </Hovercard>
  100. )}
  101. </ClassNames>
  102. {(this.props.onClose || this.props.onOpen) && (
  103. <StyledIcon onClick={this.handleIconClick}>
  104. {this.isLinked() ? <IconClose /> : this.props.onOpen ? <IconAdd /> : null}
  105. </StyledIcon>
  106. )}
  107. </IssueSyncListElementContainer>
  108. );
  109. }
  110. }
  111. export const IssueSyncListElementContainer = styled('div')`
  112. line-height: 0;
  113. display: flex;
  114. align-items: center;
  115. justify-content: space-between;
  116. &:not(:last-child) {
  117. margin-bottom: ${space(2)};
  118. }
  119. `;
  120. export const IntegrationLink = styled('a')`
  121. text-decoration: none;
  122. padding-bottom: ${space(0.25)};
  123. margin-left: ${space(1)};
  124. color: ${p => p.theme.textColor};
  125. border-bottom: 1px solid ${p => p.theme.textColor};
  126. cursor: pointer;
  127. line-height: 1;
  128. white-space: nowrap;
  129. overflow: hidden;
  130. text-overflow: ellipsis;
  131. &,
  132. &:hover {
  133. border-bottom: 1px solid ${p => p.theme.blue300};
  134. }
  135. `;
  136. const StyledIcon = styled('span')`
  137. color: ${p => p.theme.textColor};
  138. cursor: pointer;
  139. `;
  140. export default IssueSyncListElement;