platform.tsx 8.4 KB

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