multiPlatformPicker.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 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, {
  11. filterAliases,
  12. PlatformKey,
  13. popularPlatformCategories,
  14. } from 'sentry/data/platformCategories';
  15. import platforms from 'sentry/data/platforms';
  16. import {IconClose, IconProject, IconSearch} from 'sentry/icons';
  17. import {t, tct} from 'sentry/locale';
  18. import {inputStyles} from 'sentry/styles/input';
  19. import space from 'sentry/styles/space';
  20. import {Organization, PlatformIntegration} from 'sentry/types';
  21. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  22. import EmptyMessage from 'sentry/views/settings/components/emptyMessage';
  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. <SearchBar>
  178. <IconSearch size="xs" />
  179. <input
  180. type="text"
  181. value={filter}
  182. placeholder={t('Filter Platforms')}
  183. onChange={e => {
  184. setFilter(e.target.value);
  185. }}
  186. />
  187. </SearchBar>
  188. </NavContainer>
  189. <PlatformList className={listClassName} {...listProps}>
  190. {platformList.map(platform => (
  191. <PlatformCard
  192. data-test-id={`platform-${platform.id}`}
  193. key={platform.id}
  194. platform={platform}
  195. selected={props.platforms.includes(platform.id)}
  196. onClear={(e: React.MouseEvent) => {
  197. removePlatform(platform.id);
  198. e.stopPropagation();
  199. }}
  200. onClick={() => {
  201. // do nothing if already selected
  202. if (props.platforms.includes(platform.id)) {
  203. return;
  204. }
  205. trackAdvancedAnalyticsEvent('growth.select_platform', {
  206. platform_id: platform.id,
  207. source,
  208. organization,
  209. });
  210. addPlatform(platform.id);
  211. }}
  212. />
  213. ))}
  214. </PlatformList>
  215. {platformList.length === 0 && (
  216. <EmptyMessage
  217. icon={<IconProject size="xl" />}
  218. title={t("We don't have an SDK for that yet!")}
  219. >
  220. {tct(
  221. `Not finding your platform? You can still create your project,
  222. but looks like we don't have an official SDK for your platform
  223. yet. However, there's a rich ecosystem of community supported
  224. SDKs (including Perl, CFML, Clojure, and ActionScript). Try
  225. [search:searching for Sentry clients] or contacting support.`,
  226. {
  227. search: (
  228. <ExternalLink href="https://github.com/search?q=-org%3Agetsentry+topic%3Asentry&type=Repositories" />
  229. ),
  230. }
  231. )}
  232. </EmptyMessage>
  233. )}
  234. </Fragment>
  235. );
  236. }
  237. const NavContainer = styled('div')`
  238. margin-bottom: ${space(2)};
  239. display: flex;
  240. flex-direction: row;
  241. align-items: start;
  242. border-bottom: 1px solid ${p => p.theme.border};
  243. `;
  244. const SearchBar = styled('div')`
  245. ${p => inputStyles(p)};
  246. padding: 0 8px;
  247. color: ${p => p.theme.subText};
  248. display: flex;
  249. align-items: center;
  250. font-size: 15px;
  251. margin-top: -${space(0.75)};
  252. input {
  253. border: none;
  254. background: none;
  255. padding: 2px 4px;
  256. width: 100%;
  257. /* Ensure a consistent line height to keep the input the desired height */
  258. line-height: 24px;
  259. &:focus {
  260. outline: none;
  261. }
  262. }
  263. max-width: 300px;
  264. min-width: 150px;
  265. margin-left: auto;
  266. flex-shrink: 0;
  267. flex-basis: 0;
  268. flex-grow: 1;
  269. `;
  270. const CategoryNav = styled(NavTabs)`
  271. margin: 0;
  272. margin-top: 4px;
  273. white-space: nowrap;
  274. overflow-x: scroll;
  275. overflow-y: hidden;
  276. margin-right: ${space(1)};
  277. flex-shrink: 1;
  278. flex-grow: 0;
  279. > li {
  280. float: none;
  281. display: inline-block;
  282. }
  283. ::-webkit-scrollbar {
  284. display: none;
  285. }
  286. -ms-overflow-style: none;
  287. scrollbar-width: none;
  288. `;
  289. const StyledPlatformIcon = styled(PlatformIcon)`
  290. margin: ${space(2)};
  291. `;
  292. const ClearButton = styled(Button)`
  293. position: absolute;
  294. top: -6px;
  295. right: -6px;
  296. min-height: 0;
  297. height: 22px;
  298. width: 22px;
  299. display: flex;
  300. align-items: center;
  301. justify-content: center;
  302. border-radius: 50%;
  303. background: ${p => p.theme.background};
  304. color: ${p => p.theme.textColor};
  305. `;
  306. ClearButton.defaultProps = {
  307. icon: <IconClose isCircled size="xs" />,
  308. borderless: true,
  309. size: 'xs',
  310. };
  311. const PlatformCard = styled(({platform, selected, onClear, ...props}) => (
  312. <div {...props}>
  313. <StyledPlatformIcon
  314. platform={platform.id}
  315. size={56}
  316. radius={5}
  317. withLanguageIcon
  318. format="lg"
  319. />
  320. <h3>{platform.name}</h3>
  321. {selected && <ClearButton onClick={onClear} aria-label={t('Clear')} />}
  322. </div>
  323. ))`
  324. position: relative;
  325. display: flex;
  326. flex-direction: column;
  327. align-items: center;
  328. padding: 0 0 14px;
  329. border-radius: 4px;
  330. cursor: pointer;
  331. background: ${p => p.selected && p.theme.alert.info.backgroundLight};
  332. &:hover {
  333. background: ${p => p.theme.alert.muted.backgroundLight};
  334. }
  335. h3 {
  336. flex-grow: 1;
  337. display: flex;
  338. align-items: center;
  339. justify-content: center;
  340. width: 100%;
  341. color: ${p => (p.selected ? p.theme.textColor : p.theme.subText)};
  342. text-align: center;
  343. font-size: ${p => p.theme.fontSizeExtraSmall};
  344. text-transform: uppercase;
  345. margin: 0;
  346. padding: 0 ${space(0.5)};
  347. line-height: 1.2;
  348. }
  349. `;
  350. export default PlatformPicker;