platformPicker.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import * as React from 'react';
  2. import keydown from 'react-keydown';
  3. import styled from '@emotion/styled';
  4. import debounce from 'lodash/debounce';
  5. import {PlatformIcon} from 'platformicons';
  6. import Button from 'app/components/button';
  7. import ExternalLink from 'app/components/links/externalLink';
  8. import ListLink from 'app/components/links/listLink';
  9. import NavTabs from 'app/components/navTabs';
  10. import categoryList, {filterAliases, PlatformKey} from 'app/data/platformCategories';
  11. import platforms from 'app/data/platforms';
  12. import {IconClose, IconProject, IconSearch} from 'app/icons';
  13. import {t, tct} from 'app/locale';
  14. import {inputStyles} from 'app/styles/input';
  15. import space from 'app/styles/space';
  16. import {Organization, PlatformIntegration} from 'app/types';
  17. import {trackAdvancedAnalyticsEvent} from 'app/utils/advancedAnalytics';
  18. import EmptyMessage from 'app/views/settings/components/emptyMessage';
  19. const PLATFORM_CATEGORIES = [...categoryList, {id: 'all', name: t('All')}] as const;
  20. const PlatformList = styled('div')`
  21. display: grid;
  22. grid-gap: ${space(1)};
  23. grid-template-columns: repeat(auto-fill, 112px);
  24. margin-bottom: ${space(2)};
  25. `;
  26. type Category = typeof PLATFORM_CATEGORIES[number]['id'];
  27. type Props = {
  28. setPlatform: (key: PlatformKey | null) => void;
  29. platform?: string | null;
  30. showOther?: boolean;
  31. listClassName?: string;
  32. listProps?: React.ComponentProps<typeof PlatformList>;
  33. noAutoFilter?: boolean;
  34. defaultCategory?: Category;
  35. organization?: Organization;
  36. source?: string;
  37. };
  38. type State = {
  39. category: Category;
  40. filter: string;
  41. };
  42. class PlatformPicker extends React.Component<Props, State> {
  43. static defaultProps = {
  44. showOther: true,
  45. };
  46. state: State = {
  47. category: this.props.defaultCategory ?? PLATFORM_CATEGORIES[0].id,
  48. filter: this.props.noAutoFilter ? '' : (this.props.platform || '').split('-')[0],
  49. };
  50. get platformList() {
  51. const {category} = this.state;
  52. const currentCategory = categoryList.find(({id}) => id === category);
  53. const filter = this.state.filter.toLowerCase();
  54. const subsetMatch = (platform: PlatformIntegration) =>
  55. platform.id.includes(filter) ||
  56. platform.name.toLowerCase().includes(filter) ||
  57. filterAliases[platform.id as PlatformKey]?.some(alias => alias.includes(filter));
  58. const categoryMatch = (platform: PlatformIntegration) =>
  59. category === 'all' ||
  60. (currentCategory?.platforms as undefined | string[])?.includes(platform.id);
  61. const filtered = platforms
  62. .filter(this.state.filter ? subsetMatch : categoryMatch)
  63. .sort((a, b) => a.id.localeCompare(b.id));
  64. return this.props.showOther ? filtered : filtered.filter(({id}) => id !== 'other');
  65. }
  66. logSearch = debounce(() => {
  67. if (this.state.filter) {
  68. trackAdvancedAnalyticsEvent(
  69. 'growth.platformpicker_search',
  70. {
  71. search: this.state.filter.toLowerCase(),
  72. num_results: this.platformList.length,
  73. source: this.props.source,
  74. },
  75. this.props.organization ?? null
  76. );
  77. }
  78. }, 300);
  79. @keydown('/')
  80. focusSearch(e: KeyboardEvent) {
  81. if (e.target !== this.searchInput.current) {
  82. this.searchInput?.current?.focus();
  83. e.preventDefault();
  84. }
  85. }
  86. searchInput = React.createRef<HTMLInputElement>();
  87. render() {
  88. const platformList = this.platformList;
  89. const {setPlatform, listProps, listClassName} = this.props;
  90. const {filter, category} = this.state;
  91. return (
  92. <React.Fragment>
  93. <NavContainer>
  94. <CategoryNav>
  95. {PLATFORM_CATEGORIES.map(({id, name}) => (
  96. <ListLink
  97. key={id}
  98. onClick={(e: React.MouseEvent) => {
  99. trackAdvancedAnalyticsEvent(
  100. 'growth.platformpicker_category',
  101. {category: id, source: this.props.source},
  102. this.props.organization ?? null
  103. );
  104. this.setState({category: id, filter: ''});
  105. e.preventDefault();
  106. }}
  107. to=""
  108. isActive={() => id === (filter ? 'all' : category)}
  109. >
  110. {name}
  111. </ListLink>
  112. ))}
  113. </CategoryNav>
  114. <SearchBar>
  115. <IconSearch size="xs" />
  116. <input
  117. type="text"
  118. ref={this.searchInput}
  119. value={filter}
  120. placeholder={t('Filter Platforms')}
  121. onChange={e => this.setState({filter: e.target.value}, this.logSearch)}
  122. />
  123. </SearchBar>
  124. </NavContainer>
  125. <PlatformList className={listClassName} {...listProps}>
  126. {platformList.map(platform => (
  127. <PlatformCard
  128. data-test-id={`platform-${platform.id}`}
  129. key={platform.id}
  130. platform={platform}
  131. selected={this.props.platform === platform.id}
  132. onClear={(e: React.MouseEvent) => {
  133. setPlatform(null);
  134. e.stopPropagation();
  135. }}
  136. onClick={() => {
  137. trackAdvancedAnalyticsEvent(
  138. 'growth.select_platform',
  139. {
  140. platform_id: platform.id,
  141. source: this.props.source,
  142. },
  143. this.props.organization ?? null
  144. );
  145. setPlatform(platform.id as PlatformKey);
  146. }}
  147. />
  148. ))}
  149. </PlatformList>
  150. {platformList.length === 0 && (
  151. <EmptyMessage
  152. icon={<IconProject size="xl" />}
  153. title={t("We don't have an SDK for that yet!")}
  154. >
  155. {tct(
  156. `Not finding your platform? You can still create your project,
  157. but looks like we don't have an official SDK for your platform
  158. yet. However, there's a rich ecosystem of community supported
  159. SDKs (including Perl, CFML, Clojure, and ActionScript). Try
  160. [search:searching for Sentry clients] or contacting support.`,
  161. {
  162. search: (
  163. <ExternalLink href="https://github.com/search?q=-org%3Agetsentry+topic%3Asentry&type=Repositories" />
  164. ),
  165. }
  166. )}
  167. </EmptyMessage>
  168. )}
  169. </React.Fragment>
  170. );
  171. }
  172. }
  173. const NavContainer = styled('div')`
  174. margin-bottom: ${space(2)};
  175. display: grid;
  176. grid-gap: ${space(2)};
  177. grid-template-columns: 1fr minmax(0, 300px);
  178. align-items: start;
  179. border-bottom: 1px solid ${p => p.theme.border};
  180. `;
  181. const SearchBar = styled('div')`
  182. ${p => inputStyles(p)};
  183. padding: 0 8px;
  184. color: ${p => p.theme.subText};
  185. display: flex;
  186. align-items: center;
  187. font-size: 15px;
  188. margin-top: -${space(0.75)};
  189. input {
  190. border: none;
  191. background: none;
  192. padding: 2px 4px;
  193. width: 100%;
  194. /* Ensure a consistent line height to keep the input the desired height */
  195. line-height: 24px;
  196. &:focus {
  197. outline: none;
  198. }
  199. }
  200. `;
  201. const CategoryNav = styled(NavTabs)`
  202. margin: 0;
  203. margin-top: 4px;
  204. white-space: nowrap;
  205. > li {
  206. float: none;
  207. display: inline-block;
  208. }
  209. `;
  210. const StyledPlatformIcon = styled(PlatformIcon)`
  211. margin: ${space(2)};
  212. `;
  213. const ClearButton = styled(Button)`
  214. position: absolute;
  215. top: -6px;
  216. right: -6px;
  217. height: 22px;
  218. width: 22px;
  219. display: flex;
  220. align-items: center;
  221. justify-content: center;
  222. border-radius: 50%;
  223. background: ${p => p.theme.background};
  224. color: ${p => p.theme.textColor};
  225. `;
  226. ClearButton.defaultProps = {
  227. icon: <IconClose isCircled size="xs" />,
  228. borderless: true,
  229. size: 'xsmall',
  230. };
  231. const PlatformCard = styled(({platform, selected, onClear, ...props}) => (
  232. <div {...props}>
  233. <StyledPlatformIcon
  234. platform={platform.id}
  235. size={56}
  236. radius={5}
  237. withLanguageIcon
  238. format="lg"
  239. />
  240. <h3>{platform.name}</h3>
  241. {selected && <ClearButton onClick={onClear} />}
  242. </div>
  243. ))`
  244. position: relative;
  245. display: flex;
  246. flex-direction: column;
  247. align-items: center;
  248. padding: 0 0 14px;
  249. border-radius: 4px;
  250. cursor: pointer;
  251. background: ${p => p.selected && p.theme.alert.info.backgroundLight};
  252. &:hover {
  253. background: ${p => p.theme.alert.muted.backgroundLight};
  254. }
  255. h3 {
  256. flex-grow: 1;
  257. display: flex;
  258. align-items: center;
  259. justify-content: center;
  260. width: 100%;
  261. color: ${p => (p.selected ? p.theme.textColor : p.theme.subText)};
  262. text-align: center;
  263. font-size: ${p => p.theme.fontSizeExtraSmall};
  264. text-transform: uppercase;
  265. margin: 0;
  266. padding: 0 ${space(0.5)};
  267. line-height: 1.2;
  268. }
  269. `;
  270. export default PlatformPicker;