platformPicker.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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/analytics/trackAdvancedAnalyticsEvent';
  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('growth.platformpicker_search', {
  69. search: this.state.filter.toLowerCase(),
  70. num_results: this.platformList.length,
  71. source: this.props.source,
  72. organization: this.props.organization ?? null,
  73. });
  74. }
  75. }, 300);
  76. @keydown('/')
  77. focusSearch(e: KeyboardEvent) {
  78. if (e.target !== this.searchInput.current) {
  79. this.searchInput?.current?.focus();
  80. e.preventDefault();
  81. }
  82. }
  83. searchInput = React.createRef<HTMLInputElement>();
  84. render() {
  85. const platformList = this.platformList;
  86. const {setPlatform, listProps, listClassName} = this.props;
  87. const {filter, category} = this.state;
  88. return (
  89. <React.Fragment>
  90. <NavContainer>
  91. <CategoryNav>
  92. {PLATFORM_CATEGORIES.map(({id, name}) => (
  93. <ListLink
  94. key={id}
  95. onClick={(e: React.MouseEvent) => {
  96. trackAdvancedAnalyticsEvent('growth.platformpicker_category', {
  97. category: id,
  98. source: this.props.source,
  99. organization: this.props.organization ?? null,
  100. });
  101. this.setState({category: id, filter: ''});
  102. e.preventDefault();
  103. }}
  104. to=""
  105. isActive={() => id === (filter ? 'all' : category)}
  106. >
  107. {name}
  108. </ListLink>
  109. ))}
  110. </CategoryNav>
  111. <SearchBar>
  112. <IconSearch size="xs" />
  113. <input
  114. type="text"
  115. ref={this.searchInput}
  116. value={filter}
  117. placeholder={t('Filter Platforms')}
  118. onChange={e => this.setState({filter: e.target.value}, this.logSearch)}
  119. />
  120. </SearchBar>
  121. </NavContainer>
  122. <PlatformList className={listClassName} {...listProps}>
  123. {platformList.map(platform => (
  124. <PlatformCard
  125. data-test-id={`platform-${platform.id}`}
  126. key={platform.id}
  127. platform={platform}
  128. selected={this.props.platform === platform.id}
  129. onClear={(e: React.MouseEvent) => {
  130. setPlatform(null);
  131. e.stopPropagation();
  132. }}
  133. onClick={() => {
  134. trackAdvancedAnalyticsEvent('growth.select_platform', {
  135. platform_id: platform.id,
  136. source: this.props.source,
  137. organization: this.props.organization ?? null,
  138. });
  139. setPlatform(platform.id as PlatformKey);
  140. }}
  141. />
  142. ))}
  143. </PlatformList>
  144. {platformList.length === 0 && (
  145. <EmptyMessage
  146. icon={<IconProject size="xl" />}
  147. title={t("We don't have an SDK for that yet!")}
  148. >
  149. {tct(
  150. `Not finding your platform? You can still create your project,
  151. but looks like we don't have an official SDK for your platform
  152. yet. However, there's a rich ecosystem of community supported
  153. SDKs (including Perl, CFML, Clojure, and ActionScript). Try
  154. [search:searching for Sentry clients] or contacting support.`,
  155. {
  156. search: (
  157. <ExternalLink href="https://github.com/search?q=-org%3Agetsentry+topic%3Asentry&type=Repositories" />
  158. ),
  159. }
  160. )}
  161. </EmptyMessage>
  162. )}
  163. </React.Fragment>
  164. );
  165. }
  166. }
  167. const NavContainer = styled('div')`
  168. margin-bottom: ${space(2)};
  169. display: grid;
  170. grid-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 SearchBar = styled('div')`
  176. ${p => inputStyles(p)};
  177. padding: 0 8px;
  178. color: ${p => p.theme.subText};
  179. display: flex;
  180. align-items: center;
  181. font-size: 15px;
  182. margin-top: -${space(0.75)};
  183. input {
  184. border: none;
  185. background: none;
  186. padding: 2px 4px;
  187. width: 100%;
  188. /* Ensure a consistent line height to keep the input the desired height */
  189. line-height: 24px;
  190. &:focus {
  191. outline: none;
  192. }
  193. }
  194. `;
  195. const CategoryNav = styled(NavTabs)`
  196. margin: 0;
  197. margin-top: 4px;
  198. white-space: nowrap;
  199. > li {
  200. float: none;
  201. display: inline-block;
  202. }
  203. `;
  204. const StyledPlatformIcon = styled(PlatformIcon)`
  205. margin: ${space(2)};
  206. `;
  207. const ClearButton = styled(Button)`
  208. position: absolute;
  209. top: -6px;
  210. right: -6px;
  211. height: 22px;
  212. width: 22px;
  213. display: flex;
  214. align-items: center;
  215. justify-content: center;
  216. border-radius: 50%;
  217. background: ${p => p.theme.background};
  218. color: ${p => p.theme.textColor};
  219. `;
  220. ClearButton.defaultProps = {
  221. icon: <IconClose isCircled size="xs" />,
  222. borderless: true,
  223. size: 'xsmall',
  224. };
  225. const PlatformCard = styled(({platform, selected, onClear, ...props}) => (
  226. <div {...props}>
  227. <StyledPlatformIcon
  228. platform={platform.id}
  229. size={56}
  230. radius={5}
  231. withLanguageIcon
  232. format="lg"
  233. />
  234. <h3>{platform.name}</h3>
  235. {selected && <ClearButton onClick={onClear} />}
  236. </div>
  237. ))`
  238. position: relative;
  239. display: flex;
  240. flex-direction: column;
  241. align-items: center;
  242. padding: 0 0 14px;
  243. border-radius: 4px;
  244. cursor: pointer;
  245. background: ${p => p.selected && p.theme.alert.info.backgroundLight};
  246. &:hover {
  247. background: ${p => p.theme.alert.muted.backgroundLight};
  248. }
  249. h3 {
  250. flex-grow: 1;
  251. display: flex;
  252. align-items: center;
  253. justify-content: center;
  254. width: 100%;
  255. color: ${p => (p.selected ? p.theme.textColor : p.theme.subText)};
  256. text-align: center;
  257. font-size: ${p => p.theme.fontSizeExtraSmall};
  258. text-transform: uppercase;
  259. margin: 0;
  260. padding: 0 ${space(0.5)};
  261. line-height: 1.2;
  262. }
  263. `;
  264. export default PlatformPicker;