platform.tsx 8.7 KB

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