platform.tsx 8.7 KB

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