issueSyncListElement.tsx 3.7 KB

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