platformPicker.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 type {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. &.centered {
  27. justify-content: center;
  28. }
  29. `;
  30. const selectablePlatforms = platforms.filter(platform =>
  31. createablePlatforms.has(platform.id)
  32. );
  33. export type Category = (typeof categoryList)[number]['id'];
  34. export type Platform = PlatformIntegration & {
  35. category: Category;
  36. };
  37. interface PlatformPickerProps {
  38. setPlatform: (props: Platform | null) => void;
  39. defaultCategory?: Category;
  40. listClassName?: string;
  41. listProps?: React.HTMLAttributes<HTMLDivElement>;
  42. modal?: boolean;
  43. navClassName?: string;
  44. noAutoFilter?: boolean;
  45. organization?: Organization;
  46. platform?: string | null;
  47. showFilterBar?: boolean;
  48. showOther?: boolean;
  49. source?: string;
  50. }
  51. type State = {
  52. category: Category;
  53. filter: string;
  54. };
  55. class PlatformPicker extends Component<PlatformPickerProps, State> {
  56. static defaultProps = {
  57. showOther: true,
  58. };
  59. state: State = {
  60. category: this.props.defaultCategory ?? categoryList[0].id,
  61. filter: this.props.noAutoFilter ? '' : (this.props.platform || '').split('-')[0],
  62. };
  63. get platformList() {
  64. const {category} = this.state;
  65. const currentCategory = categoryList.find(({id}) => id === category);
  66. const filter = this.state.filter.toLowerCase();
  67. const subsetMatch = (platform: PlatformIntegration) =>
  68. platform.id.includes(filter) ||
  69. platform.name.toLowerCase().includes(filter) ||
  70. filterAliases[platform.id as PlatformKey]?.some(alias => alias.includes(filter));
  71. const categoryMatch = (platform: PlatformIntegration) => {
  72. return currentCategory?.platforms?.has(platform.id);
  73. };
  74. // temporary replacement of selectablePlatforms while `nintendo` is behind feature flag
  75. const tempSelectablePlatforms = selectablePlatforms;
  76. if (this.props.organization?.features.includes('selectable-nintendo-platform')) {
  77. const nintendo = platforms.find(p => p.id === 'nintendo');
  78. if (nintendo) {
  79. if (!tempSelectablePlatforms.includes(nintendo)) {
  80. tempSelectablePlatforms.push(nintendo);
  81. }
  82. }
  83. }
  84. const filtered = tempSelectablePlatforms
  85. .filter(this.state.filter ? subsetMatch : categoryMatch)
  86. .sort((a, b) => a.id.localeCompare(b.id));
  87. return this.props.showOther ? filtered : filtered.filter(({id}) => id !== 'other');
  88. }
  89. logSearch = debounce(() => {
  90. if (this.state.filter) {
  91. trackAnalytics('growth.platformpicker_search', {
  92. search: this.state.filter.toLowerCase(),
  93. num_results: this.platformList.length,
  94. source: this.props.source,
  95. organization: this.props.organization ?? null,
  96. });
  97. }
  98. }, DEFAULT_DEBOUNCE_DURATION);
  99. render() {
  100. const platformList = this.platformList;
  101. const {
  102. setPlatform,
  103. listProps,
  104. listClassName,
  105. navClassName,
  106. showFilterBar = true,
  107. } = this.props;
  108. const {filter, category} = this.state;
  109. return (
  110. <Fragment>
  111. <NavContainer className={navClassName}>
  112. <CategoryNav>
  113. {categoryList.map(({id, name}) => (
  114. <ListLink
  115. key={id}
  116. onClick={(e: React.MouseEvent) => {
  117. trackAnalytics('growth.platformpicker_category', {
  118. category: id,
  119. source: this.props.source,
  120. organization: this.props.organization ?? null,
  121. });
  122. this.setState({category: id, filter: ''});
  123. e.preventDefault();
  124. }}
  125. to=""
  126. isActive={() => id === (filter ? 'all' : category)}
  127. >
  128. {name}
  129. </ListLink>
  130. ))}
  131. </CategoryNav>
  132. {showFilterBar && (
  133. <StyledSearchBar
  134. size="sm"
  135. query={filter}
  136. placeholder={t('Filter Platforms')}
  137. onChange={val => this.setState({filter: val}, this.logSearch)}
  138. />
  139. )}
  140. </NavContainer>
  141. <PlatformList className={listClassName} {...listProps}>
  142. {platformList.map(platform => {
  143. return (
  144. <PlatformCard
  145. data-test-id={`platform-${platform.id}`}
  146. key={platform.id}
  147. platform={platform}
  148. selected={this.props.platform === platform.id}
  149. onClear={(e: React.MouseEvent) => {
  150. setPlatform(null);
  151. e.stopPropagation();
  152. }}
  153. onClick={() => {
  154. trackAnalytics('growth.select_platform', {
  155. platform_id: platform.id,
  156. source: this.props.source,
  157. organization: this.props.organization ?? null,
  158. });
  159. setPlatform({...platform, category});
  160. }}
  161. />
  162. );
  163. })}
  164. </PlatformList>
  165. {platformList.length === 0 && (
  166. <EmptyMessage
  167. icon={<IconProject size="xl" />}
  168. title={t("We don't have an SDK for that yet!")}
  169. >
  170. {tct(
  171. `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”].`,
  172. {
  173. linkOther: (
  174. <Button
  175. aria-label={t("Select 'Other'")}
  176. priority="link"
  177. onClick={() => {
  178. this.setState({filter: otherPlatform.name});
  179. setPlatform({...otherPlatform, category});
  180. }}
  181. />
  182. ),
  183. }
  184. )}
  185. </EmptyMessage>
  186. )}
  187. </Fragment>
  188. );
  189. }
  190. }
  191. const NavContainer = styled('div')`
  192. margin-bottom: ${space(2)};
  193. display: grid;
  194. gap: ${space(2)};
  195. grid-template-columns: 1fr minmax(0, 300px);
  196. align-items: start;
  197. border-bottom: 1px solid ${p => p.theme.border};
  198. &.centered {
  199. grid-template-columns: none;
  200. justify-content: center;
  201. }
  202. `;
  203. const StyledSearchBar = styled(SearchBar)`
  204. min-width: 6rem;
  205. max-width: 12rem;
  206. margin-top: -${space(0.25)};
  207. margin-left: auto;
  208. flex-shrink: 0;
  209. flex-basis: 0;
  210. flex-grow: 1;
  211. `;
  212. const CategoryNav = styled(NavTabs)`
  213. margin: 0;
  214. margin-top: 4px;
  215. white-space: nowrap;
  216. > li {
  217. float: none;
  218. display: inline-block;
  219. }
  220. `;
  221. const StyledPlatformIcon = styled(PlatformIcon)`
  222. margin: ${space(2)};
  223. border: 1px solid ${p => p.theme.gray200};
  224. border-radius: ${p => p.theme.borderRadius};
  225. `;
  226. const ClearButton = styled(Button)`
  227. position: absolute;
  228. top: -6px;
  229. right: -6px;
  230. min-height: 0;
  231. height: 22px;
  232. width: 22px;
  233. display: flex;
  234. align-items: center;
  235. justify-content: center;
  236. border-radius: 50%;
  237. background: ${p => p.theme.background};
  238. color: ${p => p.theme.textColor};
  239. `;
  240. ClearButton.defaultProps = {
  241. icon: <IconClose isCircled />,
  242. borderless: true,
  243. size: 'xs',
  244. };
  245. const PlatformCard = styled(({platform, selected, onClear, ...props}) => (
  246. <div {...props}>
  247. <StyledPlatformIcon
  248. platform={platform.id}
  249. size={56}
  250. radius={5}
  251. withLanguageIcon
  252. format="lg"
  253. />
  254. <h3>{platform.name}</h3>
  255. {selected && <ClearButton onClick={onClear} aria-label={t('Clear')} />}
  256. </div>
  257. ))`
  258. position: relative;
  259. display: flex;
  260. flex-direction: column;
  261. align-items: center;
  262. padding: 0 0 14px;
  263. border-radius: 4px;
  264. background: ${p => p.selected && p.theme.alert.info.backgroundLight};
  265. cursor: pointer;
  266. &:hover {
  267. background: ${p => p.theme.alert.muted.backgroundLight};
  268. }
  269. h3 {
  270. flex-grow: 1;
  271. display: flex;
  272. align-items: center;
  273. justify-content: center;
  274. width: 100%;
  275. color: ${p => (p.selected ? p.theme.textColor : p.theme.subText)};
  276. text-align: center;
  277. font-size: ${p => p.theme.fontSizeExtraSmall};
  278. text-transform: uppercase;
  279. margin: 0;
  280. padding: 0 ${space(0.5)};
  281. line-height: 1.2;
  282. }
  283. `;
  284. export default PlatformPicker;