platform.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import {Fragment, useCallback, useContext, useEffect, useMemo, useState} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import omit from 'lodash/omit';
  5. import Feature from 'sentry/components/acl/feature';
  6. import {Alert} from 'sentry/components/alert';
  7. import {Button} from 'sentry/components/button';
  8. import ButtonBar from 'sentry/components/buttonBar';
  9. import NotFound from 'sentry/components/errors/notFound';
  10. import HookOrDefault from 'sentry/components/hookOrDefault';
  11. import {SdkDocumentation} from 'sentry/components/onboarding/gettingStartedDoc/sdkDocumentation';
  12. import {
  13. platformProductAvailability,
  14. ProductSolution,
  15. } from 'sentry/components/onboarding/productSelection';
  16. import {
  17. performance as performancePlatforms,
  18. replayPlatforms,
  19. } from 'sentry/data/platformCategories';
  20. import {Platform} from 'sentry/data/platformPickerCategories';
  21. import platforms from 'sentry/data/platforms';
  22. import {t} from 'sentry/locale';
  23. import ConfigStore from 'sentry/stores/configStore';
  24. import {space} from 'sentry/styles/space';
  25. import type {PlatformIntegration, PlatformKey} from 'sentry/types';
  26. import {OnboardingSelectedSDK} from 'sentry/types';
  27. import {IssueAlertRule} from 'sentry/types/alerts';
  28. import {trackAnalytics} from 'sentry/utils/analytics';
  29. import {useApiQuery} from 'sentry/utils/queryClient';
  30. import {decodeList} from 'sentry/utils/queryString';
  31. import useOrganization from 'sentry/utils/useOrganization';
  32. import useProjects from 'sentry/utils/useProjects';
  33. import {SetupDocsLoader} from 'sentry/views/onboarding/setupDocsLoader';
  34. import {GettingStartedWithProjectContext} from 'sentry/views/projects/gettingStartedWithProjectContext';
  35. import {OtherPlatformsInfo} from './otherPlatformsInfo';
  36. import {PlatformDocHeader} from './platformDocHeader';
  37. const allPlatforms: PlatformIntegration[] = [
  38. ...platforms,
  39. {
  40. id: 'other',
  41. name: t('Other'),
  42. link: 'https://docs.sentry.io/platforms/',
  43. type: 'language',
  44. language: 'other',
  45. },
  46. ];
  47. const ProductUnavailableCTAHook = HookOrDefault({
  48. hookName: 'component:product-unavailable-cta',
  49. });
  50. type Props = RouteComponentProps<{projectId: string}, {}>;
  51. export function ProjectInstallPlatform({location, params}: Props) {
  52. const organization = useOrganization();
  53. const gettingStartedWithProjectContext = useContext(GettingStartedWithProjectContext);
  54. const isSelfHosted = ConfigStore.get('isSelfHosted');
  55. const {projects, initiallyLoaded} = useProjects({
  56. slugs: [params.projectId],
  57. orgId: organization.slug,
  58. });
  59. const loadingProjects = !initiallyLoaded;
  60. const project = !loadingProjects
  61. ? projects.find(proj => proj.slug === params.projectId)
  62. : undefined;
  63. const currentPlatformKey = project?.platform ?? 'other';
  64. const currentPlatform = allPlatforms.find(p => p.id === currentPlatformKey);
  65. const [showLoaderOnboarding, setShowLoaderOnboarding] = useState(
  66. currentPlatform?.id === 'javascript'
  67. );
  68. const products = useMemo(
  69. () => decodeList(location.query.product ?? []) as ProductSolution[],
  70. [location.query.product]
  71. );
  72. const {
  73. data: projectAlertRules,
  74. isLoading: projectAlertRulesIsLoading,
  75. isError: projectAlertRulesIsError,
  76. } = useApiQuery<IssueAlertRule[]>(
  77. [`/projects/${organization.slug}/${project?.slug}/rules/`],
  78. {
  79. enabled: !!project?.slug,
  80. staleTime: 0,
  81. }
  82. );
  83. useEffect(() => {
  84. setShowLoaderOnboarding(currentPlatform?.id === 'javascript');
  85. }, [currentPlatform?.id]);
  86. useEffect(() => {
  87. if (!project || projectAlertRulesIsLoading || projectAlertRulesIsError) {
  88. return;
  89. }
  90. if (gettingStartedWithProjectContext.project?.id === project.id) {
  91. return;
  92. }
  93. const platformKey = Object.keys(platforms).find(
  94. key => platforms[key].id === project.platform
  95. );
  96. if (!platformKey) {
  97. return;
  98. }
  99. gettingStartedWithProjectContext.setProject({
  100. id: project.id,
  101. name: project.name,
  102. // sometimes the team slug here can be undefined
  103. teamSlug: project.team?.slug,
  104. alertRules: projectAlertRules,
  105. platform: {
  106. ...omit(platforms[platformKey], 'id'),
  107. key: platforms[platformKey].id,
  108. } as OnboardingSelectedSDK,
  109. });
  110. }, [
  111. gettingStartedWithProjectContext,
  112. project,
  113. projectAlertRules,
  114. projectAlertRulesIsLoading,
  115. projectAlertRulesIsError,
  116. ]);
  117. const platform: Platform = {
  118. key: currentPlatformKey,
  119. id: currentPlatform?.id,
  120. name: currentPlatform?.name,
  121. link: currentPlatform?.link,
  122. };
  123. const hideLoaderOnboarding = useCallback(() => {
  124. setShowLoaderOnboarding(false);
  125. if (!project?.id || !currentPlatform) {
  126. return;
  127. }
  128. trackAnalytics('onboarding.js_loader_npm_docs_shown', {
  129. organization,
  130. platform: currentPlatform.id,
  131. project_id: project?.id,
  132. });
  133. }, [organization, currentPlatform, project?.id]);
  134. if (!project) {
  135. return null;
  136. }
  137. if (!platform.id && platform.key !== 'other') {
  138. return <NotFound />;
  139. }
  140. // because we fall back to 'other' this will always be defined
  141. if (!currentPlatform) {
  142. return null;
  143. }
  144. const issueStreamLink = `/organizations/${organization.slug}/issues/`;
  145. const performanceOverviewLink = `/organizations/${organization.slug}/performance/`;
  146. const replayLink = `/organizations/${organization.slug}/replays/`;
  147. const showPerformancePrompt = performancePlatforms.includes(platform.id as PlatformKey);
  148. const showReplayButton = replayPlatforms.includes(platform.id as PlatformKey);
  149. const isGettingStarted = window.location.href.indexOf('getting-started') > 0;
  150. const showDocsWithProductSelection =
  151. (platformProductAvailability[platform.key] ?? []).length > 0;
  152. return (
  153. <Fragment>
  154. {!isSelfHosted && showDocsWithProductSelection && (
  155. <ProductUnavailableCTAHook organization={organization} />
  156. )}
  157. <PlatformDocHeader projectSlug={project.slug} platform={platform} />
  158. {platform.key === 'other' ? (
  159. <OtherPlatformsInfo
  160. projectSlug={project.slug}
  161. platform={platform.name ?? 'other'}
  162. />
  163. ) : showLoaderOnboarding ? (
  164. <SetupDocsLoader
  165. organization={organization}
  166. project={project}
  167. location={location}
  168. platform={currentPlatform.id}
  169. close={hideLoaderOnboarding}
  170. />
  171. ) : (
  172. <SdkDocumentation
  173. platform={currentPlatform}
  174. organization={organization}
  175. projectSlug={project.slug}
  176. projectId={project.id}
  177. activeProductSelection={products}
  178. />
  179. )}
  180. <div>
  181. {isGettingStarted && showPerformancePrompt && (
  182. <Feature
  183. features="performance-view"
  184. hookName="feature-disabled:performance-new-project"
  185. >
  186. {({hasFeature}) => {
  187. if (hasFeature) {
  188. return null;
  189. }
  190. return (
  191. <StyledAlert type="info" showIcon>
  192. {t(
  193. `Your selected platform supports performance, but your organization does not have performance enabled.`
  194. )}
  195. </StyledAlert>
  196. );
  197. }}
  198. </Feature>
  199. )}
  200. <StyledButtonBar gap={1}>
  201. <Button
  202. priority="primary"
  203. busy={loadingProjects}
  204. to={{
  205. pathname: issueStreamLink,
  206. query: {
  207. project: project?.id,
  208. },
  209. hash: '#welcome',
  210. }}
  211. >
  212. {t('Take me to Issues')}
  213. </Button>
  214. <Button
  215. busy={loadingProjects}
  216. to={{
  217. pathname: performanceOverviewLink,
  218. query: {
  219. project: project?.id,
  220. },
  221. }}
  222. >
  223. {t('Take me to Performance')}
  224. </Button>
  225. {showReplayButton && (
  226. <Button
  227. busy={loadingProjects}
  228. to={{
  229. pathname: replayLink,
  230. query: {
  231. project: project?.id,
  232. },
  233. }}
  234. >
  235. {t('Take me to Session Replay')}
  236. </Button>
  237. )}
  238. </StyledButtonBar>
  239. </div>
  240. </Fragment>
  241. );
  242. }
  243. const StyledButtonBar = styled(ButtonBar)`
  244. margin-top: ${space(3)};
  245. width: max-content;
  246. @media (max-width: ${p => p.theme.breakpoints.small}) {
  247. width: auto;
  248. grid-row-gap: ${space(1)};
  249. grid-auto-flow: row;
  250. }
  251. `;
  252. const StyledAlert = styled(Alert)`
  253. margin-top: ${space(2)};
  254. `;