platformPicker.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import debounce from 'lodash/debounce';
  4. import {PlatformIcon} from 'platformicons';
  5. import Button from 'sentry/components/button';
  6. import ExternalLink from 'sentry/components/links/externalLink';
  7. import ListLink from 'sentry/components/links/listLink';
  8. import NavTabs from 'sentry/components/navTabs';
  9. import {DEFAULT_DEBOUNCE_DURATION} from 'sentry/constants';
  10. import categoryList, {filterAliases, PlatformKey} from 'sentry/data/platformCategories';
  11. import platforms from 'sentry/data/platforms';
  12. import {IconClose, IconProject, IconSearch} from 'sentry/icons';
  13. import {t, tct} from 'sentry/locale';
  14. import {inputStyles} from 'sentry/styles/input';
  15. import space from 'sentry/styles/space';
  16. import {Organization, PlatformIntegration} from 'sentry/types';
  17. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  18. import EmptyMessage from 'sentry/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. 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. interface PlatformPickerProps {
  28. setPlatform: (key: PlatformKey | null) => void;
  29. defaultCategory?: Category;
  30. listClassName?: string;
  31. listProps?: React.HTMLAttributes<HTMLDivElement>;
  32. noAutoFilter?: boolean;
  33. organization?: Organization;
  34. platform?: string | null;
  35. showOther?: boolean;
  36. source?: string;
  37. }
  38. type State = {
  39. category: Category;
  40. filter: string;
  41. };
  42. class PlatformPicker extends Component<PlatformPickerProps, 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. }, DEFAULT_DEBOUNCE_DURATION);
  76. render() {
  77. const platformList = this.platformList;
  78. const {setPlatform, listProps, listClassName} = this.props;
  79. const {filter, category} = this.state;
  80. return (
  81. <Fragment>
  82. <NavContainer>
  83. <CategoryNav>
  84. {PLATFORM_CATEGORIES.map(({id, name}) => (
  85. <ListLink
  86. key={id}
  87. onClick={(e: React.MouseEvent) => {
  88. trackAdvancedAnalyticsEvent('growth.platformpicker_category', {
  89. category: id,
  90. source: this.props.source,
  91. organization: this.props.organization ?? null,
  92. });
  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. value={filter}
  108. placeholder={t('Filter Platforms')}
  109. onChange={e => this.setState({filter: e.target.value}, this.logSearch)}
  110. />
  111. </SearchBar>
  112. </NavContainer>
  113. <PlatformList className={listClassName} {...listProps}>
  114. {platformList.map(platform => (
  115. <PlatformCard
  116. data-test-id={`platform-${platform.id}`}
  117. key={platform.id}
  118. platform={platform}
  119. selected={this.props.platform === platform.id}
  120. onClear={(e: React.MouseEvent) => {
  121. setPlatform(null);
  122. e.stopPropagation();
  123. }}
  124. onClick={() => {
  125. trackAdvancedAnalyticsEvent('growth.select_platform', {
  126. platform_id: platform.id,
  127. source: this.props.source,
  128. organization: this.props.organization ?? null,
  129. });
  130. setPlatform(platform.id as PlatformKey);
  131. }}
  132. />
  133. ))}
  134. </PlatformList>
  135. {platformList.length === 0 && (
  136. <EmptyMessage
  137. icon={<IconProject size="xl" />}
  138. title={t("We don't have an SDK for that yet!")}
  139. >
  140. {tct(
  141. `Not finding your platform? You can still create your project,
  142. but looks like we don't have an official SDK for your platform
  143. yet. However, there's a rich ecosystem of community supported
  144. SDKs (including Perl, CFML, Clojure, and ActionScript). Try
  145. [search:searching for Sentry clients] or contacting support.`,
  146. {
  147. search: (
  148. <ExternalLink href="https://github.com/search?q=-org%3Agetsentry+topic%3Asentry&type=Repositories" />
  149. ),
  150. }
  151. )}
  152. </EmptyMessage>
  153. )}
  154. </Fragment>
  155. );
  156. }
  157. }
  158. const NavContainer = styled('div')`
  159. margin-bottom: ${space(2)};
  160. display: grid;
  161. gap: ${space(2)};
  162. grid-template-columns: 1fr minmax(0, 300px);
  163. align-items: start;
  164. border-bottom: 1px solid ${p => p.theme.border};
  165. `;
  166. const SearchBar = styled('div')`
  167. ${p => inputStyles(p)};
  168. padding: 0 8px;
  169. color: ${p => p.theme.subText};
  170. display: flex;
  171. align-items: center;
  172. font-size: 15px;
  173. margin-top: -${space(0.75)};
  174. input {
  175. border: none;
  176. background: none;
  177. padding: 2px 4px;
  178. width: 100%;
  179. /* Ensure a consistent line height to keep the input the desired height */
  180. line-height: 24px;
  181. &:focus {
  182. outline: none;
  183. }
  184. }
  185. `;
  186. const CategoryNav = styled(NavTabs)`
  187. margin: 0;
  188. margin-top: 4px;
  189. white-space: nowrap;
  190. > li {
  191. float: none;
  192. display: inline-block;
  193. }
  194. `;
  195. const StyledPlatformIcon = styled(PlatformIcon)`
  196. margin: ${space(2)};
  197. `;
  198. const ClearButton = styled(Button)`
  199. position: absolute;
  200. top: -6px;
  201. right: -6px;
  202. min-height: 0;
  203. height: 22px;
  204. width: 22px;
  205. display: flex;
  206. align-items: center;
  207. justify-content: center;
  208. border-radius: 50%;
  209. background: ${p => p.theme.background};
  210. color: ${p => p.theme.textColor};
  211. `;
  212. ClearButton.defaultProps = {
  213. icon: <IconClose isCircled size="xs" />,
  214. borderless: true,
  215. size: 'xs',
  216. };
  217. const PlatformCard = styled(({platform, selected, onClear, ...props}) => (
  218. <div {...props}>
  219. <StyledPlatformIcon
  220. platform={platform.id}
  221. size={56}
  222. radius={5}
  223. withLanguageIcon
  224. format="lg"
  225. />
  226. <h3>{platform.name}</h3>
  227. {selected && <ClearButton onClick={onClear} aria-label={t('Clear')} />}
  228. </div>
  229. ))`
  230. position: relative;
  231. display: flex;
  232. flex-direction: column;
  233. align-items: center;
  234. padding: 0 0 14px;
  235. border-radius: 4px;
  236. cursor: pointer;
  237. background: ${p => p.selected && p.theme.alert.info.backgroundLight};
  238. &:hover {
  239. background: ${p => p.theme.alert.muted.backgroundLight};
  240. }
  241. h3 {
  242. flex-grow: 1;
  243. display: flex;
  244. align-items: center;
  245. justify-content: center;
  246. width: 100%;
  247. color: ${p => (p.selected ? p.theme.textColor : p.theme.subText)};
  248. text-align: center;
  249. font-size: ${p => p.theme.fontSizeExtraSmall};
  250. text-transform: uppercase;
  251. margin: 0;
  252. padding: 0 ${space(0.5)};
  253. line-height: 1.2;
  254. }
  255. `;
  256. export default PlatformPicker;