platform.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 {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 replayLink = `/organizations/${organization.slug}/replays/`;
  144. const showPerformancePrompt = performancePlatforms.includes(platform.id as PlatformKey);
  145. const showReplayButton = replayPlatforms.includes(platform.id as PlatformKey);
  146. const isGettingStarted = window.location.href.indexOf('getting-started') > 0;
  147. const showDocsWithProductSelection =
  148. (platformProductAvailability[platform.key] ?? []).length > 0;
  149. return (
  150. <Fragment>
  151. {!isSelfHosted && showDocsWithProductSelection && (
  152. <ProductUnavailableCTAHook organization={organization} />
  153. )}
  154. <PlatformDocHeader projectSlug={project.slug} platform={platform} />
  155. {platform.key === 'other' ? (
  156. <OtherPlatformsInfo
  157. projectSlug={project.slug}
  158. platform={platform.name ?? 'other'}
  159. />
  160. ) : showLoaderOnboarding ? (
  161. <SetupDocsLoader
  162. organization={organization}
  163. project={project}
  164. location={location}
  165. platform={currentPlatform.id}
  166. close={hideLoaderOnboarding}
  167. />
  168. ) : (
  169. <SdkDocumentation
  170. platform={currentPlatform}
  171. organization={organization}
  172. projectSlug={project.slug}
  173. projectId={project.id}
  174. activeProductSelection={products}
  175. />
  176. )}
  177. <div>
  178. {isGettingStarted && showPerformancePrompt && (
  179. <Feature
  180. features="performance-view"
  181. hookName="feature-disabled:performance-new-project"
  182. >
  183. {({hasFeature}) => {
  184. if (hasFeature) {
  185. return null;
  186. }
  187. return (
  188. <StyledAlert type="info" showIcon>
  189. {t(
  190. `Your selected platform supports performance, but your organization does not have performance enabled.`
  191. )}
  192. </StyledAlert>
  193. );
  194. }}
  195. </Feature>
  196. )}
  197. <StyledButtonBar gap={1}>
  198. <Button
  199. priority="primary"
  200. busy={loadingProjects}
  201. to={{
  202. pathname: issueStreamLink,
  203. query: {
  204. project: project?.id,
  205. },
  206. hash: '#welcome',
  207. }}
  208. >
  209. {t('Take me to Issues')}
  210. </Button>
  211. <Button
  212. busy={loadingProjects}
  213. to={{
  214. pathname: performanceOverviewLink,
  215. query: {
  216. project: project?.id,
  217. },
  218. }}
  219. >
  220. {t('Take me to Performance')}
  221. </Button>
  222. {showReplayButton && (
  223. <Button
  224. busy={loadingProjects}
  225. to={{
  226. pathname: replayLink,
  227. query: {
  228. project: project?.id,
  229. },
  230. }}
  231. >
  232. {t('Take me to Session Replay')}
  233. </Button>
  234. )}
  235. </StyledButtonBar>
  236. </div>
  237. </Fragment>
  238. );
  239. }
  240. const StyledButtonBar = styled(ButtonBar)`
  241. margin-top: ${space(3)};
  242. width: max-content;
  243. @media (max-width: ${p => p.theme.breakpoints.small}) {
  244. width: auto;
  245. grid-row-gap: ${space(1)};
  246. grid-auto-flow: row;
  247. }
  248. `;
  249. const StyledAlert = styled(Alert)`
  250. margin-top: ${space(2)};
  251. `;