platformList.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import {css} from '@emotion/react';
  2. import styled from '@emotion/styled';
  3. import {PlatformIcon} from 'platformicons';
  4. import Tooltip from 'sentry/components/tooltip';
  5. import {PlatformKey} from 'sentry/data/platformCategories';
  6. import {tn} from 'sentry/locale';
  7. import getPlatformName from 'sentry/utils/getPlatformName';
  8. import {Theme} from 'sentry/utils/theme';
  9. type Props = {
  10. className?: string;
  11. /**
  12. * Will set container width to be size of having `this.props.max` icons
  13. * This is good for lists where the project name is displayed
  14. */
  15. consistentWidth?: boolean;
  16. direction?: 'right' | 'left';
  17. /**
  18. * Maximum number of platform icons to display
  19. */
  20. max?: number;
  21. platforms?: PlatformKey[];
  22. /**
  23. * If true and if the number of children is greater than the max prop,
  24. * a counter will be displayed at the end of the stack
  25. */
  26. showCounter?: boolean;
  27. /**
  28. * Platform icon size in pixels
  29. */
  30. size?: number;
  31. };
  32. type WrapperProps = Required<
  33. Pick<Props, 'showCounter' | 'size' | 'direction' | 'consistentWidth' | 'max'>
  34. >;
  35. function PlatformList({
  36. platforms = [],
  37. direction = 'right',
  38. max = 3,
  39. size = 16,
  40. consistentWidth = false,
  41. showCounter = false,
  42. className,
  43. }: Props) {
  44. const visiblePlatforms = platforms.slice(0, max);
  45. const numNotVisiblePlatforms = platforms.length - visiblePlatforms.length;
  46. const displayCounter = showCounter && !!numNotVisiblePlatforms;
  47. function renderContent() {
  48. if (!platforms.length) {
  49. return <StyledPlatformIcon size={size} platform="default" />;
  50. }
  51. const platformIcons = visiblePlatforms.slice().reverse();
  52. if (displayCounter) {
  53. return (
  54. <InnerWrapper>
  55. <PlatformIcons>
  56. {platformIcons.map((visiblePlatform, index) => (
  57. <Tooltip
  58. key={visiblePlatform + index}
  59. title={getPlatformName(visiblePlatform)}
  60. containerDisplayMode="inline-flex"
  61. >
  62. <StyledPlatformIcon platform={visiblePlatform} size={size} />
  63. </Tooltip>
  64. ))}
  65. </PlatformIcons>
  66. <Tooltip
  67. title={tn('%s other platform', '%s other platforms', numNotVisiblePlatforms)}
  68. containerDisplayMode="inline-flex"
  69. >
  70. <Counter>
  71. {numNotVisiblePlatforms}
  72. <Plus>{'\u002B'}</Plus>
  73. </Counter>
  74. </Tooltip>
  75. </InnerWrapper>
  76. );
  77. }
  78. return (
  79. <PlatformIcons>
  80. {platformIcons.map((visiblePlatform, index) => (
  81. <StyledPlatformIcon
  82. data-test-id={`platform-icon-${visiblePlatform}`}
  83. key={visiblePlatform + index}
  84. platform={visiblePlatform}
  85. size={size}
  86. />
  87. ))}
  88. </PlatformIcons>
  89. );
  90. }
  91. return (
  92. <Wrapper
  93. consistentWidth={consistentWidth}
  94. className={className}
  95. size={size}
  96. showCounter={displayCounter}
  97. direction={direction}
  98. max={max}
  99. >
  100. {renderContent()}
  101. </Wrapper>
  102. );
  103. }
  104. export default PlatformList;
  105. function getOverlapWidth(size: number) {
  106. return Math.round(size / 4);
  107. }
  108. const commonStyles = ({theme}: {theme: Theme}) => `
  109. cursor: default;
  110. border-radius: ${theme.borderRadius};
  111. box-shadow: 0 0 0 1px ${theme.background};
  112. :hover {
  113. z-index: 1;
  114. }
  115. `;
  116. const PlatformIcons = styled('div')`
  117. display: flex;
  118. `;
  119. const InnerWrapper = styled('div')`
  120. display: flex;
  121. position: relative;
  122. `;
  123. const Plus = styled('span')`
  124. font-size: 10px;
  125. `;
  126. const StyledPlatformIcon = styled(PlatformIcon)`
  127. ${p => commonStyles(p)};
  128. `;
  129. const Counter = styled('div')`
  130. ${p => commonStyles(p)};
  131. display: flex;
  132. align-items: center;
  133. justify-content: center;
  134. text-align: center;
  135. font-weight: 600;
  136. font-size: ${p => p.theme.fontSizeExtraSmall};
  137. background-color: ${p => p.theme.gray200};
  138. color: ${p => p.theme.gray300};
  139. padding: 0 1px;
  140. position: absolute;
  141. right: -1px;
  142. `;
  143. const Wrapper = styled('div')<WrapperProps>`
  144. display: flex;
  145. flex-shrink: 0;
  146. justify-content: ${p => (p.direction === 'right' ? 'flex-end' : 'flex-start')};
  147. ${p =>
  148. p.consistentWidth && `width: ${p.size + (p.max - 1) * getOverlapWidth(p.size)}px;`};
  149. ${PlatformIcons} {
  150. ${p =>
  151. p.showCounter
  152. ? css`
  153. z-index: 1;
  154. flex-direction: row-reverse;
  155. > * :not(:first-child) {
  156. margin-right: ${p.size * -1 + getOverlapWidth(p.size)}px;
  157. }
  158. `
  159. : css`
  160. > * :not(:first-child) {
  161. margin-left: ${p.size * -1 + getOverlapWidth(p.size)}px;
  162. }
  163. `}
  164. }
  165. ${InnerWrapper} {
  166. padding-right: ${p => p.size / 2 + 1}px;
  167. }
  168. ${Counter} {
  169. height: ${p => p.size}px;
  170. min-width: ${p => p.size}px;
  171. }
  172. `;