platform.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. Platform,
  19. } from 'sentry/data/platformCategories';
  20. import platforms from 'sentry/data/platforms';
  21. import {t} from 'sentry/locale';
  22. import ConfigStore from 'sentry/stores/configStore';
  23. import {space} from 'sentry/styles/space';
  24. import type {PlatformIntegration, PlatformKey} from 'sentry/types';
  25. import {OnboardingSelectedSDK} from 'sentry/types';
  26. import {IssueAlertRule} from 'sentry/types/alerts';
  27. import {trackAnalytics} from 'sentry/utils/analytics';
  28. import {useApiQuery} from 'sentry/utils/queryClient';
  29. import {decodeList} from 'sentry/utils/queryString';
  30. import useOrganization from 'sentry/utils/useOrganization';
  31. import useProjects from 'sentry/utils/useProjects';
  32. import {SetupDocsLoader} from 'sentry/views/onboarding/setupDocsLoader';
  33. import {GettingStartedWithProjectContext} from 'sentry/views/projects/gettingStartedWithProjectContext';
  34. import {OtherPlatformsInfo} from './otherPlatformsInfo';
  35. import {PlatformDocHeader} from './platformDocHeader';
  36. const allPlatforms: PlatformIntegration[] = [
  37. ...platforms,
  38. {
  39. id: 'other',
  40. name: t('Other'),
  41. link: 'https://docs.sentry.io/platforms/',
  42. type: 'language',
  43. language: 'other',
  44. },
  45. ];
  46. const ProductUnavailableCTAHook = HookOrDefault({
  47. hookName: 'component:product-unavailable-cta',
  48. });
  49. type Props = RouteComponentProps<{projectId: string}, {}>;
  50. export function ProjectInstallPlatform({location, params}: Props) {
  51. const organization = useOrganization();
  52. const gettingStartedWithProjectContext = useContext(GettingStartedWithProjectContext);
  53. const isSelfHosted = ConfigStore.get('isSelfHosted');
  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. // This is a feature flag that is currently only enabled for a subset of internal users until the feature is fully implemented,
  117. // but the purpose of the feature is to make the product selection feature in documents available to all users
  118. // and guide them to upgrade to a plan if one of the products is not available on their current plan.
  119. const gettingStartedDocWithProductSelection = !!organization?.features.includes(
  120. 'getting-started-doc-with-product-selection'
  121. );
  122. const platform: Platform = {
  123. key: currentPlatformKey as PlatformKey,
  124. id: currentPlatform?.id,
  125. name: currentPlatform?.name,
  126. link: currentPlatform?.link,
  127. };
  128. const hideLoaderOnboarding = useCallback(() => {
  129. setShowLoaderOnboarding(false);
  130. if (!project?.id || !currentPlatform) {
  131. return;
  132. }
  133. trackAnalytics('onboarding.js_loader_npm_docs_shown', {
  134. organization,
  135. platform: currentPlatform.id,
  136. project_id: project?.id,
  137. });
  138. }, [organization, currentPlatform, project?.id]);
  139. if (!project) {
  140. return null;
  141. }
  142. if (!platform.id && platform.key !== 'other') {
  143. return <NotFound />;
  144. }
  145. // because we fall back to 'other' this will always be defined
  146. if (!currentPlatform) {
  147. return null;
  148. }
  149. const issueStreamLink = `/organizations/${organization.slug}/issues/`;
  150. const performanceOverviewLink = `/organizations/${organization.slug}/performance/`;
  151. const showPerformancePrompt = performancePlatforms.includes(platform.id as PlatformKey);
  152. const isGettingStarted = window.location.href.indexOf('getting-started') > 0;
  153. const showDocsWithProductSelection =
  154. gettingStartedDocWithProductSelection &&
  155. (platformProductAvailability[platform.key] ?? []).length > 0;
  156. return (
  157. <Fragment>
  158. {!isSelfHosted && showDocsWithProductSelection && (
  159. <ProductUnavailableCTAHook organization={organization} />
  160. )}
  161. <PlatformDocHeader projectSlug={project.slug} platform={platform} />
  162. {platform.key === 'other' ? (
  163. <OtherPlatformsInfo
  164. projectSlug={project.slug}
  165. platform={platform.name ?? 'other'}
  166. />
  167. ) : showLoaderOnboarding ? (
  168. <SetupDocsLoader
  169. organization={organization}
  170. project={project}
  171. location={location}
  172. platform={currentPlatform.id}
  173. close={hideLoaderOnboarding}
  174. />
  175. ) : (
  176. <SdkDocumentation
  177. platform={currentPlatform}
  178. organization={organization}
  179. projectSlug={project.slug}
  180. projectId={project.id}
  181. activeProductSelection={products}
  182. />
  183. )}
  184. <div>
  185. {isGettingStarted && showPerformancePrompt && (
  186. <Feature
  187. features={['performance-view']}
  188. hookName="feature-disabled:performance-new-project"
  189. >
  190. {({hasFeature}) => {
  191. if (hasFeature) {
  192. return null;
  193. }
  194. return (
  195. <StyledAlert type="info" showIcon>
  196. {t(
  197. `Your selected platform supports performance, but your organization does not have performance enabled.`
  198. )}
  199. </StyledAlert>
  200. );
  201. }}
  202. </Feature>
  203. )}
  204. <StyledButtonBar gap={1}>
  205. <Button
  206. priority="primary"
  207. busy={loadingProjects}
  208. to={{
  209. pathname: issueStreamLink,
  210. query: {
  211. project: project?.id,
  212. },
  213. hash: '#welcome',
  214. }}
  215. >
  216. {t('Take me to Issues')}
  217. </Button>
  218. <Button
  219. busy={loadingProjects}
  220. to={{
  221. pathname: performanceOverviewLink,
  222. query: {
  223. project: project?.id,
  224. },
  225. }}
  226. >
  227. {t('Take me to Performance')}
  228. </Button>
  229. </StyledButtonBar>
  230. </div>
  231. </Fragment>
  232. );
  233. }
  234. const StyledButtonBar = styled(ButtonBar)`
  235. margin-top: ${space(3)};
  236. width: max-content;
  237. @media (max-width: ${p => p.theme.breakpoints.small}) {
  238. width: auto;
  239. grid-row-gap: ${space(1)};
  240. grid-auto-flow: row;
  241. }
  242. `;
  243. const StyledAlert = styled(Alert)`
  244. margin-top: ${space(2)};
  245. `;