issueSyncListElement.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import * as React 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 React.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. `}
  89. header={this.props.hoverCardHeader}
  90. body={this.props.hoverCardBody}
  91. bodyClassName="issue-list-body"
  92. show={this.props.showHoverCard}
  93. >
  94. {this.getIcon()}
  95. {this.getLink()}
  96. </Hovercard>
  97. )}
  98. </ClassNames>
  99. {(this.props.onClose || this.props.onOpen) && (
  100. <StyledIcon onClick={this.handleIconClick}>
  101. {this.isLinked() ? <IconClose /> : this.props.onOpen ? <IconAdd /> : null}
  102. </StyledIcon>
  103. )}
  104. </IssueSyncListElementContainer>
  105. );
  106. }
  107. }
  108. export const IssueSyncListElementContainer = styled('div')`
  109. line-height: 0;
  110. display: flex;
  111. align-items: center;
  112. justify-content: space-between;
  113. &:not(:last-child) {
  114. margin-bottom: ${space(2)};
  115. }
  116. `;
  117. export const IntegrationLink = styled('a')`
  118. text-decoration: none;
  119. padding-bottom: ${space(0.25)};
  120. margin-left: ${space(1)};
  121. color: ${p => p.theme.textColor};
  122. border-bottom: 1px solid ${p => p.theme.textColor};
  123. cursor: pointer;
  124. line-height: 1;
  125. white-space: nowrap;
  126. overflow: hidden;
  127. text-overflow: ellipsis;
  128. &,
  129. &:hover {
  130. border-bottom: 1px solid ${p => p.theme.blue300};
  131. }
  132. `;
  133. const StyledIcon = styled('span')`
  134. color: ${p => p.theme.textColor};
  135. cursor: pointer;
  136. `;
  137. export default IssueSyncListElement;