platformPicker.tsx 8.6 KB

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