multiPlatformPicker.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. import {Fragment, useEffect, useState} 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, {
  13. filterAliases,
  14. PlatformKey,
  15. popularPlatformCategories,
  16. } from 'sentry/data/platformCategories';
  17. import platforms from 'sentry/data/platforms';
  18. import {IconClose, IconProject} from 'sentry/icons';
  19. import {t, tct} from 'sentry/locale';
  20. import space from 'sentry/styles/space';
  21. import {Organization, PlatformIntegration} from 'sentry/types';
  22. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  23. const PLATFORM_CATEGORIES = [{id: 'all', name: t('All')}, ...categoryList] as const;
  24. // Category needs the all option while CategoryObj does not
  25. type Category = typeof PLATFORM_CATEGORIES[number]['id'];
  26. type CategoryObj = typeof categoryList[number];
  27. type Platform = CategoryObj['platforms'][number];
  28. // create a lookup table for each platform
  29. const indexByPlatformByCategory = {} as Record<
  30. CategoryObj['id'],
  31. Record<Platform, number>
  32. >;
  33. categoryList.forEach(category => {
  34. const indexByPlatform = {} as Record<Platform, number>;
  35. indexByPlatformByCategory[category.id] = indexByPlatform;
  36. category.platforms.forEach((platform: Platform, index: number) => {
  37. indexByPlatform[platform] = index;
  38. });
  39. });
  40. const getIndexOfPlatformInCategory = (
  41. category: CategoryObj['id'],
  42. platform: PlatformIntegration
  43. ) => {
  44. const indexByPlatform = indexByPlatformByCategory[category];
  45. return indexByPlatform[platform.id];
  46. };
  47. const isPopular = (platform: PlatformIntegration) =>
  48. popularPlatformCategories.includes(
  49. platform.id as typeof popularPlatformCategories[number]
  50. );
  51. const popularIndex = (platform: PlatformIntegration) =>
  52. getIndexOfPlatformInCategory('popular', platform);
  53. const PlatformList = styled('div')`
  54. display: grid;
  55. gap: ${space(1)};
  56. grid-template-columns: repeat(auto-fill, 112px);
  57. justify-content: center;
  58. margin-bottom: ${space(2)};
  59. `;
  60. interface PlatformPickerProps {
  61. addPlatform: (key: PlatformKey) => void;
  62. organization: Organization;
  63. platforms: PlatformKey[];
  64. removePlatform: (key: PlatformKey) => void;
  65. source: string;
  66. defaultCategory?: Category;
  67. listClassName?: string;
  68. listProps?: React.HTMLAttributes<HTMLDivElement>;
  69. noAutoFilter?: boolean;
  70. showOther?: boolean;
  71. }
  72. function PlatformPicker(props: PlatformPickerProps) {
  73. const {organization, source} = props;
  74. const [category, setCategory] = useState<Category>(
  75. props.defaultCategory ?? PLATFORM_CATEGORIES[0].id
  76. );
  77. const [filter, setFilter] = useState<string>(
  78. props.noAutoFilter ? '' : (props.platforms[0] || '').split('-')[0]
  79. );
  80. function getPlatformList() {
  81. const currentCategory = categoryList.find(({id}) => id === category);
  82. const filterLowerCase = filter.toLowerCase();
  83. const subsetMatch = (platform: PlatformIntegration) =>
  84. platform.id.includes(filterLowerCase) ||
  85. platform.name.toLowerCase().includes(filterLowerCase) ||
  86. filterAliases[platform.id]?.some(alias => alias.includes(filterLowerCase));
  87. const categoryMatch = (platform: PlatformIntegration) =>
  88. category === 'all' ||
  89. (currentCategory?.platforms as undefined | string[])?.includes(platform.id);
  90. const customCompares = (a: PlatformIntegration, b: PlatformIntegration) => {
  91. // the all category and serverless category both require custom sorts
  92. if (category === 'all') {
  93. return popularTopOfAllCompare(a, b);
  94. }
  95. if (category === 'serverless') {
  96. return serverlessCompare(a, b);
  97. }
  98. // maintain ordering otherwise
  99. return (
  100. getIndexOfPlatformInCategory(category, a) -
  101. getIndexOfPlatformInCategory(category, b)
  102. );
  103. };
  104. const popularTopOfAllCompare = (a: PlatformIntegration, b: PlatformIntegration) => {
  105. // for the all category, put popular ones at the top in the order they appear in the popular list
  106. if (isPopular(a) && isPopular(b)) {
  107. // if both popular, maintain ordering from popular list
  108. return popularIndex(a) - popularIndex(b);
  109. }
  110. // if one popular, that one should be first
  111. if (isPopular(a) !== isPopular(b)) {
  112. return isPopular(a) ? -1 : 1;
  113. }
  114. // since the all list is coming from a different source (platforms.json)
  115. // we can't go off the index of the item in platformCategories.tsx since there is no all list
  116. return a.id.localeCompare(b.id);
  117. };
  118. const serverlessCompare = (a: PlatformIntegration, b: PlatformIntegration) => {
  119. // for the serverless category, sort by service, then language
  120. // the format of the ids is language-service
  121. const aProvider = a.id.split('-')[1];
  122. const bProvider = b.id.split('-')[1];
  123. // if either of the ids are not hyphenated, standard sort
  124. if (!aProvider || !bProvider) {
  125. return a.id.localeCompare(b.id);
  126. }
  127. // compare the portions after the hyphen
  128. const compareServices = aProvider.localeCompare(bProvider);
  129. // if they have the same service provider
  130. if (!compareServices) {
  131. return a.id.localeCompare(b.id);
  132. }
  133. return compareServices;
  134. };
  135. const filtered = platforms
  136. .filter(filterLowerCase ? subsetMatch : categoryMatch)
  137. .sort(customCompares);
  138. return props.showOther ? filtered : filtered.filter(({id}) => id !== 'other');
  139. }
  140. const platformList = getPlatformList();
  141. const {addPlatform, removePlatform, listProps, listClassName} = props;
  142. const logSearch = debounce(() => {
  143. if (filter) {
  144. trackAdvancedAnalyticsEvent('growth.platformpicker_search', {
  145. search: filter.toLowerCase(),
  146. num_results: platformList.length,
  147. source,
  148. organization,
  149. });
  150. }
  151. }, DEFAULT_DEBOUNCE_DURATION);
  152. useEffect(logSearch, [filter, logSearch]);
  153. return (
  154. <Fragment>
  155. <NavContainer>
  156. <CategoryNav>
  157. {PLATFORM_CATEGORIES.map(({id, name}) => (
  158. <ListLink
  159. key={id}
  160. onClick={(e: React.MouseEvent) => {
  161. trackAdvancedAnalyticsEvent('growth.platformpicker_category', {
  162. category: id,
  163. source,
  164. organization,
  165. });
  166. setCategory(id);
  167. setFilter('');
  168. e.preventDefault();
  169. }}
  170. to=""
  171. isActive={() => id === (filter ? 'all' : category)}
  172. >
  173. {name}
  174. </ListLink>
  175. ))}
  176. </CategoryNav>
  177. <StyledSearchBar
  178. size="sm"
  179. query={filter}
  180. placeholder={t('Filter Platforms')}
  181. onChange={setFilter}
  182. />
  183. </NavContainer>
  184. <PlatformList className={listClassName} {...listProps}>
  185. {platformList.map(platform => (
  186. <PlatformCard
  187. data-test-id={`platform-${platform.id}`}
  188. key={platform.id}
  189. platform={platform}
  190. selected={props.platforms.includes(platform.id)}
  191. onClear={(e: React.MouseEvent) => {
  192. removePlatform(platform.id);
  193. e.stopPropagation();
  194. }}
  195. onClick={() => {
  196. // do nothing if already selected
  197. if (props.platforms.includes(platform.id)) {
  198. return;
  199. }
  200. trackAdvancedAnalyticsEvent('growth.select_platform', {
  201. platform_id: platform.id,
  202. source,
  203. organization,
  204. });
  205. addPlatform(platform.id);
  206. }}
  207. />
  208. ))}
  209. </PlatformList>
  210. {platformList.length === 0 && (
  211. <EmptyMessage
  212. icon={<IconProject size="xl" />}
  213. title={t("We don't have an SDK for that yet!")}
  214. >
  215. {tct(
  216. `Not finding your platform? You can still create your project,
  217. but looks like we don't have an official SDK for your platform
  218. yet. However, there's a rich ecosystem of community supported
  219. SDKs (including Perl, CFML, Clojure, and ActionScript). Try
  220. [search:searching for Sentry clients] or contacting support.`,
  221. {
  222. search: (
  223. <ExternalLink href="https://github.com/search?q=-org%3Agetsentry+topic%3Asentry&type=Repositories" />
  224. ),
  225. }
  226. )}
  227. </EmptyMessage>
  228. )}
  229. </Fragment>
  230. );
  231. }
  232. const NavContainer = styled('div')`
  233. margin-bottom: ${space(2)};
  234. display: flex;
  235. flex-direction: row;
  236. align-items: start;
  237. border-bottom: 1px solid ${p => p.theme.border};
  238. `;
  239. const StyledSearchBar = styled(SearchBar)`
  240. max-width: 300px;
  241. min-width: 150px;
  242. margin-top: -${space(0.25)};
  243. margin-left: auto;
  244. flex-shrink: 0;
  245. flex-basis: 0;
  246. flex-grow: 1;
  247. `;
  248. const CategoryNav = styled(NavTabs)`
  249. margin: 0;
  250. margin-top: 4px;
  251. white-space: nowrap;
  252. overflow-x: scroll;
  253. overflow-y: hidden;
  254. margin-right: ${space(1)};
  255. flex-shrink: 1;
  256. flex-grow: 0;
  257. > li {
  258. float: none;
  259. display: inline-block;
  260. }
  261. ::-webkit-scrollbar {
  262. display: none;
  263. }
  264. -ms-overflow-style: none;
  265. scrollbar-width: none;
  266. `;
  267. const StyledPlatformIcon = styled(PlatformIcon)`
  268. margin: ${space(2)};
  269. `;
  270. const ClearButton = styled(Button)`
  271. position: absolute;
  272. top: -6px;
  273. right: -6px;
  274. min-height: 0;
  275. height: 22px;
  276. width: 22px;
  277. display: flex;
  278. align-items: center;
  279. justify-content: center;
  280. border-radius: 50%;
  281. background: ${p => p.theme.background};
  282. color: ${p => p.theme.textColor};
  283. `;
  284. ClearButton.defaultProps = {
  285. icon: <IconClose isCircled size="xs" />,
  286. borderless: true,
  287. size: 'xs',
  288. };
  289. const PlatformCard = styled(({platform, selected, onClear, ...props}) => (
  290. <div {...props}>
  291. <StyledPlatformIcon
  292. platform={platform.id}
  293. size={56}
  294. radius={5}
  295. withLanguageIcon
  296. format="lg"
  297. />
  298. <h3>{platform.name}</h3>
  299. {selected && <ClearButton onClick={onClear} aria-label={t('Clear')} />}
  300. </div>
  301. ))`
  302. position: relative;
  303. display: flex;
  304. flex-direction: column;
  305. align-items: center;
  306. padding: 0 0 14px;
  307. border-radius: 4px;
  308. cursor: pointer;
  309. background: ${p => p.selected && p.theme.alert.info.backgroundLight};
  310. &:hover {
  311. background: ${p => p.theme.alert.muted.backgroundLight};
  312. }
  313. h3 {
  314. flex-grow: 1;
  315. display: flex;
  316. align-items: center;
  317. justify-content: center;
  318. width: 100%;
  319. color: ${p => (p.selected ? p.theme.textColor : p.theme.subText)};
  320. text-align: center;
  321. font-size: ${p => p.theme.fontSizeExtraSmall};
  322. text-transform: uppercase;
  323. margin: 0;
  324. padding: 0 ${space(0.5)};
  325. line-height: 1.2;
  326. }
  327. `;
  328. export default PlatformPicker;