platform.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. // This is a feature flag that is currently only enabled for a subset of internal users until the feature is fully implemented,
  115. // but the purpose of the feature is to make the product selection feature in documents available to all users
  116. // and guide them to upgrade to a plan if one of the products is not available on their current plan.
  117. const gettingStartedDocWithProductSelection = !!organization?.features.includes(
  118. 'getting-started-doc-with-product-selection'
  119. );
  120. const platform: Platform = {
  121. key: currentPlatformKey,
  122. id: currentPlatform?.id,
  123. name: currentPlatform?.name,
  124. link: currentPlatform?.link,
  125. };
  126. const hideLoaderOnboarding = useCallback(() => {
  127. setShowLoaderOnboarding(false);
  128. if (!project?.id || !currentPlatform) {
  129. return;
  130. }
  131. trackAnalytics('onboarding.js_loader_npm_docs_shown', {
  132. organization,
  133. platform: currentPlatform.id,
  134. project_id: project?.id,
  135. });
  136. }, [organization, currentPlatform, project?.id]);
  137. if (!project) {
  138. return null;
  139. }
  140. if (!platform.id && platform.key !== 'other') {
  141. return <NotFound />;
  142. }
  143. // because we fall back to 'other' this will always be defined
  144. if (!currentPlatform) {
  145. return null;
  146. }
  147. const issueStreamLink = `/organizations/${organization.slug}/issues/`;
  148. const performanceOverviewLink = `/organizations/${organization.slug}/performance/`;
  149. const showPerformancePrompt = performancePlatforms.includes(platform.id as PlatformKey);
  150. const isGettingStarted = window.location.href.indexOf('getting-started') > 0;
  151. const showDocsWithProductSelection =
  152. gettingStartedDocWithProductSelection &&
  153. (platformProductAvailability[platform.key] ?? []).length > 0;
  154. return (
  155. <Fragment>
  156. {!isSelfHosted && showDocsWithProductSelection && (
  157. <ProductUnavailableCTAHook organization={organization} />
  158. )}
  159. <PlatformDocHeader projectSlug={project.slug} platform={platform} />
  160. {platform.key === 'other' ? (
  161. <OtherPlatformsInfo
  162. projectSlug={project.slug}
  163. platform={platform.name ?? 'other'}
  164. />
  165. ) : showLoaderOnboarding ? (
  166. <SetupDocsLoader
  167. organization={organization}
  168. project={project}
  169. location={location}
  170. platform={currentPlatform.id}
  171. close={hideLoaderOnboarding}
  172. />
  173. ) : (
  174. <SdkDocumentation
  175. platform={currentPlatform}
  176. organization={organization}
  177. projectSlug={project.slug}
  178. projectId={project.id}
  179. activeProductSelection={products}
  180. />
  181. )}
  182. <div>
  183. {isGettingStarted && showPerformancePrompt && (
  184. <Feature
  185. features={['performance-view']}
  186. hookName="feature-disabled:performance-new-project"
  187. >
  188. {({hasFeature}) => {
  189. if (hasFeature) {
  190. return null;
  191. }
  192. return (
  193. <StyledAlert type="info" showIcon>
  194. {t(
  195. `Your selected platform supports performance, but your organization does not have performance enabled.`
  196. )}
  197. </StyledAlert>
  198. );
  199. }}
  200. </Feature>
  201. )}
  202. <StyledButtonBar gap={1}>
  203. <Button
  204. priority="primary"
  205. busy={loadingProjects}
  206. to={{
  207. pathname: issueStreamLink,
  208. query: {
  209. project: project?.id,
  210. },
  211. hash: '#welcome',
  212. }}
  213. >
  214. {t('Take me to Issues')}
  215. </Button>
  216. <Button
  217. busy={loadingProjects}
  218. to={{
  219. pathname: performanceOverviewLink,
  220. query: {
  221. project: project?.id,
  222. },
  223. }}
  224. >
  225. {t('Take me to Performance')}
  226. </Button>
  227. </StyledButtonBar>
  228. </div>
  229. </Fragment>
  230. );
  231. }
  232. const StyledButtonBar = styled(ButtonBar)`
  233. margin-top: ${space(3)};
  234. width: max-content;
  235. @media (max-width: ${p => p.theme.breakpoints.small}) {
  236. width: auto;
  237. grid-row-gap: ${space(1)};
  238. grid-auto-flow: row;
  239. }
  240. `;
  241. const StyledAlert = styled(Alert)`
  242. margin-top: ${space(2)};
  243. `;