issueSyncListElement.tsx 4.3 KB

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