platform.tsx 8.3 KB

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