platformPicker.tsx 8.0 KB

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