platform.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import {Fragment, useCallback, useContext, useEffect, useMemo, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {LocationDescriptorObject} from 'history';
  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 {setPageFiltersStorage} from 'sentry/components/organizations/pageFilters/persistence';
  15. import {
  16. performance as performancePlatforms,
  17. replayPlatforms,
  18. } from 'sentry/data/platformCategories';
  19. import type {Platform} from 'sentry/data/platformPickerCategories';
  20. import platforms from 'sentry/data/platforms';
  21. import {t} from 'sentry/locale';
  22. import ConfigStore from 'sentry/stores/configStore';
  23. import PageFiltersStore from 'sentry/stores/pageFiltersStore';
  24. import {space} from 'sentry/styles/space';
  25. import type {IssueAlertRule} from 'sentry/types/alerts';
  26. import type {OnboardingSelectedSDK} from 'sentry/types/onboarding';
  27. import type {PlatformIntegration, PlatformKey, Project} from 'sentry/types/project';
  28. import {trackAnalytics} from 'sentry/utils/analytics';
  29. import {useApiQuery} from 'sentry/utils/queryClient';
  30. import {decodeList} from 'sentry/utils/queryString';
  31. import {useLocation} from 'sentry/utils/useLocation';
  32. import {useNavigate} from 'sentry/utils/useNavigate';
  33. import useOrganization from 'sentry/utils/useOrganization';
  34. import {SetupDocsLoader} from 'sentry/views/onboarding/setupDocsLoader';
  35. import {GettingStartedWithProjectContext} from 'sentry/views/projects/gettingStartedWithProjectContext';
  36. import {OtherPlatformsInfo} from './otherPlatformsInfo';
  37. import {PlatformDocHeader} from './platformDocHeader';
  38. const ProductUnavailableCTAHook = HookOrDefault({
  39. hookName: 'component:product-unavailable-cta',
  40. });
  41. type Props = {
  42. currentPlatformKey: PlatformKey;
  43. loading: boolean;
  44. platform: PlatformIntegration | undefined;
  45. project: Project | undefined;
  46. };
  47. export function ProjectInstallPlatform({
  48. loading,
  49. project,
  50. currentPlatformKey,
  51. platform: currentPlatform,
  52. }: Props) {
  53. const organization = useOrganization();
  54. const location = useLocation();
  55. const navigate = useNavigate();
  56. const gettingStartedWithProjectContext = useContext(GettingStartedWithProjectContext);
  57. const isSelfHosted = ConfigStore.get('isSelfHosted');
  58. const isSelfHostedErrorsOnly = ConfigStore.get('isSelfHostedErrorsOnly');
  59. const [showLoaderOnboarding, setShowLoaderOnboarding] = useState(
  60. currentPlatform?.id === 'javascript'
  61. );
  62. const products = useMemo(
  63. () => decodeList(location.query.product ?? []) as ProductSolution[],
  64. [location.query.product]
  65. );
  66. const {
  67. data: projectAlertRules,
  68. isPending: projectAlertRulesIsLoading,
  69. isError: projectAlertRulesIsError,
  70. } = useApiQuery<IssueAlertRule[]>(
  71. [`/projects/${organization.slug}/${project?.slug}/rules/`],
  72. {
  73. enabled: !!project?.slug,
  74. staleTime: 0,
  75. }
  76. );
  77. useEffect(() => {
  78. setShowLoaderOnboarding(currentPlatform?.id === 'javascript');
  79. }, [currentPlatform?.id]);
  80. useEffect(() => {
  81. if (!project || projectAlertRulesIsLoading || projectAlertRulesIsError) {
  82. return;
  83. }
  84. if (gettingStartedWithProjectContext.project?.id === project.id) {
  85. return;
  86. }
  87. const platformKey = Object.keys(platforms).find(
  88. key => platforms[key].id === project.platform
  89. );
  90. if (!platformKey) {
  91. return;
  92. }
  93. gettingStartedWithProjectContext.setProject({
  94. id: project.id,
  95. name: project.name,
  96. // sometimes the team slug here can be undefined
  97. teamSlug: project.team?.slug,
  98. alertRules: projectAlertRules,
  99. platform: {
  100. ...omit(platforms[platformKey], 'id'),
  101. key: platforms[platformKey].id,
  102. } as OnboardingSelectedSDK,
  103. });
  104. }, [
  105. gettingStartedWithProjectContext,
  106. project,
  107. projectAlertRules,
  108. projectAlertRulesIsLoading,
  109. projectAlertRulesIsError,
  110. ]);
  111. const platform: Platform = {
  112. key: currentPlatformKey,
  113. id: currentPlatform?.id,
  114. name: currentPlatform?.name,
  115. link: currentPlatform?.link,
  116. };
  117. const hideLoaderOnboarding = useCallback(() => {
  118. setShowLoaderOnboarding(false);
  119. if (!project?.id || !currentPlatform) {
  120. return;
  121. }
  122. trackAnalytics('onboarding.js_loader_npm_docs_shown', {
  123. organization,
  124. platform: currentPlatform.id,
  125. project_id: project?.id,
  126. });
  127. }, [organization, currentPlatform, project?.id]);
  128. const redirectWithProjectSelection = useCallback(
  129. (to: LocationDescriptorObject) => {
  130. if (!project?.id) {
  131. return;
  132. }
  133. // We need to persist and pin the project filter
  134. // so the selection does not reset on further navigation
  135. PageFiltersStore.updateProjects([Number(project?.id)], null);
  136. PageFiltersStore.pin('projects', true);
  137. setPageFiltersStorage(organization.slug, new Set(['projects']));
  138. navigate({
  139. ...to,
  140. query: {
  141. ...to.query,
  142. project: project?.id,
  143. },
  144. });
  145. },
  146. [navigate, organization.slug, project?.id]
  147. );
  148. if (!project) {
  149. return null;
  150. }
  151. if (!platform.id && platform.key !== 'other') {
  152. return <NotFound />;
  153. }
  154. // because we fall back to 'other' this will always be defined
  155. if (!currentPlatform) {
  156. return null;
  157. }
  158. const issueStreamLink = `/organizations/${organization.slug}/issues/`;
  159. const performanceOverviewLink = `/organizations/${organization.slug}/performance/`;
  160. const replayLink = `/organizations/${organization.slug}/replays/`;
  161. const showPerformancePrompt = performancePlatforms.includes(platform.id as PlatformKey);
  162. const showReplayButton = replayPlatforms.includes(platform.id as PlatformKey);
  163. const isGettingStarted = window.location.href.indexOf('getting-started') > 0;
  164. const showDocsWithProductSelection =
  165. (platformProductAvailability[platform.key] ?? []).length > 0;
  166. return (
  167. <Fragment>
  168. {!isSelfHosted && showDocsWithProductSelection && (
  169. <ProductUnavailableCTAHook organization={organization} />
  170. )}
  171. <PlatformDocHeader projectSlug={project.slug} platform={platform} />
  172. {platform.key === 'other' ? (
  173. <OtherPlatformsInfo
  174. projectSlug={project.slug}
  175. platform={platform.name ?? 'other'}
  176. />
  177. ) : showLoaderOnboarding ? (
  178. <SetupDocsLoader
  179. organization={organization}
  180. project={project}
  181. location={location}
  182. platform={currentPlatform.id}
  183. close={hideLoaderOnboarding}
  184. />
  185. ) : (
  186. <SdkDocumentation
  187. platform={currentPlatform}
  188. organization={organization}
  189. projectSlug={project.slug}
  190. projectId={project.id}
  191. activeProductSelection={products}
  192. />
  193. )}
  194. <div>
  195. {isGettingStarted && showPerformancePrompt && (
  196. <Feature
  197. features="performance-view"
  198. hookName="feature-disabled:performance-new-project"
  199. >
  200. {({hasFeature}) => {
  201. if (hasFeature) {
  202. return null;
  203. }
  204. return (
  205. <StyledAlert type="info" showIcon>
  206. {t(
  207. `Your selected platform supports performance, but your organization does not have performance enabled.`
  208. )}
  209. </StyledAlert>
  210. );
  211. }}
  212. </Feature>
  213. )}
  214. <StyledButtonBar gap={1}>
  215. <Button
  216. priority="primary"
  217. busy={loading}
  218. onClick={() => {
  219. redirectWithProjectSelection({
  220. pathname: issueStreamLink,
  221. hash: '#welcome',
  222. });
  223. }}
  224. >
  225. {t('Take me to Issues')}
  226. </Button>
  227. {!isSelfHostedErrorsOnly && (
  228. <Button
  229. busy={loading}
  230. onClick={() => {
  231. redirectWithProjectSelection({
  232. pathname: performanceOverviewLink,
  233. });
  234. }}
  235. >
  236. {t('Take me to Performance')}
  237. </Button>
  238. )}
  239. {!isSelfHostedErrorsOnly && showReplayButton && (
  240. <Button
  241. busy={loading}
  242. onClick={() => {
  243. redirectWithProjectSelection({
  244. pathname: replayLink,
  245. });
  246. }}
  247. >
  248. {t('Take me to Session Replay')}
  249. </Button>
  250. )}
  251. </StyledButtonBar>
  252. </div>
  253. </Fragment>
  254. );
  255. }
  256. const StyledButtonBar = styled(ButtonBar)`
  257. margin-top: ${space(3)};
  258. width: max-content;
  259. @media (max-width: ${p => p.theme.breakpoints.small}) {
  260. width: auto;
  261. grid-row-gap: ${space(1)};
  262. grid-auto-flow: row;
  263. }
  264. `;
  265. const StyledAlert = styled(Alert)`
  266. margin-top: ${space(2)};
  267. `;