platformPicker.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 EmptyMessage from 'sentry/components/emptyMessage';
  7. import ExternalLink from 'sentry/components/links/externalLink';
  8. import ListLink from 'sentry/components/links/listLink';
  9. import NavTabs from 'sentry/components/navTabs';
  10. import SearchBar from 'sentry/components/searchBar';
  11. import {DEFAULT_DEBOUNCE_DURATION} from 'sentry/constants';
  12. import categoryList, {filterAliases, PlatformKey} from 'sentry/data/platformCategories';
  13. import platforms from 'sentry/data/platforms';
  14. import {IconClose, IconProject} from 'sentry/icons';
  15. import {t, tct} from 'sentry/locale';
  16. import space from 'sentry/styles/space';
  17. import {Organization, PlatformIntegration} from 'sentry/types';
  18. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  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. <StyledSearchBar
  104. size="sm"
  105. query={filter}
  106. placeholder={t('Filter Platforms')}
  107. onChange={val => this.setState({filter: val}, this.logSearch)}
  108. />
  109. </NavContainer>
  110. <PlatformList className={listClassName} {...listProps}>
  111. {platformList.map(platform => (
  112. <PlatformCard
  113. data-test-id={`platform-${platform.id}`}
  114. key={platform.id}
  115. platform={platform}
  116. selected={this.props.platform === platform.id}
  117. onClear={(e: React.MouseEvent) => {
  118. setPlatform(null);
  119. e.stopPropagation();
  120. }}
  121. onClick={() => {
  122. trackAdvancedAnalyticsEvent('growth.select_platform', {
  123. platform_id: platform.id,
  124. source: this.props.source,
  125. organization: this.props.organization ?? null,
  126. });
  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. </Fragment>
  152. );
  153. }
  154. }
  155. const NavContainer = styled('div')`
  156. margin-bottom: ${space(2)};
  157. display: grid;
  158. 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 StyledSearchBar = styled(SearchBar)`
  164. min-width: 6rem;
  165. max-width: 12rem;
  166. margin-top: -${space(0.25)};
  167. margin-left: auto;
  168. flex-shrink: 0;
  169. flex-basis: 0;
  170. flex-grow: 1;
  171. `;
  172. const CategoryNav = styled(NavTabs)`
  173. margin: 0;
  174. margin-top: 4px;
  175. white-space: nowrap;
  176. > li {
  177. float: none;
  178. display: inline-block;
  179. }
  180. `;
  181. const StyledPlatformIcon = styled(PlatformIcon)`
  182. margin: ${space(2)};
  183. `;
  184. const ClearButton = styled(Button)`
  185. position: absolute;
  186. top: -6px;
  187. right: -6px;
  188. min-height: 0;
  189. height: 22px;
  190. width: 22px;
  191. display: flex;
  192. align-items: center;
  193. justify-content: center;
  194. border-radius: 50%;
  195. background: ${p => p.theme.background};
  196. color: ${p => p.theme.textColor};
  197. `;
  198. ClearButton.defaultProps = {
  199. icon: <IconClose isCircled size="xs" />,
  200. borderless: true,
  201. size: 'xs',
  202. };
  203. const PlatformCard = styled(({platform, selected, onClear, ...props}) => (
  204. <div {...props}>
  205. <StyledPlatformIcon
  206. platform={platform.id}
  207. size={56}
  208. radius={5}
  209. withLanguageIcon
  210. format="lg"
  211. />
  212. <h3>{platform.name}</h3>
  213. {selected && <ClearButton onClick={onClear} aria-label={t('Clear')} />}
  214. </div>
  215. ))`
  216. position: relative;
  217. display: flex;
  218. flex-direction: column;
  219. align-items: center;
  220. padding: 0 0 14px;
  221. border-radius: 4px;
  222. cursor: pointer;
  223. background: ${p => p.selected && p.theme.alert.info.backgroundLight};
  224. &:hover {
  225. background: ${p => p.theme.alert.muted.backgroundLight};
  226. }
  227. h3 {
  228. flex-grow: 1;
  229. display: flex;
  230. align-items: center;
  231. justify-content: center;
  232. width: 100%;
  233. color: ${p => (p.selected ? p.theme.textColor : p.theme.subText)};
  234. text-align: center;
  235. font-size: ${p => p.theme.fontSizeExtraSmall};
  236. text-transform: uppercase;
  237. margin: 0;
  238. padding: 0 ${space(0.5)};
  239. line-height: 1.2;
  240. }
  241. `;
  242. export default PlatformPicker;