platformPicker.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import debounce from 'lodash/debounce';
  4. import {PlatformIcon} from 'platformicons';
  5. import {Button} from 'sentry/components/button';
  6. import EmptyMessage from 'sentry/components/emptyMessage';
  7. import ListLink from 'sentry/components/links/listLink';
  8. import NavTabs from 'sentry/components/navTabs';
  9. import SearchBar from 'sentry/components/searchBar';
  10. import {DEFAULT_DEBOUNCE_DURATION} from 'sentry/constants';
  11. import categoryList, {
  12. createablePlatforms,
  13. filterAliases,
  14. } from 'sentry/data/platformPickerCategories';
  15. import platforms, {otherPlatform} from 'sentry/data/platforms';
  16. import {IconClose, IconProject} from 'sentry/icons';
  17. import {t, tct} from 'sentry/locale';
  18. import {space} from 'sentry/styles/space';
  19. import {Organization, PlatformIntegration, PlatformKey} from 'sentry/types';
  20. import {trackAnalytics} from 'sentry/utils/analytics';
  21. const PlatformList = styled('div')`
  22. display: grid;
  23. gap: ${space(1)};
  24. grid-template-columns: repeat(auto-fill, 112px);
  25. margin-bottom: ${space(2)};
  26. `;
  27. const selectablePlatforms = platforms.filter(platform =>
  28. createablePlatforms.has(platform.id)
  29. );
  30. export type Category = (typeof categoryList)[number]['id'];
  31. export type Platform = PlatformIntegration & {
  32. category: Category;
  33. };
  34. interface PlatformPickerProps {
  35. setPlatform: (props: Platform | null) => void;
  36. defaultCategory?: Category;
  37. listClassName?: string;
  38. listProps?: React.HTMLAttributes<HTMLDivElement>;
  39. noAutoFilter?: boolean;
  40. organization?: Organization;
  41. platform?: string | null;
  42. showOther?: boolean;
  43. source?: string;
  44. }
  45. type State = {
  46. category: Category;
  47. filter: string;
  48. };
  49. class PlatformPicker extends Component<PlatformPickerProps, State> {
  50. static defaultProps = {
  51. showOther: true,
  52. };
  53. state: State = {
  54. category: this.props.defaultCategory ?? categoryList[0].id,
  55. filter: this.props.noAutoFilter ? '' : (this.props.platform || '').split('-')[0],
  56. };
  57. get platformList() {
  58. const {category} = this.state;
  59. const currentCategory = categoryList.find(({id}) => id === category);
  60. const filter = this.state.filter.toLowerCase();
  61. const subsetMatch = (platform: PlatformIntegration) =>
  62. platform.id.includes(filter) ||
  63. platform.name.toLowerCase().includes(filter) ||
  64. filterAliases[platform.id as PlatformKey]?.some(alias => alias.includes(filter));
  65. const categoryMatch = (platform: PlatformIntegration) => {
  66. return currentCategory?.platforms?.has(platform.id);
  67. };
  68. const filtered = selectablePlatforms
  69. .filter(this.state.filter ? subsetMatch : categoryMatch)
  70. .sort((a, b) => a.id.localeCompare(b.id));
  71. return this.props.showOther ? filtered : filtered.filter(({id}) => id !== 'other');
  72. }
  73. logSearch = debounce(() => {
  74. if (this.state.filter) {
  75. trackAnalytics('growth.platformpicker_search', {
  76. search: this.state.filter.toLowerCase(),
  77. num_results: this.platformList.length,
  78. source: this.props.source,
  79. organization: this.props.organization ?? null,
  80. });
  81. }
  82. }, DEFAULT_DEBOUNCE_DURATION);
  83. render() {
  84. const platformList = this.platformList;
  85. const {setPlatform, listProps, listClassName} = this.props;
  86. const {filter, category} = this.state;
  87. return (
  88. <Fragment>
  89. <NavContainer>
  90. <CategoryNav>
  91. {categoryList.map(({id, name}) => (
  92. <ListLink
  93. key={id}
  94. onClick={(e: React.MouseEvent) => {
  95. trackAnalytics('growth.platformpicker_category', {
  96. category: id,
  97. source: this.props.source,
  98. organization: this.props.organization ?? null,
  99. });
  100. this.setState({category: id, filter: ''});
  101. e.preventDefault();
  102. }}
  103. to=""
  104. isActive={() => id === (filter ? 'all' : category)}
  105. >
  106. {name}
  107. </ListLink>
  108. ))}
  109. </CategoryNav>
  110. <StyledSearchBar
  111. size="sm"
  112. query={filter}
  113. placeholder={t('Filter Platforms')}
  114. onChange={val => this.setState({filter: val}, this.logSearch)}
  115. />
  116. </NavContainer>
  117. <PlatformList className={listClassName} {...listProps}>
  118. {platformList.map(platform => {
  119. return (
  120. <PlatformCard
  121. data-test-id={`platform-${platform.id}`}
  122. key={platform.id}
  123. platform={platform}
  124. selected={this.props.platform === platform.id}
  125. onClear={(e: React.MouseEvent) => {
  126. setPlatform(null);
  127. e.stopPropagation();
  128. }}
  129. onClick={() => {
  130. trackAnalytics('growth.select_platform', {
  131. platform_id: platform.id,
  132. source: this.props.source,
  133. organization: this.props.organization ?? null,
  134. });
  135. setPlatform({...platform, category});
  136. }}
  137. />
  138. );
  139. })}
  140. </PlatformList>
  141. {platformList.length === 0 && (
  142. <EmptyMessage
  143. icon={<IconProject size="xl" />}
  144. title={t("We don't have an SDK for that yet!")}
  145. >
  146. {tct(
  147. `Sure you haven't misspelled? If you're using a lesser-known platform, consider choosing a more generic SDK like Browser JavaScript, Python, Node, .NET & Java or create a generic project, by selecting [linkOther:“Other”].`,
  148. {
  149. linkOther: (
  150. <Button
  151. aria-label={t("Select 'Other'")}
  152. priority="link"
  153. onClick={() => {
  154. this.setState({filter: otherPlatform.name});
  155. setPlatform({...otherPlatform, category});
  156. }}
  157. />
  158. ),
  159. }
  160. )}
  161. </EmptyMessage>
  162. )}
  163. </Fragment>
  164. );
  165. }
  166. }
  167. const NavContainer = styled('div')`
  168. margin-bottom: ${space(2)};
  169. display: grid;
  170. gap: ${space(2)};
  171. grid-template-columns: 1fr minmax(0, 300px);
  172. align-items: start;
  173. border-bottom: 1px solid ${p => p.theme.border};
  174. `;
  175. const StyledSearchBar = styled(SearchBar)`
  176. min-width: 6rem;
  177. max-width: 12rem;
  178. margin-top: -${space(0.25)};
  179. margin-left: auto;
  180. flex-shrink: 0;
  181. flex-basis: 0;
  182. flex-grow: 1;
  183. `;
  184. const CategoryNav = styled(NavTabs)`
  185. margin: 0;
  186. margin-top: 4px;
  187. white-space: nowrap;
  188. > li {
  189. float: none;
  190. display: inline-block;
  191. }
  192. `;
  193. const StyledPlatformIcon = styled(PlatformIcon)`
  194. margin: ${space(2)};
  195. `;
  196. const ClearButton = styled(Button)`
  197. position: absolute;
  198. top: -6px;
  199. right: -6px;
  200. min-height: 0;
  201. height: 22px;
  202. width: 22px;
  203. display: flex;
  204. align-items: center;
  205. justify-content: center;
  206. border-radius: 50%;
  207. background: ${p => p.theme.background};
  208. color: ${p => p.theme.textColor};
  209. `;
  210. ClearButton.defaultProps = {
  211. icon: <IconClose isCircled size="xs" />,
  212. borderless: true,
  213. size: 'xs',
  214. };
  215. const PlatformCard = styled(({platform, selected, onClear, ...props}) => (
  216. <div {...props}>
  217. <StyledPlatformIcon
  218. platform={platform.id}
  219. size={56}
  220. radius={5}
  221. withLanguageIcon
  222. format="lg"
  223. />
  224. <h3>{platform.name}</h3>
  225. {selected && <ClearButton onClick={onClear} aria-label={t('Clear')} />}
  226. </div>
  227. ))`
  228. position: relative;
  229. display: flex;
  230. flex-direction: column;
  231. align-items: center;
  232. padding: 0 0 14px;
  233. border-radius: 4px;
  234. background: ${p => p.selected && p.theme.alert.info.backgroundLight};
  235. cursor: pointer;
  236. &:hover {
  237. background: ${p => p.theme.alert.muted.backgroundLight};
  238. }
  239. h3 {
  240. flex-grow: 1;
  241. display: flex;
  242. align-items: center;
  243. justify-content: center;
  244. width: 100%;
  245. color: ${p => (p.selected ? p.theme.textColor : p.theme.subText)};
  246. text-align: center;
  247. font-size: ${p => p.theme.fontSizeExtraSmall};
  248. text-transform: uppercase;
  249. margin: 0;
  250. padding: 0 ${space(0.5)};
  251. line-height: 1.2;
  252. }
  253. `;
  254. export default PlatformPicker;