platformPicker.tsx 9.3 KB

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