platformPicker.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import 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. type Category = typeof PLATFORM_CATEGORIES[number]['id'];
  21. type Props = {
  22. setPlatform: (key: PlatformKey | null) => void;
  23. platform?: string | null;
  24. showOther?: boolean;
  25. listClassName?: string;
  26. listProps?: React.HTMLProps<HTMLDivElement>;
  27. noAutoFilter?: boolean;
  28. defaultCategory?: Category;
  29. };
  30. type State = {
  31. category: Category;
  32. filter: string;
  33. };
  34. class PlatformPicker extends React.Component<Props, State> {
  35. static defaultProps = {
  36. showOther: true,
  37. };
  38. state: State = {
  39. category: this.props.defaultCategory ?? PLATFORM_CATEGORIES[0].id,
  40. filter: this.props.noAutoFilter ? '' : (this.props.platform || '').split('-')[0],
  41. };
  42. get platformList() {
  43. const {category} = this.state;
  44. const currentCategory = categoryList.find(({id}) => id === category);
  45. const filter = this.state.filter.toLowerCase();
  46. const subsetMatch = (platform: PlatformIntegration) =>
  47. platform.id.includes(filter) ||
  48. platform.name.toLowerCase().includes(filter) ||
  49. filterAliases[platform.id as PlatformKey]?.some(alias => alias.includes(filter));
  50. const categoryMatch = (platform: PlatformIntegration) =>
  51. category === 'all' ||
  52. (currentCategory?.platforms as undefined | string[])?.includes(platform.id);
  53. const filtered = platforms
  54. .filter(this.state.filter ? subsetMatch : categoryMatch)
  55. .sort((a, b) => a.id.localeCompare(b.id));
  56. return this.props.showOther ? filtered : filtered.filter(({id}) => id !== 'other');
  57. }
  58. logSearch = debounce(() => {
  59. if (this.state.filter) {
  60. analytics('platformpicker.search', {
  61. query: this.state.filter.toLowerCase(),
  62. num_results: this.platformList.length,
  63. });
  64. }
  65. }, 300);
  66. @keydown('/')
  67. focusSearch(e: KeyboardEvent) {
  68. if (e.target !== this.searchInput.current) {
  69. this.searchInput?.current?.focus();
  70. e.preventDefault();
  71. }
  72. }
  73. searchInput = React.createRef<HTMLInputElement>();
  74. render() {
  75. const platformList = this.platformList;
  76. const {setPlatform, listProps, listClassName} = this.props;
  77. const {filter, category} = this.state;
  78. return (
  79. <React.Fragment>
  80. <NavContainer>
  81. <CategoryNav>
  82. {PLATFORM_CATEGORIES.map(({id, name}) => (
  83. <ListLink
  84. key={id}
  85. onClick={(e: React.MouseEvent) => {
  86. analytics('platformpicker.select_tab', {category: id});
  87. this.setState({category: id, filter: ''});
  88. e.preventDefault();
  89. }}
  90. to=""
  91. isActive={() => id === (filter ? 'all' : category)}
  92. >
  93. {name}
  94. </ListLink>
  95. ))}
  96. </CategoryNav>
  97. <SearchBar>
  98. <IconSearch size="xs" />
  99. <input
  100. type="text"
  101. ref={this.searchInput}
  102. value={filter}
  103. placeholder={t('Filter Platforms')}
  104. onChange={e => this.setState({filter: e.target.value}, this.logSearch)}
  105. />
  106. </SearchBar>
  107. </NavContainer>
  108. <PlatformList className={listClassName} {...listProps}>
  109. {platformList.map(platform => (
  110. <PlatformCard
  111. data-test-id={`platform-${platform.id}`}
  112. key={platform.id}
  113. platform={platform}
  114. selected={this.props.platform === platform.id}
  115. onClear={(e: React.MouseEvent) => {
  116. setPlatform(null);
  117. e.stopPropagation();
  118. }}
  119. onClick={() => {
  120. analytics('platformpicker.select_platform', {platform: platform.id});
  121. setPlatform(platform.id as PlatformKey);
  122. }}
  123. />
  124. ))}
  125. </PlatformList>
  126. {platformList.length === 0 && (
  127. <EmptyMessage
  128. icon={<IconProject size="xl" />}
  129. title={t("We don't have an SDK for that yet!")}
  130. >
  131. {tct(
  132. `Not finding your platform? You can still create your project,
  133. but looks like we don't have an official SDK for your platform
  134. yet. However, there's a rich ecosystem of community supported
  135. SDKs (including Perl, CFML, Clojure, and ActionScript). Try
  136. [search:searching for Sentry clients] or contacting support.`,
  137. {
  138. search: (
  139. <ExternalLink href="https://github.com/search?q=-org%3Agetsentry+topic%3Asentry&type=Repositories" />
  140. ),
  141. }
  142. )}
  143. </EmptyMessage>
  144. )}
  145. </React.Fragment>
  146. );
  147. }
  148. }
  149. const NavContainer = styled('div')`
  150. margin-bottom: ${space(2)};
  151. display: grid;
  152. grid-gap: ${space(2)};
  153. grid-template-columns: 1fr minmax(0, 300px);
  154. align-items: start;
  155. border-bottom: 1px solid ${p => p.theme.border};
  156. `;
  157. const SearchBar = styled('div')`
  158. ${p => inputStyles(p)};
  159. padding: 0 8px;
  160. color: ${p => p.theme.subText};
  161. display: flex;
  162. align-items: center;
  163. font-size: 15px;
  164. margin-top: -${space(0.75)};
  165. input {
  166. border: none;
  167. background: none;
  168. padding: 2px 4px;
  169. width: 100%;
  170. /* Ensure a consistent line height to keep the input the desired height */
  171. line-height: 24px;
  172. &:focus {
  173. outline: none;
  174. }
  175. }
  176. `;
  177. const CategoryNav = styled(NavTabs)`
  178. margin: 0;
  179. margin-top: 4px;
  180. white-space: nowrap;
  181. > li {
  182. float: none;
  183. display: inline-block;
  184. }
  185. `;
  186. const PlatformList = styled('div')`
  187. display: grid;
  188. grid-gap: ${space(1)};
  189. grid-template-columns: repeat(auto-fill, 112px);
  190. margin-bottom: ${space(2)};
  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 && '#ecf5fd'};
  234. &:hover {
  235. background: #ebebef;
  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.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;