issueSyncListElement.tsx 3.8 KB

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