platform.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 {performance as performancePlatforms} from 'sentry/data/platformCategories';
  17. import {Platform} from 'sentry/data/platformPickerCategories';
  18. import platforms from 'sentry/data/platforms';
  19. import {t} from 'sentry/locale';
  20. import ConfigStore from 'sentry/stores/configStore';
  21. import {space} from 'sentry/styles/space';
  22. import type {PlatformIntegration, PlatformKey} from 'sentry/types';
  23. import {OnboardingSelectedSDK} from 'sentry/types';
  24. import {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 {projects, initiallyLoaded} = useProjects({
  53. slugs: [params.projectId],
  54. orgId: organization.slug,
  55. });
  56. const loadingProjects = !initiallyLoaded;
  57. const project = !loadingProjects
  58. ? projects.find(proj => proj.slug === params.projectId)
  59. : undefined;
  60. const currentPlatformKey = project?.platform ?? 'other';
  61. const currentPlatform = allPlatforms.find(p => p.id === currentPlatformKey);
  62. const [showLoaderOnboarding, setShowLoaderOnboarding] = useState(
  63. currentPlatform?.id === 'javascript'
  64. );
  65. const products = useMemo(
  66. () => decodeList(location.query.product ?? []) as ProductSolution[],
  67. [location.query.product]
  68. );
  69. const {
  70. data: projectAlertRules,
  71. isLoading: projectAlertRulesIsLoading,
  72. isError: projectAlertRulesIsError,
  73. } = useApiQuery<IssueAlertRule[]>(
  74. [`/projects/${organization.slug}/${project?.slug}/rules/`],
  75. {
  76. enabled: !!project?.slug,
  77. staleTime: 0,
  78. }
  79. );
  80. useEffect(() => {
  81. setShowLoaderOnboarding(currentPlatform?.id === 'javascript');
  82. }, [currentPlatform?.id]);
  83. useEffect(() => {
  84. if (!project || projectAlertRulesIsLoading || projectAlertRulesIsError) {
  85. return;
  86. }
  87. if (gettingStartedWithProjectContext.project?.id === project.id) {
  88. return;
  89. }
  90. const platformKey = Object.keys(platforms).find(
  91. key => platforms[key].id === project.platform
  92. );
  93. if (!platformKey) {
  94. return;
  95. }
  96. gettingStartedWithProjectContext.setProject({
  97. id: project.id,
  98. name: project.name,
  99. // sometimes the team slug here can be undefined
  100. teamSlug: project.team?.slug,
  101. alertRules: projectAlertRules,
  102. platform: {
  103. ...omit(platforms[platformKey], 'id'),
  104. key: platforms[platformKey].id,
  105. } as OnboardingSelectedSDK,
  106. });
  107. }, [
  108. gettingStartedWithProjectContext,
  109. project,
  110. projectAlertRules,
  111. projectAlertRulesIsLoading,
  112. projectAlertRulesIsError,
  113. ]);
  114. const platform: Platform = {
  115. key: currentPlatformKey,
  116. id: currentPlatform?.id,
  117. name: currentPlatform?.name,
  118. link: currentPlatform?.link,
  119. };
  120. const hideLoaderOnboarding = useCallback(() => {
  121. setShowLoaderOnboarding(false);
  122. if (!project?.id || !currentPlatform) {
  123. return;
  124. }
  125. trackAnalytics('onboarding.js_loader_npm_docs_shown', {
  126. organization,
  127. platform: currentPlatform.id,
  128. project_id: project?.id,
  129. });
  130. }, [organization, currentPlatform, project?.id]);
  131. if (!project) {
  132. return null;
  133. }
  134. if (!platform.id && platform.key !== 'other') {
  135. return <NotFound />;
  136. }
  137. // because we fall back to 'other' this will always be defined
  138. if (!currentPlatform) {
  139. return null;
  140. }
  141. const issueStreamLink = `/organizations/${organization.slug}/issues/`;
  142. const performanceOverviewLink = `/organizations/${organization.slug}/performance/`;
  143. const showPerformancePrompt = performancePlatforms.includes(platform.id as PlatformKey);
  144. const isGettingStarted = window.location.href.indexOf('getting-started') > 0;
  145. const showDocsWithProductSelection =
  146. (platformProductAvailability[platform.key] ?? []).length > 0;
  147. return (
  148. <Fragment>
  149. {!isSelfHosted && showDocsWithProductSelection && (
  150. <ProductUnavailableCTAHook organization={organization} />
  151. )}
  152. <PlatformDocHeader projectSlug={project.slug} platform={platform} />
  153. {platform.key === 'other' ? (
  154. <OtherPlatformsInfo
  155. projectSlug={project.slug}
  156. platform={platform.name ?? 'other'}
  157. />
  158. ) : showLoaderOnboarding ? (
  159. <SetupDocsLoader
  160. organization={organization}
  161. project={project}
  162. location={location}
  163. platform={currentPlatform.id}
  164. close={hideLoaderOnboarding}
  165. />
  166. ) : (
  167. <SdkDocumentation
  168. platform={currentPlatform}
  169. organization={organization}
  170. projectSlug={project.slug}
  171. projectId={project.id}
  172. activeProductSelection={products}
  173. />
  174. )}
  175. <div>
  176. {isGettingStarted && showPerformancePrompt && (
  177. <Feature
  178. features="performance-view"
  179. hookName="feature-disabled:performance-new-project"
  180. >
  181. {({hasFeature}) => {
  182. if (hasFeature) {
  183. return null;
  184. }
  185. return (
  186. <StyledAlert type="info" showIcon>
  187. {t(
  188. `Your selected platform supports performance, but your organization does not have performance enabled.`
  189. )}
  190. </StyledAlert>
  191. );
  192. }}
  193. </Feature>
  194. )}
  195. <StyledButtonBar gap={1}>
  196. <Button
  197. priority="primary"
  198. busy={loadingProjects}
  199. to={{
  200. pathname: issueStreamLink,
  201. query: {
  202. project: project?.id,
  203. },
  204. hash: '#welcome',
  205. }}
  206. >
  207. {t('Take me to Issues')}
  208. </Button>
  209. <Button
  210. busy={loadingProjects}
  211. to={{
  212. pathname: performanceOverviewLink,
  213. query: {
  214. project: project?.id,
  215. },
  216. }}
  217. >
  218. {t('Take me to Performance')}
  219. </Button>
  220. </StyledButtonBar>
  221. </div>
  222. </Fragment>
  223. );
  224. }
  225. const StyledButtonBar = styled(ButtonBar)`
  226. margin-top: ${space(3)};
  227. width: max-content;
  228. @media (max-width: ${p => p.theme.breakpoints.small}) {
  229. width: auto;
  230. grid-row-gap: ${space(1)};
  231. grid-auto-flow: row;
  232. }
  233. `;
  234. const StyledAlert = styled(Alert)`
  235. margin-top: ${space(2)};
  236. `;