platformPicker.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import {useEffect, useMemo, useCallback, 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 SearchBar from 'sentry/components/searchBar';
  9. import {Item, TabList, TabPanels, Tabs} from 'sentry/components/tabs';
  10. import {DEFAULT_DEBOUNCE_DURATION} from 'sentry/constants';
  11. import categoryList, {filterAliases, PlatformKey} from 'sentry/data/platformCategories';
  12. import platforms from 'sentry/data/platforms';
  13. import {IconClose, IconProject} from 'sentry/icons';
  14. import {t, tct} from 'sentry/locale';
  15. import space from 'sentry/styles/space';
  16. import {Organization, PlatformIntegration} from 'sentry/types';
  17. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  18. const PLATFORM_CATEGORIES = [...categoryList, {id: 'all', name: t('All')}] as const;
  19. type Category = typeof PLATFORM_CATEGORIES[number]['id'];
  20. interface PlatformPickerProps {
  21. setPlatform: (key: PlatformKey | null) => void;
  22. defaultCategory?: Category;
  23. listClassName?: string;
  24. listProps?: React.HTMLAttributes<HTMLDivElement>;
  25. noAutoFilter?: boolean;
  26. organization?: Organization;
  27. platform?: string | null;
  28. showOther?: boolean;
  29. source?: string;
  30. }
  31. function PlatformPicker({
  32. platform,
  33. setPlatform,
  34. listProps,
  35. listClassName,
  36. defaultCategory,
  37. noAutoFilter,
  38. showOther = true,
  39. source,
  40. organization,
  41. }: PlatformPickerProps) {
  42. const [category, setCategory] = useState<Category>(
  43. defaultCategory ?? PLATFORM_CATEGORIES[0].id
  44. );
  45. const [filter, setFilter] = useState(
  46. noAutoFilter ? '' : (platform ?? '').split('-')[0]
  47. );
  48. const getPlatformList = useCallback(
  49. (cat: Category) => {
  50. const currentCategory = categoryList.find(({id}) => id === cat);
  51. const lowercaseFilter = filter.toLowerCase();
  52. const subsetMatch = (p: PlatformIntegration) =>
  53. p.id.includes(lowercaseFilter) ||
  54. p.name.toLowerCase().includes(lowercaseFilter) ||
  55. filterAliases[p.id as PlatformKey]?.some(alias =>
  56. alias.includes(lowercaseFilter)
  57. );
  58. const categoryMatch = (p: PlatformIntegration) =>
  59. category === 'all' ||
  60. (currentCategory?.platforms as undefined | string[])?.includes(p.id);
  61. const filtered = platforms
  62. .filter(filter ? subsetMatch : categoryMatch)
  63. .sort((a, b) => a.id.localeCompare(b.id));
  64. return showOther ? filtered : filtered.filter(({id}) => id !== 'other');
  65. },
  66. [category, filter, showOther]
  67. );
  68. const logSearch = useMemo(
  69. () =>
  70. debounce((search: string, num_results: number) => {
  71. if (!search) {
  72. return;
  73. }
  74. trackAdvancedAnalyticsEvent('growth.platformpicker_search', {
  75. search,
  76. num_results,
  77. source,
  78. organization: organization ?? null,
  79. });
  80. }, DEFAULT_DEBOUNCE_DURATION),
  81. [organization, source]
  82. );
  83. useEffect(() => {
  84. logSearch(filter.toLowerCase(), getPlatformList(category).length);
  85. }, [filter, logSearch, category, getPlatformList]);
  86. return (
  87. <Tabs
  88. onChange={tab => {
  89. trackAdvancedAnalyticsEvent('growth.platformpicker_category', {
  90. category: tab,
  91. source,
  92. organization: organization ?? null,
  93. });
  94. setCategory(tab);
  95. setFilter('');
  96. }}
  97. value={filter ? 'all' : category}
  98. >
  99. <NavContainer>
  100. <TabList hideBorder>
  101. {PLATFORM_CATEGORIES.map(({id, name}) => (
  102. <Item key={id}>{name}</Item>
  103. ))}
  104. </TabList>
  105. <StyledSearchBar
  106. size="sm"
  107. query={filter}
  108. placeholder={t('Filter Platforms')}
  109. onChange={setFilter}
  110. />
  111. </NavContainer>
  112. <TabPanels>
  113. {PLATFORM_CATEGORIES.map(({id}) => {
  114. const platformList = getPlatformList(id);
  115. return (
  116. <Item key={id}>
  117. {platformList.length > 0 ? (
  118. <PlatformList className={listClassName} {...listProps}>
  119. {platformList.map(platformItem => (
  120. <PlatformCard
  121. data-test-id={`platform-${platformItem.id}`}
  122. key={platformItem.id}
  123. platform={platformItem}
  124. selected={platform === platformItem.id}
  125. onClear={(e: React.MouseEvent) => {
  126. setPlatform(null);
  127. e.stopPropagation();
  128. }}
  129. onClick={() => {
  130. trackAdvancedAnalyticsEvent('growth.select_platform', {
  131. platform_id: platformItem.id,
  132. source,
  133. organization: organization ?? null,
  134. });
  135. setPlatform(platformItem.id as PlatformKey);
  136. }}
  137. />
  138. ))}{' '}
  139. </PlatformList>
  140. ) : (
  141. <EmptyMessage
  142. icon={<IconProject size="xl" />}
  143. title={t("We don't have an SDK for that yet!")}
  144. >
  145. {tct(
  146. `Not finding your platform? You can still create your project,
  147. but looks like we don't have an official SDK for your platform
  148. yet. However, there's a rich ecosystem of community supported
  149. SDKs (including Perl, CFML, Clojure, and ActionScript). Try
  150. [search:searching for Sentry clients] or contacting support.`,
  151. {
  152. search: (
  153. <ExternalLink href="https://github.com/search?q=-org%3Agetsentry+topic%3Asentry&type=Repositories" />
  154. ),
  155. }
  156. )}
  157. </EmptyMessage>
  158. )}
  159. </Item>
  160. );
  161. })}
  162. </TabPanels>
  163. </Tabs>
  164. );
  165. }
  166. const NavContainer = styled('div')`
  167. margin-bottom: ${space(2)};
  168. display: grid;
  169. gap: ${space(2)};
  170. grid-template-columns: minmax(0, 1fr) max-content;
  171. align-items: start;
  172. border-bottom: 1px solid ${p => p.theme.border};
  173. `;
  174. const StyledSearchBar = styled(SearchBar)`
  175. width: 12rem;
  176. margin-top: -${space(0.25)};
  177. @media only screen and (max-width: ${p => p.theme.breakpoints.small}) {
  178. width: 10em;
  179. }
  180. `;
  181. const PlatformList = styled('div')`
  182. display: grid;
  183. gap: ${space(1)};
  184. grid-template-columns: repeat(auto-fill, 112px);
  185. margin-bottom: ${space(2)};
  186. `;
  187. const StyledPlatformIcon = styled(PlatformIcon)`
  188. margin: ${space(2)};
  189. `;
  190. const ClearButton = styled(Button)`
  191. position: absolute;
  192. top: -6px;
  193. right: -6px;
  194. min-height: 0;
  195. height: 22px;
  196. width: 22px;
  197. display: flex;
  198. align-items: center;
  199. justify-content: center;
  200. border-radius: 50%;
  201. background: ${p => p.theme.background};
  202. color: ${p => p.theme.textColor};
  203. `;
  204. ClearButton.defaultProps = {
  205. icon: <IconClose isCircled size="xs" />,
  206. borderless: true,
  207. size: 'xs',
  208. };
  209. const PlatformCard = styled(({platform, selected, onClear, ...props}) => (
  210. <div {...props}>
  211. <StyledPlatformIcon
  212. platform={platform.id}
  213. size={56}
  214. radius={5}
  215. withLanguageIcon
  216. format="lg"
  217. />
  218. <h3>{platform.name}</h3>
  219. {selected && <ClearButton onClick={onClear} aria-label={t('Clear')} />}
  220. </div>
  221. ))`
  222. position: relative;
  223. display: flex;
  224. flex-direction: column;
  225. align-items: center;
  226. padding: 0 0 14px;
  227. border-radius: 4px;
  228. cursor: pointer;
  229. background: ${p => p.selected && p.theme.alert.info.backgroundLight};
  230. &:hover {
  231. background: ${p => p.theme.alert.muted.backgroundLight};
  232. }
  233. h3 {
  234. flex-grow: 1;
  235. display: flex;
  236. align-items: center;
  237. justify-content: center;
  238. width: 100%;
  239. color: ${p => (p.selected ? p.theme.textColor : p.theme.subText)};
  240. text-align: center;
  241. font-size: ${p => p.theme.fontSizeExtraSmall};
  242. text-transform: uppercase;
  243. margin: 0;
  244. padding: 0 ${space(0.5)};
  245. line-height: 1.2;
  246. }
  247. `;
  248. export default PlatformPicker;