multiPlatformPicker.tsx 10 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 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 popularTopOfAllCompare = (a: PlatformIntegration, b: PlatformIntegration) => {
  91. // for the all category, put popular ones at the top in the order they appear in the popular list
  92. if (category === 'all') {
  93. if (isPopular(a) && isPopular(b)) {
  94. // if both popular, maintain ordering from popular list
  95. return popularIndex(a) - popularIndex(b);
  96. }
  97. // if one popular, that one should be first
  98. if (isPopular(a) !== isPopular(b)) {
  99. return isPopular(a) ? -1 : 1;
  100. }
  101. // since the all list is coming from a different source (platforms.json)
  102. // we can't go off the index of the item in platformCategories.tsx since there is no all list
  103. return a.id.localeCompare(b.id);
  104. }
  105. // maintain ordering otherwise
  106. return (
  107. getIndexOfPlatformInCategory(category, a) -
  108. getIndexOfPlatformInCategory(category, b)
  109. );
  110. };
  111. const filtered = platforms
  112. .filter(filterLowerCase ? subsetMatch : categoryMatch)
  113. .sort(popularTopOfAllCompare);
  114. return props.showOther ? filtered : filtered.filter(({id}) => id !== 'other');
  115. }
  116. const platformList = getPlatformList();
  117. const {addPlatform, removePlatform, listProps, listClassName} = props;
  118. const logSearch = debounce(() => {
  119. if (filter) {
  120. trackAdvancedAnalyticsEvent('growth.platformpicker_search', {
  121. search: filter.toLowerCase(),
  122. num_results: platformList.length,
  123. source,
  124. organization,
  125. });
  126. }
  127. }, DEFAULT_DEBOUNCE_DURATION);
  128. useEffect(logSearch, [filter]);
  129. return (
  130. <Fragment>
  131. <NavContainer>
  132. <CategoryNav>
  133. {PLATFORM_CATEGORIES.map(({id, name}) => (
  134. <ListLink
  135. key={id}
  136. onClick={(e: React.MouseEvent) => {
  137. trackAdvancedAnalyticsEvent('growth.platformpicker_category', {
  138. category: id,
  139. source,
  140. organization,
  141. });
  142. setCategory(id);
  143. setFilter('');
  144. e.preventDefault();
  145. }}
  146. to=""
  147. isActive={() => id === (filter ? 'all' : category)}
  148. >
  149. {name}
  150. </ListLink>
  151. ))}
  152. </CategoryNav>
  153. <SearchBar>
  154. <IconSearch size="xs" />
  155. <input
  156. type="text"
  157. value={filter}
  158. placeholder={t('Filter Platforms')}
  159. onChange={e => {
  160. setFilter(e.target.value);
  161. }}
  162. />
  163. </SearchBar>
  164. </NavContainer>
  165. <PlatformList className={listClassName} {...listProps}>
  166. {platformList.map(platform => (
  167. <PlatformCard
  168. data-test-id={`platform-${platform.id}`}
  169. key={platform.id}
  170. platform={platform}
  171. selected={props.platforms.includes(platform.id)}
  172. onClear={(e: React.MouseEvent) => {
  173. removePlatform(platform.id);
  174. e.stopPropagation();
  175. }}
  176. onClick={() => {
  177. // do nothing if already selected
  178. if (props.platforms.includes(platform.id)) {
  179. return;
  180. }
  181. trackAdvancedAnalyticsEvent('growth.select_platform', {
  182. platform_id: platform.id,
  183. source,
  184. organization,
  185. });
  186. addPlatform(platform.id);
  187. }}
  188. />
  189. ))}
  190. </PlatformList>
  191. {platformList.length === 0 && (
  192. <EmptyMessage
  193. icon={<IconProject size="xl" />}
  194. title={t("We don't have an SDK for that yet!")}
  195. >
  196. {tct(
  197. `Not finding your platform? You can still create your project,
  198. but looks like we don't have an official SDK for your platform
  199. yet. However, there's a rich ecosystem of community supported
  200. SDKs (including Perl, CFML, Clojure, and ActionScript). Try
  201. [search:searching for Sentry clients] or contacting support.`,
  202. {
  203. search: (
  204. <ExternalLink href="https://github.com/search?q=-org%3Agetsentry+topic%3Asentry&type=Repositories" />
  205. ),
  206. }
  207. )}
  208. </EmptyMessage>
  209. )}
  210. </Fragment>
  211. );
  212. }
  213. const NavContainer = styled('div')`
  214. margin-bottom: ${space(2)};
  215. display: flex;
  216. flex-direction: row;
  217. align-items: start;
  218. border-bottom: 1px solid ${p => p.theme.border};
  219. `;
  220. const SearchBar = styled('div')`
  221. ${p => inputStyles(p)};
  222. padding: 0 8px;
  223. color: ${p => p.theme.subText};
  224. display: flex;
  225. align-items: center;
  226. font-size: 15px;
  227. margin-top: -${space(0.75)};
  228. input {
  229. border: none;
  230. background: none;
  231. padding: 2px 4px;
  232. width: 100%;
  233. /* Ensure a consistent line height to keep the input the desired height */
  234. line-height: 24px;
  235. &:focus {
  236. outline: none;
  237. }
  238. }
  239. max-width: 300px;
  240. min-width: 150px;
  241. margin-left: auto;
  242. flex-shrink: 0;
  243. flex-basis: 0;
  244. flex-grow: 1;
  245. `;
  246. const CategoryNav = styled(NavTabs)`
  247. margin: 0;
  248. margin-top: 4px;
  249. white-space: nowrap;
  250. overflow-x: scroll;
  251. overflow-y: hidden;
  252. margin-right: ${space(1)};
  253. flex-shrink: 1;
  254. flex-grow: 0;
  255. > li {
  256. float: none;
  257. display: inline-block;
  258. }
  259. ::-webkit-scrollbar {
  260. display: none;
  261. }
  262. -ms-overflow-style: none;
  263. scrollbar-width: none;
  264. `;
  265. const StyledPlatformIcon = styled(PlatformIcon)`
  266. margin: ${space(2)};
  267. `;
  268. const ClearButton = styled(Button)`
  269. position: absolute;
  270. top: -6px;
  271. right: -6px;
  272. min-height: 0;
  273. height: 22px;
  274. width: 22px;
  275. display: flex;
  276. align-items: center;
  277. justify-content: center;
  278. border-radius: 50%;
  279. background: ${p => p.theme.background};
  280. color: ${p => p.theme.textColor};
  281. `;
  282. ClearButton.defaultProps = {
  283. icon: <IconClose isCircled size="xs" />,
  284. borderless: true,
  285. size: 'xsmall',
  286. };
  287. const PlatformCard = styled(({platform, selected, onClear, ...props}) => (
  288. <div {...props}>
  289. <StyledPlatformIcon
  290. platform={platform.id}
  291. size={56}
  292. radius={5}
  293. withLanguageIcon
  294. format="lg"
  295. />
  296. <h3>{platform.name}</h3>
  297. {selected && <ClearButton onClick={onClear} aria-label={t('Clear')} />}
  298. </div>
  299. ))`
  300. position: relative;
  301. display: flex;
  302. flex-direction: column;
  303. align-items: center;
  304. padding: 0 0 14px;
  305. border-radius: 4px;
  306. cursor: pointer;
  307. background: ${p => p.selected && p.theme.alert.info.backgroundLight};
  308. &:hover {
  309. background: ${p => p.theme.alert.muted.backgroundLight};
  310. }
  311. h3 {
  312. flex-grow: 1;
  313. display: flex;
  314. align-items: center;
  315. justify-content: center;
  316. width: 100%;
  317. color: ${p => (p.selected ? p.theme.textColor : p.theme.subText)};
  318. text-align: center;
  319. font-size: ${p => p.theme.fontSizeExtraSmall};
  320. text-transform: uppercase;
  321. margin: 0;
  322. padding: 0 ${space(0.5)};
  323. line-height: 1.2;
  324. }
  325. `;
  326. export default PlatformPicker;