multiPlatformPicker.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. import * as React 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. margin-bottom: ${space(2)};
  58. `;
  59. interface PlatformPickerProps {
  60. addPlatform: (key: PlatformKey) => void;
  61. organization: Organization;
  62. platforms: PlatformKey[];
  63. removePlatform: (key: PlatformKey) => void;
  64. source: string;
  65. defaultCategory?: Category;
  66. listClassName?: string;
  67. listProps?: React.HTMLAttributes<HTMLDivElement>;
  68. noAutoFilter?: boolean;
  69. showOther?: boolean;
  70. }
  71. function PlatformPicker(props: PlatformPickerProps) {
  72. const {organization, source} = props;
  73. const [category, setCategory] = React.useState<Category>(
  74. props.defaultCategory ?? PLATFORM_CATEGORIES[0].id
  75. );
  76. const [filter, setFilter] = React.useState<string>(
  77. props.noAutoFilter ? '' : (props.platforms[0] || '').split('-')[0]
  78. );
  79. function getPlatformList() {
  80. const currentCategory = categoryList.find(({id}) => id === category);
  81. const filterLowerCase = filter.toLowerCase();
  82. const subsetMatch = (platform: PlatformIntegration) =>
  83. platform.id.includes(filterLowerCase) ||
  84. platform.name.toLowerCase().includes(filterLowerCase) ||
  85. filterAliases[platform.id]?.some(alias => alias.includes(filterLowerCase));
  86. const categoryMatch = (platform: PlatformIntegration) =>
  87. category === 'all' ||
  88. (currentCategory?.platforms as undefined | string[])?.includes(platform.id);
  89. const popularTopOfAllCompare = (a: PlatformIntegration, b: PlatformIntegration) => {
  90. // for the all category, put popular ones at the top in the order they appear in the popular list
  91. if (category === 'all') {
  92. if (isPopular(a) && isPopular(b)) {
  93. // if both popular, maintain ordering from popular list
  94. return popularIndex(a) - popularIndex(b);
  95. }
  96. // if one popular, that one shhould be first
  97. if (isPopular(a) !== isPopular(b)) {
  98. return isPopular(a) ? -1 : 1;
  99. }
  100. // since the all list is coming from a different source (platforms.json)
  101. // we can't go off the index of the item in platformCategories.tsx since there is no all list
  102. return a.id.localeCompare(b.id);
  103. }
  104. // maintain ordering otherwise
  105. return (
  106. getIndexOfPlatformInCategory(category, a) -
  107. getIndexOfPlatformInCategory(category, b)
  108. );
  109. };
  110. const filtered = platforms
  111. .filter(filterLowerCase ? subsetMatch : categoryMatch)
  112. .sort(popularTopOfAllCompare);
  113. return props.showOther ? filtered : filtered.filter(({id}) => id !== 'other');
  114. }
  115. const platformList = getPlatformList();
  116. const {addPlatform, removePlatform, listProps, listClassName} = props;
  117. const logSearch = debounce(() => {
  118. if (filter) {
  119. trackAdvancedAnalyticsEvent('growth.platformpicker_search', {
  120. search: filter.toLowerCase(),
  121. num_results: platformList.length,
  122. source,
  123. organization,
  124. });
  125. }
  126. }, DEFAULT_DEBOUNCE_DURATION);
  127. React.useEffect(logSearch, [filter]);
  128. return (
  129. <React.Fragment>
  130. <NavContainer>
  131. <CategoryNav>
  132. {PLATFORM_CATEGORIES.map(({id, name}) => (
  133. <ListLink
  134. key={id}
  135. onClick={(e: React.MouseEvent) => {
  136. trackAdvancedAnalyticsEvent('growth.platformpicker_category', {
  137. category: id,
  138. source,
  139. organization,
  140. });
  141. setCategory(id);
  142. setFilter('');
  143. e.preventDefault();
  144. }}
  145. to=""
  146. isActive={() => id === (filter ? 'all' : category)}
  147. >
  148. {name}
  149. </ListLink>
  150. ))}
  151. </CategoryNav>
  152. <SearchBar>
  153. <IconSearch size="xs" />
  154. <input
  155. type="text"
  156. value={filter}
  157. placeholder={t('Filter Platforms')}
  158. onChange={e => {
  159. setFilter(e.target.value);
  160. }}
  161. />
  162. </SearchBar>
  163. </NavContainer>
  164. <PlatformList className={listClassName} {...listProps}>
  165. {platformList.map(platform => (
  166. <PlatformCard
  167. data-test-id={`platform-${platform.id}`}
  168. key={platform.id}
  169. platform={platform}
  170. selected={props.platforms.includes(platform.id)}
  171. onClear={(e: React.MouseEvent) => {
  172. removePlatform(platform.id);
  173. e.stopPropagation();
  174. }}
  175. onClick={() => {
  176. // do nothing if already selected
  177. if (props.platforms.includes(platform.id)) {
  178. return;
  179. }
  180. trackAdvancedAnalyticsEvent('growth.select_platform', {
  181. platform_id: platform.id,
  182. source,
  183. organization,
  184. });
  185. addPlatform(platform.id);
  186. }}
  187. />
  188. ))}
  189. </PlatformList>
  190. {platformList.length === 0 && (
  191. <EmptyMessage
  192. icon={<IconProject size="xl" />}
  193. title={t("We don't have an SDK for that yet!")}
  194. >
  195. {tct(
  196. `Not finding your platform? You can still create your project,
  197. but looks like we don't have an official SDK for your platform
  198. yet. However, there's a rich ecosystem of community supported
  199. SDKs (including Perl, CFML, Clojure, and ActionScript). Try
  200. [search:searching for Sentry clients] or contacting support.`,
  201. {
  202. search: (
  203. <ExternalLink href="https://github.com/search?q=-org%3Agetsentry+topic%3Asentry&type=Repositories" />
  204. ),
  205. }
  206. )}
  207. </EmptyMessage>
  208. )}
  209. </React.Fragment>
  210. );
  211. }
  212. const NavContainer = styled('div')`
  213. margin-bottom: ${space(2)};
  214. display: grid;
  215. gap: ${space(2)};
  216. grid-template-columns: 1fr minmax(0, 300px);
  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. `;
  240. const CategoryNav = styled(NavTabs)`
  241. margin: 0;
  242. margin-top: 4px;
  243. white-space: nowrap;
  244. > li {
  245. float: none;
  246. display: inline-block;
  247. }
  248. `;
  249. const StyledPlatformIcon = styled(PlatformIcon)`
  250. margin: ${space(2)};
  251. `;
  252. const ClearButton = styled(Button)`
  253. position: absolute;
  254. top: -6px;
  255. right: -6px;
  256. min-height: 0;
  257. height: 22px;
  258. width: 22px;
  259. display: flex;
  260. align-items: center;
  261. justify-content: center;
  262. border-radius: 50%;
  263. background: ${p => p.theme.background};
  264. color: ${p => p.theme.textColor};
  265. `;
  266. ClearButton.defaultProps = {
  267. icon: <IconClose isCircled size="xs" />,
  268. borderless: true,
  269. size: 'xsmall',
  270. };
  271. const PlatformCard = styled(({platform, selected, onClear, ...props}) => (
  272. <div {...props}>
  273. <StyledPlatformIcon
  274. platform={platform.id}
  275. size={56}
  276. radius={5}
  277. withLanguageIcon
  278. format="lg"
  279. />
  280. <h3>{platform.name}</h3>
  281. {selected && <ClearButton onClick={onClear} aria-label={t('Clear')} />}
  282. </div>
  283. ))`
  284. position: relative;
  285. display: flex;
  286. flex-direction: column;
  287. align-items: center;
  288. padding: 0 0 14px;
  289. border-radius: 4px;
  290. cursor: pointer;
  291. background: ${p => p.selected && p.theme.alert.info.backgroundLight};
  292. &:hover {
  293. background: ${p => p.theme.alert.muted.backgroundLight};
  294. }
  295. h3 {
  296. flex-grow: 1;
  297. display: flex;
  298. align-items: center;
  299. justify-content: center;
  300. width: 100%;
  301. color: ${p => (p.selected ? p.theme.textColor : p.theme.subText)};
  302. text-align: center;
  303. font-size: ${p => p.theme.fontSizeExtraSmall};
  304. text-transform: uppercase;
  305. margin: 0;
  306. padding: 0 ${space(0.5)};
  307. line-height: 1.2;
  308. }
  309. `;
  310. export default PlatformPicker;